2014-09-17 06:26:25 +02:00
|
|
|
package appeng.container.implementations;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.BufferOverflowException;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
import net.minecraft.inventory.ICrafting;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.config.PowerMultiplier;
|
|
|
|
import appeng.api.config.SecurityPermissions;
|
|
|
|
import appeng.api.config.Settings;
|
|
|
|
import appeng.api.config.SortDir;
|
|
|
|
import appeng.api.config.SortOrder;
|
|
|
|
import appeng.api.config.ViewItems;
|
|
|
|
import appeng.api.implementations.guiobjects.IPortableCell;
|
|
|
|
import appeng.api.implementations.tiles.IMEChest;
|
|
|
|
import appeng.api.implementations.tiles.IViewCellStorage;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.networking.storage.IBaseMonitor;
|
|
|
|
import appeng.api.parts.IPart;
|
|
|
|
import appeng.api.storage.IMEMonitor;
|
|
|
|
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
|
|
|
import appeng.api.storage.ITerminalHost;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
import appeng.api.util.IConfigManager;
|
2014-09-20 22:20:00 +02:00
|
|
|
import appeng.api.util.IConfigurableObject;
|
2014-09-17 06:26:25 +02:00
|
|
|
import appeng.container.AEBaseContainer;
|
|
|
|
import appeng.container.guisync.GuiSync;
|
|
|
|
import appeng.container.slot.SlotRestrictedInput;
|
|
|
|
import appeng.core.AELog;
|
|
|
|
import appeng.core.sync.network.NetworkHandler;
|
|
|
|
import appeng.core.sync.packets.PacketMEInventoryUpdate;
|
|
|
|
import appeng.core.sync.packets.PacketValueConfig;
|
|
|
|
import appeng.me.helpers.ChannelPowerSrc;
|
|
|
|
import appeng.util.ConfigManager;
|
|
|
|
import appeng.util.IConfigManagerHost;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
2014-09-20 22:20:00 +02:00
|
|
|
public class ContainerMEMonitorable extends AEBaseContainer implements IConfigManagerHost, IConfigurableObject, IMEMonitorHandlerReceiver<IAEItemStack>
|
2014-09-17 06:26:25 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
final IMEMonitor<IAEItemStack> monitor;
|
|
|
|
final IItemList<IAEItemStack> items = AEApi.instance().storage().createItemList();
|
|
|
|
|
|
|
|
IConfigManager serverCM;
|
2014-09-29 09:54:34 +02:00
|
|
|
final IConfigManager clientCM;
|
2014-09-17 06:26:25 +02:00
|
|
|
|
|
|
|
@GuiSync(99)
|
|
|
|
public boolean canAccessViewCells = false;
|
|
|
|
|
|
|
|
@GuiSync(98)
|
|
|
|
public boolean hasPower = false;
|
|
|
|
|
2014-09-29 09:54:34 +02:00
|
|
|
public final SlotRestrictedInput[] cellView = new SlotRestrictedInput[5];
|
2014-09-17 06:26:25 +02:00
|
|
|
|
|
|
|
public IConfigManagerHost gui;
|
|
|
|
private IGridNode networkNode;
|
2014-09-29 09:54:34 +02:00
|
|
|
private final ITerminalHost host;
|
2014-09-17 06:26:25 +02:00
|
|
|
|
|
|
|
public IGridNode getNetworkNode()
|
|
|
|
{
|
|
|
|
return networkNode;
|
|
|
|
}
|
|
|
|
|
2014-09-21 01:06:22 +02:00
|
|
|
protected ContainerMEMonitorable(InventoryPlayer ip, ITerminalHost monitorable, boolean bindInventory) {
|
|
|
|
super( ip, monitorable instanceof TileEntity ? (TileEntity) monitorable : null, monitorable instanceof IPart ? (IPart) monitorable : null );
|
2014-09-17 06:26:25 +02:00
|
|
|
|
2014-09-27 05:08:58 +02:00
|
|
|
host = monitorable;
|
2014-09-17 06:26:25 +02:00
|
|
|
clientCM = new ConfigManager( this );
|
|
|
|
|
|
|
|
clientCM.registerSetting( Settings.SORT_BY, SortOrder.NAME );
|
|
|
|
clientCM.registerSetting( Settings.VIEW_MODE, ViewItems.ALL );
|
|
|
|
clientCM.registerSetting( Settings.SORT_DIRECTION, SortDir.ASCENDING );
|
|
|
|
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
2014-09-21 01:06:22 +02:00
|
|
|
serverCM = monitorable.getConfigManager();
|
2014-09-17 06:26:25 +02:00
|
|
|
|
2014-09-21 01:06:22 +02:00
|
|
|
monitor = monitorable.getItemInventory();
|
2014-09-17 06:26:25 +02:00
|
|
|
if ( monitor != null )
|
|
|
|
{
|
|
|
|
monitor.addListener( this, null );
|
|
|
|
|
|
|
|
cellInv = monitor;
|
|
|
|
|
2014-09-21 01:06:22 +02:00
|
|
|
if ( monitorable instanceof IPortableCell )
|
|
|
|
powerSrc = (IPortableCell) monitorable;
|
|
|
|
else if ( monitorable instanceof IMEChest )
|
|
|
|
powerSrc = (IMEChest) monitorable;
|
|
|
|
else if ( monitorable instanceof IGridHost )
|
2014-09-17 06:26:25 +02:00
|
|
|
{
|
2014-09-21 01:06:22 +02:00
|
|
|
IGridNode node = ((IGridHost) monitorable).getGridNode( ForgeDirection.UNKNOWN );
|
2014-09-17 06:26:25 +02:00
|
|
|
if ( node != null )
|
|
|
|
{
|
|
|
|
networkNode = node;
|
|
|
|
IGrid g = node.getGrid();
|
|
|
|
if ( g != null )
|
|
|
|
powerSrc = new ChannelPowerSrc( networkNode, (IEnergyGrid) g.getCache( IEnergyGrid.class ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
isContainerValid = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
monitor = null;
|
|
|
|
|
|
|
|
canAccessViewCells = false;
|
2014-09-21 01:06:22 +02:00
|
|
|
if ( monitorable instanceof IViewCellStorage )
|
2014-09-17 06:26:25 +02:00
|
|
|
{
|
|
|
|
for (int y = 0; y < 5; y++)
|
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
cellView[y] = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.VIEW_CELL, ((IViewCellStorage) monitorable).getViewCellStorage(), y, 206, y * 18 + 8,
|
2014-09-17 06:26:25 +02:00
|
|
|
invPlayer );
|
|
|
|
cellView[y].allowEdit = canAccessViewCells;
|
|
|
|
addSlotToContainer( cellView[y] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bindInventory )
|
|
|
|
bindPlayerInventory( ip, 0, 0 );
|
|
|
|
}
|
|
|
|
|
2014-09-21 01:06:22 +02:00
|
|
|
public ContainerMEMonitorable(InventoryPlayer ip, ITerminalHost monitorable) {
|
|
|
|
this( ip, monitorable, true );
|
2014-09-17 06:26:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void detectAndSendChanges()
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
2014-09-27 05:08:58 +02:00
|
|
|
if ( monitor != host.getItemInventory() )
|
|
|
|
isContainerValid = false;
|
|
|
|
|
2014-09-17 06:26:25 +02:00
|
|
|
for (Enum set : serverCM.getSettings())
|
|
|
|
{
|
|
|
|
Enum sideLocal = serverCM.getSetting( set );
|
|
|
|
Enum sideRemote = clientCM.getSetting( set );
|
|
|
|
|
|
|
|
if ( sideLocal != sideRemote )
|
|
|
|
{
|
|
|
|
clientCM.putSetting( set, sideLocal );
|
2014-09-29 09:23:02 +02:00
|
|
|
for (Object crafter : this.crafters)
|
2014-09-17 06:26:25 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-09-29 09:23:02 +02:00
|
|
|
NetworkHandler.instance.sendTo( new PacketValueConfig( set.name(), sideLocal.name() ), (EntityPlayerMP) crafter );
|
2014-09-17 06:26:25 +02:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() )
|
|
|
|
{
|
|
|
|
items.resetStatus();
|
|
|
|
|
|
|
|
for (Object c : this.crafters)
|
|
|
|
{
|
|
|
|
if ( c instanceof EntityPlayer )
|
|
|
|
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePowerStatus();
|
|
|
|
|
|
|
|
boolean oldCanAccessViewCells = canAccessViewCells;
|
|
|
|
canAccessViewCells = hasAccess( SecurityPermissions.BUILD, false );
|
|
|
|
if ( canAccessViewCells != oldCanAccessViewCells )
|
|
|
|
{
|
|
|
|
for (int y = 0; y < 5; y++)
|
|
|
|
{
|
|
|
|
if ( cellView[y] != null )
|
|
|
|
cellView[y].allowEdit = canAccessViewCells;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.detectAndSendChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void updatePowerStatus()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( networkNode != null )
|
|
|
|
hasPower = networkNode.isActive();
|
|
|
|
else if ( powerSrc instanceof IEnergyGrid )
|
|
|
|
hasPower = ((IEnergyGrid) powerSrc).isNetworkPowered();
|
|
|
|
else
|
|
|
|
hasPower = powerSrc.extractAEPower( 1, Actionable.SIMULATE, PowerMultiplier.CONFIG ) > 0.8;
|
|
|
|
}
|
|
|
|
catch (Throwable t)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addCraftingToCrafters(ICrafting c)
|
|
|
|
{
|
|
|
|
super.addCraftingToCrafters( c );
|
|
|
|
queueInventory( c );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void queueInventory(ICrafting c)
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() && c instanceof EntityPlayer && monitor != null )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
PacketMEInventoryUpdate piu = new PacketMEInventoryUpdate();
|
|
|
|
IItemList<IAEItemStack> monitorCache = monitor.getStorageList();
|
|
|
|
|
|
|
|
for (IAEItemStack send : monitorCache)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
piu.appendItem( send );
|
|
|
|
}
|
|
|
|
catch (BufferOverflowException boe)
|
|
|
|
{
|
|
|
|
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
|
|
|
|
|
|
|
|
piu = new PacketMEInventoryUpdate();
|
|
|
|
piu.appendItem( send );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onListUpdate()
|
|
|
|
{
|
|
|
|
for (Object c : this.crafters)
|
|
|
|
{
|
|
|
|
if ( c instanceof ICrafting )
|
|
|
|
{
|
|
|
|
ICrafting cr = (ICrafting) c;
|
|
|
|
queueInventory( cr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpdate(String field, Object oldValue, Object newValue)
|
|
|
|
{
|
|
|
|
if ( field.equals( "canAccessViewCells" ) )
|
|
|
|
{
|
|
|
|
for (int y = 0; y < 5; y++)
|
|
|
|
if ( cellView[y] != null )
|
|
|
|
cellView[y].allowEdit = canAccessViewCells;
|
|
|
|
}
|
|
|
|
|
|
|
|
super.onUpdate( field, oldValue, newValue );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContainerClosed(EntityPlayer player)
|
|
|
|
{
|
|
|
|
super.onContainerClosed( player );
|
|
|
|
if ( monitor != null )
|
|
|
|
monitor.removeListener( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeCraftingFromCrafters(ICrafting c)
|
|
|
|
{
|
|
|
|
super.removeCraftingFromCrafters( c );
|
|
|
|
|
|
|
|
if ( this.crafters.isEmpty() && monitor != null )
|
|
|
|
monitor.removeListener( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void postChange(IBaseMonitor<IAEItemStack> monitor, Iterable<IAEItemStack> change, BaseActionSource source)
|
|
|
|
{
|
|
|
|
for (IAEItemStack is : change)
|
|
|
|
items.add( is );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValid(Object verificationToken)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateSetting(IConfigManager manager, Enum settingName, Enum newValue)
|
|
|
|
{
|
|
|
|
if ( gui != null )
|
|
|
|
gui.updateSetting( manager, settingName, newValue );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IConfigManager getConfigManager()
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
return serverCM;
|
|
|
|
return clientCM;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack[] getViewCells()
|
|
|
|
{
|
|
|
|
ItemStack[] list = new ItemStack[cellView.length];
|
|
|
|
|
|
|
|
for (int x = 0; x < cellView.length; x++)
|
|
|
|
list[x] = cellView[x].getStack();
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|