2014-07-08 00:33:22 +02:00
|
|
|
package appeng.container.implementations;
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
import java.io.IOException;
|
2014-07-08 00:33:22 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2014-07-08 00:33:22 +02:00
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-07-08 05:48:21 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-07-08 00:33:22 +02:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.container.AEBaseContainer;
|
2014-07-08 05:48:21 +02:00
|
|
|
import appeng.core.sync.network.NetworkHandler;
|
|
|
|
import appeng.core.sync.packets.PacketCompressedNBT;
|
|
|
|
import appeng.helpers.DualityInterface;
|
2014-07-08 00:33:22 +02:00
|
|
|
import appeng.helpers.IInterfaceHost;
|
|
|
|
import appeng.parts.misc.PartInterface;
|
2014-07-08 05:48:21 +02:00
|
|
|
import appeng.parts.reporting.PartMonitor;
|
2014-07-08 00:33:22 +02:00
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.misc.TileInterface;
|
2014-07-08 05:48:21 +02:00
|
|
|
import appeng.util.Platform;
|
2014-07-08 00:33:22 +02:00
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
public class ContainerInterfaceTerminal extends AEBaseContainer
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this stuff is all server side..
|
|
|
|
*/
|
|
|
|
|
|
|
|
static private long autoBase = Long.MIN_VALUE;
|
|
|
|
|
|
|
|
class InvTracker
|
|
|
|
{
|
|
|
|
|
|
|
|
long which = autoBase++;
|
|
|
|
String unlocalizedName;
|
|
|
|
|
|
|
|
public InvTracker(IInventory patterns, String unlocalizedName) {
|
2014-07-08 00:33:22 +02:00
|
|
|
server = patterns;
|
2014-07-08 05:48:21 +02:00
|
|
|
client = new AppEngInternalInventory( null, server.getSizeInventory() );
|
|
|
|
this.unlocalizedName = unlocalizedName;
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
2014-07-08 05:48:21 +02:00
|
|
|
|
2014-07-08 00:33:22 +02:00
|
|
|
IInventory client;
|
|
|
|
IInventory server;
|
2014-07-08 05:48:21 +02:00
|
|
|
|
2014-07-08 00:33:22 +02:00
|
|
|
};
|
2014-07-08 05:48:21 +02:00
|
|
|
|
|
|
|
Map<IInterfaceHost, InvTracker> diList = new HashMap();
|
2014-07-08 00:33:22 +02:00
|
|
|
IGrid g;
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
public ContainerInterfaceTerminal(InventoryPlayer ip, PartMonitor anchor) {
|
|
|
|
super( ip, anchor );
|
|
|
|
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
g = anchor.getActionableNode().getGrid();
|
|
|
|
|
|
|
|
bindPlayerInventory( ip, 0, 222 - /* height of playerinventory */82 );
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
|
2014-07-08 00:33:22 +02:00
|
|
|
@Override
|
2014-07-08 05:48:21 +02:00
|
|
|
public void detectAndSendChanges()
|
|
|
|
{
|
2014-07-08 00:33:22 +02:00
|
|
|
super.detectAndSendChanges();
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
if ( g == null )
|
|
|
|
return;
|
|
|
|
|
2014-07-08 00:33:22 +02:00
|
|
|
int total = 0;
|
2014-07-08 05:48:21 +02:00
|
|
|
boolean missing = false;
|
2014-07-08 00:33:22 +02:00
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
for (IGridNode gn : g.getMachines( TileInterface.class ))
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
2014-07-08 05:48:21 +02:00
|
|
|
missing = missing || !diList.containsKey( ih );
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
for (IGridNode gn : g.getMachines( PartInterface.class ))
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
2014-07-08 05:48:21 +02:00
|
|
|
missing = missing || !diList.containsKey( ih );
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( total != diList.size() || missing )
|
2014-07-08 05:48:21 +02:00
|
|
|
regenList( data );
|
2014-07-08 00:33:22 +02:00
|
|
|
else
|
|
|
|
{
|
2014-07-08 05:48:21 +02:00
|
|
|
for (Entry<IInterfaceHost, InvTracker> en : diList.entrySet())
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
|
|
|
InvTracker inv = en.getValue();
|
2014-07-08 05:48:21 +02:00
|
|
|
for (int x = 0; x < inv.server.getSizeInventory(); x++)
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
2014-07-08 05:48:21 +02:00
|
|
|
if ( isDifferent( inv.server.getStackInSlot( x ), inv.client.getStackInSlot( x ) ) )
|
|
|
|
addItems( data, inv, x, 1 );
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
if ( !data.hasNoTags() )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
NetworkHandler.instance.sendTo( new PacketCompressedNBT( data ), (EntityPlayerMP) getPlayerInv().player );
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
2014-07-08 00:33:22 +02:00
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
data = new NBTTagCompound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isDifferent(ItemStack a, ItemStack b)
|
|
|
|
{
|
2014-07-08 00:33:22 +02:00
|
|
|
if ( a == null && b == null )
|
|
|
|
return false;
|
2014-07-08 05:48:21 +02:00
|
|
|
|
2014-07-08 00:33:22 +02:00
|
|
|
if ( a == null || b == null )
|
|
|
|
return true;
|
2014-07-08 05:48:21 +02:00
|
|
|
|
|
|
|
return !a.equals( b );
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
private void regenList(NBTTagCompound data)
|
|
|
|
{
|
2014-07-08 00:33:22 +02:00
|
|
|
diList.clear();
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
for (IGridNode gn : g.getMachines( TileInterface.class ))
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
2014-07-08 05:48:21 +02:00
|
|
|
DualityInterface dual = ih.getInterfaceDuality();
|
|
|
|
diList.put( ih, new InvTracker( dual.getPatterns(), dual.getTermName() ) );
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
for (IGridNode gn : g.getMachines( PartInterface.class ))
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
2014-07-08 05:48:21 +02:00
|
|
|
DualityInterface dual = ih.getInterfaceDuality();
|
|
|
|
diList.put( ih, new InvTracker( dual.getPatterns(), dual.getTermName() ) );
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
data.setBoolean( "clear", true );
|
|
|
|
|
|
|
|
for (Entry<IInterfaceHost, InvTracker> en : diList.entrySet())
|
|
|
|
{
|
|
|
|
InvTracker inv = en.getValue();
|
|
|
|
addItems( data, inv, 0, inv.server.getSizeInventory() );
|
|
|
|
}
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
private void addItems(NBTTagCompound data, InvTracker inv, int offset, int length)
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
2014-07-08 05:48:21 +02:00
|
|
|
String name = "=" + Long.toString( inv.which, Character.MAX_RADIX );
|
|
|
|
NBTTagCompound invv = data.getCompoundTag( name );
|
|
|
|
|
|
|
|
if ( invv.hasNoTags() )
|
|
|
|
invv.setString( "un", inv.unlocalizedName );
|
|
|
|
|
|
|
|
for (int x = 0; x < length; x++)
|
|
|
|
{
|
|
|
|
NBTTagCompound itemNBT = new NBTTagCompound();
|
|
|
|
|
|
|
|
ItemStack is = inv.server.getStackInSlot( x + offset );
|
2014-07-08 00:33:22 +02:00
|
|
|
|
2014-07-08 05:48:21 +02:00
|
|
|
// "update" client side.
|
|
|
|
inv.client.setInventorySlotContents( x + offset, is == null ? null : is.copy() );
|
|
|
|
|
|
|
|
if ( is != null )
|
|
|
|
is.writeToNBT( itemNBT );
|
|
|
|
|
|
|
|
invv.setTag( Integer.toString( x + offset ), itemNBT );
|
|
|
|
}
|
|
|
|
|
|
|
|
data.setTag( name, invv );
|
|
|
|
}
|
2014-07-08 00:33:22 +02:00
|
|
|
}
|