修复创造充电站问题

This commit is contained in:
17146 2025-01-25 01:27:00 +08:00
parent c314535962
commit 0e49b41627
3 changed files with 11 additions and 11 deletions

View file

@ -19,11 +19,11 @@ import org.jetbrains.annotations.NotNull;
* Energy Data Slot Code based on @GoryMoon's Chargers * Energy Data Slot Code based on @GoryMoon's Chargers
*/ */
public class CreativeChargingStationBlockEntity extends BlockEntity { public class CreativeChargingStationBlockEntity extends BlockEntity {
public static final int CHARGE_RADIUS = 8; public static final int CHARGE_RADIUS = 8;
private LazyOptional<EnergyStorage> energyHandler; private LazyOptional<EnergyStorage> energyHandler;
public CreativeChargingStationBlockEntity(BlockPos pos, BlockState state) { public CreativeChargingStationBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.CREATIVE_CHARGING_STATION.get(), pos, state); super(ModBlockEntities.CREATIVE_CHARGING_STATION.get(), pos, state);
this.energyHandler = LazyOptional.of(() -> new EnergyStorage(2147483647)); this.energyHandler = LazyOptional.of(() -> new EnergyStorage(2147483647));
@ -39,20 +39,18 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
} }
private void chargeEntity() { private void chargeEntity() {
assert this.level != null; if (this.level == null) return;
this.level this.level.getEntitiesOfClass(Entity.class, new AABB(this.getBlockPos()).inflate(CHARGE_RADIUS))
.getEntitiesOfClass(Entity.class, new AABB(this.getBlockPos()).inflate(CHARGE_RADIUS))
.forEach(entity -> { .forEach(entity -> {
if (entity instanceof IChargeEntity chargeEntity && chargeEntity.canCharge()) { if (entity instanceof IChargeEntity chargeEntity && chargeEntity.canCharge()) {
chargeEntity.charge(2147483647); chargeEntity.charge(10000000);
} }
}); });
} }
private void chargeBlock() { private void chargeBlock() {
assert this.level != null; if (this.level == null) return;
for (Direction direction : Direction.values()) { for (Direction direction : Direction.values()) {
var blockEntity = this.level.getBlockEntity(this.getBlockPos().relative(direction)); var blockEntity = this.level.getBlockEntity(this.getBlockPos().relative(direction));
@ -63,14 +61,13 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
blockEntity.getCapability(ForgeCapabilities.ENERGY).ifPresent(energy -> { blockEntity.getCapability(ForgeCapabilities.ENERGY).ifPresent(energy -> {
if (energy.canReceive() && energy.getEnergyStored() < energy.getMaxEnergyStored()) { if (energy.canReceive() && energy.getEnergyStored() < energy.getMaxEnergyStored()) {
energy.receiveEnergy(2147483647, false); energy.receiveEnergy(10000000, false);
blockEntity.setChanged(); blockEntity.setChanged();
} }
}); });
} }
} }
@Override @Override
public ClientboundBlockEntityDataPacket getUpdatePacket() { public ClientboundBlockEntityDataPacket getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this); return ClientboundBlockEntityDataPacket.create(this);

View file

@ -54,7 +54,9 @@ public class EnergyVehicleEntity extends VehicleEntity implements IChargeEntity
@Override @Override
public void charge(int amount) { public void charge(int amount) {
this.setEnergy(Math.min(this.getEnergy() + amount, this.getMaxEnergy())); 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 @Override

View file

@ -250,7 +250,8 @@ public class ModItems {
public static final RegistryObject<Item> DRAGON_TEETH = block(ModBlocks.DRAGON_TEETH); public static final RegistryObject<Item> DRAGON_TEETH = block(ModBlocks.DRAGON_TEETH);
public static final RegistryObject<Item> REFORGING_TABLE = block(ModBlocks.REFORGING_TABLE); public static final RegistryObject<Item> REFORGING_TABLE = block(ModBlocks.REFORGING_TABLE);
public static final RegistryObject<Item> CHARGING_STATION = block(ModBlocks.CHARGING_STATION); public static final RegistryObject<Item> CHARGING_STATION = block(ModBlocks.CHARGING_STATION);
public static final RegistryObject<Item> CREATIVE_CHARGING_STATION = block(ModBlocks.CREATIVE_CHARGING_STATION); public static final RegistryObject<Item> CREATIVE_CHARGING_STATION = BLOCKS.register("creative_charging_station",
() -> new BlockItem(ModBlocks.CREATIVE_CHARGING_STATION.get(), new Item.Properties().rarity(Rarity.EPIC)));
public static final RegistryObject<Item> LEAD_BLOCK = block(ModBlocks.LEAD_BLOCK); public static final RegistryObject<Item> LEAD_BLOCK = block(ModBlocks.LEAD_BLOCK);
public static final RegistryObject<Item> STEEL_BLOCK = block(ModBlocks.STEEL_BLOCK); public static final RegistryObject<Item> STEEL_BLOCK = block(ModBlocks.STEEL_BLOCK);
public static final RegistryObject<Item> TUNGSTEN_BLOCK = block(ModBlocks.TUNGSTEN_BLOCK); public static final RegistryObject<Item> TUNGSTEN_BLOCK = block(ModBlocks.TUNGSTEN_BLOCK);