From 4e14febd3d9a81c7a3d5c011f41b5d10aee47ed9 Mon Sep 17 00:00:00 2001 From: MCZME <13183052+mczme@user.noreply.gitee.com> Date: Sat, 10 Aug 2024 01:24:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90:=E8=8F=9C=E6=9D=BF=E9=85=8D?= =?UTF-8?q?=E6=96=B9=E7=94=9F=E6=88=90=E5=92=8C=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e | 3 + .../minecraft/advancement/recipes/test_0.json | 32 ++++++++++ .../data/minecraft/recipe/test_0.json | 10 +++ .../common/block/ChoppingBoardBlock.java | 45 ++++++++++---- .../entity/ChoppingBoardBlockEntity.java | 28 +++++++++ .../lingshi/common/data/DataGenerators.java | 7 +++ .../builder/ChoppingBoardRecipeBuilder.java | 61 ++++++++++++++++++ .../recipe/ChoppingBoardRecipeDatagen.java | 25 ++++++++ .../common/recipe/ChoppingBoardRecipe.java | 62 +++++++++++++++++++ .../input/ChoppingBoardRecipeInput.java | 18 ++++++ .../ChoppingBoardRecipeSerializer.java | 35 +++++++++++ .../lingshi/common/registry/ModRecipes.java | 26 ++++++++ .../common/registry/ModSerializer.java | 23 +++++++ src/main/java/mczme/lingshi/lingshi.java | 6 +- 14 files changed, 365 insertions(+), 16 deletions(-) create mode 100644 src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e create mode 100644 src/generated/resources/data/minecraft/advancement/recipes/test_0.json create mode 100644 src/generated/resources/data/minecraft/recipe/test_0.json create mode 100644 src/main/java/mczme/lingshi/common/data/builder/ChoppingBoardRecipeBuilder.java create mode 100644 src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java create mode 100644 src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java create mode 100644 src/main/java/mczme/lingshi/common/recipe/input/ChoppingBoardRecipeInput.java create mode 100644 src/main/java/mczme/lingshi/common/recipe/serializer/ChoppingBoardRecipeSerializer.java create mode 100644 src/main/java/mczme/lingshi/common/registry/ModRecipes.java create mode 100644 src/main/java/mczme/lingshi/common/registry/ModSerializer.java diff --git a/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e new file mode 100644 index 0000000..bdb8f4d --- /dev/null +++ b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e @@ -0,0 +1,3 @@ +// 1.21 2024-08-09T23:10:31.7156256 Recipes +b665c30882c5051f7b3c99ddda78d192c3fe7fc8 data/minecraft/advancement/recipes/test_0.json +b9d787861ce5d722954082566de7df25a0ec7c7c data/minecraft/recipe/test_0.json diff --git a/src/generated/resources/data/minecraft/advancement/recipes/test_0.json b/src/generated/resources/data/minecraft/advancement/recipes/test_0.json new file mode 100644 index 0000000..17cbd91 --- /dev/null +++ b/src/generated/resources/data/minecraft/advancement/recipes/test_0.json @@ -0,0 +1,32 @@ +{ + "parent": "minecraft:recipes/root", + "criteria": { + "has_the_recipe": { + "conditions": { + "recipe": "minecraft:test_0" + }, + "trigger": "minecraft:recipe_unlocked" + }, + "test": { + "conditions": { + "items": [ + { + "items": "minecraft:apple" + } + ] + }, + "trigger": "minecraft:inventory_changed" + } + }, + "requirements": [ + [ + "has_the_recipe", + "test" + ] + ], + "rewards": { + "recipes": [ + "minecraft:test_0" + ] + } +} \ No newline at end of file diff --git a/src/generated/resources/data/minecraft/recipe/test_0.json b/src/generated/resources/data/minecraft/recipe/test_0.json new file mode 100644 index 0000000..4e200c2 --- /dev/null +++ b/src/generated/resources/data/minecraft/recipe/test_0.json @@ -0,0 +1,10 @@ +{ + "type": "lingshi:chopping_board__serializer", + "ingredient": { + "item": "minecraft:apple" + }, + "result": { + "count": 1, + "id": "minecraft:wooden_axe" + } +} \ No newline at end of file diff --git a/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java b/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java index 05e1d07..edd4781 100644 --- a/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java +++ b/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java @@ -10,6 +10,7 @@ import net.minecraft.world.ItemInteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; @@ -30,6 +31,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; + public class ChoppingBoardBlock extends BaseEntityBlock { public static final VoxelShape NORTH_SHAPE = Block.box(1.0D, 0.0D, 4.0D, 15.0D, 1.0D, 13.0D); @@ -64,7 +67,19 @@ public ItemInteractionResult useItemOn( blockEntity.setTheItem(stack); blockEntity.setChanged(); return ItemInteractionResult.SUCCESS; - } else { + } else if(!blockEntity.getTheItem().isEmpty()&& !stack.isEmpty()) { + if(pPlayer.getMainHandItem().is(Items.DIAMOND)) + { + List stacks = blockEntity.getRecipeAndResult(blockEntity.getTheItem()); + if(stacks == null||stacks.getFirst().isEmpty()) { + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; + } + stacks.forEach(itemStack -> Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), itemStack)); + blockEntity.setTheItem(ItemStack.EMPTY); + blockEntity.setChanged(); + return ItemInteractionResult.SUCCESS; + } + }else { return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } } @@ -86,6 +101,15 @@ protected InteractionResult useWithoutItem(BlockState pState, Level pLevel, Bloc return InteractionResult.PASS; } + @Override + protected void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) { + if (pLevel.getBlockEntity(pPos) instanceof ChoppingBoardBlockEntity blockEntity) { + Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), blockEntity.getTheItem()); + blockEntity.setTheItem(ItemStack.EMPTY); + } + super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston); + } + @Nullable @Override public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) { @@ -94,18 +118,13 @@ public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pS @Override protected VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { - switch (pState.getValue(FACING)) { - case NORTH: - return NORTH_SHAPE; - case SOUTH: - return SOUTH_SHAPE; - case WEST: - return WEST_SHAPE; - case EAST: - return EAST_SHAPE; - default: - return NORTH_SHAPE; - } + return switch (pState.getValue(FACING)) { + case NORTH -> NORTH_SHAPE; + case SOUTH -> SOUTH_SHAPE; + case WEST -> WEST_SHAPE; + case EAST -> EAST_SHAPE; + default -> NORTH_SHAPE; + }; } @Override diff --git a/src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java b/src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java index e2ec53e..2d85c6d 100644 --- a/src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java +++ b/src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java @@ -1,16 +1,24 @@ package mczme.lingshi.common.block.entity; +import mczme.lingshi.common.recipe.ChoppingBoardRecipe; +import mczme.lingshi.common.recipe.input.ChoppingBoardRecipeInput; import mczme.lingshi.common.registry.BlockEntitys; +import mczme.lingshi.common.registry.ModRecipes; import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.*; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.ticks.ContainerSingleItem; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + public class ChoppingBoardBlockEntity extends BlockEntity implements ContainerSingleItem { private ItemStack item = ItemStack.EMPTY; @@ -57,6 +65,26 @@ public void setTheItem(ItemStack pItem) { this.item= pItem; } + public List getRecipeAndResult(ItemStack itemStack){ + List list = new ArrayList<>(); + ChoppingBoardRecipeInput input = new ChoppingBoardRecipeInput(itemStack); + if (level.isClientSide()) { + return null; + } + RecipeManager recipes = level.getRecipeManager(); + Optional> optional = recipes.getRecipeFor( + ModRecipes.CHOPPING_BOARD_RECIPE.get(), + input, + level + ); + ItemStack result = optional + .map(RecipeHolder::value) + .map(e -> e.assemble(input, level.registryAccess())) + .orElse(ItemStack.EMPTY); + list.add(result); + return list; + } + @Override public boolean stillValid(Player pPlayer) { diff --git a/src/main/java/mczme/lingshi/common/data/DataGenerators.java b/src/main/java/mczme/lingshi/common/data/DataGenerators.java index 9422d4a..3003308 100644 --- a/src/main/java/mczme/lingshi/common/data/DataGenerators.java +++ b/src/main/java/mczme/lingshi/common/data/DataGenerators.java @@ -2,6 +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.lingshi; import net.minecraft.core.HolderLookup; import net.minecraft.data.DataGenerator; @@ -47,5 +48,11 @@ public static void onGatherData(GatherDataEvent event) { event.includeServer(), new BlockStates(output, existingFileHelper) ); + +// chopping_board recipe datagen + event.getGenerator().addProvider( + event.includeServer(), + new ChoppingBoardRecipeDatagen(output, lookupProvider) + ); } } diff --git a/src/main/java/mczme/lingshi/common/data/builder/ChoppingBoardRecipeBuilder.java b/src/main/java/mczme/lingshi/common/data/builder/ChoppingBoardRecipeBuilder.java new file mode 100644 index 0000000..4098add --- /dev/null +++ b/src/main/java/mczme/lingshi/common/data/builder/ChoppingBoardRecipeBuilder.java @@ -0,0 +1,61 @@ +package mczme.lingshi.common.data.builder; + +import mczme.lingshi.common.recipe.ChoppingBoardRecipe; +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 org.jetbrains.annotations.Nullable; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class ChoppingBoardRecipeBuilder implements RecipeBuilder { + + protected final ItemStack result; + protected final Map> criteria = new LinkedHashMap<>(); + @Nullable + protected String group; + + private final Ingredient inputItem; + + public ChoppingBoardRecipeBuilder(ItemStack result, Ingredient inputItem) { + this.result = result; + this.inputItem = inputItem; + } + + @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) { + Advancement.Builder advancement = pRecipeOutput.advancement() + .addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(pId)) + .rewards(AdvancementRewards.Builder.recipe(pId)) + .requirements(AdvancementRequirements.Strategy.OR); + this.criteria.forEach(advancement::addCriterion); + ChoppingBoardRecipe recipe = new ChoppingBoardRecipe(this.inputItem, this.result); + pRecipeOutput.accept(pId, recipe, advancement.build(pId.withPrefix("recipes/"))); + } +} diff --git a/src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java b/src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java new file mode 100644 index 0000000..bea6a73 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java @@ -0,0 +1,25 @@ +package mczme.lingshi.common.data.recipe; + +import mczme.lingshi.common.data.builder.ChoppingBoardRecipeBuilder; +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.Items; +import net.minecraft.world.item.crafting.Ingredient; + +import java.util.concurrent.CompletableFuture; + +public class ChoppingBoardRecipeDatagen extends RecipeProvider { + public ChoppingBoardRecipeDatagen(PackOutput pOutput, CompletableFuture pRegistries) { + super(pOutput, pRegistries); + } + + @Override + protected void buildRecipes(RecipeOutput output) { + new ChoppingBoardRecipeBuilder(new ItemStack(Items.WOODEN_AXE),Ingredient.of(Items.APPLE)) + .unlockedBy("test",has(Items.APPLE)).save(output, "test_0"); + } + +} diff --git a/src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java b/src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java new file mode 100644 index 0000000..86fd128 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java @@ -0,0 +1,62 @@ +package mczme.lingshi.common.recipe; + +import mczme.lingshi.common.recipe.input.ChoppingBoardRecipeInput; +import mczme.lingshi.common.registry.ModRecipes; +import mczme.lingshi.common.registry.ModSerializer; +import net.minecraft.core.HolderLookup; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.RecipeSerializer; +import net.minecraft.world.item.crafting.RecipeType; +import net.minecraft.world.level.Level; + +public class ChoppingBoardRecipe implements Recipe { + + private final Ingredient inputItem; + private final ItemStack result; + + public ChoppingBoardRecipe(Ingredient inputItem, ItemStack result) { + this.inputItem = inputItem; + this.result = result; + } + + @Override + public boolean matches(ChoppingBoardRecipeInput pInput, Level pLevel) { + return this.inputItem.test(pInput.stack()); + } + + @Override + public ItemStack assemble(ChoppingBoardRecipeInput pInput, HolderLookup.Provider pRegistries) { + return this.result.copy(); + } + + @Override + public boolean canCraftInDimensions(int width, int height) { + return width * height >= 1; + } + + @Override + public ItemStack getResultItem(HolderLookup.Provider registries) { + return this.result; + } + + @Override + public RecipeSerializer getSerializer() { + return ModSerializer.CHOPPING_BOARD__SERIALIZER.get(); + } + + @Override + public RecipeType getType() { + return ModRecipes.CHOPPING_BOARD_RECIPE.get(); + } + + public Ingredient getInputItem() { + return inputItem; + } + + public ItemStack getResult() { + return result; + } + +} diff --git a/src/main/java/mczme/lingshi/common/recipe/input/ChoppingBoardRecipeInput.java b/src/main/java/mczme/lingshi/common/recipe/input/ChoppingBoardRecipeInput.java new file mode 100644 index 0000000..dab3f65 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/recipe/input/ChoppingBoardRecipeInput.java @@ -0,0 +1,18 @@ +package mczme.lingshi.common.recipe.input; + +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.RecipeInput; +import org.jetbrains.annotations.NotNull; + +public record ChoppingBoardRecipeInput(ItemStack stack) implements RecipeInput { + @Override + public @NotNull ItemStack getItem(int slot) { + if (slot != 0) throw new IllegalArgumentException("No item for index " + slot); + return this.stack(); + } + + @Override + public int size() { + return 1; + } +} diff --git a/src/main/java/mczme/lingshi/common/recipe/serializer/ChoppingBoardRecipeSerializer.java b/src/main/java/mczme/lingshi/common/recipe/serializer/ChoppingBoardRecipeSerializer.java new file mode 100644 index 0000000..d231951 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/recipe/serializer/ChoppingBoardRecipeSerializer.java @@ -0,0 +1,35 @@ +package mczme.lingshi.common.recipe.serializer; + +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import mczme.lingshi.common.recipe.ChoppingBoardRecipe; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.item.crafting.RecipeSerializer; +import org.jetbrains.annotations.NotNull; + +public class ChoppingBoardRecipeSerializer implements RecipeSerializer { + + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group( + Ingredient.CODEC.fieldOf("ingredient").forGetter(ChoppingBoardRecipe::getInputItem), + ItemStack.CODEC.fieldOf("result").forGetter(ChoppingBoardRecipe::getResult) + ).apply(inst, ChoppingBoardRecipe::new)); + public static final StreamCodec STREAM_CODEC = + StreamCodec.composite( + Ingredient.CONTENTS_STREAM_CODEC, ChoppingBoardRecipe::getInputItem, + ItemStack.STREAM_CODEC, ChoppingBoardRecipe::getResult, + ChoppingBoardRecipe::new + ); + + @Override + public @NotNull MapCodec codec() { + return CODEC; + } + + @Override + public @NotNull StreamCodec streamCodec() { + return STREAM_CODEC; + } +} diff --git a/src/main/java/mczme/lingshi/common/registry/ModRecipes.java b/src/main/java/mczme/lingshi/common/registry/ModRecipes.java new file mode 100644 index 0000000..fc8a6eb --- /dev/null +++ b/src/main/java/mczme/lingshi/common/registry/ModRecipes.java @@ -0,0 +1,26 @@ +package mczme.lingshi.common.registry; + +import mczme.lingshi.common.recipe.ChoppingBoardRecipe; +import mczme.lingshi.lingshi; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.crafting.RecipeType; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.neoforge.registries.DeferredRegister; + +import java.util.function.Supplier; + +public class ModRecipes { + public static final DeferredRegister> RECIPE_TYPES = + DeferredRegister.create(Registries.RECIPE_TYPE, lingshi.MODID); + + public static final Supplier> CHOPPING_BOARD_RECIPE = + RECIPE_TYPES.register( + "chopping_board_recipe", + () -> RecipeType.simple(ResourceLocation.fromNamespaceAndPath(lingshi.MODID, "chopping_board_recipe")) + ); + + public static void register(IEventBus modEventBus) { + RECIPE_TYPES.register(modEventBus); + } +} diff --git a/src/main/java/mczme/lingshi/common/registry/ModSerializer.java b/src/main/java/mczme/lingshi/common/registry/ModSerializer.java new file mode 100644 index 0000000..3a422c4 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/registry/ModSerializer.java @@ -0,0 +1,23 @@ +package mczme.lingshi.common.registry; + +import mczme.lingshi.common.recipe.ChoppingBoardRecipe; +import mczme.lingshi.common.recipe.serializer.ChoppingBoardRecipeSerializer; +import mczme.lingshi.lingshi; +import net.minecraft.core.registries.Registries; +import net.minecraft.world.item.crafting.RecipeSerializer; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.neoforge.registries.DeferredRegister; + +import java.util.function.Supplier; + +public class ModSerializer { + + public static final DeferredRegister> RECIPE_SERIALIZERS = DeferredRegister.create(Registries.RECIPE_SERIALIZER, lingshi.MODID); + + public static final Supplier> CHOPPING_BOARD__SERIALIZER = + RECIPE_SERIALIZERS.register("chopping_board__serializer", ChoppingBoardRecipeSerializer::new); + + public static void register (IEventBus modEventBus) { + RECIPE_SERIALIZERS.register(modEventBus); + } +} diff --git a/src/main/java/mczme/lingshi/lingshi.java b/src/main/java/mczme/lingshi/lingshi.java index 007d7bb..0a61af7 100644 --- a/src/main/java/mczme/lingshi/lingshi.java +++ b/src/main/java/mczme/lingshi/lingshi.java @@ -1,9 +1,7 @@ package mczme.lingshi; import mczme.lingshi.common.createtab.CreateTabs; -import mczme.lingshi.common.registry.BlockEntitys; -import mczme.lingshi.common.registry.ModBlocks; -import mczme.lingshi.common.registry.ModItems; +import mczme.lingshi.common.registry.*; import org.slf4j.Logger; import com.mojang.logging.LogUtils; @@ -27,6 +25,8 @@ public lingshi(IEventBus modEventBus, ModContainer modContainer) ModItems.register(modEventBus); CreateTabs.register(modEventBus); BlockEntitys.register(modEventBus); + ModRecipes.register(modEventBus); + ModSerializer.register(modEventBus); }