Add comparator support for energy storage and generator blocks (#1901)

* Add comparator support for energy storage blocks

Does not update automatically yet, so there needs to be a call to
World.updateHorizontalAdjacent.

* Switch to ceil() in energy storage comparator calculation

Should prevent low amounts of energy of rounding to 0.

* Add comparator support to generators

Requires TechReborn/RebornCore#132.
This commit is contained in:
Juuxel 2019-12-07 20:36:41 +02:00 committed by modmuss50
parent 4764271967
commit 87a36d1c23
4 changed files with 66 additions and 10 deletions

View file

@ -24,10 +24,14 @@
package techreborn.blocks.generator; package techreborn.blocks.generator;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView; import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import reborncore.api.blockentity.IMachineGuiHandler; import reborncore.api.blockentity.IMachineGuiHandler;
import reborncore.common.blocks.BlockMachineBase; import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.powerSystem.PowerAcceptorBlockEntity;
import techreborn.init.TRContent.SolarPanels; import techreborn.init.TRContent.SolarPanels;
import techreborn.blockentity.generator.SolarPanelBlockEntity; import techreborn.blockentity.generator.SolarPanelBlockEntity;
@ -52,4 +56,14 @@ public class BlockSolarPanel extends BlockMachineBase {
public IMachineGuiHandler getGui() { public IMachineGuiHandler getGui() {
return null; return null;
} }
@Override
public boolean hasComparatorOutput(BlockState state) {
return true;
}
@Override
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return PowerAcceptorBlockEntity.calculateComparatorOutputFromEnergy(world.getBlockEntity(pos));
}
} }

View file

@ -0,0 +1,31 @@
package techreborn.blocks.generator;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import reborncore.common.powerSystem.PowerAcceptorBlockEntity;
import techreborn.blocks.GenericMachineBlock;
import techreborn.client.EGui;
import java.util.function.Supplier;
/**
* An extension of {@link GenericMachineBlock} that provides utilities
* for generators, like comparator output based on energy.
*/
public class GenericGeneratorBlock extends GenericMachineBlock {
public GenericGeneratorBlock(EGui gui, Supplier<BlockEntity> blockEntityClass) {
super(gui, blockEntityClass);
}
@Override
public boolean hasComparatorOutput(BlockState state) {
return true;
}
@Override
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return PowerAcceptorBlockEntity.calculateComparatorOutputFromEnergy(world.getBlockEntity(pos));
}
}

View file

@ -45,6 +45,7 @@ import reborncore.api.ToolManager;
import reborncore.api.blockentity.IMachineGuiHandler; import reborncore.api.blockentity.IMachineGuiHandler;
import reborncore.common.BaseBlockEntityProvider; import reborncore.common.BaseBlockEntityProvider;
import reborncore.common.blocks.BlockWrenchEventHandler; import reborncore.common.blocks.BlockWrenchEventHandler;
import reborncore.common.powerSystem.PowerAcceptorBlockEntity;
import reborncore.common.util.WrenchUtils; import reborncore.common.util.WrenchUtils;
import java.util.Locale; import java.util.Locale;
@ -137,4 +138,14 @@ public abstract class BlockEnergyStorage extends BaseBlockEntityProvider {
} }
setFacing(facing, worldIn, pos); setFacing(facing, worldIn, pos);
} }
@Override
public boolean hasComparatorOutput(BlockState state) {
return true;
}
@Override
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return PowerAcceptorBlockEntity.calculateComparatorOutputFromEnergy(world.getBlockEntity(pos));
}
} }

View file

