From ebf92f521b6757632d96a97f65bcb580f72e8b20 Mon Sep 17 00:00:00 2001 From: MCZME <13183052+mczme@user.noreply.gitee.com> Date: Tue, 20 Aug 2024 01:00:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9A=E7=83=B9=E9=A5=AAHU?= =?UTF-8?q?D=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e | 4 +- .../e0d3d0b8d9c807675613821fa865a35f707cd83f | 4 +- .../mczme/lingshi/client/event/Registry.java | 7 ++ .../client/menu/CookingItemStackHandler.java | 17 ++++ .../lingshi/client/menu/SkilletMenu.java | 2 +- .../SkilletRecipeBookComponent.java | 4 +- .../lingshi/client/screen/CookingHud.java | 92 +++++++++++++++++++ .../block/entity/SkilletBlockEntity.java | 74 ++++++++++----- .../data/builder/SkilletRecipeBuilder.java | 8 +- .../data/recipe/SkilletRecipeDatagen.java | 3 +- .../datamap/ingredient/CookingFoodData.java | 5 +- .../mczme/lingshi/common/event/Registry.java | 20 ++++ .../lingshi/common/network/CookingData.java | 25 +++++ .../CookingDataClientPayloadHandler.java | 32 +++++++ .../CookingDataServerPayloadHandler.java | 22 +++++ .../lingshi/common/recipe/SkilletRecipe.java | 67 +++++++++++--- .../serializer/SkilletRecipeSerializer.java | 19 ++-- .../lingshi/common/registry/ModItems.java | 2 + .../ListUtility.java => util/ListUtil.java} | 4 +- .../mczme/lingshi/common/util/ModCodec.java | 19 ++++ .../lingshi/common/utility/ModCodec.java | 28 ------ .../resources/META-INF/accesstransformer.cfg | 1 + .../resources/META-INF/neoforge.mods.toml | 4 +- 24 files changed, 371 insertions(+), 94 deletions(-) create mode 100644 src/main/java/mczme/lingshi/client/menu/CookingItemStackHandler.java create mode 100644 src/main/java/mczme/lingshi/client/screen/CookingHud.java create mode 100644 src/main/java/mczme/lingshi/common/network/CookingData.java create mode 100644 src/main/java/mczme/lingshi/common/network/CookingDataClientPayloadHandler.java create mode 100644 src/main/java/mczme/lingshi/common/network/CookingDataServerPayloadHandler.java rename src/main/java/mczme/lingshi/common/{utility/ListUtility.java => util/ListUtil.java} (83%) create mode 100644 src/main/java/mczme/lingshi/common/util/ModCodec.java delete mode 100644 src/main/java/mczme/lingshi/common/utility/ModCodec.java create mode 100644 src/main/resources/META-INF/accesstransformer.cfg diff --git a/build.gradle b/build.gradle index 6588f7a..a38962f 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ base { // Mojang ships Java 21 to end users starting in 1.20.5, so mods should target Java 21. java.toolchain.languageVersion = JavaLanguageVersion.of(21) -//minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') +minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') //minecraft.accessTransformers.entry public net.minecraft.client.Minecraft textureManager # textureManager // Default run configurations. diff --git a/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e index 4118c14..938600d 100644 --- a/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e +++ b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e @@ -1,8 +1,8 @@ -// 1.21 2024-08-16T23:38:40.1170951 Recipes +// 1.21 2024-08-19T18:23:04.1006546 Recipes 30317ddefa8c34feeae2fb9b47b40dae9a2c1335 data/lingshi/advancement/recipes/skillet/rice_apple.json 60b5c5b8536660e2fc3758499991b48002d68196 data/lingshi/advancement/recipes/skillet/test01.json 24367c601743c0d2f9df76c4a1e59f6650f0de4a data/lingshi/advancement/recipes/skillet/test02.json 3e41620c4ec5b19f87046dad7fe5c7ed6ede21c2 data/lingshi/recipe/chopping_board/rice.json -1268fc69304435f6b1c56650c7e2f8fcc9fe7e6e data/lingshi/recipe/skillet/rice_apple.json +a25f37985d35ec9c738209ef29f82c466a34f01c data/lingshi/recipe/skillet/rice_apple.json c80602ecff2ea09399aff222a6d36c7b28d0164f data/lingshi/recipe/skillet/test01.json 89bc0e33a11e930f7b69c4d726d92b4f9446b3d3 data/lingshi/recipe/skillet/test02.json diff --git a/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f b/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f index 28ea919..09cda82 100644 --- a/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f +++ b/src/generated/resources/.cache/e0d3d0b8d9c807675613821fa865a35f707cd83f @@ -1,2 +1,2 @@ -// 1.21 2024-08-18T01:50:29.4609975 Data Maps -15e4de66fa16cad8d36f7b96c6aa67d7cb0b0f1a data/lingshi/data_maps/item/cooking_food_item.json +// 1.21 2024-08-19T23:11:55.0223064 Data Maps +7efa3dffa87b902d4fac709bea335215c17fdf2a data/lingshi/data_maps/item/cooking_food_item.json diff --git a/src/main/java/mczme/lingshi/client/event/Registry.java b/src/main/java/mczme/lingshi/client/event/Registry.java index 2fda3a9..ec13ab7 100644 --- a/src/main/java/mczme/lingshi/client/event/Registry.java +++ b/src/main/java/mczme/lingshi/client/event/Registry.java @@ -6,6 +6,7 @@ import mczme.lingshi.client.BlockEntityRenderer.ChoppingBoardBER; import mczme.lingshi.client.BlockEntityRenderer.SkilletBER; import mczme.lingshi.client.recipebook.CookingFoodRecipeLabel; +import mczme.lingshi.client.screen.CookingHud; import mczme.lingshi.client.screen.SkilletScreen; import mczme.lingshi.common.recipe.SkilletRecipe; import mczme.lingshi.common.registry.BlockEntityTypes; @@ -24,6 +25,7 @@ import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.neoforge.client.event.EntityRenderersEvent; +import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent; import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent; import net.neoforged.neoforge.client.event.RegisterRecipeBookCategoriesEvent; import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions; @@ -118,4 +120,9 @@ public void modifyFogRender(Camera camera, FogRenderer.FogMode mode, float rende }, ModFluids.MOD_FLUID_TYPE.get()); } + //HUD + @SubscribeEvent + public static void registerGuiLayersEvent(RegisterGuiLayersEvent event) { + event.registerAboveAll(ResourceLocation.fromNamespaceAndPath(lingshi.MODID,"cooking_hud"), CookingHud.getInstance()); + } } diff --git a/src/main/java/mczme/lingshi/client/menu/CookingItemStackHandler.java b/src/main/java/mczme/lingshi/client/menu/CookingItemStackHandler.java new file mode 100644 index 0000000..52a0a16 --- /dev/null +++ b/src/main/java/mczme/lingshi/client/menu/CookingItemStackHandler.java @@ -0,0 +1,17 @@ +package mczme.lingshi.client.menu; + +import net.minecraft.world.item.ItemStack; +import net.neoforged.neoforge.items.ItemStackHandler; +import org.jetbrains.annotations.NotNull; + +public class CookingItemStackHandler extends ItemStackHandler { + + public CookingItemStackHandler(int size){ + super(size); + } + + @Override + protected int getStackLimit(int slot, @NotNull ItemStack stack) { + return 1; + } +} diff --git a/src/main/java/mczme/lingshi/client/menu/SkilletMenu.java b/src/main/java/mczme/lingshi/client/menu/SkilletMenu.java index 5ce77e6..f9dd2b8 100644 --- a/src/main/java/mczme/lingshi/client/menu/SkilletMenu.java +++ b/src/main/java/mczme/lingshi/client/menu/SkilletMenu.java @@ -21,7 +21,7 @@ public class SkilletMenu extends RecipeBookMenu pRecipe, List pSlots) { ItemStack resultitem = pRecipe.value().getResultItem(this.minecraft.level.registryAccess()); this.ghostRecipe.setRecipe(pRecipe); this.ghostRecipe.addIngredient(Ingredient.of(resultitem), pSlots.get(6).x, pSlots.get(6).y); - if(skilletRecipe.getContainer() != ItemStack.EMPTY){ - this.ghostRecipe.addIngredient(Ingredient.of(skilletRecipe.getContainer()), pSlots.get(5).x, pSlots.get(5).y); + if(skilletRecipe.getContainer().container() != ItemStack.EMPTY){ + this.ghostRecipe.addIngredient(Ingredient.of(skilletRecipe.getContainer().container()), pSlots.get(5).x, pSlots.get(5).y); } NonNullList nonnulllist = pRecipe.value().getIngredients(); diff --git a/src/main/java/mczme/lingshi/client/screen/CookingHud.java b/src/main/java/mczme/lingshi/client/screen/CookingHud.java new file mode 100644 index 0000000..3f78475 --- /dev/null +++ b/src/main/java/mczme/lingshi/client/screen/CookingHud.java @@ -0,0 +1,92 @@ +package mczme.lingshi.client.screen; + +import mczme.lingshi.common.block.entity.SkilletBlockEntity; +import mczme.lingshi.common.datamap.DataMapTypes; +import mczme.lingshi.common.datamap.ingredient.CookingFoodData; +import mczme.lingshi.common.network.CookingDataClientPayloadHandler; +import mczme.lingshi.common.registry.BlockEntityTypes; +import mczme.lingshi.lingshi; +import net.minecraft.client.DeltaTracker; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.LayeredDraw; +import net.minecraft.core.BlockPos; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.ClipContext; +import net.minecraft.world.level.block.entity.BlockEntityType; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.Vec3; +import net.neoforged.neoforge.items.ItemStackHandler; +import org.jetbrains.annotations.NotNull; + +public class CookingHud implements LayeredDraw.Layer { + + public static final CookingHud hud = new CookingHud(); + + public static final ResourceLocation HUD_Sprite = ResourceLocation.fromNamespaceAndPath(lingshi.MODID, "textures/gui/cooking_hud.png"); + private ItemStackHandler itemStackHandler; + private int[] cookingTime; + private ItemStack result; + private int MAX_SLOT; + private Player player; + private BlockPos blockPos; + + private final int X = 28; + private final int Y = 140; + + @Override + public void render(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker pDeltaTracker) { + if (Minecraft.getInstance().level == null) { + return; + } + this.player = Minecraft.getInstance().player; + if (player != null && getHitResult(BlockEntityTypes.SKILLET_BLOCKENTITY.get())) { + getData(); + for (int i = 0; i < MAX_SLOT; i++) { + if (!itemStackHandler.getStackInSlot(i).isEmpty()) { + pGuiGraphics.renderItem(itemStackHandler.getStackInSlot(i), X-18, Y + i * 18); + CookingFoodData cookingFoodData = itemStackHandler.getStackInSlot(i).getItemHolder().getData(DataMapTypes.COOKING_FOOD_ITEM); + if (cookingFoodData != null) { + drawItemProgress(pGuiGraphics, cookingFoodData, i); + } + } + } + } + + } + + private void getData() { + SkilletBlockEntity blockEntity = null; + if (Minecraft.getInstance().level != null) { + blockEntity = (SkilletBlockEntity) Minecraft.getInstance().level.getBlockEntity(blockPos); + } + if (blockEntity != null) { + this.itemStackHandler = blockEntity.getItemStacks(); + this.cookingTime = blockEntity.getCookingTime(); + this.result = CookingDataClientPayloadHandler.getResult(); + this.MAX_SLOT = blockEntity.getMAX(); + } + } + + private boolean getHitResult(BlockEntityType pType) { + Vec3 start = player.getEyePosition(); + Vec3 end = player.getLookAngle().normalize().scale(3).add(start); + BlockHitResult blockHitResult = Minecraft.getInstance().level.clip(new ClipContext(start, end, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, player)); + this.blockPos = blockHitResult.getBlockPos(); + return Minecraft.getInstance().level.getBlockEntity(blockPos) != null && Minecraft.getInstance().level.getBlockEntity(blockPos).getType() == pType; + } + + private void drawItemProgress(GuiGraphics pGuiGraphics, CookingFoodData cookingFoodData, int i) { + int pHeight = 8; + pGuiGraphics.blit(HUD_Sprite, X, Y + i * 18 + 7, cookingFoodData.cookedTime() * 2, pHeight, 0, 0, cookingFoodData.cookedTime() * 2 + 2, pHeight, 64, 64); + pGuiGraphics.blit(HUD_Sprite, X + cookingFoodData.cookedTime() * 2, Y+ i * 18 + 7, (cookingFoodData.burntTime() - cookingFoodData.cookedTime()) * 2, pHeight, 2, 8, (cookingFoodData.burntTime() - cookingFoodData.cookedTime()) * 2, pHeight, 64, 64); + pGuiGraphics.blit(HUD_Sprite, X + cookingFoodData.burntTime() * 2, Y + i * 18 + 7, 62 - cookingFoodData.burntTime() * 2, pHeight, cookingFoodData.burntTime() * 2 + 2, 24, 62 - cookingFoodData.burntTime() * 2, pHeight, 64, 64); + pGuiGraphics.blit(HUD_Sprite, X+1 + cookingTime[i] / 10, Y + i * 18 + 9, 4, 4, 0, 32, 4, 4, 64, 64); + } + + public static CookingHud getInstance() { + return hud; + } +} 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 3d55393..38ae647 100644 --- a/src/main/java/mczme/lingshi/common/block/entity/SkilletBlockEntity.java +++ b/src/main/java/mczme/lingshi/common/block/entity/SkilletBlockEntity.java @@ -1,5 +1,6 @@ package mczme.lingshi.common.block.entity; +import mczme.lingshi.client.menu.CookingItemStackHandler; import mczme.lingshi.client.menu.SkilletMenu; import mczme.lingshi.common.block.entity.baseblockentity.ICanBeHeated; import mczme.lingshi.common.datamap.DataMapTypes; @@ -12,6 +13,8 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientGamePacketListener; @@ -39,13 +42,14 @@ public class SkilletBlockEntity extends BlockEntity implements MenuProvider, ICa private final int MAX_SLOT = 5; private FluidStack fluidStacks = FluidStack.EMPTY; - private ItemStackHandler itemStackHandler = new ItemStackHandler(MAX_SLOT + 2); + private final CookingItemStackHandler itemStackHandler = new CookingItemStackHandler(MAX_SLOT + 2); private final int[] cookingTime = new int[5]; public SkilletBlockEntity(BlockPos pPos, BlockState pBlockState) { super(BlockEntityTypes.SKILLET_BLOCKENTITY.get(), pPos, pBlockState); + } public boolean isFull() { @@ -81,7 +85,7 @@ public FluidStack getFluid() { return fluidStacks; } - public ItemStackHandler getItemStacks() { + public CookingItemStackHandler getItemStacks() { return itemStackHandler; } @@ -95,36 +99,40 @@ public void setItem(ItemStack item) { } public void setItem(ItemStack item, int slot) { - itemStackHandler.setStackInSlot(slot, item); + itemStackHandler.insertItem(slot, item, false); } public void setFluid(FluidStack fluid) { fluidStacks = fluid; } - public int size(){ + public int size() { int size = 0; for (int i = 0; i < MAX_SLOT; i++) { - if(!itemStackHandler.getStackInSlot(i).isEmpty()){ + if (!itemStackHandler.getStackInSlot(i).isEmpty()) { size++; } } return size; } - public void clear(){ + public void clear() { for (int i = 0; i < MAX_SLOT; i++) { itemStackHandler.setStackInSlot(i, ItemStack.EMPTY); } } - public void consume(){ + public void consume() { for (int i = 0; i < MAX_SLOT; i++) { int count = itemStackHandler.getStackInSlot(i).getCount(); itemStackHandler.getStackInSlot(i).setCount(count - 1); } } + public int[] getCookingTime() { + return cookingTime; + } + public int getMAX() { return MAX_SLOT; } @@ -141,6 +149,7 @@ public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) { if (!fluidStacks.isEmpty()) { tag.put("fluid", fluidStacks.save(pRegistries)); } + saveCookingTime(tag); return tag; } @@ -150,6 +159,9 @@ protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistrie if (pTag.get("fluid") != null) { fluidStacks = FluidStack.parse(pRegistries, pTag.getCompound("fluid")).orElse(FluidStack.EMPTY); } + if (pTag.get("cookingTime") != null && pTag.getInt("Size") != 0) { + loadCookingTime(pTag.getList("cookingTime", Tag.TAG_COMPOUND),pTag.getInt("Size")); + } } @Override @@ -159,6 +171,24 @@ protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistrie if (!this.fluidStacks.isEmpty()) { pTag.put("fluid", fluidStacks.save(pRegistries)); } + saveCookingTime(pTag); + } + + private void saveCookingTime(CompoundTag nbt){ + ListTag nbtTagList = new ListTag(); + for (int i = 0; i < MAX_SLOT; i++) { + CompoundTag itemTag = new CompoundTag(); + itemTag.putInt("index", cookingTime[i]); + nbtTagList.add(itemTag); + } + nbt.put("cookingTime", nbtTagList); + nbt.putInt("Size", MAX_SLOT); + } + + private void loadCookingTime(ListTag pTag,int size){ + for (int i = 0; i < size; i++) { + cookingTime[i]= pTag.getCompound(i).getInt("index"); + } } @Override @@ -181,7 +211,7 @@ public static void serverTick(Level pLevel, BlockPos pPo if (heat_flag) { if (!flag) { - int cookedTime=0,burntTime=0; + int cookedTime = 0, burntTime = 0; for (int i = 0; i < MAX_SLOT; i++) { ItemStack itemStack = itemStackHandler.getStackInSlot(i); if (itemStack.isEmpty()) { @@ -189,38 +219,38 @@ public static void serverTick(Level pLevel, BlockPos pPo } CookingFoodData cookingFoodData = itemStack.getItemHolder().getData(DataMapTypes.COOKING_FOOD_ITEM); if (cookingFoodData != null) { - cookedTime=cookingFoodData.cookedTime(); - burntTime=cookingFoodData.burntTime(); + cookedTime = cookingFoodData.cookedTime(); + burntTime = cookingFoodData.burntTime(); blockEntity.cookingTime[i]++; } } - SkilletRecipeInput input = new SkilletRecipeInput(itemStackHandler,blockEntity.getFluid()); + SkilletRecipeInput input = new SkilletRecipeInput(itemStackHandler, blockEntity.getFluid()); Optional> optional = pLevel.getRecipeManager().getRecipeFor( ModRecipes.SKILLET_RECIPE.get(), input, pLevel ); ItemStack result = optional.map(RecipeHolder::value) - .map(e->e.assemble(input,pLevel.registryAccess())) + .map(e -> e.assemble(input, pLevel.registryAccess())) .orElse(ItemStack.EMPTY); if (!result.isEmpty()) { - int size=0; + int size = 0; for (int i = 0; i < MAX_SLOT; i++) { - if(blockEntity.cookingTime[i]>cookedTime*20){ + if (blockEntity.cookingTime[i] > cookedTime * 20) { size++; } } - if (size==blockEntity.size()){ - Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY()+0.2, pPos.getZ(), result); - Arrays.fill(blockEntity.cookingTime,0); + if (size == blockEntity.size()) { + Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY() + 0.2, pPos.getZ(), result); + Arrays.fill(blockEntity.cookingTime, 0); blockEntity.consume(); - pLevel.sendBlockUpdated(pPos, blockState, blockState, Block.UPDATE_CLIENTS); } - }else { - Arrays.fill(blockEntity.cookingTime,0); - } + } else { + Arrays.fill(blockEntity.cookingTime, 0); } + pLevel.sendBlockUpdated(pPos, blockState, blockState, Block.UPDATE_CLIENTS); } - blockEntity.setChanged(); } + blockEntity.setChanged(); } +} diff --git a/src/main/java/mczme/lingshi/common/data/builder/SkilletRecipeBuilder.java b/src/main/java/mczme/lingshi/common/data/builder/SkilletRecipeBuilder.java index c38a4a2..3e9f39b 100644 --- a/src/main/java/mczme/lingshi/common/data/builder/SkilletRecipeBuilder.java +++ b/src/main/java/mczme/lingshi/common/data/builder/SkilletRecipeBuilder.java @@ -28,7 +28,7 @@ public class SkilletRecipeBuilder implements RecipeBuilder { private final List items; private final FluidStack fluid; - private ItemStack container; + private SkilletRecipe.SkilletCookingContainer container; private final ItemStack result; protected final Map> criteria = new LinkedHashMap<>(); @@ -59,7 +59,7 @@ public SkilletRecipeBuilder setLabel(@Nullable CookingFoodRecipeLabel pLabelName return this; } - public SkilletRecipeBuilder setContainer(ItemStack container) { + public SkilletRecipeBuilder setContainer(SkilletRecipe.SkilletCookingContainer container) { this.container = container; return this; } @@ -77,7 +77,9 @@ public void save(RecipeOutput pRecipeOutput, ResourceLocation pId) { .rewards(AdvancementRewards.Builder.recipe(id)) .requirements(AdvancementRequirements.Strategy.OR); this.criteria.forEach(advancement::addCriterion); - SkilletRecipe recipe = new SkilletRecipe(this.items, this.fluid, this.container, this.result, Objects.requireNonNullElse(this.group, ""), Objects.requireNonNullElse(this.label, MISC)); + SkilletRecipe recipe = new SkilletRecipe(this.items, this.fluid, this.container, this.result + , Objects.requireNonNullElse(this.group, "") + , Objects.requireNonNullElse(this.label, MISC)); pRecipeOutput.accept(id, recipe, advancement.build(id.withPrefix("recipes/"))); } } diff --git a/src/main/java/mczme/lingshi/common/data/recipe/SkilletRecipeDatagen.java b/src/main/java/mczme/lingshi/common/data/recipe/SkilletRecipeDatagen.java index c833fc5..3a6bca3 100644 --- a/src/main/java/mczme/lingshi/common/data/recipe/SkilletRecipeDatagen.java +++ b/src/main/java/mczme/lingshi/common/data/recipe/SkilletRecipeDatagen.java @@ -2,6 +2,7 @@ import mczme.lingshi.client.recipebook.CookingFoodRecipeLabel; import mczme.lingshi.common.data.builder.SkilletRecipeBuilder; +import mczme.lingshi.common.recipe.SkilletRecipe; import mczme.lingshi.common.registry.ModItems; import net.minecraft.data.recipes.RecipeOutput; import net.minecraft.world.item.ItemStack; @@ -19,7 +20,7 @@ public SkilletRecipeDatagen(RecipeOutput output) { protected void buildRecipes(RecipeOutput output) { build(List.of(Ingredient.of(ModItems.RICE.get())), null, new ItemStack(Items.APPLE.asItem())) .setLabel(CookingFoodRecipeLabel.PAN_FRY) - .setContainer(new ItemStack(Items.BOWL)).save(output,"rice_apple"); + .setContainer(new SkilletRecipe.SkilletCookingContainer(new ItemStack(Items.BOWL),0)).save(output,"rice_apple"); build(List.of(Ingredient.of(Items.GOLD_BLOCK),Ingredient.of(Items.IRON_BLOCK)), null, new ItemStack(Items.DIAMOND.asItem())) .setLabel(CookingFoodRecipeLabel.BOIL).save(output,"test01"); build(List.of(Ingredient.of(Items.DIAMOND_BLOCK),Ingredient.of(Items.MILK_BUCKET)), null, new ItemStack(Items.WHEAT.asItem())) 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 d03f7ee..f1b1053 100644 --- a/src/main/java/mczme/lingshi/common/datamap/ingredient/CookingFoodData.java +++ b/src/main/java/mczme/lingshi/common/datamap/ingredient/CookingFoodData.java @@ -9,14 +9,13 @@ public record CookingFoodData(int cookedTime,int burntTime) { public CookingFoodData(int cookedTime,int burntTime){ if(cookedTime > MAX_TIME) this.cookedTime = MAX_TIME; - else if(cookedTime < 0) this.cookedTime = 0; - else this.cookedTime = cookedTime; + else this.cookedTime = Math.max(cookedTime, 0); this.burntTime = burntTime; } public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Codec.INT.fieldOf("cooked_time").forGetter(CookingFoodData::cookedTime), - Codec.INT.fieldOf("complete_time").forGetter(CookingFoodData::burntTime) + Codec.INT.fieldOf("burnt_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 a66910b..6872876 100644 --- a/src/main/java/mczme/lingshi/common/event/Registry.java +++ b/src/main/java/mczme/lingshi/common/event/Registry.java @@ -1,9 +1,15 @@ package mczme.lingshi.common.event; +import mczme.lingshi.common.network.CookingData; +import mczme.lingshi.common.network.CookingDataClientPayloadHandler; +import mczme.lingshi.common.network.CookingDataServerPayloadHandler; import mczme.lingshi.lingshi; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; +import net.neoforged.neoforge.network.handling.DirectionalPayloadHandler; +import net.neoforged.neoforge.network.registration.PayloadRegistrar; import net.neoforged.neoforge.registries.datamaps.RegisterDataMapTypesEvent; import static mczme.lingshi.common.datamap.DataMapTypes.*; @@ -24,4 +30,18 @@ private static void registerCapabilities(RegisterCapabilitiesEvent event) { } +// network + @SubscribeEvent + public static void registerPayloadHandlers(final RegisterPayloadHandlersEvent event) { + final PayloadRegistrar registrar = event.registrar("cooking_data"); + registrar.playBidirectional( + CookingData.TYPE, + CookingData.STREAM_CODEC, + new DirectionalPayloadHandler<>( + CookingDataClientPayloadHandler::handleData, + CookingDataServerPayloadHandler::handleData + ) + ); + } + } diff --git a/src/main/java/mczme/lingshi/common/network/CookingData.java b/src/main/java/mczme/lingshi/common/network/CookingData.java new file mode 100644 index 0000000..8708a35 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/network/CookingData.java @@ -0,0 +1,25 @@ +package mczme.lingshi.common.network; + +import mczme.lingshi.lingshi; +import net.minecraft.core.BlockPos; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.NotNull; + +public record CookingData(BlockPos pPos, ItemStack result) implements CustomPacketPayload { + + public static final CustomPacketPayload.Type TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(lingshi.MODID, "skillet_cooking_data")); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + BlockPos.STREAM_CODEC, CookingData::pPos, + ItemStack.OPTIONAL_STREAM_CODEC,CookingData::result, + CookingData::new); + + @Override + public @NotNull Type type() { + return TYPE; + } +} diff --git a/src/main/java/mczme/lingshi/common/network/CookingDataClientPayloadHandler.java b/src/main/java/mczme/lingshi/common/network/CookingDataClientPayloadHandler.java new file mode 100644 index 0000000..3a279ab --- /dev/null +++ b/src/main/java/mczme/lingshi/common/network/CookingDataClientPayloadHandler.java @@ -0,0 +1,32 @@ +package mczme.lingshi.common.network; + +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.world.item.ItemStack; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public class CookingDataClientPayloadHandler { + + private static BlockPos blockPos; + private static ItemStack result; + + public static void handleData(final CookingData data, final IPayloadContext context) { + context.enqueueWork(() -> { + blockPos = data.pPos(); + result = data.result(); + }) + .exceptionally(e -> { + // Handle exception + context.disconnect(Component.translatable("lingshi.networking.failed", e.getMessage())); + return null; + }); + } + + public static BlockPos getBlockPos(){ + return blockPos; + } + + public static ItemStack getResult() { + return result; + } +} diff --git a/src/main/java/mczme/lingshi/common/network/CookingDataServerPayloadHandler.java b/src/main/java/mczme/lingshi/common/network/CookingDataServerPayloadHandler.java new file mode 100644 index 0000000..a1f9186 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/network/CookingDataServerPayloadHandler.java @@ -0,0 +1,22 @@ +package mczme.lingshi.common.network; + +import net.minecraft.network.chat.Component; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public class CookingDataServerPayloadHandler { + + public static void handleData(final CookingData data, final IPayloadContext context) { + // Do something with the data, on the network thread + + // Do something with the data, on the main thread + context.enqueueWork(() -> { + + }) + .exceptionally(e -> { + // Handle exception + context.disconnect(Component.translatable("lingshi.networking.failed", e.getMessage())); + return null; + }); + } + +} diff --git a/src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java b/src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java index 534fcac..3481411 100644 --- a/src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java +++ b/src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java @@ -1,11 +1,16 @@ package mczme.lingshi.common.recipe; +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; import mczme.lingshi.client.recipebook.CookingFoodRecipeLabel; 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.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.Recipe; @@ -17,21 +22,23 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import static mczme.lingshi.common.util.ModCodec.ModoptionalField; import static net.neoforged.neoforge.common.util.RecipeMatcher.findMatches; -public class SkilletRecipe extends CookingFoodRecipe implements Recipe{ +public class SkilletRecipe extends CookingFoodRecipe implements Recipe { public static final int MAX_SLOT = 5; private final List items; private final FluidStack fluids; - private final ItemStack container; + private final SkilletCookingContainer container; private final ItemStack result; final String group; - public SkilletRecipe(List items, FluidStack fluids,ItemStack container, ItemStack result, String group, CookingFoodRecipeLabel label) { + public SkilletRecipe(List items, FluidStack fluids, SkilletCookingContainer container, ItemStack result, String group, CookingFoodRecipeLabel label) { super(label); this.items = items; this.fluids = fluids; @@ -41,17 +48,17 @@ public SkilletRecipe(List items, FluidStack fluids,ItemStack contain } @Override - public NonNullList getIngredients() { + public @NotNull NonNullList getIngredients() { NonNullList list = NonNullList.create(); list.addAll(items); return list; } @Override - public boolean matches(SkilletRecipeInput pInput, Level pLevel) { + public boolean matches(SkilletRecipeInput pInput, @NotNull Level pLevel) { List inputs = new ArrayList<>(); int i = 0; - if(!pInput.isEmpty()){ + if (!pInput.isEmpty()) { for (int j = 0; j < pInput.size(); ++j) { ItemStack itemstack = pInput.getItem(j); if (!itemstack.isEmpty()) { @@ -61,11 +68,11 @@ public boolean matches(SkilletRecipeInput pInput, Level pLevel) { } } - return i == this.items.size() && findMatches(inputs, this.items) != null && pInput.getFluid()==FluidStack.EMPTY; + return i == this.items.size() && findMatches(inputs, this.items) != null && pInput.getFluid() == FluidStack.EMPTY; } @Override - public ItemStack assemble(SkilletRecipeInput pInput, HolderLookup.Provider pRegistries) { + public @NotNull ItemStack assemble(@NotNull SkilletRecipeInput pInput, HolderLookup.@NotNull Provider pRegistries) { return this.result.copy(); } @@ -75,17 +82,17 @@ public boolean canCraftInDimensions(int pWidth, int pHeight) { } @Override - public ItemStack getResultItem(HolderLookup.Provider pRegistries) { + public @NotNull ItemStack getResultItem(HolderLookup.@NotNull Provider pRegistries) { return this.result; } @Override - public RecipeSerializer getSerializer() { + public @NotNull RecipeSerializer getSerializer() { return ModSerializer.SKILLET_SERIALIZER.get(); } @Override - public RecipeType getType() { + public @NotNull RecipeType getType() { return ModRecipes.SKILLET_RECIPE.get(); } @@ -101,7 +108,7 @@ public List getItems() { return items; } - public ItemStack getContainer() { + public SkilletCookingContainer getContainer() { return container; } @@ -109,4 +116,40 @@ public ItemStack getContainer() { public @NotNull String getGroup() { return this.group; } + + public record SkilletCookingContainer(ItemStack container, int stirFryCount) { + + public static final SkilletCookingContainer EMPTY = new SkilletCookingContainer(ItemStack.EMPTY,0); + + public static final Codec CODEC = Codec.lazyInitialized( + ()->RecordCodecBuilder.create(inst->inst.group( + ModoptionalField("container", ItemStack.OPTIONAL_CODEC, ItemStack.EMPTY).forGetter(SkilletCookingContainer::container), + ModoptionalField("stir_fry_count",Codec.INT,0).forGetter(SkilletCookingContainer::stirFryCount) + ).apply(inst,SkilletCookingContainer::new))); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ItemStack.OPTIONAL_STREAM_CODEC,SkilletCookingContainer::container, + ByteBufCodecs.INT,SkilletCookingContainer::stirFryCount, + SkilletCookingContainer::new + ); + + @Override + public ItemStack container() { + return container.copy(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SkilletCookingContainer that = (SkilletCookingContainer) o; + return stirFryCount == that.stirFryCount && Objects.equals(container, that.container); + } + + @Override + public int hashCode() { + return Objects.hash(container, stirFryCount); + } + } + } diff --git a/src/main/java/mczme/lingshi/common/recipe/serializer/SkilletRecipeSerializer.java b/src/main/java/mczme/lingshi/common/recipe/serializer/SkilletRecipeSerializer.java index 43e80a9..1d49a85 100644 --- a/src/main/java/mczme/lingshi/common/recipe/serializer/SkilletRecipeSerializer.java +++ b/src/main/java/mczme/lingshi/common/recipe/serializer/SkilletRecipeSerializer.java @@ -13,17 +13,18 @@ import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.RecipeSerializer; import net.neoforged.neoforge.fluids.FluidStack; +import org.jetbrains.annotations.NotNull; import java.util.List; -import static mczme.lingshi.common.utility.ModCodec.ModoptionalField; +import static mczme.lingshi.common.util.ModCodec.ModoptionalField; public class SkilletRecipeSerializer implements RecipeSerializer { public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group( Ingredient.CODEC.listOf().fieldOf("ingredients").forGetter(SkilletRecipe::getItems), ModoptionalField("fluid",FluidStack.OPTIONAL_CODEC,FluidStack.EMPTY).forGetter(SkilletRecipe::getFluid), - ModoptionalField("container", ItemStack.OPTIONAL_CODEC,ItemStack.EMPTY).forGetter(SkilletRecipe::getContainer), + ModoptionalField("container", SkilletRecipe.SkilletCookingContainer.CODEC,SkilletRecipe.SkilletCookingContainer.EMPTY).forGetter(SkilletRecipe::getContainer), ItemStack.CODEC.fieldOf("result").forGetter(SkilletRecipe::getResult), Codec.STRING.fieldOf("group").forGetter(SkilletRecipe::getGroup), CookingFoodRecipeLabel.CODEC.fieldOf("label").forGetter(SkilletRecipe::getLabel) @@ -38,7 +39,7 @@ public class SkilletRecipeSerializer implements RecipeSerializer StreamCodec.composite( INGREDIENT_STREAM_CODEC, SkilletRecipe::getItems, StreamCodec.of(SkilletRecipeSerializer::FluidStack_encode, SkilletRecipeSerializer::FluidStack_decode), SkilletRecipe::getFluid, - StreamCodec.of(SkilletRecipeSerializer::ItemStack_encode, SkilletRecipeSerializer::ItemStack_decode), SkilletRecipe::getContainer, + SkilletRecipe.SkilletCookingContainer.STREAM_CODEC, SkilletRecipe::getContainer, ItemStack.STREAM_CODEC, SkilletRecipe::getResult, ByteBufCodecs.STRING_UTF8, SkilletRecipe::getGroup, CookingFoodLabel_STREAM_CODEC, SkilletRecipe::getLabel, @@ -46,12 +47,12 @@ public class SkilletRecipeSerializer implements RecipeSerializer ); @Override - public MapCodec codec() { + public @NotNull MapCodec codec() { return CODEC; } @Override - public StreamCodec streamCodec() { + public @NotNull StreamCodec streamCodec() { return STREAM_CODEC; } @@ -64,14 +65,6 @@ public static void FluidStack_encode(RegistryFriendlyByteBuf pBuffer, FluidStack FluidStack.OPTIONAL_STREAM_CODEC.encode(pBuffer, fluidStack); } - public static ItemStack ItemStack_decode(RegistryFriendlyByteBuf pBuffer) { - return ItemStack.OPTIONAL_STREAM_CODEC.decode(pBuffer); - } - - public static void ItemStack_encode(RegistryFriendlyByteBuf pBuffer, ItemStack itemStack) { - ItemStack.OPTIONAL_STREAM_CODEC.encode(pBuffer, itemStack); - } - public static CookingFoodRecipeLabel decode(FriendlyByteBuf buffer) { return CookingFoodRecipeLabel.isinside(buffer.readUtf()); } diff --git a/src/main/java/mczme/lingshi/common/registry/ModItems.java b/src/main/java/mczme/lingshi/common/registry/ModItems.java index f2ad34d..72b9831 100644 --- a/src/main/java/mczme/lingshi/common/registry/ModItems.java +++ b/src/main/java/mczme/lingshi/common/registry/ModItems.java @@ -1,6 +1,7 @@ package mczme.lingshi.common.registry; import mczme.lingshi.common.item.KnifeItem; +import mczme.lingshi.common.item.SpatulaItem; import mczme.lingshi.lingshi; import net.minecraft.world.item.*; import net.neoforged.bus.api.IEventBus; @@ -24,6 +25,7 @@ public class ModItems { public static final Supplier SKILLET = registerWithCreateTab("skillet", () -> new BlockItem(ModBlocks.SKILLET.get(),new Item.Properties())); public static final Supplier CHOPPING_BOARD = registerWithCreateTab("chopping_board", () -> new BlockItem(ModBlocks.CHOPPING_BOARD.get(), new Item.Properties())); + public static final Supplier SPATULA = registerWithCreateTab("spatula",()->new SpatulaItem(new Item.Properties())); public static final Supplier IRON_KNIFE = registerWithCreateTab("iron_knife", () -> new KnifeItem(Tiers.IRON,new Item.Properties().attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F)))); public static final Supplier OIL_BUCKET = registerWithCreateTab("oil_bucket",()->new BucketItem(ModFluids.OIL_SOURCE.get(),new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1))); diff --git a/src/main/java/mczme/lingshi/common/utility/ListUtility.java b/src/main/java/mczme/lingshi/common/util/ListUtil.java similarity index 83% rename from src/main/java/mczme/lingshi/common/utility/ListUtility.java rename to src/main/java/mczme/lingshi/common/util/ListUtil.java index 2175e14..be43107 100644 --- a/src/main/java/mczme/lingshi/common/utility/ListUtility.java +++ b/src/main/java/mczme/lingshi/common/util/ListUtil.java @@ -1,11 +1,11 @@ -package mczme.lingshi.common.utility; +package mczme.lingshi.common.util; import net.minecraft.world.item.ItemStack; import java.util.ArrayList; import java.util.List; -public class ListUtility { +public class ListUtil { public static List copy(List list){ List newList = new ArrayList<>(); diff --git a/src/main/java/mczme/lingshi/common/util/ModCodec.java b/src/main/java/mczme/lingshi/common/util/ModCodec.java new file mode 100644 index 0000000..7952cf4 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/util/ModCodec.java @@ -0,0 +1,19 @@ +package mczme.lingshi.common.util; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.OptionalFieldCodec; + +import java.util.Optional; + +public class ModCodec { + +// 序列化工具,可以对null进行处理 + public static MapCodec ModoptionalField(final String name, final Codec codec, T defaultValue) { + return new OptionalFieldCodec<>(name, codec, false).xmap( + o -> o.orElse(defaultValue), + a -> a==null ? Optional.empty() : Optional.of(a) + ); + } + +} diff --git a/src/main/java/mczme/lingshi/common/utility/ModCodec.java b/src/main/java/mczme/lingshi/common/utility/ModCodec.java deleted file mode 100644 index cdd866f..0000000 --- a/src/main/java/mczme/lingshi/common/utility/ModCodec.java +++ /dev/null @@ -1,28 +0,0 @@ -package mczme.lingshi.common.utility; - -import com.mojang.serialization.Codec; -import com.mojang.serialization.MapCodec; -import com.mojang.serialization.codecs.OptionalFieldCodec; -import net.minecraft.world.item.ItemStack; -import net.neoforged.neoforge.fluids.FluidStack; - -import java.util.Optional; - -public class ModCodec { - -// 序列化工具,可以对null进行处理 - public static MapCodec ModoptionalField(final String name, final Codec codec, ItemStack defaultValue) { - return new OptionalFieldCodec<>(name, codec, false).xmap( - o -> o.orElse(defaultValue), - a -> a==null ? Optional.empty() : Optional.of(a) - ); - } - - public static MapCodec ModoptionalField(final String name, final Codec codec, FluidStack defaultValue) { - return new OptionalFieldCodec<>(name, codec, false).xmap( - o -> o.orElse(defaultValue), - a -> a==null ? Optional.empty() : Optional.of(a) - ); - } - -} diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ + diff --git a/src/main/resources/META-INF/neoforge.mods.toml b/src/main/resources/META-INF/neoforge.mods.toml index 0e217a4..cfbacc4 100644 --- a/src/main/resources/META-INF/neoforge.mods.toml +++ b/src/main/resources/META-INF/neoforge.mods.toml @@ -56,8 +56,8 @@ description='''${mod_description}''' # The [[accessTransformers]] block allows you to declare where your AT file is. # If this block is omitted, a fallback attempt will be made to load an AT from META-INF/accesstransformer.cfg -#[[accessTransformers]] -#file="META-INF/accesstransformer.cfg" +[[accessTransformers]] +file="META-INF/accesstransformer.cfg" # The coremods config file path is not configurable and is always loaded from META-INF/coremods.json