From 810ccd64f34135fcf9ad306f85a5da50983e27ca Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Thu, 19 Dec 2024 23:22:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0fumo25=20BE=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/block/FuMO25Block.java | 22 +++++- .../block/entity/FuMO25BlockEntity.java | 75 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java b/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java index d1e92e942..42f9b5aff 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java @@ -1,10 +1,15 @@ package com.atsuishio.superbwarfare.block; +import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity; +import com.atsuishio.superbwarfare.init.ModBlockEntities; import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.EntityBlock; import net.minecraft.world.level.block.SoundType; 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.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.Nullable; @@ -18,6 +23,21 @@ public class FuMO25Block extends Block implements EntityBlock { @Nullable @Override public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) { - return null; + return new FuMO25BlockEntity(pPos, pState); + } + + @Nullable + @Override + public BlockEntityTicker getTicker(Level pLevel, BlockState pState, BlockEntityType pBlockEntityType) { + if (!pLevel.isClientSide) { + return createTickerHelper(pBlockEntityType, ModBlockEntities.FUMO_25.get(), FuMO25BlockEntity::serverTick); + } else { + return createTickerHelper(pBlockEntityType, ModBlockEntities.FUMO_25.get(), FuMO25BlockEntity::clientTick); + } + } + + @Nullable + protected static BlockEntityTicker createTickerHelper(BlockEntityType pServerType, BlockEntityType pClientType, BlockEntityTicker pTicker) { + return pClientType == pServerType ? (BlockEntityTicker) pTicker : null; } } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java index 5736a17df..2d98cc848 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java @@ -2,19 +2,62 @@ package com.atsuishio.superbwarfare.block.entity; import com.atsuishio.superbwarfare.init.ModBlockEntities; 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.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.util.Mth; import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.level.Level; 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.capabilities.ForgeCapabilities; +import net.minecraftforge.common.util.LazyOptional; +import net.minecraftforge.energy.EnergyStorage; import org.jetbrains.annotations.Nullable; public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { + public static final int MAX_ENERGY = 1000000; + + private LazyOptional energyHandler; + + public FuncType type = FuncType.NORMAL; + public FuMO25BlockEntity(BlockPos pPos, BlockState pBlockState) { super(ModBlockEntities.FUMO_25.get(), pPos, pBlockState); + + this.energyHandler = LazyOptional.of(() -> new EnergyStorage(MAX_ENERGY)); + } + + public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, FuMO25BlockEntity blockEntity) { + + } + + public static void clientTick(Level pLevel, BlockPos pPos, BlockState pState, FuMO25BlockEntity blockEntity) { + + } + + @Override + public void load(CompoundTag pTag) { + super.load(pTag); + + if (pTag.contains("Energy")) { + getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> ((EnergyStorage) handler).deserializeNBT(pTag.get("Energy"))); + } + this.type = FuncType.values()[Mth.clamp(pTag.getInt("Type"), 0, 3)]; + } + + @Override + protected void saveAdditional(CompoundTag pTag) { + super.saveAdditional(pTag); + + getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> pTag.put("Energy", ((EnergyStorage) handler).serializeNBT())); + pTag.putInt("Type", this.type.ordinal()); } @Override @@ -27,4 +70,36 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) { return null; } + + @Override + public ClientboundBlockEntityDataPacket getUpdatePacket() { + return ClientboundBlockEntityDataPacket.create(this); + } + + @Override + public LazyOptional getCapability(Capability cap, Direction side) { + if (cap == ForgeCapabilities.ENERGY) { + return energyHandler.cast(); + } + return super.getCapability(cap, side); + } + + @Override + public void invalidateCaps() { + super.invalidateCaps(); + this.energyHandler.invalidate(); + } + + @Override + public void reviveCaps() { + super.reviveCaps(); + this.energyHandler = LazyOptional.of(() -> new EnergyStorage(MAX_ENERGY)); + } + + public enum FuncType { + NORMAL, + WIDER, + GLOW, + GUIDE + } }