diff --git a/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e index e7d8977..17e86d0 100644 --- a/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e +++ b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e @@ -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 diff --git a/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f b/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f index fdae234..7f11dc4 100644 --- a/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f +++ b/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f @@ -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 diff --git a/src/generated/resources/data/lingshi/data_maps/item/cooking_food.json b/src/generated/resources/data/lingshi/data_maps/item/cooking_food.json index 4ba6700..197b315 100644 --- a/src/generated/resources/data/lingshi/data_maps/item/cooking_food.json +++ b/src/generated/resources/data/lingshi/data_maps/item/cooking_food.json @@ -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 } } } \ No newline at end of file diff --git a/src/generated/resources/data/lingshi/recipe/chopping_board/rice.json b/src/generated/resources/data/lingshi/recipe/chopping_board/rice.json index 6e6605f..c815ef0 100644 --- a/src/generated/resources/data/lingshi/recipe/chopping_board/rice.json +++ b/src/generated/resources/data/lingshi/recipe/chopping_board/rice.json @@ -1,5 +1,5 @@ { - "type": "lingshi:chopping_board__serializer", + "type": "lingshi:chopping_board_serializer", "input": { "item": "lingshi:rice_of_ear" }, diff --git a/src/main/java/mczme/lingshi/common/block/SkilletBlock.java b/src/main/java/mczme/lingshi/common/block/SkilletBlock.java index 4c6d91c..744b28a 100644 --- a/src/main/java/mczme/lingshi/common/block/SkilletBlock.java +++ b/src/main/java/mczme/lingshi/common/block/SkilletBlock.java @@ -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; @@ -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; @@ -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); @@ -50,12 +54,20 @@ protected MapCodec 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 diff --git a/src/main/java/mczme/lingshi/common/block/entity/SkilletBlockEntity.java b/src/main/java/mczme/lingshi/common/block/entity/SkilletBlockEntity.java index e934171..54485a7 100644 --- a/src/main/java/mczme/lingshi/common/block/entity/SkilletBlockEntity.java +++ b/src/main/java/mczme/lingshi/common/block/entity/SkilletBlockEntity.java @@ -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 itemStacks = NonNullList.withSize(5, ItemStack.EMPTY); + private NonNullList 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 getItemStacks(){ + return itemStacks; + } + + public NonNullList 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); @@ -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 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 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; } } diff --git a/src/main/java/mczme/lingshi/common/data/CookingFoodDataGen.java b/src/main/java/mczme/lingshi/common/data/CookingFoodDataGen.java index f9baac7..aa3f6c0 100644 --- a/src/main/java/mczme/lingshi/common/data/CookingFoodDataGen.java +++ b/src/main/java/mczme/lingshi/common/data/CookingFoodDataGen.java @@ -23,19 +23,19 @@ protected CookingFoodDataGen(PackOutput packOutput, CompletableFuture item , float time){ - builder(COOKING_FOOD).add(ResourceLocation.parse(item.get().toString()),new CookingFoodData(time),false); + protected void addCookingFood(Supplier item , float cookedTime,float burntTime){ + builder(COOKING_FOOD).add(ResourceLocation.parse(item.get().toString()),new CookingFoodData(cookedTime,burntTime),false); } - protected void addCookingFood(TagKey tag, float time){ - builder(COOKING_FOOD).add(tag,new CookingFoodData(time),false); + protected void addCookingFood(TagKey tag, float cookedTime,float burntTime){ + builder(COOKING_FOOD).add(tag,new CookingFoodData(cookedTime,burntTime),false); } } diff --git a/src/main/java/mczme/lingshi/common/data/DataGenerators.java b/src/main/java/mczme/lingshi/common/data/DataGenerators.java index bca72f8..5713f6b 100644 --- a/src/main/java/mczme/lingshi/common/data/DataGenerators.java +++ b/src/main/java/mczme/lingshi/common/data/DataGenerators.java @@ -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; @@ -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( diff --git a/src/main/java/mczme/lingshi/common/data/builder/SkilletRecipeBuilder.java b/src/main/java/mczme/lingshi/common/data/builder/SkilletRecipeBuilder.java new file mode 100644 index 0000000..24df67a --- /dev/null +++ b/src/main/java/mczme/lingshi/common/data/builder/SkilletRecipeBuilder.java @@ -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 items; + private final List fluids; + private final ItemStack result; + + protected final Map> criteria = new LinkedHashMap<>(); + @Nullable + protected String group; + + public SkilletRecipeBuilder(List items, List 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/"))); + } +} diff --git a/src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java b/src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java index bb91b7d..743682c 100644 --- a/src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java +++ b/src/main/java/mczme/lingshi/common/data/recipe/ChoppingBoardRecipeDatagen.java @@ -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 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 result, Ingredient tool, Ingredient inputItem,RecipeOutput output,String pid){ + new ChoppingBoardRecipeBuilder(result, tool, inputItem).save(output, pid); } } diff --git a/src/main/java/mczme/lingshi/common/data/recipe/Recipes.java b/src/main/java/mczme/lingshi/common/data/recipe/Recipes.java new file mode 100644 index 0000000..06cb1a3 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/data/recipe/Recipes.java @@ -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 pRegistries) { + super(pOutput, pRegistries); + } + + @Override + protected void buildRecipes(RecipeOutput output) { + new ChoppingBoardRecipeDatagen(output); + new SkilletRecipeDatagen(output); + } + + public static Criterion has(MinMaxBounds.Ints pCount, ItemLike pItem) { + return inventoryTrigger(ItemPredicate.Builder.item().of(pItem).withCount(pCount)); + } + + public static Criterion has(ItemLike pItemLike) { + return inventoryTrigger(ItemPredicate.Builder.item().of(pItemLike)); + } + + public static Criterion has(TagKey pTag) { + return inventoryTrigger(ItemPredicate.Builder.item().of(pTag)); + } + +} diff --git a/src/main/java/mczme/lingshi/common/data/recipe/SkilletRecipeDatagen.java b/src/main/java/mczme/lingshi/common/data/recipe/SkilletRecipeDatagen.java new file mode 100644 index 0000000..08226ff --- /dev/null +++ b/src/main/java/mczme/lingshi/common/data/recipe/SkilletRecipeDatagen.java @@ -0,0 +1,25 @@ +package mczme.lingshi.common.data.recipe; + +import mczme.lingshi.common.data.builder.SkilletRecipeBuilder; +import mczme.lingshi.common.registry.ModItems; +import net.minecraft.data.recipes.RecipeOutput; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.crafting.Ingredient; +import net.neoforged.neoforge.fluids.FluidStack; + +import java.util.List; + +public class SkilletRecipeDatagen { + public SkilletRecipeDatagen(RecipeOutput output) { + buildRecipes(output); + } + + protected void buildRecipes(RecipeOutput output) { + build(List.of(Ingredient.of(Items.DIAMOND),Ingredient.of(Items.IRON_BLOCK)), List.of(), new ItemStack(Items.APPLE.asItem()), output,"dd"); + } + + private void build(List items,List fluids, ItemStack result,RecipeOutput output,String pid){ + new SkilletRecipeBuilder(items, fluids, result).unlockedBy(result.toString(),Recipes.has(ModItems.SKILLET.get())).save(output,pid); + } +} diff --git a/src/main/java/mczme/lingshi/common/datamap/ingredient/CookingFoodData.java b/src/main/java/mczme/lingshi/common/datamap/ingredient/CookingFoodData.java index e58db0d..a35c251 100644 --- a/src/main/java/mczme/lingshi/common/datamap/ingredient/CookingFoodData.java +++ b/src/main/java/mczme/lingshi/common/datamap/ingredient/CookingFoodData.java @@ -4,17 +4,19 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; -public record CookingFoodData(float time) { +public record CookingFoodData(float cookedTime,float burntTime) { 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 CookingFoodData(float cookedTime,float burntTime){ + if(cookedTime > MAX_TIME) this.cookedTime = MAX_TIME; + else if(cookedTime < 0) this.cookedTime = 0; + else this.cookedTime = cookedTime; + this.burntTime = burntTime; } public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( - Codec.FLOAT.fieldOf("time").forGetter(CookingFoodData::time) + Codec.FLOAT.fieldOf("cooked_time").forGetter(CookingFoodData::cookedTime), + Codec.FLOAT.fieldOf("complete_time").forGetter(CookingFoodData::burntTime) ).apply(instance, CookingFoodData::new)); } diff --git a/src/main/java/mczme/lingshi/common/event/Registry.java b/src/main/java/mczme/lingshi/common/event/Registry.java index 369d738..71ad950 100644 --- a/src/main/java/mczme/lingshi/common/event/Registry.java +++ b/src/main/java/mczme/lingshi/common/event/Registry.java @@ -1,16 +1,27 @@ package mczme.lingshi.common.event; +import mczme.lingshi.common.registry.BlockEntityTypes; import mczme.lingshi.lingshi; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.capabilities.Capabilities; +import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; 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 { + +// data map @SubscribeEvent private static void registerDataMapTypes(RegisterDataMapTypesEvent event) { event.register(COOKING_FOOD); } -} + +// Capabilities + @SubscribeEvent + private static void registerCapabilities(RegisterCapabilitiesEvent event) { + + }} diff --git a/src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java b/src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java index 8978e91..05edfe1 100644 --- a/src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java +++ b/src/main/java/mczme/lingshi/common/recipe/ChoppingBoardRecipe.java @@ -54,7 +54,7 @@ public boolean canCraftInDimensions(int width, int height) { @Override public RecipeSerializer getSerializer() { - return ModSerializer.CHOPPING_BOARD__SERIALIZER.get(); + return ModSerializer.CHOPPING_BOARD_SERIALIZER.get(); } @Override diff --git a/src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java b/src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java new file mode 100644 index 0000000..7cd37d0 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java @@ -0,0 +1,92 @@ +package mczme.lingshi.common.recipe; + +import mczme.lingshi.common.recipe.input.SkilletRecipeInput; +import mczme.lingshi.common.registry.ModRecipes; +import mczme.lingshi.common.registry.ModSerializer; +import net.minecraft.core.HolderLookup; +import net.minecraft.core.NonNullList; +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; +import net.neoforged.neoforge.fluids.FluidStack; + +import java.util.ArrayList; +import java.util.List; + +import static net.neoforged.neoforge.common.util.RecipeMatcher.findMatches; + +public class SkilletRecipe implements Recipe { + + public static final int MAX_SLOT = 5; + + private final List items; + private final List fluids; + private final ItemStack result; + + public SkilletRecipe(List items, List fluids, ItemStack result) { + this.items = items; + this.fluids = fluids; + this.result = result; + } + + @Override + public NonNullList getIngredients() { + NonNullList list = NonNullList.create(); + list.addAll(items); + return list; + } + + @Override + public boolean matches(SkilletRecipeInput pInput, Level pLevel) { + List inputs = new ArrayList<>(); + int i = 0; + for (int j = 0; j < MAX_SLOT; ++j) { + ItemStack itemstack = pInput.getItem(j); + if (!itemstack.isEmpty()) { + ++i; + inputs.add(itemstack); + } + } + return i == this.items.size() && findMatches(inputs, this.items) != null; + } + + @Override + public ItemStack assemble(SkilletRecipeInput pInput, HolderLookup.Provider pRegistries) { + return this.result.copy(); + } + + @Override + public boolean canCraftInDimensions(int pWidth, int pHeight) { + return pWidth * pHeight >= 1; + } + + @Override + public ItemStack getResultItem(HolderLookup.Provider pRegistries) { + return this.result; + } + + @Override + public RecipeSerializer getSerializer() { + return ModSerializer.SKILLET_SERIALIZER.get(); + } + + @Override + public RecipeType getType() { + return ModRecipes.SKILLET_RECIPE.get(); + } + + public ItemStack getResult() { + return result; + } + + public List getFluids() { + return fluids; + } + + public List getItems() { + return items; + } +} diff --git a/src/main/java/mczme/lingshi/common/recipe/input/SkilletRecipeInput.java b/src/main/java/mczme/lingshi/common/recipe/input/SkilletRecipeInput.java new file mode 100644 index 0000000..86e3c97 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/recipe/input/SkilletRecipeInput.java @@ -0,0 +1,37 @@ +package mczme.lingshi.common.recipe.input; + +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.RecipeInput; +import net.neoforged.neoforge.fluids.FluidStack; + +import java.util.List; + +public class SkilletRecipeInput implements RecipeInput { + + private final List items; + private final List fluids; + + public SkilletRecipeInput(List items, List fluids) { + this.items = items; + this.fluids = fluids; + } + + @Override + public ItemStack getItem(int pIndex) { + return this.items.get(pIndex); + } + + public FluidStack getFluid(int pIndex) { + return this.fluids.get(pIndex); + } + + @Override + public int size() { + return items.size()+fluids.size(); + } + + @Override + public boolean isEmpty() { + return this.items.isEmpty() && this.fluids.isEmpty(); + } +} diff --git a/src/main/java/mczme/lingshi/common/recipe/serializer/SkilletRecipeSerializer.java b/src/main/java/mczme/lingshi/common/recipe/serializer/SkilletRecipeSerializer.java new file mode 100644 index 0000000..df894de --- /dev/null +++ b/src/main/java/mczme/lingshi/common/recipe/serializer/SkilletRecipeSerializer.java @@ -0,0 +1,50 @@ +package mczme.lingshi.common.recipe.serializer; + +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import mczme.lingshi.common.recipe.SkilletRecipe; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +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 net.neoforged.neoforge.fluids.FluidStack; + +import java.util.List; + +public class SkilletRecipeSerializer implements RecipeSerializer { + + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group( + Ingredient.CODEC.listOf().fieldOf("ingredients").forGetter(SkilletRecipe::getItems), + FluidStack.CODEC.listOf().fieldOf("fluids").forGetter(SkilletRecipe::getFluids), + ItemStack.CODEC.fieldOf("result").forGetter(SkilletRecipe::getResult) + ).apply(inst, SkilletRecipe::new)); + + public static final StreamCodec> FLUIDSTACK_STREAM_CODEC = + FluidStack.STREAM_CODEC.apply( + ByteBufCodecs.list(SkilletRecipe.MAX_SLOT) + ); + public static final StreamCodec> INGREDIENT_STREAM_CODEC = + Ingredient.CONTENTS_STREAM_CODEC.apply( + ByteBufCodecs.list(SkilletRecipe.MAX_SLOT) + ); + + public static final StreamCodec STREAM_CODEC = + StreamCodec.composite( + INGREDIENT_STREAM_CODEC,SkilletRecipe::getItems, + FLUIDSTACK_STREAM_CODEC,SkilletRecipe::getFluids, + ItemStack.STREAM_CODEC,SkilletRecipe::getResult, + SkilletRecipe::new + ); + + @Override + public MapCodec codec() { + return CODEC; + } + + @Override + public 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 index c7a78c1..fd9b01f 100644 --- a/src/main/java/mczme/lingshi/common/registry/ModRecipes.java +++ b/src/main/java/mczme/lingshi/common/registry/ModRecipes.java @@ -1,6 +1,7 @@ package mczme.lingshi.common.registry; import mczme.lingshi.common.recipe.ChoppingBoardRecipe; +import mczme.lingshi.common.recipe.SkilletRecipe; import mczme.lingshi.lingshi; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; @@ -19,7 +20,11 @@ public class ModRecipes { "chopping_board_recipe", () -> RecipeType.simple(ResourceLocation.fromNamespaceAndPath(lingshi.MODID, "chopping_board")) ); - + public static final Supplier> SKILLET_RECIPE = + RECIPE_TYPES.register( + "skillet_recipe", + () -> RecipeType.simple(ResourceLocation.fromNamespaceAndPath(lingshi.MODID, "right_click_block")) + ); 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 index 3a422c4..15dac00 100644 --- a/src/main/java/mczme/lingshi/common/registry/ModSerializer.java +++ b/src/main/java/mczme/lingshi/common/registry/ModSerializer.java @@ -1,6 +1,8 @@ package mczme.lingshi.common.registry; +import mczme.lingshi.common.recipe.serializer.SkilletRecipeSerializer; import mczme.lingshi.common.recipe.ChoppingBoardRecipe; +import mczme.lingshi.common.recipe.SkilletRecipe; import mczme.lingshi.common.recipe.serializer.ChoppingBoardRecipeSerializer; import mczme.lingshi.lingshi; import net.minecraft.core.registries.Registries; @@ -14,8 +16,10 @@ 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 final Supplier> CHOPPING_BOARD_SERIALIZER = + RECIPE_SERIALIZERS.register("chopping_board_serializer", ChoppingBoardRecipeSerializer::new); + public static final Supplier> SKILLET_SERIALIZER = + RECIPE_SERIALIZERS.register("skillet_serializer", SkilletRecipeSerializer::new); public static void register (IEventBus modEventBus) { RECIPE_SERIALIZERS.register(modEventBus);