Power net work

This commit is contained in:
modmuss50 2016-02-29 17:30:46 +00:00
parent 6de20dcba1
commit 80d5e00706
5 changed files with 67 additions and 6 deletions

View file

@ -1,10 +1,13 @@
package techreborn.blocks; package techreborn.blocks;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import reborncore.common.BaseTileBlock; import reborncore.common.BaseTileBlock;
import techreborn.client.TechRebornCreativeTabMisc; import techreborn.client.TechRebornCreativeTabMisc;
import techreborn.powernet.PowerCable;
import techreborn.tiles.TileCable; import techreborn.tiles.TileCable;
public class BlockCable extends BaseTileBlock { public class BlockCable extends BaseTileBlock {
@ -19,4 +22,12 @@ public class BlockCable extends BaseTileBlock {
public TileEntity createNewTileEntity(World worldIn, int meta) { public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileCable(); return new TileCable();
} }
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
if(worldIn.getTileEntity(pos) instanceof TileCable){
((TileCable) worldIn.getTileEntity(pos)).onRemoved();
}
super.breakBlock(worldIn, pos, state);
}
} }

View file

@ -64,4 +64,9 @@ public class PowerCable {
public void setNetwork(PowerNetwork network) { public void setNetwork(PowerNetwork network) {
this.network = network; this.network = network;
} }
public void removeCable(){
network.removeCable(this);
this.network = null;
}
} }

View file

@ -2,14 +2,20 @@ package techreborn.powernet;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.Event; import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent; import net.minecraftforge.fml.common.gameevent.TickEvent;
import java.util.ArrayList;
import java.util.List;
public class PowerEvent extends Event { public class PowerEvent extends Event {
World world; World world;
public static List<PowerNetwork> networks = new ArrayList<>();
public PowerEvent(World world) { public PowerEvent(World world) {
this.world = world; this.world = world;
} }
@ -21,4 +27,21 @@ public class PowerEvent extends Event {
public void worldTick(TickEvent.WorldTickEvent event){ public void worldTick(TickEvent.WorldTickEvent event){
MinecraftForge.EVENT_BUS.post(new PowerEvent(event.world)); MinecraftForge.EVENT_BUS.post(new PowerEvent(event.world));
} }
@SubscribeEvent
public void worldUnload(WorldEvent.Unload event){
List<PowerNetwork> deadNetworks = new ArrayList<>();
for(PowerNetwork network : networks){
if(network.cables.size() != 0){
if(network.cables.get(0).container.getWorld() == event.world){
MinecraftForge.EVENT_BUS.unregister(network);
deadNetworks.add(network);
}
} else {
MinecraftForge.EVENT_BUS.unregister(network);
deadNetworks.add(network);
}
}
networks.removeAll(deadNetworks);
}
} }

View file

@ -12,7 +12,7 @@ import java.util.List;
public class PowerNetwork { public class PowerNetwork {
List<PowerCable> cables = new ArrayList<PowerCable>(); List<PowerCable> cables = new ArrayList<>();
int networkPower; int networkPower;
@ -27,9 +27,15 @@ public class PowerNetwork {
for(PowerCable cable : network.cables){ for(PowerCable cable : network.cables){
cable.setNetwork(this); cable.setNetwork(this);
} }
network.destoryNetwork();
return this; return this;
} }
public void destoryNetwork(){
cables.clear();
MinecraftForge.EVENT_BUS.unregister(this);
}
public void addCable(PowerCable cable){ public void addCable(PowerCable cable){
cables.add(cable); cables.add(cable);
@ -44,6 +50,10 @@ public class PowerNetwork {
@SubscribeEvent @SubscribeEvent
public void tick(PowerEvent event){ public void tick(PowerEvent event){
World world = event.world; World world = event.world;
if(cables.size() == 0){
destoryNetwork();
return;
}
for(PowerCable cable : cables){ for(PowerCable cable : cables){
if(cable.container.getWorld() == world){ if(cable.container.getWorld() == world){
for(PowerNode node : cable.nodes){ for(PowerNode node : cable.nodes){
@ -55,21 +65,21 @@ public class PowerNetwork {
switch (node.type){ switch (node.type){
case COLLECTOR: case COLLECTOR:
if(iEnergyInterfaceTile.canProvideEnergy(node.facingFromCable)){ if(iEnergyInterfaceTile.canProvideEnergy(node.facingFromCable)){
double transfur = Math.min(maxTransfer, Math.min(iEnergyInterfaceTile.getMaxOutput(), iEnergyInterfaceTile.getEnergy())); double transfer = Math.min(maxTransfer, Math.min(iEnergyInterfaceTile.getMaxOutput(), iEnergyInterfaceTile.getEnergy()));
networkPower += iEnergyInterfaceTile.useEnergy(transfur); networkPower += iEnergyInterfaceTile.useEnergy(transfer);
} }
break; break;
case RECEIVER: case RECEIVER:
if(iEnergyInterfaceTile.canAcceptEnergy(node.facingFromCable)){ if(iEnergyInterfaceTile.canAcceptEnergy(node.facingFromCable)){
double transfur = Math.min(maxTransfer, Math.min(iEnergyInterfaceTile.getMaxOutput(), iEnergyInterfaceTile.getEnergy())); double transfer = Math.min(maxTransfer, Math.min(iEnergyInterfaceTile.getMaxOutput(), iEnergyInterfaceTile.getEnergy()));
networkPower -= iEnergyInterfaceTile.addEnergy(transfur); networkPower -= iEnergyInterfaceTile.addEnergy(transfer);
} }
break; break;
} }
} }
} }
} }
System.out.println(networkPower); System.out.println(networkPower + "cables:" + cables.size() + ":" + this);
} }
} }

View file

@ -45,4 +45,16 @@ public class TileCable extends TileEntity implements IPowerCableContainer, ITick
network.addCable(cable); network.addCable(cable);
cable.checkNodes(); cable.checkNodes();
} }
public void onRemoved(){
if(cable != null){
cable.removeCable();
}
}
@Override
public void onChunkUnload() {
super.onChunkUnload();
onRemoved();
}
} }