Minecraft1.12.2 方块实体教程
MC中有许多很有趣的方块实体如旗帜、钟、附魔台…我们今天在1.18的版本下实现一个类似于熔炉的方块实体。
1.在blocks包中新建一个我们的方块实体包virusgenerator,包中新建一个我们的方块类VirusGeneratorBlock:
VirusGeneratorBlock.java
package com.joy187.re8joymod.blocks.virusgenerator;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
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.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;
import com.joy187.re8joymod.init.BlockEntityInit;
public class VirusGeneratorBlock extends BaseEntityBlock {
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
public static final BooleanProperty LIT = BlockStateProperties.LIT;
public VirusGeneratorBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(LIT, Boolean.valueOf(false)));
}
private static final VoxelShape SHAPE = Block.box(0, 0, 0, 16, 15, 16);
@Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return SHAPE;
}
/* FACING */
@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
return this.defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite());
}
@Override
public BlockState rotate(BlockState pState, Rotation pRotation) {
return pState.setValue(FACING, pRotation.rotate(pState.getValue(FACING)));
}
@Override
public BlockState mirror(BlockState pState, Mirror pMirror) {
return pState.rotate(pMirror.getRotation(pState.getValue(FACING)));
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
pBuilder.add(FACING,LIT);
}
@Override
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}
@Override
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
if (pState.getBlock() != pNewState.getBlock()) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
if (blockEntity instanceof VirusGeneratorBlockEntity) {
((VirusGeneratorBlockEntity) blockEntity).drops();
}
}
super.onRemove(pState, pLevel, pPos, pNewState, pIsMoving);
}
@Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos,
Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
if (!pLevel.isClientSide()) {
BlockEntity entity = pLevel.getBlockEntity(pPos);
if(entity instanceof VirusGeneratorBlockEntity) {
NetworkHooks.openGui(((ServerPlayer)pPlayer), (VirusGeneratorBlockEntity)entity, pPos);
} else {
throw new IllegalStateException("Our Container provider is missing!");
}
}
return InteractionResult.sidedSuccess(pLevel.isClientSide());
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return new VirusGeneratorBlockEntity(pPos, pState);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
return createTickerHelper(pBlockEntityType, BlockEntityInit.VIRUS_GENERATOR_BLOCK_ENTITY.get(),
VirusGeneratorBlockEntity::tick);
}
}
在BlockInit中注册我们的方块:
public static final RegistryObject<Block> VIRUS_GENERATOR_BLOCK = registerBlock("virus_generator",
() -> new VirusGeneratorBlock(BlockBehaviour.Properties.copy(Blocks.IRON_BLOCK).noOcclusion()),
Main.TUTORIAL_TAB);
2.在virusgenerator包中新建一个我们的实体类VirusGeneratorBlockEntity:
VirusGeneratorBlockEntity.java
package com.joy187.re8joymod.blocks.virusgenerator;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.Containers;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.AbstractFurnaceBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.joy187.re8joymod.init.BlockEntityInit;
import com.joy187.re8joymod.init.ItemInit;
import com.joy187.re8joymod.screen.VirusGeneratorMenu;
import javax.annotation.Nonnull;
import java.util.Optional;
import java.util.Random;
public class VirusGeneratorBlockEntity extends BlockEntity implements MenuProvider {
private BlockState blockState;
private final ItemStackHandler itemHandler = new ItemStackHandler(4) {
@Override
protected void onContentsChanged(int slot) {
setChanged();
}
};
private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();
protected final ContainerData data;
private int progress = 0;
//你熔炼一个物品所需要的时间刻tick
private int maxProgress = 72;
public VirusGeneratorBlockEntity(BlockPos pWorldPosition, BlockState pBlockState) {
super(BlockEntityInit.VIRUS_GENERATOR_BLOCK_ENTITY.get(), pWorldPosition, pBlockState);
this.data = new ContainerData(

本文详细介绍了如何在Minecraft 1.18版本中创建一个功能类似熔炉的VirusGenerator Block,包括方块实体的实现、配方设计、GUI界面制作及资源文件配置。跟随教程,你可以自制一个能处理物品的方块实体。
最低0.47元/天 解锁文章
196

被折叠的 条评论
为什么被折叠?



