Started work on the new PowerNetwork

This commit is contained in:
modmuss50 2016-02-27 19:02:28 +00:00
parent 11a31e7bcd
commit 4b5d05a063
9 changed files with 275 additions and 44 deletions

View file

@ -0,0 +1,22 @@
package techreborn.blocks;
import net.minecraft.block.material.Material;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import reborncore.common.BaseTileBlock;
import techreborn.client.TechRebornCreativeTabMisc;
import techreborn.tiles.TileCable;
public class BlockCable extends BaseTileBlock {
public BlockCable() {
super(Material.iron);
setUnlocalizedName("techreborn.cable");
setCreativeTab(TechRebornCreativeTabMisc.instance);
}
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileCable();
}
}

View file

@ -8,28 +8,7 @@ import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.oredict.OreDictionary;
import reborncore.common.tile.TileMachineBase;
import techreborn.Core;
import techreborn.blocks.BlockChunkLoader;
import techreborn.blocks.BlockComputerCube;
import techreborn.blocks.BlockDigitalChest;
import techreborn.blocks.BlockElectricCraftingTable;
import techreborn.blocks.BlockFusionCoil;
import techreborn.blocks.BlockFusionControlComputer;
import techreborn.blocks.BlockHighlyAdvancedMachine;
import techreborn.blocks.BlockIronFence;
import techreborn.blocks.BlockMachineCasing;
import techreborn.blocks.BlockMachineFrame;
import techreborn.blocks.BlockOre;
import techreborn.blocks.BlockOre2;
import techreborn.blocks.BlockPlayerDetector;
import techreborn.blocks.BlockQuantumChest;
import techreborn.blocks.BlockQuantumTank;
import techreborn.blocks.BlockRubberLeaves;
import techreborn.blocks.BlockRubberLog;
import techreborn.blocks.BlockRubberPlank;
import techreborn.blocks.BlockRubberSapling;
import techreborn.blocks.BlockStorage;
import techreborn.blocks.BlockStorage2;
import techreborn.blocks.BlockSupercondensator;
import techreborn.blocks.*;
import techreborn.blocks.generator.BlockDieselGenerator;
import techreborn.blocks.generator.BlockDragonEggSiphoner;
import techreborn.blocks.generator.BlockGasTurbine;
@ -79,28 +58,7 @@ import techreborn.itemblocks.ItemBlockQuantumTank;
import techreborn.itemblocks.ItemBlockRubberSapling;
import techreborn.itemblocks.ItemBlockStorage;
import techreborn.itemblocks.ItemBlockStorage2;
import techreborn.tiles.TileAesu;
import techreborn.tiles.TileAlloyFurnace;
import techreborn.tiles.TileAlloySmelter;
import techreborn.tiles.TileAssemblingMachine;
import techreborn.tiles.TileBlastFurnace;
import techreborn.tiles.TileCentrifuge;
import techreborn.tiles.TileChargeBench;
import techreborn.tiles.TileChemicalReactor;
import techreborn.tiles.TileChunkLoader;
import techreborn.tiles.TileDigitalChest;
import techreborn.tiles.TileImplosionCompressor;
import techreborn.tiles.TileIndustrialElectrolyzer;
import techreborn.tiles.TileIndustrialGrinder;
import techreborn.tiles.TileIndustrialSawmill;
import techreborn.tiles.TileMachineCasing;
import techreborn.tiles.TileMatterFabricator;
import techreborn.tiles.TilePlayerDectector;
import techreborn.tiles.TileQuantumChest;
import techreborn.tiles.TileQuantumTank;
import techreborn.tiles.TileRollingMachine;
import techreborn.tiles.TileThermalGenerator;
import techreborn.tiles.TileVacuumFreezer;
import techreborn.tiles.*;
import techreborn.tiles.fusionReactor.TileEntityFusionController;
import techreborn.tiles.generator.TileDieselGenerator;
import techreborn.tiles.generator.TileDragonEggSiphoner;
@ -183,6 +141,7 @@ public class ModBlocks {
public static Block rubberPlanks;
public static Block ironFence;
public static Block cable;
public static void init() {
thermalGenerator = new BlockThermalGenerator();
@ -403,6 +362,10 @@ public class ModBlocks {
ironFence = new BlockIronFence();
GameRegistry.registerBlock(ironFence, "ironFence");
cable = new BlockCable();
GameRegistry.registerBlock(cable, "cable");
GameRegistry.registerTileEntity(TileCable.class, "tilecable");
registerOreDict();
Core.logHelper.info("TechReborns Blocks Loaded");
}

View file

@ -0,0 +1,17 @@
package techreborn.powernet;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
public interface IPowerCableContainer {
World getWorld();
BlockPos getPos();
boolean canConnectTo(EnumFacing facing);
PowerCable getPowerCable();
}

View file

@ -0,0 +1,67 @@
package techreborn.powernet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import reborncore.api.power.IEnergyInterfaceTile;
import java.util.ArrayList;
import java.util.List;
public class PowerCable {
List<PowerNode> nodes = new ArrayList<PowerNode>();
IPowerCableContainer container;
PowerNetwork network;
public PowerCable(IPowerCableContainer container) {
this.container = container;
}
public void blockUpdate(){
checkNodes();
}
public void checkNodes(){
nodes.clear();
for(EnumFacing dir : EnumFacing.VALUES){
if(!container.canConnectTo(dir)){
continue;
}
BlockPos blockPos = container.getPos().offset(dir);
if(container.getWorld().isBlockLoaded(blockPos)){
TileEntity tileEntity = container.getWorld().getTileEntity(blockPos);
if(network != null){
if(tileEntity instanceof IPowerCableContainer){
IPowerCableContainer powerCableContainer = (IPowerCableContainer) tileEntity;
if(powerCableContainer.getPowerCable().network != network){
network.merge(powerCableContainer.getPowerCable().network);
}
}
}
if(!(tileEntity instanceof IEnergyInterfaceTile)){
continue;
}
IEnergyInterfaceTile energyInterfaceTile = (IEnergyInterfaceTile) tileEntity;
PowerNode node = new PowerNode(blockPos, tileEntity, dir);
if(energyInterfaceTile.canAcceptEnergy(dir)){
// This tile will be classed as a receiver
node.type = PowerType.RECEIVER;
} else if(energyInterfaceTile.canProvideEnergy(dir)){
//This tile can then provide
node.type = PowerType.COLLECTOR;
}
}
}
}
public PowerNetwork getNetwork() {
return network;
}
public void setNetwork(PowerNetwork network) {
this.network = network;
}
}

View file

@ -0,0 +1,24 @@
package techreborn.powernet;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
public class PowerEvent extends Event {
World world;
public PowerEvent(World world) {
this.world = world;
}
public PowerEvent() {
}
@SubscribeEvent
public void worldTick(TickEvent.WorldTickEvent event){
MinecraftForge.EVENT_BUS.post(new PowerEvent(event.world));
}
}

View file

@ -0,0 +1,65 @@
package techreborn.powernet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import reborncore.api.power.IEnergyInterfaceTile;
import java.util.ArrayList;
import java.util.List;
public class PowerNetwork {
List<PowerCable> cables = new ArrayList<PowerCable>();
int networkPower;
double maxTransfur;
public PowerNetwork merge(PowerNetwork network){
cables.addAll(network.cables);
for(PowerCable cable : network.cables){
cable.setNetwork(this);
}
return this;
}
public void addCable(PowerCable cable){
cables.add(cable);
cable.setNetwork(this);
}
public void removeCable(PowerCable cable){
cables.remove(cable);
cable.setNetwork(null);
}
public void tick(World world){
for(PowerCable cable : cables){
if(cable.container.getWorld() == world){
for(PowerNode node : cable.nodes){
TileEntity tileEntity = node.nodeTile;
if(!(tileEntity instanceof IEnergyInterfaceTile)){
continue;
}
IEnergyInterfaceTile iEnergyInterfaceTile = (IEnergyInterfaceTile) tileEntity;
switch (node.type){
case COLLECTOR:
if(iEnergyInterfaceTile.canProvideEnergy(node.facingFromCable)){
double transfur = Math.min(maxTransfur, Math.min(iEnergyInterfaceTile.getMaxOutput(), iEnergyInterfaceTile.getEnergy()));
networkPower += iEnergyInterfaceTile.useEnergy(transfur);
}
break;
case RECEIVER:
if(iEnergyInterfaceTile.canAcceptEnergy(node.facingFromCable)){
double transfur = Math.min(maxTransfur, Math.min(iEnergyInterfaceTile.getMaxOutput(), iEnergyInterfaceTile.getEnergy()));
networkPower -= iEnergyInterfaceTile.addEnergy(transfur);
}
break;
}
}
}
}
}
}

View file

@ -0,0 +1,23 @@
package techreborn.powernet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
public class PowerNode {
BlockPos nodePos;
TileEntity nodeTile;
PowerType type = PowerType.UNKNOWN;
EnumFacing facingFromCable;
public PowerNode(BlockPos nodePos, TileEntity nodeTile, EnumFacing facingFromCable) {
this.nodePos = nodePos;
this.nodeTile = nodeTile;
this.facingFromCable = facingFromCable;
}
}

View file

@ -0,0 +1,8 @@
package techreborn.powernet;
public enum PowerType {
COLLECTOR,
RECEIVER,
UNKNOWN
}

View file

@ -0,0 +1,42 @@
package techreborn.tiles;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import techreborn.powernet.IPowerCableContainer;
import techreborn.powernet.PowerCable;
import techreborn.powernet.PowerEvent;
import techreborn.powernet.PowerNetwork;
public class TileCable extends TileEntity implements IPowerCableContainer, ITickable{
PowerCable cable = null;
@Override
public boolean canConnectTo(EnumFacing facing) {
return true;
}
@Override
public void update() {
if(cable == null){
cable = new PowerCable(this);
} else {
cable.checkNodes();
}
}
public void createCable(){
if(cable != null){
cable.getNetwork().removeCable(cable);
cable = null;
}
PowerNetwork network = new PowerNetwork();
cable = new PowerCable(this);
cable.setNetwork(network);
network.addCable(cable);
}
}