Applied-Energistics-2-tiler.../core/sync/GuiBridge.java

458 lines
15 KiB
Java
Raw Normal View History

package appeng.core.sync;
import static appeng.core.sync.GuiHostType.ITEM;
import static appeng.core.sync.GuiHostType.ITEM_OR_WORLD;
import static appeng.core.sync.GuiHostType.WORLD;
import java.lang.reflect.Constructor;
2013-12-28 22:03:20 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
2014-01-20 17:41:37 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
2014-02-03 05:33:50 +01:00
import appeng.api.AEApi;
2014-02-01 06:37:27 +01:00
import appeng.api.config.SecurityPermissions;
import appeng.api.exceptions.AppEngException;
2014-02-03 05:33:50 +01:00
import appeng.api.features.IWirelessTermHandler;
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.IUpgradeableHost;
import appeng.api.implementations.guiobjects.IGuiItem;
import appeng.api.implementations.guiobjects.INetworkTool;
2014-01-24 17:35:58 +01:00
import appeng.api.implementations.guiobjects.IPortableCell;
2014-02-01 06:37:27 +01:00
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridNode;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.security.ISecurityGrid;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartHost;
2014-02-17 01:50:25 +01:00
import appeng.api.storage.ITerminalHost;
import appeng.client.gui.GuiNull;
2014-01-26 07:44:50 +01:00
import appeng.container.AEBaseContainer;
import appeng.container.ContainerNull;
2014-01-26 07:44:50 +01:00
import appeng.container.ContainerOpenContext;
2014-01-20 17:41:37 +01:00
import appeng.container.implementations.ContainerCellWorkbench;
import appeng.container.implementations.ContainerChest;
import appeng.container.implementations.ContainerCondenser;
import appeng.container.implementations.ContainerCraftAmount;
import appeng.container.implementations.ContainerCraftConfirm;
import appeng.container.implementations.ContainerCraftingTerm;
import appeng.container.implementations.ContainerDrive;
2014-02-18 06:51:31 +01:00
import appeng.container.implementations.ContainerFormationPlane;
import appeng.container.implementations.ContainerGrinder;
2014-01-20 17:41:37 +01:00
import appeng.container.implementations.ContainerIOPort;
import appeng.container.implementations.ContainerInscriber;
2013-12-28 22:03:20 +01:00
import appeng.container.implementations.ContainerInterface;
import appeng.container.implementations.ContainerLevelEmitter;
2014-04-24 08:14:26 +02:00
import appeng.container.implementations.ContainerMAC;
import appeng.container.implementations.ContainerMEMonitorable;
2014-01-20 17:41:37 +01:00
import appeng.container.implementations.ContainerMEPortableCell;
2014-01-23 17:28:12 +01:00
import appeng.container.implementations.ContainerNetworkStatus;
import appeng.container.implementations.ContainerNetworkTool;
2014-04-08 02:43:57 +02:00
import appeng.container.implementations.ContainerPatternTerm;
2014-01-26 07:44:50 +01:00
import appeng.container.implementations.ContainerPriority;
2014-01-20 17:41:37 +01:00
import appeng.container.implementations.ContainerQNB;
import appeng.container.implementations.ContainerQuartzKnife;
2014-01-26 07:44:50 +01:00
import appeng.container.implementations.ContainerSecurity;
2014-02-28 09:12:00 +01:00
import appeng.container.implementations.ContainerSkyChest;
import appeng.container.implementations.ContainerSpatialIOPort;
2014-01-24 17:35:58 +01:00
import appeng.container.implementations.ContainerStorageBus;
2014-01-20 17:41:37 +01:00
import appeng.container.implementations.ContainerUpgradeable;
import appeng.container.implementations.ContainerVibrationChamber;
import appeng.container.implementations.ContainerWireless;
2014-02-03 05:33:50 +01:00
import appeng.container.implementations.ContainerWirelessTerm;
import appeng.helpers.IInterfaceHost;
2014-01-26 07:44:50 +01:00
import appeng.helpers.IPriorityHost;
2014-02-03 05:33:50 +01:00
import appeng.helpers.WirelessTerminalGuiObject;
import appeng.items.contents.QuartzKnifeObj;
2014-02-18 06:51:31 +01:00
import appeng.parts.automation.PartFormationPlane;
import appeng.parts.automation.PartLevelEmitter;
2014-01-24 17:35:58 +01:00
import appeng.parts.misc.PartStorageBus;
import appeng.parts.reporting.PartCraftingTerminal;
2014-04-08 02:43:57 +02:00
import appeng.parts.reporting.PartPatternTerminal;
2014-02-01 06:37:27 +01:00
import appeng.server.AccessType;
2014-04-24 08:14:26 +02:00
import appeng.tile.crafting.TileMolecularAssembler;
import appeng.tile.grindstone.TileGrinder;
2014-01-06 07:54:25 +01:00
import appeng.tile.misc.TileCellWorkbench;
import appeng.tile.misc.TileCondenser;
import appeng.tile.misc.TileInscriber;
2014-01-26 07:44:50 +01:00
import appeng.tile.misc.TileSecurity;
import appeng.tile.misc.TileVibrationChamber;
import appeng.tile.networking.TileWireless;
2014-01-20 17:41:37 +01:00
import appeng.tile.qnb.TileQuantumBridge;
import appeng.tile.spatial.TileSpatialIOPort;
import appeng.tile.storage.TileChest;
import appeng.tile.storage.TileDrive;
2014-01-20 17:41:37 +01:00
import appeng.tile.storage.TileIOPort;
2014-02-28 09:12:00 +01:00
import appeng.tile.storage.TileSkyChest;
import appeng.util.Platform;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.relauncher.ReflectionHelper;
public enum GuiBridge implements IGuiHandler
{
GUI_Handler(),
GUI_GRINDER(ContainerGrinder.class, TileGrinder.class, WORLD, null),
GUI_QNB(ContainerQNB.class, TileQuantumBridge.class, WORLD, SecurityPermissions.BUILD),
GUI_SKYCHEST(ContainerSkyChest.class, TileSkyChest.class, WORLD, null),
2014-02-28 09:12:00 +01:00
GUI_CHEST(ContainerChest.class, TileChest.class, WORLD, SecurityPermissions.BUILD),
GUI_WIRELESS(ContainerWireless.class, TileWireless.class, WORLD, SecurityPermissions.BUILD),
GUI_ME(ContainerMEMonitorable.class, ITerminalHost.class, WORLD, null),
GUI_PORTABLE_CELL(ContainerMEPortableCell.class, IPortableCell.class, ITEM, null),
GUI_WIRELESS_TERM(ContainerWirelessTerm.class, WirelessTerminalGuiObject.class, ITEM, null),
2014-02-03 05:33:50 +01:00
GUI_NETWORK_STATUS(ContainerNetworkStatus.class, INetworkTool.class, ITEM, null),
2014-01-23 17:28:12 +01:00
GUI_NETWORK_TOOL(ContainerNetworkTool.class, INetworkTool.class, ITEM, null),
2014-01-23 17:28:12 +01:00
GUI_QUARTZ_KNIFE(ContainerQuartzKnife.class, QuartzKnifeObj.class, ITEM, null),
GUI_DRIVE(ContainerDrive.class, TileDrive.class, WORLD, SecurityPermissions.BUILD),
2013-12-28 22:03:20 +01:00
GUI_VIBRATIONCHAMBER(ContainerVibrationChamber.class, TileVibrationChamber.class, WORLD, null),
2013-12-30 06:56:43 +01:00
GUI_CONDENSER(ContainerCondenser.class, TileCondenser.class, WORLD, null),
2014-01-20 17:41:37 +01:00
GUI_INTERFACE(ContainerInterface.class, IInterfaceHost.class, WORLD, SecurityPermissions.BUILD),
2014-01-20 17:41:37 +01:00
GUI_BUS(ContainerUpgradeable.class, IUpgradeableHost.class, WORLD, SecurityPermissions.BUILD),
2014-01-20 17:41:37 +01:00
GUI_IOPORT(ContainerIOPort.class, TileIOPort.class, WORLD, SecurityPermissions.BUILD),
GUI_STORAGEBUS(ContainerStorageBus.class, PartStorageBus.class, WORLD, SecurityPermissions.BUILD),
2014-01-24 17:35:58 +01:00
GUI_FPLANE(ContainerFormationPlane.class, PartFormationPlane.class, WORLD, SecurityPermissions.BUILD),
2014-02-18 06:51:31 +01:00
GUI_PRIORITY(ContainerPriority.class, IPriorityHost.class, WORLD, SecurityPermissions.BUILD),
2014-01-26 07:44:50 +01:00
GUI_SECURITY(ContainerSecurity.class, TileSecurity.class, WORLD, SecurityPermissions.SECURITY),
2014-01-26 07:44:50 +01:00
GUI_CRAFTING_TERMINAL(ContainerCraftingTerm.class, PartCraftingTerminal.class, WORLD, SecurityPermissions.CRAFT),
GUI_PATTERN_TERMINAL(ContainerPatternTerm.class, PartPatternTerminal.class, WORLD, SecurityPermissions.CRAFT),
2014-04-08 02:43:57 +02:00
// extends (Container/Gui) + Bus
GUI_LEVELEMITTER(ContainerLevelEmitter.class, PartLevelEmitter.class, WORLD, SecurityPermissions.BUILD),
GUI_SPATIALIOPORT(ContainerSpatialIOPort.class, TileSpatialIOPort.class, WORLD, SecurityPermissions.BUILD),
2014-01-06 07:54:25 +01:00
GUI_INSCRIBER(ContainerInscriber.class, TileInscriber.class, WORLD, null),
GUI_CELLWORKBENCH(ContainerCellWorkbench.class, TileCellWorkbench.class, WORLD, null),
GUI_MAC(ContainerMAC.class, TileMolecularAssembler.class, WORLD, null),
2014-04-24 08:14:26 +02:00
GUI_CRAFTING_AMOUNT(ContainerCraftAmount.class, ITerminalHost.class, ITEM_OR_WORLD, SecurityPermissions.CRAFT),
GUI_CRAFTING_CONFIRM(ContainerCraftConfirm.class, ITerminalHost.class, ITEM_OR_WORLD, SecurityPermissions.CRAFT);
private Class Tile;
private Class Gui;
private Class Container;
private GuiHostType type;
2014-02-01 06:37:27 +01:00
private SecurityPermissions requiredPermission;
private GuiBridge() {
Tile = null;
Gui = null;
Container = null;
}
/**
* I honestly wish I could just use the GuiClass Names myself, but I can't access them without MC's Server
* Exploding.
*/
private void getGui()
{
if ( Platform.isClient() )
{
String start = Container.getName();
String GuiClass = start.replaceFirst( "container.", "client.gui." ).replace( ".Container", ".Gui" );
if ( start.equals( GuiClass ) )
throw new RuntimeException( "Unable to find gui class" );
Gui = ReflectionHelper.getClass( this.getClass().getClassLoader(), GuiClass );
if ( Gui == null )
throw new RuntimeException( "Cannot Load class: " + GuiClass );
}
}
2014-02-01 06:37:27 +01:00
private GuiBridge(Class _Container, SecurityPermissions requiredPermission) {
this.requiredPermission = requiredPermission;
Container = _Container;
Tile = null;
getGui();
}
private GuiBridge(Class _Container, Class _Tile, GuiHostType type, SecurityPermissions requiredPermission) {
2014-02-01 06:37:27 +01:00
this.requiredPermission = requiredPermission;
Container = _Container;
this.type = type;
Tile = _Tile;
getGui();
}
public boolean CorrectTileOrPart(Object tE)
{
if ( Tile == null )
throw new RuntimeException( "This Gui Cannot use the standard Handler." );
return Tile.isInstance( tE );
}
public Object ConstructContainer(InventoryPlayer inventory, ForgeDirection side, Object tE)
{
try
{
Constructor[] c = Container.getConstructors();
if ( c.length == 0 )
throw new AppEngException( "Invalid Gui Class" );
2014-04-09 05:12:01 +02:00
Constructor target = findConstructor( c, inventory, tE );
if ( target == null )
{
2014-04-09 05:14:03 +02:00
throw new RuntimeException( "Cannot find " + Container.getName() + "( " + typeName( inventory ) + ", " + typeName( tE ) + " )" );
2014-04-09 05:12:01 +02:00
}
return target.newInstance( inventory, tE );
}
catch (Throwable t)
{
throw new RuntimeException( t );
}
}
2013-12-28 22:03:20 +01:00
public Object ConstructGui(InventoryPlayer inventory, ForgeDirection side, Object tE)
{
try
{
Constructor[] c = Gui.getConstructors();
if ( c.length == 0 )
throw new AppEngException( "Invalid Gui Class" );
2014-04-09 05:12:01 +02:00
Constructor target = findConstructor( c, inventory, tE );
if ( target == null )
{
2014-04-09 05:14:03 +02:00
throw new RuntimeException( "Cannot find " + Container.getName() + "( " + typeName( inventory ) + ", " + typeName( tE ) + " )" );
2014-04-09 05:12:01 +02:00
}
return target.newInstance( inventory, tE );
}
catch (Throwable t)
{
throw new RuntimeException( t );
}
}
2014-04-09 05:12:01 +02:00
private String typeName(Object inventory)
{
if ( inventory == null )
return "NULL";
return inventory.getClass().getName();
}
private Constructor findConstructor(Constructor[] c, InventoryPlayer inventory, Object tE)
{
for (Constructor con : c)
{
Class[] types = con.getParameterTypes();
if ( types.length == 2 )
{
2014-04-09 06:26:04 +02:00
if ( types[0].isAssignableFrom( inventory.getClass() ) && types[1].isAssignableFrom( tE.getClass() ) )
return con;
}
}
return null;
}
2014-01-26 07:44:50 +01:00
private Object updateGui(Object newContainer, World w, int x, int y, int z, ForgeDirection side)
{
if ( newContainer instanceof AEBaseContainer )
{
AEBaseContainer bc = (AEBaseContainer) newContainer;
bc.openContext = new ContainerOpenContext();
bc.openContext.w = w;
bc.openContext.x = x;
bc.openContext.y = y;
bc.openContext.z = z;
bc.openContext.side = side;
}
return newContainer;
}
@Override
public Object getServerGuiElement(int ID_ORDINAL, EntityPlayer player, World w, int x, int y, int z)
{
2013-12-28 22:03:20 +01:00
ForgeDirection side = ForgeDirection.getOrientation( ID_ORDINAL & 0x07 );
GuiBridge ID = values()[ID_ORDINAL >> 3];
if ( ID.type.isItem() )
{
2014-01-20 17:41:37 +01:00
ItemStack it = player.inventory.getCurrentItem();
2014-02-03 05:33:50 +01:00
Object myItem = getGuiObject( it, player, w, x, y, z );
if ( myItem != null && ID.CorrectTileOrPart( myItem ) )
return updateGui( ID.ConstructContainer( player.inventory, side, myItem ), w, x, y, z, side );
}
if ( ID.type.isTile() )
{
2014-02-09 02:34:52 +01:00
TileEntity TE = w.getTileEntity( x, y, z );
2014-01-20 17:41:37 +01:00
if ( TE instanceof IPartHost )
{
((IPartHost) TE).getPart( side );
IPart part = ((IPartHost) TE).getPart( side );
if ( ID.CorrectTileOrPart( part ) )
2014-01-26 07:44:50 +01:00
return updateGui( ID.ConstructContainer( player.inventory, side, part ), w, x, y, z, side );
2014-01-20 17:41:37 +01:00
}
else
{
if ( ID.CorrectTileOrPart( TE ) )
2014-01-26 07:44:50 +01:00
return updateGui( ID.ConstructContainer( player.inventory, side, TE ), w, x, y, z, side );
2014-01-20 17:41:37 +01:00
}
}
2013-12-28 22:03:20 +01:00
return new ContainerNull();
}
2014-02-03 05:33:50 +01:00
private Object getGuiObject(ItemStack it, EntityPlayer player, World w, int x, int y, int z)
{
if ( it != null )
{
if ( it.getItem() instanceof IGuiItem )
{
return ((IGuiItem) it.getItem()).getGuiObject( it, w, x, y, z );
}
IWirelessTermHandler wh = AEApi.instance().registries().wireless().getWirelessTerminalHandler( it );
if ( wh != null )
return new WirelessTerminalGuiObject( wh, it, player, w, x, y, z );
}
return null;
}
@Override
public Object getClientGuiElement(int ID_ORDINAL, EntityPlayer player, World w, int x, int y, int z)
{
2013-12-28 22:03:20 +01:00
ForgeDirection side = ForgeDirection.getOrientation( ID_ORDINAL & 0x07 );
GuiBridge ID = values()[ID_ORDINAL >> 3];
if ( ID.type.isItem() )
{
2014-01-20 17:41:37 +01:00
ItemStack it = player.inventory.getCurrentItem();
2014-02-03 05:33:50 +01:00
Object myItem = getGuiObject( it, player, w, x, y, z );
if ( ID.CorrectTileOrPart( myItem ) )
return ID.ConstructGui( player.inventory, side, myItem );
}
if ( ID.type.isTile() )
{
2014-02-09 02:34:52 +01:00
TileEntity TE = w.getTileEntity( x, y, z );
2014-01-20 17:41:37 +01:00
if ( TE instanceof IPartHost )
{
((IPartHost) TE).getPart( side );
IPart part = ((IPartHost) TE).getPart( side );
if ( ID.CorrectTileOrPart( part ) )
return ID.ConstructGui( player.inventory, side, part );
}
else
{
if ( ID.CorrectTileOrPart( TE ) )
return ID.ConstructGui( player.inventory, side, TE );
}
}
2013-12-28 22:03:20 +01:00
return new GuiNull( new ContainerNull() );
}
2014-02-01 06:37:27 +01:00
public boolean hasPermissions(TileEntity te, int x, int y, int z, ForgeDirection side, EntityPlayer player)
{
World w = player.getEntityWorld();
if ( Platform.hasPermissions( x, y, z, player, AccessType.BLOCK_ACCESS ) )
{
if ( type.isItem() )
2014-02-01 06:37:27 +01:00
{
ItemStack it = player.inventory.getCurrentItem();
if ( it != null && it.getItem() instanceof IGuiItem )
{
Object myItem = ((IGuiItem) it.getItem()).getGuiObject( it, w, x, y, z );
if ( CorrectTileOrPart( myItem ) )
{
return true;
}
}
}
if ( type.isTile() )
2014-02-01 06:37:27 +01:00
{
2014-02-09 02:34:52 +01:00
TileEntity TE = w.getTileEntity( x, y, z );
2014-02-01 06:37:27 +01:00
if ( TE instanceof IPartHost )
{
((IPartHost) TE).getPart( side );
IPart part = ((IPartHost) TE).getPart( side );
if ( CorrectTileOrPart( part ) )
return securityCheck( part, player );
}
else
{
if ( CorrectTileOrPart( TE ) )
return securityCheck( TE, player );
}
}
}
return false;
}
private boolean securityCheck(Object te, EntityPlayer player)
{
if ( te instanceof IActionHost && requiredPermission != null )
{
boolean requirePower = false;
IGridNode gn = ((IActionHost) te).getActionableNode();
if ( gn != null )
{
IGrid g = gn.getGrid();
if ( g != null )
{
if ( requirePower )
{
IEnergyGrid eg = g.getCache( IEnergyGrid.class );
if ( !eg.isNetworkPowered() )
{
return false;
}
}
ISecurityGrid sg = g.getCache( ISecurityGrid.class );
if ( sg.hasPermission( player, requiredPermission ) )
return true;
}
}
return false;
}
return true;
}
public GuiHostType getType()
{
return type;
}
}