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 a5c5eb5 commit 0336a98
Show file tree
Hide file tree
Showing 20 changed files with 471 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// 1.21 2024-08-12T04:47:15.7919427 Recipes
58cbad8a6a47dab3adccda12ea358f3d04aee08a data/lingshi/recipe/chopping_board/rice.json
// 1.21 2024-08-14T05:22:07.8393123 Recipes
80c03e628f24c66af32ca2f2a7f7efcfedeacb9f data/lingshi/advancement/recipes/skillet/dd.json
4f3b846962abd318042306445c7ecd01a176ef1f data/lingshi/recipe/chopping_board/rice.json
f8fc62d1b3c33ff6e83d1d8f585d6dc4deff2cd3 data/lingshi/recipe/skillet/dd.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21 2024-08-13T22:21:22.8027373 Data Maps
3136fc55269a4deb6ec159d30383e6941c2ee6a1 data/lingshi/data_maps/item/cooking_food.json
// 1.21 2024-08-14T01:55:49.7601336 Data Maps
a8bc32918970acec484472a12e6492804a8783f1 data/lingshi/data_maps/item/cooking_food.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"values": {
"lingshi:rice": {
"time": 15.0
"complete_time": 25.0,
"cooked_time": 15.0
},
"minecraft:apple": {
"time": 5.0
"complete_time": 20.0,
"cooked_time": 5.0
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "lingshi:chopping_board__serializer",
"type": "lingshi:chopping_board_serializer",
"input": {
"item": "lingshi:rice_of_ear"
},
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/mczme/lingshi/common/block/SkilletBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import mczme.lingshi.common.block.entity.SkilletBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
Expand All @@ -25,6 +26,7 @@
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
Expand All @@ -37,7 +39,9 @@ public class SkilletBlock extends BaseEntityBlock {

public static final BooleanProperty HAS_SUPPORT = BooleanProperty.create("has_support");
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
public static final VoxelShape SHAPE = Block.box(1, 0, 1, 15, 3, 15);
public static final VoxelShape SHAPE = Shapes.join(Block.box(1, 0, 1, 15, 3, 15),
Block.box(2,1,2,14,3,14),
BooleanOp.ONLY_FIRST);

public SkilletBlock(Properties pProperties) {
super(pProperties);
Expand All @@ -50,12 +54,20 @@ protected MapCodec<? extends BaseEntityBlock> codec() {
}

public ItemInteractionResult useItemOn(ItemStack pStack, BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHitResult) {
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
if(pLevel.getBlockEntity(pPos) instanceof SkilletBlockEntity blockEntity){
}

return ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION;
}

@Override
protected InteractionResult useWithoutItem(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, BlockHitResult pHitResult) {
return InteractionResult.PASS;
if(pLevel.getBlockEntity(pPos) instanceof SkilletBlockEntity blockEntity){
if(pPlayer.isShiftKeyDown() && !pLevel.isClientSide() && pPlayer instanceof ServerPlayer){
// pPlayer.openMenu()
}
}
return InteractionResult.sidedSuccess(pLevel.isClientSide);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,54 @@
import mczme.lingshi.common.registry.BlockEntityTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.fluids.FluidStack;

public class SkilletBlockEntity extends BlockEntity {


private NonNullList<ItemStack> itemStacks = NonNullList.withSize(5, ItemStack.EMPTY);
private NonNullList<FluidStack> fluidStacks = NonNullList.withSize(5, FluidStack.EMPTY);
private final int MAX_SLOT = 5;

public SkilletBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(BlockEntityTypes.SKILLET_BLOCKENTITY.get(), pPos, pBlockState);
}

public boolean isFull(){
return itemStacks.size()+fluidStacks.size() >= MAX_SLOT;
}

public ItemStack getItem(int slot){
return itemStacks.get(slot);
}

public FluidStack getFluid(int slot){
return fluidStacks.get(slot);
}

public NonNullList<ItemStack> getItemStacks(){
return itemStacks;
}

public NonNullList<FluidStack> getFluidStacks(){
return fluidStacks;
}

public void setItem(ItemStack item){
itemStacks.add(item);
}

public void setFluid(FluidStack fluid){
fluidStacks.add(fluid);
}

@Override
public ClientboundBlockEntityDataPacket getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
Expand All @@ -28,11 +63,54 @@ public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) {

@Override
protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) {

try{
ContainerHelper.loadAllItems(pTag, itemStacks, pRegistries);
loadFluid(pTag,fluidStacks,pRegistries);
}catch (Exception e){
itemStacks=NonNullList.withSize(5, ItemStack.EMPTY);
fluidStacks=NonNullList.withSize(5, FluidStack.EMPTY);
}
}

@Override
protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) {
super.saveAdditional(pTag, pRegistries);
if (!this.itemStacks.isEmpty()) {
ContainerHelper.saveAllItems(pTag, this.itemStacks, pRegistries);
}
if (!this.fluidStacks.isEmpty()) {
saveFluid(pTag, this.fluidStacks, true, pRegistries);
}
}

private void loadFluid(CompoundTag pTag, NonNullList<FluidStack> pItems, HolderLookup.Provider pLevelRegistry){
ListTag listtag = (ListTag)pTag.get("Fluids");

for (int i = 0; i < listtag.size(); i++) {
CompoundTag compoundtag = listtag.getCompound(i);
int j = compoundtag.getByte("Slot") & 255;
if (j >= 0 && j < pItems.size()) {
pItems.set(j, FluidStack.parse(pLevelRegistry, compoundtag).orElse(FluidStack.EMPTY));
}
}
}

private CompoundTag saveFluid(CompoundTag pTag, NonNullList<FluidStack> pItems, boolean pAlwaysPutTag, HolderLookup.Provider pLevelRegistry){
ListTag listtag = new ListTag();

for (int i = 0; i < pItems.size(); i++) {
FluidStack fluidstack = pItems.get(i);
if (!fluidstack.isEmpty()) {
CompoundTag compoundtag = new CompoundTag();
compoundtag.putByte("Slot", (byte)i);
listtag.add(fluidstack.save(pLevelRegistry, compoundtag));
}
}

if (!listtag.isEmpty() || pAlwaysPutTag) {
pTag.put("Fluids", listtag);
}

return pTag;
}
}
16 changes: 8 additions & 8 deletions src/main/java/mczme/lingshi/common/data/CookingFoodDataGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ protected CookingFoodDataGen(PackOutput packOutput, CompletableFuture<HolderLook

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

protected void addCookingFood(Item item , float time){
builder(COOKING_FOOD).add(ResourceLocation.parse(item.toString()),new CookingFoodData(time),false);
protected void addCookingFood(Item item , float cookedTime,float burntTime){
builder(COOKING_FOOD).add(ResourceLocation.parse(item.toString()),new CookingFoodData(cookedTime,burntTime),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(Supplier<Item> item , float cookedTime,float burntTime){
builder(COOKING_FOOD).add(ResourceLocation.parse(item.get().toString()),new CookingFoodData(cookedTime,burntTime),false);
}

protected void addCookingFood(TagKey<Item> tag, float time){
builder(COOKING_FOOD).add(tag,new CookingFoodData(time),false);
protected void addCookingFood(TagKey<Item> tag, float cookedTime,float burntTime){
builder(COOKING_FOOD).add(tag,new CookingFoodData(cookedTime,burntTime),false);
}
}
9 changes: 5 additions & 4 deletions src/main/java/mczme/lingshi/common/data/DataGenerators.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import mczme.lingshi.common.data.lang.ChineseLanguageProvider;
import mczme.lingshi.common.data.loot.BlockLoot;
import mczme.lingshi.common.data.recipe.ChoppingBoardRecipeDatagen;
import mczme.lingshi.common.data.recipe.Recipes;
import mczme.lingshi.common.data.tag.BlockTags;
import mczme.lingshi.common.data.tag.ItemTags;
import mczme.lingshi.lingshi;
Expand Down Expand Up @@ -51,18 +51,19 @@ public static void onGatherData(GatherDataEvent event) {
new BlockStates(output, existingFileHelper)
);

// Item Model datagen
// Item Model datagen
event.getGenerator().addProvider(
event.includeClient(),
new ItemModels(output, existingFileHelper)
);

// chopping_board recipe datagen
// recipe datagen
event.getGenerator().addProvider(
event.includeServer(),
new ChoppingBoardRecipeDatagen(output, lookupProvider)
new Recipes(output, lookupProvider)
);


// block tag datagen
BlockTags pBlockTags = new BlockTags(output, lookupProvider, existingFileHelper);
event.getGenerator().addProvider(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package mczme.lingshi.common.data.builder;

import mczme.lingshi.common.recipe.SkilletRecipe;
import mczme.lingshi.lingshi;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementRequirements;
import net.minecraft.advancements.AdvancementRewards;
import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.RecipeUnlockedTrigger;
import net.minecraft.data.recipes.RecipeBuilder;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.fluids.FluidStack;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SkilletRecipeBuilder implements RecipeBuilder {

private final List<Ingredient> items;
private final List<FluidStack> fluids;
private final ItemStack result;

protected final Map<String, Criterion<?>> criteria = new LinkedHashMap<>();
@Nullable
protected String group;

public SkilletRecipeBuilder(List<Ingredient> items, List<FluidStack> fluids, ItemStack result) {
this.items = items;
this.fluids = fluids;
this.result = result;
}

@Override
public RecipeBuilder unlockedBy(String pName, Criterion<?> pCriterion) {
this.criteria.put(pName, pCriterion);
return this;
}

@Override
public RecipeBuilder group(@Nullable String pGroupName) {
this.group = pGroupName;
return this;
}

@Override
public Item getResult() {
return this.result.getItem();
}

@Override
public void save(RecipeOutput pRecipeOutput, ResourceLocation pId) {
ResourceLocation id = ResourceLocation.fromNamespaceAndPath(lingshi.MODID,"skillet/"+pId.getPath());
Advancement.Builder advancement = pRecipeOutput.advancement()
.addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(id))
.rewards(AdvancementRewards.Builder.recipe(id))
.requirements(AdvancementRequirements.Strategy.OR);
this.criteria.forEach(advancement::addCriterion);
SkilletRecipe recipe = new SkilletRecipe(this.items, this.fluids, this.result);
pRecipeOutput.accept(id, recipe, advancement.build(id.withPrefix("recipes/")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
import mczme.lingshi.common.data.builder.ChoppingBoardRecipeBuilder;
import mczme.lingshi.common.registry.ModItems;
import mczme.lingshi.common.tag.NeoforgeTags;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.PackOutput;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.data.recipes.RecipeProvider;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class ChoppingBoardRecipeDatagen extends RecipeProvider {
public ChoppingBoardRecipeDatagen(PackOutput pOutput, CompletableFuture<HolderLookup.Provider> pRegistries) {
super(pOutput, pRegistries);
public class ChoppingBoardRecipeDatagen {
public ChoppingBoardRecipeDatagen(RecipeOutput output) {
buildRecipes(output);
}

@Override
protected void buildRecipes(RecipeOutput output) {
new ChoppingBoardRecipeBuilder(List.of(new ItemStack(ModItems.RICE.get(),3)),Ingredient.of(NeoforgeTags.KNIFE),Ingredient.of(ModItems.RICE_OF_EAR.get()))
.save(output, "rice");
build(List.of(new ItemStack(ModItems.RICE.get(),3)),Ingredient.of(NeoforgeTags.KNIFE),Ingredient.of(ModItems.RICE_OF_EAR.get()),output,"rice");
}

private void build(List<ItemStack> result, Ingredient tool, Ingredient inputItem,RecipeOutput output,String pid){
new ChoppingBoardRecipeBuilder(result, tool, inputItem).save(output, pid);
}

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

import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.InventoryChangeTrigger;
import net.minecraft.advancements.critereon.ItemPredicate;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.PackOutput;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.data.recipes.RecipeProvider;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;

import java.util.concurrent.CompletableFuture;

public class Recipes extends RecipeProvider {
public Recipes(PackOutput pOutput, CompletableFuture<HolderLookup.Provider> pRegistries) {
super(pOutput, pRegistries);
}

@Override
protected void buildRecipes(RecipeOutput output) {
new ChoppingBoardRecipeDatagen(output);
new SkilletRecipeDatagen(output);
}

public static Criterion<InventoryChangeTrigger.TriggerInstance> has(MinMaxBounds.Ints pCount, ItemLike pItem) {
return inventoryTrigger(ItemPredicate.Builder.item().of(pItem).withCount(pCount));
}

public static Criterion<InventoryChangeTrigger.TriggerInstance> has(ItemLike pItemLike) {
return inventoryTrigger(ItemPredicate.Builder.item().of(pItemLike));
}

public static Criterion<InventoryChangeTrigger.TriggerInstance> has(TagKey<Item> pTag) {
return inventoryTrigger(ItemPredicate.Builder.item().of(pTag));
}

}
Loading

0 comments on commit 0336a98

Please sign in to comment.