优化各种能量存储能力实现
This commit is contained in:
parent
59fc1aa2bc
commit
aa990802ae
11 changed files with 76 additions and 64 deletions
|
@ -1,7 +1,6 @@
|
|||
package com.atsuishio.superbwarfare.block.entity;
|
||||
|
||||
import com.atsuishio.superbwarfare.block.ChargingStationBlock;
|
||||
import com.atsuishio.superbwarfare.entity.vehicle.IChargeEntity;
|
||||
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
||||
import com.atsuishio.superbwarfare.menu.ChargingStationMenu;
|
||||
import com.atsuishio.superbwarfare.network.dataslot.ContainerEnergyData;
|
||||
|
@ -193,12 +192,12 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
if (this.level == null) return;
|
||||
|
||||
List<Entity> entities = this.level.getEntitiesOfClass(Entity.class, new AABB(this.getBlockPos()).inflate(CHARGE_RADIUS));
|
||||
entities.forEach(entity -> {
|
||||
if (entity instanceof IChargeEntity chargeEntity && handler.getEnergyStored() > 0 && chargeEntity.canCharge()) {
|
||||
chargeEntity.charge(Math.min(CHARGE_OTHER_SPEED, handler.getEnergyStored()));
|
||||
handler.extractEnergy(Math.min(CHARGE_OTHER_SPEED, handler.getEnergyStored()), false);
|
||||
entities.forEach(entity -> entity.getCapability(ForgeCapabilities.ENERGY).ifPresent(cap -> {
|
||||
if (cap.canReceive()) {
|
||||
int charged = cap.receiveEnergy(Math.min(handler.getEnergyStored(), CHARGE_OTHER_SPEED), false);
|
||||
handler.extractEnergy(charged, false);
|
||||
}
|
||||
});
|
||||
}));
|
||||
this.setChanged();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.atsuishio.superbwarfare.block.entity;
|
||||
|
||||
import com.atsuishio.superbwarfare.entity.vehicle.IChargeEntity;
|
||||
import com.atsuishio.superbwarfare.capability.energy.InfinityEnergyStorage;
|
||||
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
|
@ -12,9 +12,11 @@ import net.minecraft.world.phys.AABB;
|
|||
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.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Energy Data Slot Code based on @GoryMoon's Chargers
|
||||
*/
|
||||
|
@ -22,11 +24,11 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
|
||||
public static final int CHARGE_RADIUS = 8;
|
||||
|
||||
private LazyOptional<EnergyStorage> energyHandler;
|
||||
private LazyOptional<IEnergyStorage> energyHandler;
|
||||
|
||||
public CreativeChargingStationBlockEntity(BlockPos pos, BlockState state) {
|
||||
super(ModBlockEntities.CREATIVE_CHARGING_STATION.get(), pos, state);
|
||||
this.energyHandler = LazyOptional.of(() -> new EnergyStorage(2147483647));
|
||||
this.energyHandler = LazyOptional.of(InfinityEnergyStorage::new);
|
||||
}
|
||||
|
||||
public static void serverTick(CreativeChargingStationBlockEntity blockEntity) {
|
||||
|
@ -41,12 +43,12 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
private void chargeEntity() {
|
||||
if (this.level == null) return;
|
||||
|
||||
this.level.getEntitiesOfClass(Entity.class, new AABB(this.getBlockPos()).inflate(CHARGE_RADIUS))
|
||||
.forEach(entity -> {
|
||||
if (entity instanceof IChargeEntity chargeEntity && chargeEntity.canCharge()) {
|
||||
chargeEntity.charge(10000000);
|
||||
List<Entity> entities = this.level.getEntitiesOfClass(Entity.class, new AABB(this.getBlockPos()).inflate(CHARGE_RADIUS));
|
||||
entities.forEach(entity -> entity.getCapability(ForgeCapabilities.ENERGY).ifPresent(cap -> {
|
||||
if (cap.canReceive()) {
|
||||
cap.receiveEnergy(Integer.MAX_VALUE, false);
|
||||
}
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
private void chargeBlock() {
|
||||
|
@ -61,7 +63,7 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
|
||||
blockEntity.getCapability(ForgeCapabilities.ENERGY).ifPresent(energy -> {
|
||||
if (energy.canReceive() && energy.getEnergyStored() < energy.getMaxEnergyStored()) {
|
||||
energy.receiveEnergy(10000000, false);
|
||||
energy.receiveEnergy(Integer.MAX_VALUE, false);
|
||||
blockEntity.setChanged();
|
||||
}
|
||||
});
|
||||
|
@ -75,10 +77,7 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
|
||||
@Override
|
||||
public <T> @NotNull LazyOptional<T> getCapability(@NotNull Capability<T> cap, Direction side) {
|
||||
if (cap == ForgeCapabilities.ENERGY) {
|
||||
return energyHandler.cast();
|
||||
}
|
||||
return super.getCapability(cap, side);
|
||||
return ForgeCapabilities.ENERGY.orEmpty(cap, energyHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -90,6 +89,6 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
@Override
|
||||
public void reviveCaps() {
|
||||
super.reviveCaps();
|
||||
this.energyHandler = LazyOptional.of(() -> new EnergyStorage(2147483647));
|
||||
this.energyHandler = LazyOptional.of(InfinityEnergyStorage::new);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.atsuishio.superbwarfare.capability.energy;
|
||||
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
/**
|
||||
* 无限供电能力,纯逆天
|
||||
*/
|
||||
public class InfinityEnergyStorage implements IEnergyStorage {
|
||||
@Override
|
||||
public int receiveEnergy(int maxReceive, boolean simulate) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractEnergy(int maxExtract, boolean simulate) {
|
||||
return maxExtract;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored() {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored() {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtract() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReceive() {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -22,10 +22,6 @@ public class ItemEnergyProvider implements ICapabilityProvider {
|
|||
@Nonnull
|
||||
@Override
|
||||
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction dire) {
|
||||
if (cap == ForgeCapabilities.ENERGY) {
|
||||
return capability.cast();
|
||||
}
|
||||
|
||||
return LazyOptional.empty();
|
||||
return ForgeCapabilities.ENERGY.orEmpty(cap, capability);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ public class VehicleHudOverlay {
|
|||
poseStack.popPose();
|
||||
|
||||
if (vehicle instanceof IArmedVehicleEntity iVehicle && iVehicle.getAmmoCount(player) != -1 && iVehicle.isDriver(player)) {
|
||||
boolean iCharge = iVehicle instanceof IChargeEntity;
|
||||
boolean iCharge = iVehicle instanceof EnergyVehicleEntity;
|
||||
|
||||
// 渲染当前弹药量
|
||||
poseStack.pushPose();
|
||||
|
|
|
@ -64,7 +64,7 @@ import java.util.List;
|
|||
|
||||
import static com.atsuishio.superbwarfare.tools.ParticleTool.sendParticle;
|
||||
|
||||
public class Bmp2Entity extends ContainerMobileEntity implements GeoEntity, IChargeEntity, ILandArmorEntity, MultiWeaponVehicleEntity {
|
||||
public class Bmp2Entity extends ContainerMobileEntity implements GeoEntity, ILandArmorEntity, MultiWeaponVehicleEntity {
|
||||
|
||||
public static final EntityDataAccessor<Integer> FIRE_ANIM = SynchedEntityData.defineId(Bmp2Entity.class, EntityDataSerializers.INT);
|
||||
public static final EntityDataAccessor<Float> DELTA_ROT = SynchedEntityData.defineId(Bmp2Entity.class, EntityDataSerializers.FLOAT);
|
||||
|
|
|
@ -14,9 +14,8 @@ import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
|||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.joml.Math;
|
||||
|
||||
public class EnergyVehicleEntity extends VehicleEntity implements IChargeEntity {
|
||||
public class EnergyVehicleEntity extends VehicleEntity {
|
||||
|
||||
public static final EntityDataAccessor<Integer> ENERGY = SynchedEntityData.defineId(EnergyVehicleEntity.class, EntityDataSerializers.INT);
|
||||
|
||||
|
@ -85,18 +84,6 @@ public class EnergyVehicleEntity extends VehicleEntity implements IChargeEntity
|
|||
return 100000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void charge(int amount) {
|
||||
long energy = (long) this.getEnergy() + (long) amount;
|
||||
int e = energy > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) energy;
|
||||
this.setEnergy(Math.min(e, this.getMaxEnergy()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCharge() {
|
||||
return this.getEnergy() < this.getMaxEnergy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap) {
|
||||
return ForgeCapabilities.ENERGY.orEmpty(cap, energy);
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package com.atsuishio.superbwarfare.entity.vehicle;
|
||||
|
||||
public interface IChargeEntity {
|
||||
|
||||
void charge(int amount);
|
||||
|
||||
boolean canCharge();
|
||||
}
|
|
@ -63,7 +63,7 @@ import java.util.List;
|
|||
|
||||
import static com.atsuishio.superbwarfare.tools.ParticleTool.sendParticle;
|
||||
|
||||
public class Lav150Entity extends ContainerMobileEntity implements GeoEntity, IChargeEntity, ILandArmorEntity, MultiWeaponVehicleEntity {
|
||||
public class Lav150Entity extends ContainerMobileEntity implements GeoEntity, ILandArmorEntity, MultiWeaponVehicleEntity {
|
||||
|
||||
public static final EntityDataAccessor<Integer> FIRE_ANIM = SynchedEntityData.defineId(Lav150Entity.class, EntityDataSerializers.INT);
|
||||
public static final EntityDataAccessor<Float> DELTA_ROT = SynchedEntityData.defineId(Lav150Entity.class, EntityDataSerializers.FLOAT);
|
||||
|
|
|
@ -53,7 +53,7 @@ import java.util.List;
|
|||
|
||||
import static com.atsuishio.superbwarfare.tools.ParticleTool.sendParticle;
|
||||
|
||||
public class SpeedboatEntity extends ContainerMobileEntity implements GeoEntity, IChargeEntity, IArmedVehicleEntity {
|
||||
public class SpeedboatEntity extends ContainerMobileEntity implements GeoEntity, IArmedVehicleEntity {
|
||||
|
||||
public static final EntityDataAccessor<Integer> FIRE_ANIM = SynchedEntityData.defineId(SpeedboatEntity.class, EntityDataSerializers.INT);
|
||||
public static final EntityDataAccessor<Float> DELTA_ROT = SynchedEntityData.defineId(SpeedboatEntity.class, EntityDataSerializers.FLOAT);
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package com.atsuishio.superbwarfare.item;
|
||||
|
||||
import com.atsuishio.superbwarfare.capability.energy.ItemEnergyProvider;
|
||||
import com.atsuishio.superbwarfare.capability.energy.InfinityEnergyStorage;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class CreativeChargingStationBlockItem extends BlockItem {
|
||||
public CreativeChargingStationBlockItem(Block pBlock, Properties pProperties) {
|
||||
|
@ -17,14 +20,12 @@ public class CreativeChargingStationBlockItem extends BlockItem {
|
|||
|
||||
@Override
|
||||
public ICapabilityProvider initCapabilities(ItemStack stack, CompoundTag tag) {
|
||||
return new ItemEnergyProvider(stack, 2147483647);
|
||||
return new ICapabilityProvider() {
|
||||
@Override
|
||||
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
|
||||
return ForgeCapabilities.ENERGY.orEmpty(cap, LazyOptional.of(InfinityEnergyStorage::new));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inventoryTick(ItemStack pStack, Level pLevel, Entity pEntity, int pSlotId, boolean pIsSelected) {
|
||||
pStack.getCapability(ForgeCapabilities.ENERGY).ifPresent(energy ->
|
||||
energy.receiveEnergy(2147483647 - energy.getEnergyStored(), false)
|
||||
);
|
||||
super.inventoryTick(pStack, pLevel, pEntity, pSlotId, pIsSelected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue