Skip to content

Commit

Permalink
添加:食物烹饪时间的数据映射
Browse files Browse the repository at this point in the history
  • Loading branch information
MCZME committed Aug 13, 2024
1 parent 3804ccf commit a5c5eb5
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1.21 2024-08-13T22:21:22.8027373 Data Maps
3136fc55269a4deb6ec159d30383e6941c2ee6a1 data/lingshi/data_maps/item/cooking_food.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"values": {
"lingshi:rice": {
"time": 15.0
},
"minecraft:apple": {
"time": 5.0
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/mczme/lingshi/common/data/CookingFoodDataGen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mczme.lingshi.common.data;

import mczme.lingshi.common.datamap.ingredient.CookingFoodData;
import mczme.lingshi.common.registry.ModItems;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.neoforged.neoforge.common.data.DataMapProvider;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

import static mczme.lingshi.common.datamap.DataMapTypes.COOKING_FOOD;

public class CookingFoodDataGen extends DataMapProvider {

protected CookingFoodDataGen(PackOutput packOutput, CompletableFuture<HolderLookup.Provider> lookupProvider) {
super(packOutput, lookupProvider);
}

@Override
protected void gather() {
addCookingFood(Items.APPLE,5);
addCookingFood(ModItems.RICE,15);
}

protected void addCookingFood(Item item , float time){
builder(COOKING_FOOD).add(ResourceLocation.parse(item.toString()),new CookingFoodData(time),false);
}

protected void addCookingFood(Supplier<Item> item , float time){
builder(COOKING_FOOD).add(ResourceLocation.parse(item.get().toString()),new CookingFoodData(time),false);
}

protected void addCookingFood(TagKey<Item> tag, float time){
builder(COOKING_FOOD).add(tag,new CookingFoodData(time),false);
}
}
8 changes: 7 additions & 1 deletion src/main/java/mczme/lingshi/common/data/DataGenerators.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void onGatherData(GatherDataEvent event) {

// lang datagen
event.getGenerator().addProvider(
event.includeServer(),
event.includeClient(),
new ChineseLanguageProvider(output)
);

Expand Down Expand Up @@ -75,5 +75,11 @@ public static void onGatherData(GatherDataEvent event) {
event.includeServer(),
new ItemTags(output, lookupProvider, pBlockTags.contentsGetter(), existingFileHelper)
);

// Cooking Food datagen
event.getGenerator().addProvider(
event.includeServer(),
new CookingFoodDataGen(output, lookupProvider)
);
}
}
16 changes: 16 additions & 0 deletions src/main/java/mczme/lingshi/common/datamap/DataMapTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mczme.lingshi.common.datamap;

import mczme.lingshi.common.datamap.ingredient.CookingFoodData;
import mczme.lingshi.lingshi;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.neoforged.neoforge.registries.datamaps.DataMapType;

public class DataMapTypes {
public static final DataMapType<Item, CookingFoodData> COOKING_FOOD = DataMapType.builder(
ResourceLocation.fromNamespaceAndPath(lingshi.MODID, "cooking_food"),
Registries.ITEM, CookingFoodData.CODEC
).build();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mczme.lingshi.common.datamap.ingredient;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;


public record CookingFoodData(float time) {
static float MAX_TIME = 30;

public CookingFoodData(float time){
if(time > MAX_TIME) this.time = MAX_TIME;
else if(time < 0) this.time = 0;
else this.time = time;
}

public static final Codec<CookingFoodData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.FLOAT.fieldOf("time").forGetter(CookingFoodData::time)
).apply(instance, CookingFoodData::new));

}
16 changes: 16 additions & 0 deletions src/main/java/mczme/lingshi/common/event/Registry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mczme.lingshi.common.event;

import mczme.lingshi.lingshi;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.registries.datamaps.RegisterDataMapTypesEvent;

import static mczme.lingshi.common.datamap.DataMapTypes.COOKING_FOOD;

@EventBusSubscriber(modid = lingshi.MODID, bus = EventBusSubscriber.Bus.MOD)
public class Registry {
@SubscribeEvent
private static void registerDataMapTypes(RegisterDataMapTypesEvent event) {
event.register(COOKING_FOOD);
}
}

0 comments on commit a5c5eb5

Please sign in to comment.