From ecd0486df6d4d85cc18669ae9d0344bfab9e707d Mon Sep 17 00:00:00 2001 From: MCZME <13183052+mczme@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 03:30:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0:=E8=8F=9C=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../85f12f813aff948f91f5cd129c0ffa86bcb17361 | 4 +- .../resources/assets/lingshi/lang/zh_cn.json | 1 + .../common/block/ChoppingBoardBlock.java | 80 +++++++++++++++++++ .../common/block/RiceSeedlingBlock.java | 6 +- .../entity/ChoppingBoardBlockEntity.java | 65 +++++++++++++++ .../data/lang/ChineseLanguageProvider.java | 2 + .../lingshi/common/registry/BlockEntitys.java | 25 ++++++ .../lingshi/common/registry/ModBlocks.java | 2 + .../lingshi/common/registry/ModItems.java | 2 + src/main/java/mczme/lingshi/lingshi.java | 2 + 11 files changed, 185 insertions(+), 6 deletions(-) create mode 100644 src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java create mode 100644 src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java create mode 100644 src/main/java/mczme/lingshi/common/registry/BlockEntitys.java diff --git a/gradle.properties b/gradle.properties index 8ab08ec..f138230 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,7 +16,7 @@ minecraft_version=1.21 # as they do not follow standard versioning conventions. minecraft_version_range=[1.21,1.21.1) # The Neo version must agree with the Minecraft version to get a valid artifact -neo_version=21.0.114-beta +neo_version=21.0.164 # The Neo version range can use any version of Neo as bounds neo_version_range=[21.0.0-beta,) # The loader version range can only use the major version of FML as bounds diff --git a/src/generated/resources/.cache/85f12f813aff948f91f5cd129c0ffa86bcb17361 b/src/generated/resources/.cache/85f12f813aff948f91f5cd129c0ffa86bcb17361 index 7f111df..ec27b16 100644 --- a/src/generated/resources/.cache/85f12f813aff948f91f5cd129c0ffa86bcb17361 +++ b/src/generated/resources/.cache/85f12f813aff948f91f5cd129c0ffa86bcb17361 @@ -1,2 +1,2 @@ -// 1.21 2024-08-06T22:30:49.6383033 Languages: zh_cn for mod: lingshi -99918422a5d56dab647af19828bb12044cdf0336 assets/lingshi/lang/zh_cn.json +// 1.21 2024-08-07T23:57:08.977702 Languages: zh_cn for mod: lingshi +79a73567edeeb26224979ad3941587adb9a443e5 assets/lingshi/lang/zh_cn.json diff --git a/src/generated/resources/assets/lingshi/lang/zh_cn.json b/src/generated/resources/assets/lingshi/lang/zh_cn.json index af66bf7..4328c60 100644 --- a/src/generated/resources/assets/lingshi/lang/zh_cn.json +++ b/src/generated/resources/assets/lingshi/lang/zh_cn.json @@ -1,4 +1,5 @@ { + "block.lingshi.chopping_board": "菜板", "block.lingshi.rice": "稻米", "block.lingshi.rice_seedling": "稻苗", "block.lingshi.tea_tree": "茶树", diff --git a/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java b/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java new file mode 100644 index 0000000..971d6f4 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java @@ -0,0 +1,80 @@ +package mczme.lingshi.common.block; + +import com.mojang.serialization.MapCodec; +import mczme.lingshi.common.block.entity.ChoppingBoardBlockEntity; +import net.minecraft.core.BlockPos; +import net.minecraft.world.Containers; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.ItemInteractionResult; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.BaseEntityBlock; +import net.minecraft.world.level.block.RenderShape; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.phys.BlockHitResult; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class ChoppingBoardBlock extends BaseEntityBlock { + + public ChoppingBoardBlock(Properties properties) { + super(properties); + } + + @Override + protected MapCodec codec() { + return null; + } + + @Override + public ItemInteractionResult useItemOn( + ItemStack pStack, BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHitResult + ) { + if (pLevel.getBlockEntity(pPos) instanceof ChoppingBoardBlockEntity blockEntity) { + if (!pLevel.isClientSide) { + ItemStack stack = pStack.consumeAndReturn(1, pPlayer); + if (blockEntity.getTheItem().isEmpty() && !stack.isEmpty()) { + blockEntity.setTheItem(stack); + blockEntity.setChanged(); + return ItemInteractionResult.SUCCESS; + }else { + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; + } + } else { + return ItemInteractionResult.CONSUME; + } + } + return ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION; + + } + + @Override + protected InteractionResult useWithoutItem(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, BlockHitResult pHitResult) { + if (pLevel.getBlockEntity(pPos) instanceof ChoppingBoardBlockEntity blockentity) { + if (!pLevel.isClientSide) { + ItemStack stack = blockentity.getTheItem(); + if (!stack.isEmpty()) { + Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), stack); + blockentity.setTheItem(ItemStack.EMPTY); + blockentity.setChanged(); + return InteractionResult.SUCCESS; + } + } + } + return InteractionResult.PASS; + } + + @Nullable + @Override + public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) { + return new ChoppingBoardBlockEntity(pPos, pState); + } + + @Override + public RenderShape getRenderShape(BlockState pState) { + return RenderShape.MODEL; + } +} diff --git a/src/main/java/mczme/lingshi/common/block/RiceSeedlingBlock.java b/src/main/java/mczme/lingshi/common/block/RiceSeedlingBlock.java index 209106f..32121d4 100644 --- a/src/main/java/mczme/lingshi/common/block/RiceSeedlingBlock.java +++ b/src/main/java/mczme/lingshi/common/block/RiceSeedlingBlock.java @@ -146,12 +146,12 @@ public void performBonemeal(ServerLevel pLevel, RandomSource pRandom, BlockPos p pLevel.setBlockAndUpdate(pPos, pState.setValue(WATERLOGGED, false)); } } else { - RiceSeedlingTopBlock riceUpper = ModBlocks.RICE_SEEDING_TOP.get(); + RiceSeedlingTopBlock Upper = ModBlocks.RICE_SEEDING_TOP.get(); int remainingGrowth = ageGrowth - this.getMaxAge() - 1; - if (riceUpper.defaultBlockState().canSurvive(pLevel, pPos.above()) && pLevel.isEmptyBlock(pPos.above())) { + if (Upper.defaultBlockState().canSurvive(pLevel, pPos.above()) && pLevel.isEmptyBlock(pPos.above())) { pLevel.setBlockAndUpdate(pPos, pState.setValue(AGE, this.getMaxAge())); pLevel.setBlockAndUpdate(pPos, pState.setValue(EMERGE, true)); - pLevel.setBlock(pPos.above(), riceUpper.defaultBlockState().setValue(RiceSeedlingTopBlock.AGE, remainingGrowth), 2); + pLevel.setBlock(pPos.above(), Upper.defaultBlockState().setValue(RiceSeedlingTopBlock.AGE, remainingGrowth), 2); } } } diff --git a/src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java b/src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java new file mode 100644 index 0000000..e2ec53e --- /dev/null +++ b/src/main/java/mczme/lingshi/common/block/entity/ChoppingBoardBlockEntity.java @@ -0,0 +1,65 @@ +package mczme.lingshi.common.block.entity; + +import mczme.lingshi.common.registry.BlockEntitys; +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.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.ticks.ContainerSingleItem; + +public class ChoppingBoardBlockEntity extends BlockEntity implements ContainerSingleItem { + + private ItemStack item = ItemStack.EMPTY; + + public ChoppingBoardBlockEntity( BlockPos pPos, BlockState pBlockState) { + super(BlockEntitys.CHOPPING_BOARD_BLOCKENTITY.get(), pPos, pBlockState); + } + + + + public ClientboundBlockEntityDataPacket getUpdatePacket() { + return ClientboundBlockEntityDataPacket.create(this); + } + + @Override + public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) { + return this.saveCustomOnly(pRegistries); + } + + @Override + protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) { + if (pTag.contains("item", 10)) { + this.item = ItemStack.parse(pRegistries, pTag.getCompound("item")).orElse(ItemStack.EMPTY); + } else { + this.item = ItemStack.EMPTY; + } + } + + @Override + protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) { + super.saveAdditional(pTag, pRegistries); + if(!this.item.isEmpty()){ + pTag.put("item", this.item.save(pRegistries)); + } + } + + @Override + public ItemStack getTheItem() { + return this.item; + } + + @Override + public void setTheItem(ItemStack pItem) { + this.item= pItem; + } + + + @Override + public boolean stillValid(Player pPlayer) { + return true; + } +} diff --git a/src/main/java/mczme/lingshi/common/data/lang/ChineseLanguageProvider.java b/src/main/java/mczme/lingshi/common/data/lang/ChineseLanguageProvider.java index 6f8ceb7..ba1a25f 100644 --- a/src/main/java/mczme/lingshi/common/data/lang/ChineseLanguageProvider.java +++ b/src/main/java/mczme/lingshi/common/data/lang/ChineseLanguageProvider.java @@ -21,5 +21,7 @@ protected void addTranslations() { this.add(ModItems.TEA_TREE.get(), "茶树"); this.add(ModItems.TEA_LEAF.get(), "茶叶"); + + this.add(ModItems.CHOPPING_BOARD.get(), "菜板"); } } diff --git a/src/main/java/mczme/lingshi/common/registry/BlockEntitys.java b/src/main/java/mczme/lingshi/common/registry/BlockEntitys.java new file mode 100644 index 0000000..39eaee0 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/registry/BlockEntitys.java @@ -0,0 +1,25 @@ +package mczme.lingshi.common.registry; + +import mczme.lingshi.common.block.entity.ChoppingBoardBlockEntity; +import mczme.lingshi.lingshi; +import net.minecraft.core.registries.Registries; +import net.minecraft.world.level.block.entity.BlockEntityType; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.neoforge.registries.DeferredRegister; + +import java.util.function.Supplier; + +public class BlockEntitys { + + public static final DeferredRegister> BLOCK_ENTITIES = + DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, lingshi.MODID); + + public static final Supplier> CHOPPING_BOARD_BLOCKENTITY = + BLOCK_ENTITIES.register("chopping_board_blockentity", () -> + BlockEntityType.Builder.of(ChoppingBoardBlockEntity::new, + ModBlocks.CHOPPING_BOARD.get()).build(null)); + + public static void register(IEventBus eventBus) { + BLOCK_ENTITIES.register(eventBus); + } +} diff --git a/src/main/java/mczme/lingshi/common/registry/ModBlocks.java b/src/main/java/mczme/lingshi/common/registry/ModBlocks.java index b114cb4..c2cd0d7 100644 --- a/src/main/java/mczme/lingshi/common/registry/ModBlocks.java +++ b/src/main/java/mczme/lingshi/common/registry/ModBlocks.java @@ -28,6 +28,8 @@ public class ModBlocks { public static final Supplier TEA_TREE = BLOCKS.registerBlock("tea_tree", TeaTreeBlock::new,BlockBehaviour.Properties.of()); public static final Supplier TEA_LEAF = BLOCKS.registerBlock("tea_leaf", TeaBlock::new,BlockBehaviour.Properties.of()); + public static final Supplier CHOPPING_BOARD = BLOCKS.registerBlock("chopping_board", ChoppingBoardBlock::new,BlockBehaviour.Properties.of()); + public static void register(IEventBus modEventBus) { BLOCKS.register(modEventBus); } diff --git a/src/main/java/mczme/lingshi/common/registry/ModItems.java b/src/main/java/mczme/lingshi/common/registry/ModItems.java index d9df940..9aaaea4 100644 --- a/src/main/java/mczme/lingshi/common/registry/ModItems.java +++ b/src/main/java/mczme/lingshi/common/registry/ModItems.java @@ -21,6 +21,8 @@ public class ModItems { public static final Supplier TEA_TREE = registerWithCreateTab("tea_tree", () -> new BlockItem(ModBlocks.TEA_TREE.get(), new Item.Properties())); public static final Supplier TEA_LEAF = registerWithCreateTab("tea_leaf", () -> new Item(new Item.Properties())); + public static final Supplier CHOPPING_BOARD = registerWithCreateTab("chopping_board", () -> new BlockItem(ModBlocks.CHOPPING_BOARD.get(), new Item.Properties())); + private static Supplier registerWithCreateTab(String item_name, Supplier itemSupplier) { Supplier item = ITEMS.register(item_name, itemSupplier); ITEMS_LIST.add(item); diff --git a/src/main/java/mczme/lingshi/lingshi.java b/src/main/java/mczme/lingshi/lingshi.java index 52e3837..007d7bb 100644 --- a/src/main/java/mczme/lingshi/lingshi.java +++ b/src/main/java/mczme/lingshi/lingshi.java @@ -1,6 +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 org.slf4j.Logger; @@ -25,6 +26,7 @@ public lingshi(IEventBus modEventBus, ModContainer modContainer) ModBlocks.register(modEventBus); ModItems.register(modEventBus); CreateTabs.register(modEventBus); + BlockEntitys.register(modEventBus); }