添加fumo25 BE基本方法
This commit is contained in:
parent
74bbe96c5b
commit
810ccd64f3
2 changed files with 96 additions and 1 deletions
|
@ -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 <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> 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 <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTickerHelper(BlockEntityType<A> pServerType, BlockEntityType<E> pClientType, BlockEntityTicker<? super E> pTicker) {
|
||||
return pClientType == pServerType ? (BlockEntityTicker<A>) pTicker : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<EnergyStorage> 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 <T> LazyOptional<T> getCapability(Capability<T> 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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue