Applied-Energistics-2-tiler.../src/main/java/appeng/container/implementations/ContainerInterfaceTerminal.java

394 lines
11 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.container.implementations;
2014-09-24 02:26:27 +02:00
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
2014-09-24 02:26:27 +02:00
import appeng.api.config.Settings;
import appeng.api.config.YesNo;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridNode;
import appeng.api.networking.security.IActionHost;
import appeng.container.AEBaseContainer;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketCompressedNBT;
import appeng.helpers.DualityInterface;
import appeng.helpers.IInterfaceHost;
import appeng.helpers.InventoryAction;
import appeng.items.misc.ItemEncodedPattern;
import appeng.parts.misc.PartInterface;
import appeng.parts.reporting.PartMonitor;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.misc.TileInterface;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.inv.AdaptorIInventory;
import appeng.util.inv.AdaptorPlayerHand;
import appeng.util.inv.WrapperInvSlot;
public class ContainerInterfaceTerminal extends AEBaseContainer
{
/**
* this stuff is all server side..
*/
static private long autoBase = Long.MIN_VALUE;
static class InvTracker
2014-09-24 02:26:27 +02:00
{
final long which = autoBase++;
final String unlocalizedName;
2014-09-24 02:26:27 +02:00
public InvTracker(DualityInterface dual, IInventory patterns, String unlocalizedName) {
2014-12-29 15:13:47 +01:00
this.server = patterns;
this.client = new AppEngInternalInventory( null, this.server.getSizeInventory() );
2014-09-24 02:26:27 +02:00
this.unlocalizedName = unlocalizedName;
this.sortBy = dual.getSortValue();
}
final IInventory client;
final IInventory server;
public final long sortBy;
2014-09-24 02:26:27 +02:00
2014-09-28 00:50:06 +02:00
}
2014-09-24 02:26:27 +02:00
final Map<IInterfaceHost, InvTracker> diList = new HashMap<IInterfaceHost, InvTracker>();
final Map<Long, InvTracker> byId = new HashMap<Long, InvTracker>();
2014-09-24 02:26:27 +02:00
IGrid g;
public ContainerInterfaceTerminal(InventoryPlayer ip, PartMonitor anchor) {
super( ip, anchor );
if ( Platform.isServer() )
2014-12-29 15:13:47 +01:00
this.g = anchor.getActionableNode().getGrid();
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.bindPlayerInventory( ip, 0, 222 - /* height of player inventory */82 );
2014-09-24 02:26:27 +02:00
}
NBTTagCompound data = new NBTTagCompound();
static class PatternInvSlot extends WrapperInvSlot
2014-09-24 02:26:27 +02:00
{
public PatternInvSlot(IInventory inv) {
super( inv );
}
@Override
public boolean isItemValid(ItemStack itemstack)
{
return itemstack != null && itemstack.getItem() instanceof ItemEncodedPattern;
}
2014-09-28 00:50:06 +02:00
}
2014-09-24 02:26:27 +02:00
@Override
public void doAction(EntityPlayerMP player, InventoryAction action, int slot, long id)
{
2014-12-29 15:13:47 +01:00
InvTracker inv = this.byId.get( id );
2014-09-24 02:26:27 +02:00
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 );
IInventory theSlot = slotInv.getWrapper( slot );
InventoryAdaptor interfaceSlot = new AdaptorIInventory( theSlot );
switch (action)
{
case PICKUP_OR_SET_DOWN:
2014-09-24 02:26:27 +02:00
if ( hasItemInHand )
{
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 );
}
}
}
else
{
IInventory mySlot = slotInv.getWrapper( slot );
mySlot.setInventorySlotContents( 0, playerHand.addItems( mySlot.getStackInSlot( 0 ) ) );
}
break;
case SPLIT_OR_PLACE_SINGLE:
2014-09-24 02:26:27 +02:00
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 );
}
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 ) ) );
}
break;
case CREATIVE_DUPLICATE:
if ( player.capabilities.isCreativeMode && !hasItemInHand )
{
player.inventory.setItemStack( is == null ? null : is.copy() );
}
break;
default:
return;
}
2014-12-29 15:13:47 +01:00
this.updateHeld( player );
2014-09-24 02:26:27 +02:00
}
}
@Override
public void detectAndSendChanges()
{
if ( Platform.isClient() )
return;
super.detectAndSendChanges();
2014-12-29 15:13:47 +01:00
if ( this.g == null )
2014-09-24 02:26:27 +02:00
return;
int total = 0;
boolean missing = false;
2014-12-29 15:13:47 +01:00
IActionHost host = this.getActionHost();
2014-09-24 02:26:27 +02:00
if ( host != null )
{
IGridNode agn = host.getActionableNode();
if ( agn != null && agn.isActive() )
{
2014-12-29 15:13:47 +01:00
for (IGridNode gn : this.g.getMachines( TileInterface.class ))
2014-09-24 02:26:27 +02:00
{
if ( gn.isActive() )
{
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
if ( ih.getInterfaceDuality().getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.NO )
continue;
2014-12-29 15:13:47 +01:00
InvTracker t = this.diList.get( ih );
2014-09-24 02:26:27 +02:00
if ( t == null )
missing = true;
else
{
DualityInterface dual = ih.getInterfaceDuality();
if ( !t.unlocalizedName.equals( dual.getTermName() ) )
missing = true;
}
total++;
}
}
2014-12-29 15:13:47 +01:00
for (IGridNode gn : this.g.getMachines( PartInterface.class ))
2014-09-24 02:26:27 +02:00
{
if ( gn.isActive() )
{
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
if ( ih.getInterfaceDuality().getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.NO )
continue;
2014-12-29 15:13:47 +01:00
InvTracker t = this.diList.get( ih );
2014-09-24 02:26:27 +02:00
if ( t == null )
missing = true;
else
{
DualityInterface dual = ih.getInterfaceDuality();
if ( !t.unlocalizedName.equals( dual.getTermName() ) )
missing = true;
}
total++;
}
}
}
}
2014-12-29 15:13:47 +01:00
if ( total != this.diList.size() || missing )
this.regenList( this.data );
2014-09-24 02:26:27 +02:00
else
{
2014-12-29 15:13:47 +01:00
for (Entry<IInterfaceHost, InvTracker> en : this.diList.entrySet())
2014-09-24 02:26:27 +02:00
{
InvTracker inv = en.getValue();
for (int x = 0; x < inv.server.getSizeInventory(); x++)
{
2014-12-29 15:13:47 +01:00
if ( this.isDifferent( inv.server.getStackInSlot( x ), inv.client.getStackInSlot( x ) ) )
this.addItems( this.data, inv, x, 1 );
2014-09-24 02:26:27 +02:00
}
}
}
2014-12-29 15:13:47 +01:00
if ( !this.data.hasNoTags() )
2014-09-24 02:26:27 +02:00
{
try
{
2014-12-29 15:13:47 +01:00
NetworkHandler.instance.sendTo( new PacketCompressedNBT( this.data ), (EntityPlayerMP) this.getPlayerInv().player );
2014-09-24 02:26:27 +02:00
}
catch (IOException e)
{
// :P
}
2014-12-29 15:13:47 +01:00
this.data = new NBTTagCompound();
2014-09-24 02:26:27 +02:00
}
}
private boolean isDifferent(ItemStack a, ItemStack b)
{
if ( a == null && b == null )
return false;
if ( a == null || b == null )
return true;
return !ItemStack.areItemStacksEqual( a, b );
}
private void regenList(NBTTagCompound data)
{
2014-12-29 15:13:47 +01:00
this.byId.clear();
this.diList.clear();
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
IActionHost host = this.getActionHost();
2014-09-24 02:26:27 +02:00
if ( host != null )
{
IGridNode agn = host.getActionableNode();
if ( agn != null && agn.isActive() )
{
2014-12-29 15:13:47 +01:00
for (IGridNode gn : this.g.getMachines( TileInterface.class ))
2014-09-24 02:26:27 +02:00
{
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
DualityInterface dual = ih.getInterfaceDuality();
if ( gn.isActive() && dual.getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.YES )
2014-12-29 15:13:47 +01:00
this.diList.put( ih, new InvTracker( dual, dual.getPatterns(), dual.getTermName() ) );
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
for (IGridNode gn : this.g.getMachines( PartInterface.class ))
2014-09-24 02:26:27 +02:00
{
IInterfaceHost ih = (IInterfaceHost) gn.getMachine();
DualityInterface dual = ih.getInterfaceDuality();
if ( gn.isActive() && dual.getConfigManager().getSetting( Settings.INTERFACE_TERMINAL ) == YesNo.YES )
2014-12-29 15:13:47 +01:00
this.diList.put( ih, new InvTracker( dual, dual.getPatterns(), dual.getTermName() ) );
2014-09-24 02:26:27 +02:00
}
}
}
data.setBoolean( "clear", true );
2014-12-29 15:13:47 +01:00
for (Entry<IInterfaceHost, InvTracker> en : this.diList.entrySet())
2014-09-24 02:26:27 +02:00
{
InvTracker inv = en.getValue();
2014-12-29 15:13:47 +01:00
this.byId.put( inv.which, inv );
this.addItems( data, inv, 0, inv.server.getSizeInventory() );
2014-09-24 02:26:27 +02:00
}
}
private void addItems(NBTTagCompound data, InvTracker inv, int offset, int length)
{
String name = '=' + Long.toString( inv.which, Character.MAX_RADIX );
NBTTagCompound tag = data.getCompoundTag( name );
2014-09-24 02:26:27 +02:00
if ( tag.hasNoTags() )
2014-09-24 02:26:27 +02:00
{
tag.setLong( "sortBy", inv.sortBy );
tag.setString( "un", inv.unlocalizedName );
2014-09-24 02:26:27 +02:00
}
for (int x = 0; x < length; x++)
{
NBTTagCompound itemNBT = new NBTTagCompound();
ItemStack is = inv.server.getStackInSlot( x + offset );
// "update" client side.
inv.client.setInventorySlotContents( x + offset, is == null ? null : is.copy() );
if ( is != null )
is.writeToNBT( itemNBT );
tag.setTag( Integer.toString( x + offset ), itemNBT );
2014-09-24 02:26:27 +02:00
}
data.setTag( name, tag );
2014-09-24 02:26:27 +02:00
}
}