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

380 lines
10 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>.
*/
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;
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>
{
final IMEMonitor<IAEItemStack> monitor;
final IItemList<IAEItemStack> items = AEApi.instance().storage().createItemList();
IConfigManager serverCM;
final IConfigManager clientCM;
@GuiSync(99)
public boolean canAccessViewCells = false;
@GuiSync(98)
public boolean hasPower = false;
public final SlotRestrictedInput[] cellView = new SlotRestrictedInput[5];
public IConfigManagerHost gui;
private IGridNode networkNode;
private final ITerminalHost host;
public IGridNode getNetworkNode()
{
2014-12-29 15:13:47 +01:00
return this.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-12-29 15:13:47 +01:00
this.host = monitorable;
this.clientCM = new ConfigManager( this );
2014-12-29 15:13:47 +01:00
this.clientCM.registerSetting( Settings.SORT_BY, SortOrder.NAME );
this.clientCM.registerSetting( Settings.VIEW_MODE, ViewItems.ALL );
this.clientCM.registerSetting( Settings.SORT_DIRECTION, SortDir.ASCENDING );
if ( Platform.isServer() )
{
2014-12-29 15:13:47 +01:00
this.serverCM = monitorable.getConfigManager();
2014-12-29 15:13:47 +01:00
this.monitor = monitorable.getItemInventory();
if ( this.monitor != null )
{
2014-12-29 15:13:47 +01:00
this.monitor.addListener( this, null );
2014-12-29 15:13:47 +01:00
this.cellInv = this.monitor;
2014-09-21 01:06:22 +02:00
if ( monitorable instanceof IPortableCell )
2014-12-29 15:13:47 +01:00
this.powerSrc = (IPortableCell) monitorable;
2014-09-21 01:06:22 +02:00
else if ( monitorable instanceof IMEChest )
2014-12-29 15:13:47 +01:00
this.powerSrc = (IMEChest) monitorable;
2014-09-21 01:06:22 +02:00
else if ( monitorable instanceof IGridHost )
{
2014-09-21 01:06:22 +02:00
IGridNode node = ((IGridHost) monitorable).getGridNode( ForgeDirection.UNKNOWN );
if ( node != null )
{
2014-12-29 15:13:47 +01:00
this.networkNode = node;
IGrid g = node.getGrid();
if ( g != null )
2014-12-29 15:13:47 +01:00
this.powerSrc = new ChannelPowerSrc( this.networkNode, (IEnergyGrid) g.getCache( IEnergyGrid.class ) );
}
}
}
else
2014-12-29 15:13:47 +01:00
this.isContainerValid = false;
}
else
2014-12-29 15:13:47 +01:00
this.monitor = null;
2014-12-29 15:13:47 +01:00
this.canAccessViewCells = false;
2014-09-21 01:06:22 +02:00
if ( monitorable instanceof IViewCellStorage )
{
for (int y = 0; y < 5; y++)
{
2014-12-29 15:13:47 +01:00
this.cellView[y] = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.VIEW_CELL, ((IViewCellStorage) monitorable).getViewCellStorage(), y, 206, y * 18 + 8,
this.invPlayer );
this.cellView[y].allowEdit = this.canAccessViewCells;
this.addSlotToContainer( this.cellView[y] );
}
}
if ( bindInventory )
2014-12-29 15:13:47 +01:00
this.bindPlayerInventory( ip, 0, 0 );
}
2014-09-21 01:06:22 +02:00
public ContainerMEMonitorable(InventoryPlayer ip, ITerminalHost monitorable) {
this( ip, monitorable, true );
}
@Override
public void detectAndSendChanges()
{
if ( Platform.isServer() )
{
2014-12-29 15:13:47 +01:00
if ( this.monitor != this.host.getItemInventory() )
this.isContainerValid = false;
2014-12-29 15:13:47 +01:00
for (Enum set : this.serverCM.getSettings())
{
2014-12-29 15:13:47 +01:00
Enum sideLocal = this.serverCM.getSetting( set );
Enum sideRemote = this.clientCM.getSetting( set );
if ( sideLocal != sideRemote )
{
2014-12-29 15:13:47 +01:00
this.clientCM.putSetting( set, sideLocal );
for (Object crafter : this.crafters)
{
try
{
NetworkHandler.instance.sendTo( new PacketValueConfig( set.name(), sideLocal.name() ), (EntityPlayerMP) crafter );
}
catch (IOException e)
{
AELog.error( e );
}
}
}
}
2014-12-29 15:13:47 +01:00
if ( !this.items.isEmpty() )
{
try
{
2014-12-29 15:13:47 +01:00
IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
PacketMEInventoryUpdate piu = new PacketMEInventoryUpdate();
2014-12-29 15:13:47 +01:00
for (IAEItemStack is : this.items)
{
IAEItemStack send = monitorCache.findPrecise( is );
if ( send == null )
{
is.setStackSize( 0 );
piu.appendItem( is );
}
else
piu.appendItem( send );
}
if ( !piu.isEmpty() )
{
2014-12-29 15:13:47 +01:00
this.items.resetStatus();
for (Object c : this.crafters)
{
if ( c instanceof EntityPlayer )
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
}
}
}
catch (IOException e)
{
AELog.error( e );
}
}
2014-12-29 15:13:47 +01:00
this.updatePowerStatus();
2014-12-29 15:13:47 +01:00
boolean oldCanAccessViewCells = this.canAccessViewCells;
this.canAccessViewCells = this.hasAccess( SecurityPermissions.BUILD, false );
if ( this.canAccessViewCells != oldCanAccessViewCells )
{
for (int y = 0; y < 5; y++)
{
2014-12-29 15:13:47 +01:00
if ( this.cellView[y] != null )
this.cellView[y].allowEdit = this.canAccessViewCells;
}
}
super.detectAndSendChanges();
}
}
protected void updatePowerStatus()
{
try
{
2014-12-29 15:13:47 +01:00
if ( this.networkNode != null )
this.hasPower = this.networkNode.isActive();
else if ( this.powerSrc instanceof IEnergyGrid )
this.hasPower = ((IEnergyGrid) this.powerSrc).isNetworkPowered();
else
2014-12-29 15:13:47 +01:00
this.hasPower = this.powerSrc.extractAEPower( 1, Actionable.SIMULATE, PowerMultiplier.CONFIG ) > 0.8;
}
catch (Throwable t)
{
// :P
}
}
@Override
public void addCraftingToCrafters(ICrafting c)
{
super.addCraftingToCrafters( c );
2014-12-29 15:13:47 +01:00
this.queueInventory( c );
}
public void queueInventory(ICrafting c)
{
2014-12-29 15:13:47 +01:00
if ( Platform.isServer() && c instanceof EntityPlayer && this.monitor != null )
{
try
{
PacketMEInventoryUpdate piu = new PacketMEInventoryUpdate();
2014-12-29 15:13:47 +01:00
IItemList<IAEItemStack> monitorCache = this.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;
2014-12-29 15:13:47 +01:00
this.queueInventory( cr );
}
}
}
@Override
public void onUpdate(String field, Object oldValue, Object newValue)
{
if ( field.equals( "canAccessViewCells" ) )
{
for (int y = 0; y < 5; y++)
2014-12-29 15:13:47 +01:00
if ( this.cellView[y] != null )
this.cellView[y].allowEdit = this.canAccessViewCells;
}
super.onUpdate( field, oldValue, newValue );
}
@Override
public void onContainerClosed(EntityPlayer player)
{
super.onContainerClosed( player );
2014-12-29 15:13:47 +01:00
if ( this.monitor != null )
this.monitor.removeListener( this );
}
@Override
public void removeCraftingFromCrafters(ICrafting c)
{
super.removeCraftingFromCrafters( c );
2014-12-29 15:13:47 +01:00
if ( this.crafters.isEmpty() && this.monitor != null )
this.monitor.removeListener( this );
}
@Override
public void postChange(IBaseMonitor<IAEItemStack> monitor, Iterable<IAEItemStack> change, BaseActionSource source)
{
for (IAEItemStack is : change)
2014-12-29 15:13:47 +01:00
this.items.add( is );
}
@Override
public boolean isValid(Object verificationToken)
{
return true;
}
@Override
public void updateSetting(IConfigManager manager, Enum settingName, Enum newValue)
{
2014-12-29 15:13:47 +01:00
if ( this.gui != null )
this.gui.updateSetting( manager, settingName, newValue );
}
@Override
public IConfigManager getConfigManager()
{
if ( Platform.isServer() )
2014-12-29 15:13:47 +01:00
return this.serverCM;
return this.clientCM;
}
public ItemStack[] getViewCells()
{
2014-12-29 15:13:47 +01:00
ItemStack[] list = new ItemStack[this.cellView.length];
2014-12-29 15:13:47 +01:00
for (int x = 0; x < this.cellView.length; x++)
list[x] = this.cellView[x].getStack();
return list;
}
}