2013-12-27 23:59:59 +01:00
|
|
|
package appeng.container.implementations;
|
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
2014-01-23 17:28:12 +01:00
|
|
|
import net.minecraft.inventory.ICrafting;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-01-23 17:28:12 +01:00
|
|
|
import appeng.api.AEApi;
|
2014-01-23 20:02:48 +01:00
|
|
|
import appeng.api.implementations.guiobjects.INetworkTool;
|
2014-01-23 17:28:12 +01:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridBlock;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.container.AEBaseContainer;
|
2014-02-16 05:33:13 +01:00
|
|
|
import appeng.core.AELog;
|
2014-02-09 02:34:52 +01:00
|
|
|
import appeng.core.sync.network.NetworkHandler;
|
2014-01-23 17:28:12 +01:00
|
|
|
import appeng.core.sync.packets.PacketMEInventoryUpdate;
|
2014-02-16 05:33:13 +01:00
|
|
|
import appeng.core.sync.packets.PacketProgressBar;
|
2014-01-23 17:28:12 +01:00
|
|
|
import appeng.util.Platform;
|
|
|
|
import appeng.util.item.AEItemStack;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public class ContainerNetworkStatus extends AEBaseContainer
|
|
|
|
{
|
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
IGrid network;
|
|
|
|
|
|
|
|
public ContainerNetworkStatus(InventoryPlayer ip, INetworkTool te) {
|
|
|
|
super( ip, null, null );
|
|
|
|
IGridHost host = te.getGridHost();
|
|
|
|
|
|
|
|
if ( host != null )
|
|
|
|
{
|
|
|
|
findNode( host, ForgeDirection.UNKNOWN );
|
|
|
|
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
findNode( host, d );
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:31:56 +01:00
|
|
|
if ( network == null && Platform.isServer() )
|
2014-02-09 02:34:52 +01:00
|
|
|
isContainerValid = false;
|
2014-01-23 17:28:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void findNode(IGridHost host, ForgeDirection d)
|
|
|
|
{
|
|
|
|
if ( network == null )
|
|
|
|
{
|
|
|
|
IGridNode node = host.getGridNode( d );
|
|
|
|
if ( node != null )
|
|
|
|
network = node.getGrid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:31:56 +01:00
|
|
|
int delay = 40;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
public long avgAddition;
|
|
|
|
public long powerUsage;
|
2014-03-06 06:45:14 +01:00
|
|
|
public long currentPower;
|
|
|
|
public long maxPower;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
@Override
|
2014-03-06 06:45:14 +01:00
|
|
|
public void updateFullProgressBar(int id, long value)
|
2014-01-23 17:28:12 +01:00
|
|
|
{
|
2014-03-06 06:45:14 +01:00
|
|
|
if ( id == 0 )
|
|
|
|
avgAddition = value;
|
|
|
|
|
|
|
|
if ( id == 1 )
|
|
|
|
powerUsage = value;
|
2014-01-23 17:28:12 +01:00
|
|
|
|
2014-03-06 06:45:14 +01:00
|
|
|
if ( id == 2 )
|
|
|
|
currentPower = value;
|
|
|
|
|
|
|
|
if ( id == 3 )
|
|
|
|
maxPower = value;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
@Override
|
|
|
|
public void detectAndSendChanges()
|
|
|
|
{
|
|
|
|
delay++;
|
2014-01-24 17:31:56 +01:00
|
|
|
if ( Platform.isServer() && delay > 15 && network != null )
|
2014-01-23 17:28:12 +01:00
|
|
|
{
|
|
|
|
delay = 0;
|
|
|
|
|
|
|
|
IEnergyGrid eg = network.getCache( IEnergyGrid.class );
|
|
|
|
if ( eg != null )
|
|
|
|
{
|
|
|
|
avgAddition = (long) (100.0 * eg.getAvgPowerInjection());
|
|
|
|
powerUsage = (long) (100.0 * eg.getAvgPowerUsage());
|
2014-03-06 06:45:14 +01:00
|
|
|
currentPower = (long) (100.0 * eg.getStoredPower());
|
|
|
|
maxPower = (long) (100.0 * eg.getMaxStoredPower());
|
2014-01-23 17:28:12 +01:00
|
|
|
|
|
|
|
for (Object c : this.crafters)
|
|
|
|
{
|
|
|
|
ICrafting icrafting = (ICrafting) c;
|
2014-02-16 05:33:13 +01:00
|
|
|
try
|
|
|
|
{
|
2014-03-06 06:45:14 +01:00
|
|
|
NetworkHandler.instance.sendTo( new PacketProgressBar( 0, avgAddition ), (EntityPlayerMP) icrafting );
|
|
|
|
NetworkHandler.instance.sendTo( new PacketProgressBar( 1, powerUsage ), (EntityPlayerMP) icrafting );
|
|
|
|
NetworkHandler.instance.sendTo( new PacketProgressBar( 2, currentPower ), (EntityPlayerMP) icrafting );
|
|
|
|
NetworkHandler.instance.sendTo( new PacketProgressBar( 3, maxPower ), (EntityPlayerMP) icrafting );
|
2014-02-16 05:33:13 +01:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
2014-01-23 17:28:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PacketMEInventoryUpdate piu;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
piu = new PacketMEInventoryUpdate();
|
|
|
|
|
|
|
|
for (Class<? extends IGridHost> machineClass : network.getMachinesClasses())
|
|
|
|
{
|
|
|
|
IItemList<IAEItemStack> list = AEApi.instance().storage().createItemList();
|
|
|
|
for (IGridNode machine : network.getMachines( machineClass ))
|
|
|
|
{
|
|
|
|
IGridBlock blk = machine.getGridBlock();
|
|
|
|
ItemStack is = blk.getMachineRepresentation();
|
2014-02-09 08:55:44 +01:00
|
|
|
if ( is != null && is.getItem() != null )
|
2014-01-23 17:28:12 +01:00
|
|
|
{
|
|
|
|
IAEItemStack ais = AEItemStack.create( is );
|
|
|
|
ais.setStackSize( 1 );
|
|
|
|
ais.setCountRequestable( (long) (blk.getIdlePowerUsage() * 100.0) );
|
|
|
|
list.add( ais );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (IAEItemStack ais : list)
|
|
|
|
piu.appendItem( ais );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Object c : this.crafters)
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
if ( c instanceof EntityPlayer )
|
|
|
|
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
|
2014-01-23 17:28:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
super.detectAndSendChanges();
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|