Skip to content

Commit

Permalink
添加:菜板
Browse files Browse the repository at this point in the history
  • Loading branch information
MCZME committed Aug 7, 2024
1 parent 4105e58 commit ecd0486
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/generated/resources/assets/lingshi/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"block.lingshi.chopping_board": "菜板",
"block.lingshi.rice": "稻米",
"block.lingshi.rice_seedling": "稻苗",
"block.lingshi.tea_tree": "茶树",
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java
Original file line number Diff line number Diff line change
@@ -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<? extends BaseEntityBlock> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "菜板");
}
}
25 changes: 25 additions & 0 deletions src/main/java/mczme/lingshi/common/registry/BlockEntitys.java
Original file line number Diff line number Diff line change
@@ -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<BlockEntityType<?>> BLOCK_ENTITIES =
DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, lingshi.MODID);

public static final Supplier<BlockEntityType<ChoppingBoardBlockEntity>> 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);
}
}
2 changes: 2 additions & 0 deletions src/main/java/mczme/lingshi/common/registry/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class ModBlocks {
public static final Supplier<TeaTreeBlock> TEA_TREE = BLOCKS.registerBlock("tea_tree", TeaTreeBlock::new,BlockBehaviour.Properties.of());
public static final Supplier<TeaBlock> TEA_LEAF = BLOCKS.registerBlock("tea_leaf", TeaBlock::new,BlockBehaviour.Properties.of());

public static final Supplier<ChoppingBoardBlock> CHOPPING_BOARD = BLOCKS.registerBlock("chopping_board", ChoppingBoardBlock::new,BlockBehaviour.Properties.of());

public static void register(IEventBus modEventBus) {
BLOCKS.register(modEventBus);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mczme/lingshi/common/registry/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ModItems {
public static final Supplier<Item> TEA_TREE = registerWithCreateTab("tea_tree", () -> new BlockItem(ModBlocks.TEA_TREE.get(), new Item.Properties()));
public static final Supplier<Item> TEA_LEAF = registerWithCreateTab("tea_leaf", () -> new Item(new Item.Properties()));

public static final Supplier<Item> CHOPPING_BOARD = registerWithCreateTab("chopping_board", () -> new BlockItem(ModBlocks.CHOPPING_BOARD.get(), new Item.Properties()));

private static Supplier<Item> registerWithCreateTab(String item_name, Supplier<Item> itemSupplier) {
Supplier<Item> item = ITEMS.register(item_name, itemSupplier);
ITEMS_LIST.add(item);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mczme/lingshi/lingshi.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,6 +26,7 @@ public lingshi(IEventBus modEventBus, ModContainer modContainer)
ModBlocks.register(modEventBus);
ModItems.register(modEventBus);
CreateTabs.register(modEventBus);
BlockEntitys.register(modEventBus);

}

Expand Down

0 comments on commit ecd0486

Please sign in to comment.