From 8764a30316f9507b579642da4cf1905e121f819f Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Fri, 6 Dec 2024 22:55:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=85=85=E7=94=B5=E7=AB=99?= =?UTF-8?q?=E5=8F=91=E7=94=B5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block/ChargingStationBlock.java | 14 ++++ .../entity/ChargingStationBlockEntity.java | 75 +++++++++++++++++-- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/ChargingStationBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/ChargingStationBlock.java index 0a88b934c..80b3aedf6 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/ChargingStationBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/ChargingStationBlock.java @@ -1,9 +1,14 @@ package com.atsuishio.superbwarfare.block; +import com.atsuishio.superbwarfare.block.entity.ChargingStationBlockEntity; +import com.atsuishio.superbwarfare.init.ModBlockEntities; import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; import net.minecraft.world.level.block.BaseEntityBlock; 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; @@ -17,6 +22,15 @@ public class ChargingStationBlock extends BaseEntityBlock { @Nullable @Override public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) { + return new ChargingStationBlockEntity(pPos, pState); + } + + @Nullable + @Override + public BlockEntityTicker getTicker(Level pLevel, BlockState pState, BlockEntityType pBlockEntityType) { + if (!pLevel.isClientSide) { + return createTickerHelper(pBlockEntityType, ModBlockEntities.CHARGING_STATION.get(), ChargingStationBlockEntity::serverTick); + } return null; } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/ChargingStationBlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/ChargingStationBlockEntity.java index 6659d232d..0688178aa 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/ChargingStationBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/ChargingStationBlockEntity.java @@ -18,10 +18,15 @@ import net.minecraft.world.item.ItemStack; 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 net.minecraftforge.items.wrapper.SidedInvWrapper; import org.jetbrains.annotations.Nullable; +import java.util.concurrent.atomic.AtomicBoolean; + public class ChargingStationBlockEntity extends BlockEntity implements WorldlyContainer, MenuProvider { protected static final int SLOT_FUEL = 0; @@ -31,26 +36,70 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo private static final int[] SLOTS_FOR_SIDES = new int[]{0}; private static final int[] SLOTS_FOR_DOWN = new int[]{0}; + public static final int MAX_ENERGY = 4000000; public static final int MAX_DATA_COUNT = 2; - public static final int FUEL_TIME = 200; - public static final int CHARGE_SPEED = 20; + public static final int DEFAULT_FUEL_TIME = 1600; + public static final int CHARGE_SPEED = 128; protected NonNullList items = NonNullList.withSize(2, ItemStack.EMPTY); + private final LazyOptional energyHandler; private LazyOptional[] itemHandlers = SidedInvWrapper.create(this, Direction.UP, Direction.DOWN, Direction.NORTH); - public int fuelTick; - public int energy; + public int fuelTick = 0; + public int maxFuelTick = DEFAULT_FUEL_TIME; public ChargingStationBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.CHARGING_STATION.get(), pos, state); + + this.energyHandler = LazyOptional.of(() -> new EnergyStorage(MAX_ENERGY)); } public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, ChargingStationBlockEntity blockEntity) { if (blockEntity.fuelTick > 0) { + blockEntity.fuelTick--; + blockEntity.energyHandler.ifPresent(handler -> { + int energy = handler.getEnergyStored(); + if (energy < handler.getMaxEnergyStored()) { + handler.receiveEnergy(CHARGE_SPEED, false); + blockEntity.setChanged(); + } + }); + } else { + if (blockEntity.getItem(SLOT_FUEL).isEmpty()) return; + AtomicBoolean flag = new AtomicBoolean(false); + blockEntity.energyHandler.ifPresent(handler -> { + if (handler.getEnergyStored() >= handler.getMaxEnergyStored()) { + flag.set(true); + } + }); + if (flag.get()) return; + ItemStack fuel = blockEntity.getItem(SLOT_FUEL); + if (fuel.getBurnTime(null) > 0) { + blockEntity.fuelTick = fuel.getBurnTime(null); + blockEntity.maxFuelTick = fuel.getBurnTime(null); + fuel.shrink(1); + blockEntity.setChanged(); + } else if (fuel.getItem().isEdible()) { + var properties = fuel.getFoodProperties(null); + if (properties == null) return; + int nutrition = properties.getNutrition(); + float saturation = properties.getSaturationModifier() * 2.0f * nutrition; + int tick = nutrition * 80 + (int) (saturation * 200); + + if (fuel.hasCraftingRemainingItem()) { + tick += 400; + } + + fuel.shrink(1); + + blockEntity.fuelTick = tick; + blockEntity.maxFuelTick = tick; + blockEntity.setChanged(); + } } } @@ -62,8 +111,13 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo public void load(CompoundTag pTag) { super.load(pTag); - this.energy = pTag.getInt("Energy"); + if (pTag.contains("Energy")) { + getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> { + ((EnergyStorage) handler).deserializeNBT(pTag.get("Energy")); + }); + } this.fuelTick = pTag.getInt("FuelTick"); + this.maxFuelTick = pTag.getInt("MaxFuelTick"); this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY); ContainerHelper.loadAllItems(pTag, this.items); } @@ -72,8 +126,9 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo protected void saveAdditional(CompoundTag pTag) { super.saveAdditional(pTag); - pTag.putInt("Energy", this.energy); + getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> pTag.put("Energy", ((EnergyStorage) handler).serializeNBT())); pTag.putInt("FuelTick", this.fuelTick); + pTag.putInt("MaxFuelTick", this.maxFuelTick); ContainerHelper.saveAllItems(pTag, this.items); } @@ -169,4 +224,12 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo ContainerHelper.saveAllItems(compoundtag, this.items, true); return compoundtag; } + + @Override + public LazyOptional getCapability(Capability cap, Direction side) { + if (cap == ForgeCapabilities.ENERGY) { + return energyHandler.cast(); + } + return super.getCapability(cap, side); + } }