diff --git a/src/main/java/edu/uidaho/electricblocks/RegistryHandler.java b/src/main/java/edu/uidaho/electricblocks/RegistryHandler.java index 795b93f..3562db1 100644 --- a/src/main/java/edu/uidaho/electricblocks/RegistryHandler.java +++ b/src/main/java/edu/uidaho/electricblocks/RegistryHandler.java @@ -11,6 +11,7 @@ import edu.uidaho.electricblocks.items.WireItem; import edu.uidaho.electricblocks.items.BusItem; import edu.uidaho.electricblocks.tileentities.ExternalGridTileEntity; +import edu.uidaho.electricblocks.tileentities.GeneratorTileEntity; import edu.uidaho.electricblocks.tileentities.LampTileEntity; import net.minecraft.block.Block; import net.minecraft.item.Item; @@ -46,6 +47,8 @@ public class RegistryHandler { TILE_ENTITIES.register("externalgrid_tileentity", () -> TileEntityType.Builder.create(ExternalGridTileEntity::new).build(null)); public static final RegistryObject> LAMP_TILE_ENTITY = TILE_ENTITIES.register("lamp_tileentity", () -> TileEntityType.Builder.create(LampTileEntity::new).build(null)); + public static final RegistryObject> GENERATOR_TILE_ENTITY = + TILE_ENTITIES.register("generator_tileentity", () -> TileEntityType.Builder.create(GeneratorTileEntity::new).build(null)); public static void init() { BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus()); diff --git a/src/main/java/edu/uidaho/electricblocks/blocks/PortableGeneratorBlock.java b/src/main/java/edu/uidaho/electricblocks/blocks/PortableGeneratorBlock.java index 8da1b90..56a9405 100644 --- a/src/main/java/edu/uidaho/electricblocks/blocks/PortableGeneratorBlock.java +++ b/src/main/java/edu/uidaho/electricblocks/blocks/PortableGeneratorBlock.java @@ -1,8 +1,14 @@ package edu.uidaho.electricblocks.blocks; +import javax.annotation.Nullable; + +import edu.uidaho.electricblocks.tileentities.GeneratorTileEntity; import net.minecraft.block.Block; +import net.minecraft.block.BlockState; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.IBlockReader; public class PortableGeneratorBlock extends Block { @@ -14,4 +20,15 @@ public PortableGeneratorBlock() { .harvestLevel(0) ); } + + @Override + public boolean hasTileEntity(BlockState state) { + return true; + } + + @Nullable + @Override + public TileEntity createTileEntity(BlockState state, IBlockReader world) { + return new GeneratorTileEntity(); + } } diff --git a/src/main/java/edu/uidaho/electricblocks/tileentities/GeneratorTileEntity.java b/src/main/java/edu/uidaho/electricblocks/tileentities/GeneratorTileEntity.java new file mode 100644 index 0000000..8fddadc --- /dev/null +++ b/src/main/java/edu/uidaho/electricblocks/tileentities/GeneratorTileEntity.java @@ -0,0 +1,106 @@ +package edu.uidaho.electricblocks.tileentities; + +import java.util.UUID; + +import javax.annotation.Nullable; + +import com.google.gson.JsonObject; + +import edu.uidaho.electricblocks.RegistryHandler; +import edu.uidaho.electricblocks.electric.Volt; +import edu.uidaho.electricblocks.electric.Watt; +import edu.uidaho.electricblocks.simulation.SimulationTileEntity; +import edu.uidaho.electricblocks.simulation.SimulationType; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.play.server.SUpdateTileEntityPacket; + +public class GeneratorTileEntity extends SimulationTileEntity { + + private boolean inService = false; + private boolean slack = true; + private Watt maxPower = new Watt(1000); + private Watt resultPower = new Watt(0); + private Volt nominalVoltage = new Volt(120); + + public GeneratorTileEntity() { + super(RegistryHandler.GENERATOR_TILE_ENTITY.get(), SimulationType.GENERATOR); + } + + @Override + public CompoundNBT write(CompoundNBT compound) { + super.write(compound); + compound.putBoolean("inService", inService); + compound.putBoolean("slack", slack); + compound.putDouble("maxPower", maxPower.getWatts()); + compound.putDouble("resultPower", resultPower.getWatts()); + compound.putDouble("nominalVoltage", nominalVoltage.getVolts()); + compound.putUniqueId("simId", simId); + return compound; + } + + @Override + public void read(CompoundNBT compound) { + super.read(compound); + inService = compound.getBoolean("inService"); + slack = compound.getBoolean("slack"); + maxPower = new Watt(compound.getDouble("maxPower")); + resultPower = new Watt(compound.getDouble("resultPower")); + nominalVoltage = new Volt(compound.getDouble("nominalVoltage")); + simId = compound.getUniqueId("simId"); + } + + @Nullable + @Override + public SUpdateTileEntityPacket getUpdatePacket() { + CompoundNBT tag = new CompoundNBT(); + write(tag); + return new SUpdateTileEntityPacket(getPos(), -1, tag); + } + + @Override + public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) { + CompoundNBT tag = pkt.getNbtCompound(); + read(tag); + } + + @Override + public void receiveSimulationResults(JsonObject results) { + double resultPower = results.get("p_mw").getAsDouble() * 1000000; + this.resultPower = new Watt(resultPower); + notifyUpdate(); + } + + @Override + public void zeroSim() { + resultPower = new Watt(0); + notifyUpdate(); + } + + @Override + public JsonObject toJson() { + JsonObject json = new JsonObject(); + JsonObject bus = new JsonObject(); + UUID busId = embededBusses.get("main"); + bus.addProperty("etype", SimulationType.BUS.toString()); + + JsonObject obj = new JsonObject(); + obj.addProperty("etype", getSimulationType().toString()); + obj.addProperty("slack", slack); + obj.addProperty("in_service", inService); + obj.addProperty("bus", busId.toString()); + obj.addProperty("p_mw", maxPower.getMegaWatts()); + obj.addProperty("vm_pu", nominalVoltage.getVolts()); + + json.add(busId.toString(), bus); + json.add(getSimulationID().toString(), obj); + return json; + } + + @Override + public void initEmbeddedBusses() { + embededBusses.put("main", UUID.randomUUID()); + } + + +}