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;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
import net.minecraft.inventory.ICrafting;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-02-21 21:36:40 +01:00
|
|
|
import appeng.api.AEApi;
|
2014-03-28 05:31:06 +01:00
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.config.PowerMultiplier;
|
2014-03-05 04:12:23 +01:00
|
|
|
import appeng.api.config.SecurityPermissions;
|
2014-02-17 01:50:25 +01:00
|
|
|
import appeng.api.config.Settings;
|
|
|
|
import appeng.api.config.SortDir;
|
|
|
|
import appeng.api.config.SortOrder;
|
|
|
|
import appeng.api.config.ViewItems;
|
2014-01-23 20:02:48 +01:00
|
|
|
import appeng.api.implementations.guiobjects.IPortableCell;
|
|
|
|
import appeng.api.implementations.tiles.IMEChest;
|
2014-03-04 08:52:07 +01:00
|
|
|
import appeng.api.implementations.tiles.IViewCellStorage;
|
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-06-27 07:44:56 +02:00
|
|
|
import appeng.api.networking.storage.IBaseMonitor;
|
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-02-12 17:35:42 +01:00
|
|
|
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
2014-02-17 01:50:25 +01:00
|
|
|
import appeng.api.storage.ITerminalHost;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-02-17 01:50:25 +01:00
|
|
|
import appeng.api.util.IConfigManager;
|
|
|
|
import appeng.api.util.IConfigureableObject;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.container.AEBaseContainer;
|
2014-05-07 07:22:42 +02:00
|
|
|
import appeng.container.guisync.GuiSync;
|
2014-03-04 08:52:07 +01:00
|
|
|
import appeng.container.slot.SlotRestrictedInput;
|
|
|
|
import appeng.container.slot.SlotRestrictedInput.PlaceableItemType;
|
2014-02-07 21:37:22 +01:00
|
|
|
import appeng.core.AELog;
|
2014-02-09 02:34:52 +01:00
|
|
|
import appeng.core.sync.network.NetworkHandler;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.core.sync.packets.PacketMEInventoryUpdate;
|
2014-02-17 01:50:25 +01:00
|
|
|
import appeng.core.sync.packets.PacketValueConfig;
|
2014-03-28 05:31:06 +01:00
|
|
|
import appeng.me.helpers.ChannelPowerSrc;
|
2014-02-17 01:50:25 +01:00
|
|
|
import appeng.util.ConfigManager;
|
|
|
|
import appeng.util.IConfigManagerHost;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.util.Platform;
|
|
|
|
|
2014-02-17 01:50:25 +01:00
|
|
|
public class ContainerMEMonitorable extends AEBaseContainer implements IConfigManagerHost, IConfigureableObject, IMEMonitorHandlerReceiver<IAEItemStack>
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
final IMEMonitor<IAEItemStack> monitor;
|
2014-02-21 21:36:40 +01:00
|
|
|
final IItemList<IAEItemStack> items = AEApi.instance().storage().createItemList();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-17 01:50:25 +01:00
|
|
|
IConfigManager serverCM;
|
|
|
|
IConfigManager clientCM;
|
|
|
|
|
2014-05-07 07:22:42 +02:00
|
|
|
@GuiSync(99)
|
2014-03-05 04:12:23 +01:00
|
|
|
public boolean canAccessViewCells = false;
|
2014-05-07 07:22:42 +02:00
|
|
|
|
|
|
|
@GuiSync(98)
|
2014-03-28 05:31:06 +01:00
|
|
|
public boolean hasPower = false;
|
|
|
|
|
2014-03-05 04:12:23 +01:00
|
|
|
public SlotRestrictedInput cellView[] = new SlotRestrictedInput[5];
|
2014-03-04 08:52:07 +01:00
|
|
|
|
2014-02-17 01:50:25 +01:00
|
|
|
public IConfigManagerHost gui;
|
2014-03-28 05:31:06 +01:00
|
|
|
private IGridNode networkNode;
|
2014-02-17 01:50:25 +01:00
|
|
|
|
2014-05-01 06:01:22 +02:00
|
|
|
public IGridNode getNetworkNode()
|
|
|
|
{
|
|
|
|
return networkNode;
|
|
|
|
}
|
|
|
|
|
2014-02-17 01:50:25 +01:00
|
|
|
protected ContainerMEMonitorable(InventoryPlayer ip, ITerminalHost 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-17 01:50:25 +01: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 );
|
|
|
|
|
2014-02-01 06:37:27 +01:00
|
|
|
if ( Platform.isServer() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-17 01:50:25 +01:00
|
|
|
serverCM = montiorable.getConfigManager();
|
|
|
|
|
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 )
|
|
|
|
{
|
2014-03-28 05:31:06 +01:00
|
|
|
networkNode = node;
|
2014-02-03 06:02:54 +01:00
|
|
|
IGrid g = node.getGrid();
|
|
|
|
if ( g != null )
|
2014-03-28 05:31:06 +01:00
|
|
|
powerSrc = new ChannelPowerSrc( networkNode, (IEnergyGrid) g.getCache( IEnergyGrid.class ) );
|
2014-02-03 06:02:54 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 06:02:54 +01:00
|
|
|
else
|
2014-02-09 02:34:52 +01:00
|
|
|
isContainerValid = false;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
monitor = null;
|
2014-01-30 03:48:09 +01:00
|
|
|
|
2014-03-05 04:12:23 +01:00
|
|
|
canAccessViewCells = false;
|
2014-03-04 08:52:07 +01:00
|
|
|
if ( montiorable instanceof IViewCellStorage )
|
2014-03-05 04:12:23 +01:00
|
|
|
{
|
|
|
|
for (int y = 0; y < 5; y++)
|
|
|
|
{
|
2014-07-13 03:03:17 +02:00
|
|
|
cellView[y] = new SlotRestrictedInput( PlaceableItemType.VIEWCELL, ((IViewCellStorage) montiorable).getViewCellStorage(), y, 206, y * 18 + 8,
|
|
|
|
invPlayer );
|
2014-03-05 04:12:23 +01:00
|
|
|
cellView[y].allowEdit = canAccessViewCells;
|
|
|
|
addSlotToContainer( cellView[y] );
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 08:52:07 +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-02-17 01:50:25 +01:00
|
|
|
public ContainerMEMonitorable(InventoryPlayer ip, ITerminalHost 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() )
|
|
|
|
{
|
2014-02-17 01:50:25 +01:00
|
|
|
for (Enum set : serverCM.getSettings())
|
|
|
|
{
|
|
|
|
Enum sideLocal = serverCM.getSetting( set );
|
|
|
|
Enum sideRemote = clientCM.getSetting( set );
|
|
|
|
|
|
|
|
if ( sideLocal != sideRemote )
|
|
|
|
{
|
|
|
|
clientCM.putSetting( set, sideLocal );
|
|
|
|
for (int j = 0; j < this.crafters.size(); ++j)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
NetworkHandler.instance.sendTo( new PacketValueConfig( set.name(), sideLocal.name() ), (EntityPlayerMP) this.crafters.get( j ) );
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
if ( c instanceof EntityPlayer )
|
|
|
|
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2014-02-07 21:37:22 +01:00
|
|
|
AELog.error( e );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 05:31:06 +01:00
|
|
|
updatePowerStatus();
|
|
|
|
|
2014-03-05 04:12:23 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
super.detectAndSendChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 05:31:06 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public void addCraftingToCrafters(ICrafting c)
|
|
|
|
{
|
|
|
|
super.addCraftingToCrafters( c );
|
2014-03-28 05:31:06 +01:00
|
|
|
queueInventory( c );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-28 05:31:06 +01:00
|
|
|
public void queueInventory(ICrafting c)
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
if ( Platform.isServer() && c instanceof EntityPlayer && 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
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) 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
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2014-02-07 21:37:22 +01:00
|
|
|
AELog.error( e );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 05:31:06 +01:00
|
|
|
@Override
|
|
|
|
public void onListUpdate()
|
|
|
|
{
|
|
|
|
for (Object c : this.crafters)
|
|
|
|
{
|
|
|
|
if ( c instanceof ICrafting )
|
|
|
|
{
|
|
|
|
ICrafting cr = (ICrafting) c;
|
|
|
|
queueInventory( cr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 04:12:23 +01:00
|
|
|
@Override
|
2014-05-07 07:22:42 +02:00
|
|
|
public void onUpdate(String field, Object oldValue, Object newValue)
|
2014-03-05 04:12:23 +01:00
|
|
|
{
|
2014-05-07 07:22:42 +02:00
|
|
|
if ( field.equals( "canAccessViewCells" ) )
|
|
|
|
{
|
|
|
|
for (int y = 0; y < 5; y++)
|
|
|
|
if ( cellView[y] != null )
|
|
|
|
cellView[y].allowEdit = canAccessViewCells;
|
|
|
|
}
|
2014-03-05 04:12:23 +01:00
|
|
|
|
2014-05-07 07:22:42 +02:00
|
|
|
super.onUpdate( field, oldValue, newValue );
|
2014-03-05 04:12:23 +01:00
|
|
|
}
|
|
|
|
|
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-06-27 07:44:56 +02:00
|
|
|
public void postChange(IBaseMonitor<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;
|
|
|
|
}
|
|
|
|
|
2014-02-17 01:50:25 +01:00
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|