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-16 04:20:03 +02:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-07-31 04:02:37 +02:00
|
|
|
import appeng.api.config.Settings;
|
|
|
|
import appeng.api.config.YesNo;
|
2014-07-08 00:33:22 +02:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridNode;
|
2014-07-09 10:17:11 +02:00
|
|
|
import appeng.api.networking.security.IActionHost;
|
2014-07-08 00:33:22 +02:00
|
|
|
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;
|
2014-07-09 10:17:11 +02:00
|
|
|
import appeng.helpers.InventoryAction;
|
|
|
|
import appeng.items.misc.ItemEncodedPattern;
|
2014-07-08 00:33:22 +02:00
|
|
|
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-09 10:17:11 +02:00
|
|
|
import appeng.util.InventoryAdaptor;
|
2014-07-08 05:48:21 +02:00
|
|
|
import appeng.util.Platform;
|
2014-07-09 10:17:11 +02:00
|
|
|
import appeng.util.inv.AdaptorIInventory;
|
|
|
|
import appeng.util.inv.AdaptorPlayerHand;
|
|
|
|
import appeng.util.inv.WrapperInvSlot;
|
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;
|
|
|
|
|
2014-07-13 03:20:21 +02:00
|
|
|
public InvTracker(DualityInterface dual, 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-13 03:20:21 +02:00
|
|
|
this.sortBy = dual.getSortValue();
|
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-13 03:20:21 +02:00
|
|
|
public long sortBy;
|
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-09 10:17:11 +02:00
|
|
|
Map<Long, InvTracker> byId = 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-09 10:17:11 +02:00
|
|
|
class PatternInvSlot extends WrapperInvSlot
|
|
|
|
{
|
|
|
|
|
|
|
|
public PatternInvSlot(IInventory inv) {
|
|
|
|
super( inv );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-12 23:28:55 +02:00
|
|
|
public boolean isItemValid(ItemStack itemstack)
|
2014-07-09 10:17:11 +02:00
|
|
|
{
|
|
|
|
return itemstack != null && itemstack.getItem() instanceof ItemEncodedPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doAction(EntityPlayerMP player, InventoryAction action, int slot, long id)
|
|
|
|
{
|
|
|
|
InvTracker inv = byId.get( id );
|
|
|
|
if ( inv != null )
|
|
|
|
{
|
|
|
|
ItemStack is = inv.server.getStackInSlot( slot );
|
|
|
|
boolean hasItemInHand = player.inventory.getItemStack() != null;
|
|
|
|
|
|
|
|
InventoryAdaptor playerHand = new AdaptorPlayerHand( player );
|
|
|
|
|
|
|
|
WrapperInvSlot slotInv = new PatternInvSlot( inv.server );
|
2014-07-14 04:35:56 +02:00
|
|
|
|
|
|
|
IInventory theSlot = slotInv.getWrapper( slot );
|
|
|
|
InventoryAdaptor interfaceSlot = new AdaptorIInventory( theSlot );
|
2014-07-09 10:17:11 +02:00
|
|
|
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case PICKUP_OR_SETDOWN:
|
|
|
|
|
|
|
|
if ( hasItemInHand )
|
|
|
|
{
|
2014-07-14 04:35:56 +02:00
|
|
|
ItemStack inSlot = theSlot.getStackInSlot( 0 );
|
|
|
|
if ( inSlot == null )
|
|
|
|
player.inventory.setItemStack( interfaceSlot.addItems( player.inventory.getItemStack() ) );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inSlot = inSlot.copy();
|
|
|
|
ItemStack inHand = player.inventory.getItemStack().copy();
|
|
|
|
|
|
|
|
theSlot.setInventorySlotContents( 0, null );
|
|
|
|
player.inventory.setItemStack( null );
|
|
|
|
|
|
|
|
player.inventory.setItemStack( interfaceSlot.addItems( inHand.copy() ) );
|
|
|
|
|
|
|
|
if ( player.inventory.getItemStack() == null )
|
|
|
|
player.inventory.setItemStack( inSlot );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
player.inventory.setItemStack( inHand );
|
|
|
|
theSlot.setInventorySlotContents( 0, inSlot );
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 10:17:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-12 23:28:55 +02:00
|
|
|
IInventory mySlot = slotInv.getWrapper( slot );
|
|
|
|
mySlot.setInventorySlotContents( 0, playerHand.addItems( mySlot.getStackInSlot( 0 ) ) );
|
2014-07-09 10:17:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SPLIT_OR_PLACESINGLE:
|
|
|
|
|
|
|
|
if ( hasItemInHand )
|
|
|
|
{
|
|
|
|
ItemStack extra = playerHand.removeItems( 1, null, null );
|
|
|
|
if ( extra != null )
|
|
|
|
extra = interfaceSlot.addItems( extra );
|
|
|
|
if ( extra != null )
|
|
|
|
playerHand.addItems( extra );
|
|
|
|
}
|
|
|
|
else if ( is != null )
|
|
|
|
{
|
|
|
|
ItemStack extra = interfaceSlot.removeItems( (is.stackSize + 1) / 2, null, null );
|
|
|
|
if ( extra != null )
|
|
|
|
extra = playerHand.addItems( extra );
|
|
|
|
if ( extra != null )
|
|
|
|
interfaceSlot.addItems( extra );
|
|
|
|
}
|
|
|
|
|
2014-07-16 04:20:03 +02:00
|
|
|
break;
|
|
|
|
case SHIFT_CLICK:
|
|
|
|
|
|
|
|
IInventory mySlot = slotInv.getWrapper( slot );
|
|
|
|
InventoryAdaptor playerInv = InventoryAdaptor.getAdaptor( player, ForgeDirection.UNKNOWN );
|
|
|
|
mySlot.setInventorySlotContents( 0, playerInv.addItems( mySlot.getStackInSlot( 0 ) ) );
|
|
|
|
|
|
|
|
break;
|
|
|
|
case MOVE_REGION:
|
|
|
|
|
|
|
|
InventoryAdaptor playerInvAd = InventoryAdaptor.getAdaptor( player, ForgeDirection.UNKNOWN );
|
|
|
|
for (int x = 0; x < inv.server.getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
inv.server.setInventorySlotContents( x, playerInvAd.addItems( inv.server.getStackInSlot( x ) ) );
|
|
|
|
}
|
|
|
|
|
2014-07-09 10:17:11 +02:00
|
|
|
break;
|
|
|
|
case CREATIVE_DUPLICATE:
|
|
|
|
|
|
|
|
if ( player.capabilities.isCreativeMode && !hasItemInHand )
|
|
|
|
{
|
|
|
|
player.inventory.setItemStack( is == null ? null : is.copy() );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHeld( player );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 00:33:22 +02:00
|
|
|
@Override
|
2014-07-08 05:48:21 +02:00
|
|
|
public void detectAndSendChanges()
|
|
|
|
{
|
2014-07-09 10:17:11 +02:00
|
|
|
if ( Platform.isClient() )
|
|
|
|
return;
|
|
|
|
|
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-09 10:17:11 +02:00
|
|
|
IActionHost host = getActionHost();
|
|
|
|
if ( host != null )
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
2014-07-09 10:17:11 +02:00
|
|
|
IGridNode agn = host.getActionableNode();
|
|
|
|
if ( agn.isActive() )
|
|
|
|
{
|
|
|
|
for (IGridNode gn : g.getMachines( TileInterface.class ))
|
|
|
|
{
|
2014-07-14 04:59:19 +02:00
|
|
|
if ( gn.isActive() )
|
2014-07-09 10:17:11 +02:00
|
|
|
{
|
2014-07-14 04:59:19 +02:00
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
2014-07-31 04:02:37 +02:00
|
|
|
if ( ih.getInterfaceDuality().getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.NO )
|
|
|
|
continue;
|
|
|
|
|
2014-07-14 04:59:19 +02:00
|
|
|
InvTracker t = diList.get( ih );
|
|
|
|
|
|
|
|
if ( t == null )
|
2014-07-09 10:17:11 +02:00
|
|
|
missing = true;
|
2014-07-14 04:59:19 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DualityInterface dual = ih.getInterfaceDuality();
|
|
|
|
if ( !t.unlocalizedName.equals( dual.getTermName() ) )
|
|
|
|
missing = true;
|
|
|
|
}
|
2014-07-09 10:17:11 +02:00
|
|
|
|
2014-07-14 04:59:19 +02:00
|
|
|
total++;
|
|
|
|
}
|
2014-07-09 10:17:11 +02:00
|
|
|
}
|
2014-07-08 00:33:22 +02:00
|
|
|
|
2014-07-09 10:17:11 +02:00
|
|
|
for (IGridNode gn : g.getMachines( PartInterface.class ))
|
|
|
|
{
|
2014-07-14 04:59:19 +02:00
|
|
|
if ( gn.isActive() )
|
2014-07-09 10:17:11 +02:00
|
|
|
{
|
2014-07-14 04:59:19 +02:00
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
2014-07-31 04:02:37 +02:00
|
|
|
if ( ih.getInterfaceDuality().getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.NO )
|
|
|
|
continue;
|
|
|
|
|
2014-07-14 04:59:19 +02:00
|
|
|
InvTracker t = diList.get( ih );
|
|
|
|
|
|
|
|
if ( t == null )
|
2014-07-09 10:17:11 +02:00
|
|
|
missing = true;
|
2014-07-14 04:59:19 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DualityInterface dual = ih.getInterfaceDuality();
|
|
|
|
if ( !t.unlocalizedName.equals( dual.getTermName() ) )
|
|
|
|
missing = true;
|
|
|
|
}
|
2014-07-09 10:17:11 +02:00
|
|
|
|
2014-07-14 04:59:19 +02:00
|
|
|
total++;
|
|
|
|
}
|
2014-07-09 10:17:11 +02:00
|
|
|
}
|
|
|
|
}
|
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-09 10:17:11 +02:00
|
|
|
byId.clear();
|
2014-07-08 00:33:22 +02:00
|
|
|
diList.clear();
|
|
|
|
|
2014-07-09 10:17:11 +02:00
|
|
|
IActionHost host = getActionHost();
|
|
|
|
if ( host != null )
|
2014-07-08 00:33:22 +02:00
|
|
|
{
|
2014-07-09 10:17:11 +02:00
|
|
|
IGridNode agn = host.getActionableNode();
|
|
|
|
if ( agn.isActive() )
|
|
|
|
{
|
|
|
|
for (IGridNode gn : g.getMachines( TileInterface.class ))
|
|
|
|
{
|
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
|
|
|
DualityInterface dual = ih.getInterfaceDuality();
|
2014-07-31 04:02:37 +02:00
|
|
|
if ( gn.isActive() && dual.getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.YES )
|
2014-07-24 06:18:23 +02:00
|
|
|
diList.put( ih, new InvTracker( dual, dual.getPatterns(), dual.getTermName() ) );
|
2014-07-09 10:17:11 +02:00
|
|
|
}
|
2014-07-08 00:33:22 +02:00
|
|
|
|
2014-07-09 10:17:11 +02:00
|
|
|
for (IGridNode gn : g.getMachines( PartInterface.class ))
|
|
|
|
{
|
|
|
|
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
|
|
|
|
DualityInterface dual = ih.getInterfaceDuality();
|
2014-07-31 04:02:37 +02:00
|
|
|
if ( gn.isActive() && dual.getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.YES )
|
2014-07-24 06:18:23 +02:00
|
|
|
diList.put( ih, new InvTracker( dual, dual.getPatterns(), dual.getTermName() ) );
|
2014-07-09 10:17:11 +02:00
|
|
|
}
|
|
|
|
}
|
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();
|
2014-07-09 10:17:11 +02:00
|
|
|
byId.put( inv.which, inv );
|
2014-07-08 05:48:21 +02:00
|
|
|
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() )
|
2014-07-13 03:20:21 +02:00
|
|
|
{
|
|
|
|
invv.setLong( "sortBy", inv.sortBy );
|
2014-07-08 05:48:21 +02:00
|
|
|
invv.setString( "un", inv.unlocalizedName );
|
2014-07-13 03:20:21 +02:00
|
|
|
}
|
2014-07-08 05:48:21 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|