@ -410,20 +410,20 @@ public class TRContent {
SOLID_CANNING_MACHINE(new GenericMachineBlock(EGui.SOLID_CANNING_MACHINE, SoildCanningMachineBlockEntity::new)), SOLID_CANNING_MACHINE(new GenericMachineBlock(EGui.SOLID_CANNING_MACHINE, SoildCanningMachineBlockEntity::new)),
WIRE_MILL(new GenericMachineBlock(EGui.WIRE_MILL, WireMillBlockEntity::new)), WIRE_MILL(new GenericMachineBlock(EGui.WIRE_MILL, WireMillBlockEntity::new)),
DIESEL_GENERATOR(new GenericMachineBlock(EGui.DIESEL_GENERATOR, DieselGeneratorBlockEntity::new)), DIESEL_GENERATOR(new GenericGeneratorBlock(EGui.DIESEL_GENERATOR, DieselGeneratorBlockEntity::new)),
DRAGON_EGG_SYPHON(new GenericMachineBlock(null, DragonEggSyphonBlockEntity::new)), DRAGON_EGG_SYPHON(new GenericGeneratorBlock(null, DragonEggSyphonBlockEntity::new)),
FUSION_COIL(new BlockFusionCoil()), FUSION_COIL(new BlockFusionCoil()),
FUSION_CONTROL_COMPUTER(new BlockFusionControlComputer()), FUSION_CONTROL_COMPUTER(new BlockFusionControlComputer()),
GAS_TURBINE(new GenericMachineBlock(EGui.GAS_TURBINE, GasTurbineBlockEntity::new)), GAS_TURBINE(new GenericGeneratorBlock(EGui.GAS_TURBINE, GasTurbineBlockEntity::new)),
LIGHTNING_ROD(new GenericMachineBlock(null, LightningRodBlockEntity::new)), LIGHTNING_ROD(new GenericGeneratorBlock(null, LightningRodBlockEntity::new)),
MAGIC_ENERGY_ABSORBER (new BlockMagicEnergyAbsorber()), MAGIC_ENERGY_ABSORBER (new BlockMagicEnergyAbsorber()),
MAGIC_ENERGY_CONVERTER(new BlockMagicEnergyConverter()), MAGIC_ENERGY_CONVERTER(new BlockMagicEnergyConverter()),
PLASMA_GENERATOR(new GenericMachineBlock(EGui.PLASMA_GENERATOR, PlasmaGeneratorBlockEntity::new)), PLASMA_GENERATOR(new GenericGeneratorBlock(EGui.PLASMA_GENERATOR, PlasmaGeneratorBlockEntity::new)),
SEMI_FLUID_GENERATOR(new GenericMachineBlock(EGui.SEMIFLUID_GENERATOR, SemiFluidGeneratorBlockEntity::new)), SEMI_FLUID_GENERATOR(new GenericGeneratorBlock(EGui.SEMIFLUID_GENERATOR, SemiFluidGeneratorBlockEntity::new)),
SOLID_FUEL_GENERATOR(new GenericMachineBlock(EGui.GENERATOR, SolidFuelGeneratorBlockEntity::new)), SOLID_FUEL_GENERATOR(new GenericGeneratorBlock(EGui.GENERATOR, SolidFuelGeneratorBlockEntity::new)),
THERMAL_GENERATOR(new GenericMachineBlock(EGui.THERMAL_GENERATOR, ThermalGeneratorBlockEntity::new)), THERMAL_GENERATOR(new GenericGeneratorBlock(EGui.THERMAL_GENERATOR, ThermalGeneratorBlockEntity::new)),
WATER_MILL(new GenericMachineBlock(null, WaterMillBlockEntity::new)), WATER_MILL(new GenericGeneratorBlock(null, WaterMillBlockEntity::new)),
WIND_MILL(new GenericMachineBlock(null, WindMillBlockEntity::new)), WIND_MILL(new GenericGeneratorBlock(null, WindMillBlockEntity::new)),
CREATIVE_QUANTUM_CHEST(new BlockCreativeQuantumChest()), CREATIVE_QUANTUM_CHEST(new BlockCreativeQuantumChest()),
CREATIVE_QUANTUM_TANK(new GenericMachineBlock(EGui.QUANTUM_TANK, CreativeQuantumTankBlockEntity::new)), CREATIVE_QUANTUM_TANK(new GenericMachineBlock(EGui.QUANTUM_TANK, CreativeQuantumTankBlockEntity::new)),