2013-12-27 23:59:59 +01:00
|
|
|
package appeng.container.implementations;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-02-06 17:57:01 +01:00
|
|
|
import java.nio.BufferOverflowException;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
import net.minecraft.inventory.ICrafting;
|
|
|
|
import net.minecraft.network.packet.Packet;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
2014-01-23 20:02:48 +01:00
|
|
|
import appeng.api.implementations.guiobjects.IPortableCell;
|
|
|
|
import appeng.api.implementations.tiles.IMEChest;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
2014-01-05 09:43:49 +01:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
2014-01-02 06:51:41 +01:00
|
|
|
import appeng.api.parts.IPart;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.IMEMonitor;
|
2014-01-01 22:46:16 +01:00
|
|
|
import appeng.api.storage.IMEMonitorHandlerReciever;
|
2014-01-05 09:43:49 +01:00
|
|
|
import appeng.api.storage.IStorageMonitorable;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
import appeng.container.AEBaseContainer;
|
2014-02-07 21:37:22 +01:00
|
|
|
import appeng.core.AELog;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.core.sync.packets.PacketMEInventoryUpdate;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
import appeng.util.item.ItemList;
|
|
|
|
import cpw.mods.fml.common.network.PacketDispatcher;
|
|
|
|
import cpw.mods.fml.common.network.Player;
|
|
|
|
|
2014-01-01 22:46:16 +01:00
|
|
|
public class ContainerMEMonitorable extends AEBaseContainer implements IMEMonitorHandlerReciever<IAEItemStack>
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
final IMEMonitor<IAEItemStack> monitor;
|
|
|
|
final IItemList<IAEItemStack> items = new ItemList<IAEItemStack>();
|
|
|
|
|
2014-02-01 06:37:27 +01:00
|
|
|
protected ContainerMEMonitorable(InventoryPlayer ip, IStorageMonitorable montiorable, boolean bindInventory) {
|
2014-01-02 06:51:41 +01:00
|
|
|
super( ip, montiorable instanceof TileEntity ? (TileEntity) montiorable : null, montiorable instanceof IPart ? (IPart) montiorable : null );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-01 06:37:27 +01:00
|
|
|
if ( Platform.isServer() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
monitor = montiorable.getItemInventory();
|
2014-02-03 06:02:54 +01:00
|
|
|
if ( monitor != null )
|
|
|
|
{
|
|
|
|
monitor.addListener( this, null );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-03 06:02:54 +01:00
|
|
|
cellInv = monitor;
|
2014-01-20 17:41:37 +01:00
|
|
|
|
2014-02-03 06:02:54 +01:00
|
|
|
if ( montiorable instanceof IPortableCell )
|
|
|
|
powerSrc = (IPortableCell) montiorable;
|
|
|
|
else if ( montiorable instanceof IMEChest )
|
|
|
|
powerSrc = (IMEChest) montiorable;
|
|
|
|
else if ( montiorable instanceof IGridHost )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-03 06:02:54 +01:00
|
|
|
IGridNode node = ((IGridHost) montiorable).getGridNode( ForgeDirection.UNKNOWN );
|
|
|
|
if ( node != null )
|
|
|
|
{
|
|
|
|
IGrid g = node.getGrid();
|
|
|
|
if ( g != null )
|
|
|
|
powerSrc = g.getCache( IEnergyGrid.class );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 06:02:54 +01:00
|
|
|
else
|
|
|
|
ip.player.closeScreen();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
monitor = null;
|
2014-01-30 03:48:09 +01:00
|
|
|
|
2014-02-01 06:37:27 +01:00
|
|
|
if ( bindInventory )
|
|
|
|
bindPlayerInventory( ip, 0, 0 );
|
2014-01-23 17:28:12 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
public ContainerMEMonitorable(InventoryPlayer ip, IStorageMonitorable montiorable) {
|
2014-02-01 06:37:27 +01:00
|
|
|
this( ip, montiorable, true );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void detectAndSendChanges()
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
|
|
|
if ( !items.isEmpty() )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
IItemList<IAEItemStack> monitorCache = monitor.getStorageList();
|
|
|
|
|
|
|
|
PacketMEInventoryUpdate piu = new PacketMEInventoryUpdate();
|
|
|
|
|
|
|
|
for (IAEItemStack is : items)
|
|
|
|
{
|
|
|
|
IAEItemStack send = monitorCache.findPrecise( is );
|
|
|
|
if ( send == null )
|
|
|
|
{
|
|
|
|
is.setStackSize( 0 );
|
|
|
|
piu.appendItem( is );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
piu.appendItem( send );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !piu.isEmpty() )
|
|
|
|
{
|
|
|
|
Packet p = piu.getPacket();
|
|
|
|
items.resetStatus();
|
|
|
|
|
|
|
|
for (Object c : this.crafters)
|
|
|
|
{
|
|
|
|
if ( c instanceof Player )
|
|
|
|
PacketDispatcher.sendPacketToPlayer( p, (Player) c );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2014-02-07 21:37:22 +01:00
|
|
|
AELog.error( e );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.detectAndSendChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addCraftingToCrafters(ICrafting c)
|
|
|
|
{
|
|
|
|
super.addCraftingToCrafters( c );
|
|
|
|
|
2014-02-03 06:02:54 +01:00
|
|
|
if ( Platform.isServer() && c instanceof Player && monitor != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
PacketMEInventoryUpdate piu = new PacketMEInventoryUpdate();
|
|
|
|
IItemList<IAEItemStack> monitorCache = monitor.getStorageList();
|
|
|
|
|
|
|
|
for (IAEItemStack send : monitorCache)
|
|
|
|
{
|
2014-02-06 17:57:01 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
piu.appendItem( send );
|
|
|
|
}
|
|
|
|
catch (BufferOverflowException boe)
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
|
|
|
Packet p = piu.getPacket();
|
|
|
|
PacketDispatcher.sendPacketToPlayer( p, (Player) c );
|
2014-02-06 17:57:01 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
piu = new PacketMEInventoryUpdate();
|
2014-02-06 17:57:01 +01:00
|
|
|
piu.appendItem( send );
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Packet p = piu.getPacket();
|
|
|
|
PacketDispatcher.sendPacketToPlayer( p, (Player) c );
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2014-02-07 21:37:22 +01:00
|
|
|
AELog.error( e );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContainerClosed(EntityPlayer player)
|
|
|
|
{
|
|
|
|
super.onContainerClosed( player );
|
|
|
|
if ( monitor != null )
|
|
|
|
monitor.removeListener( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeCraftingFromCrafters(ICrafting c)
|
|
|
|
{
|
|
|
|
super.removeCraftingFromCrafters( c );
|
|
|
|
|
2014-02-03 06:02:54 +01:00
|
|
|
if ( this.crafters.isEmpty() && monitor != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
monitor.removeListener( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-01-05 09:43:49 +01:00
|
|
|
public void postChange(IMEMonitor<IAEItemStack> monitor, IAEItemStack change, BaseActionSource source)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
items.add( change );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValid(Object verificationToken)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|