Skip to content

Commit

Permalink
feat: added tile information to generator block
Browse files Browse the repository at this point in the history
  • Loading branch information
zachoooo committed Dec 7, 2020
1 parent 72b009a commit 2a25423
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/edu/uidaho/electricblocks/RegistryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class RegistryHandler {
TILE_ENTITIES.register("externalgrid_tileentity", () -> TileEntityType.Builder.create(ExternalGridTileEntity::new).build(null));
public static final RegistryObject<TileEntityType<LampTileEntity>> LAMP_TILE_ENTITY =
TILE_ENTITIES.register("lamp_tileentity", () -> TileEntityType.Builder.create(LampTileEntity::new).build(null));
public static final RegistryObject<TileEntityType<GeneratorTileEntity>> GENERATOR_TILE_ENTITY =
TILE_ENTITIES.register("generator_tileentity", () -> TileEntityType.Builder.create(GeneratorTileEntity::new).build(null));

public static void init() {
BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus());
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}


}

0 comments on commit 2a25423

Please sign in to comment.