From 80bdd7c6acbb851c3367cfc510b5150a9ed74586 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Fri, 6 Dec 2024 23:17: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?=E7=BB=99=E6=BF=80=E5=85=89=E7=82=AE=E5=85=85=E7=94=B5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/ChargingStationBlockEntity.java | 21 +++++++++++++++++++ .../entity/AnnihilatorEntity.java | 6 +++++- .../superbwarfare/entity/IChargeEntity.java | 6 ++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/atsuishio/superbwarfare/entity/IChargeEntity.java 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 0688178aa..65c1240dd 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/ChargingStationBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/ChargingStationBlockEntity.java @@ -1,5 +1,6 @@ package com.atsuishio.superbwarfare.block.entity; +import com.atsuishio.superbwarfare.entity.IChargeEntity; import com.atsuishio.superbwarfare.init.ModBlockEntities; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -11,6 +12,7 @@ import net.minecraft.world.Container; import net.minecraft.world.ContainerHelper; import net.minecraft.world.MenuProvider; import net.minecraft.world.WorldlyContainer; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; @@ -18,6 +20,7 @@ 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.minecraft.world.phys.AABB; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.util.LazyOptional; @@ -25,6 +28,7 @@ import net.minecraftforge.energy.EnergyStorage; import net.minecraftforge.items.wrapper.SidedInvWrapper; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; public class ChargingStationBlockEntity extends BlockEntity implements WorldlyContainer, MenuProvider { @@ -40,6 +44,7 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo public static final int MAX_DATA_COUNT = 2; public static final int DEFAULT_FUEL_TIME = 1600; public static final int CHARGE_SPEED = 128; + public static final int CHARGE_RADIUS = 4; protected NonNullList items = NonNullList.withSize(2, ItemStack.EMPTY); @@ -101,6 +106,22 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo blockEntity.setChanged(); } } + + blockEntity.energyHandler.ifPresent(handler -> { + int energy = handler.getEnergyStored(); + if (energy > 0) { + List entities = pLevel.getEntitiesOfClass(Entity.class, new AABB(pPos).inflate(CHARGE_RADIUS)); + entities.forEach(entity -> { + if (entity instanceof IChargeEntity chargeEntity) { + if (handler.getEnergyStored() > 0) { + handler.extractEnergy(Math.min(CHARGE_SPEED, handler.getEnergyStored()), false); + chargeEntity.charge(Math.min(CHARGE_SPEED, handler.getEnergyStored())); + } + } + }); + blockEntity.setChanged(); + } + }); } public NonNullList getItems() { diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java index 46dbb76c6..0a1016181 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/AnnihilatorEntity.java @@ -52,7 +52,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import java.util.Comparator; -public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntity { +public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntity, IChargeEntity { public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(AnnihilatorEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor HEALTH = SynchedEntityData.defineId(AnnihilatorEntity.class, EntityDataSerializers.FLOAT); @@ -492,4 +492,8 @@ public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntit return this.cache; } + @Override + public void charge(int amount) { + this.entityData.set(ENERGY, Math.min(this.entityData.get(ENERGY) + amount, CannonConfig.ANNIHILATOR_MAX_ENERGY.get().floatValue())); + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/IChargeEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/IChargeEntity.java new file mode 100644 index 000000000..e99933610 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/entity/IChargeEntity.java @@ -0,0 +1,6 @@ +package com.atsuishio.superbwarfare.entity; + +public interface IChargeEntity { + + void charge(int amount); +}