Applied-Energistics-2-tiler.../helpers/WirelessTerminalGuiObject.java

315 lines
6.7 KiB
Java
Raw Normal View History

2014-02-03 05:33:50 +01:00
package appeng.helpers;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.AEApi;
2014-02-03 05:33:50 +01:00
import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
import appeng.api.features.IWirelessTermHandler;
import appeng.api.implementations.guiobjects.IPortableCell;
2014-03-25 18:13:34 +01:00
import appeng.api.implementations.tiles.IWirelessAccessPoint;
2014-02-03 05:33:50 +01:00
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IMachineSet;
2014-02-03 05:33:50 +01:00
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.IActionHost;
2014-02-03 05:33:50 +01:00
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.IMEMonitorHandlerReceiver;
2014-02-03 05:33:50 +01:00
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AECableType;
2014-03-25 18:13:34 +01:00
import appeng.api.util.DimensionalCoord;
2014-02-17 01:50:25 +01:00
import appeng.api.util.IConfigManager;
import appeng.tile.networking.TileWireless;
2014-02-03 05:33:50 +01:00
public class WirelessTerminalGuiObject implements IPortableCell, IActionHost
2014-02-03 05:33:50 +01:00
{
IWirelessTermHandler wth;
String encryptionKey;
IGrid targetGrid;
IStorageGrid sg;
IMEMonitor<IAEItemStack> itemStorage;
2014-03-25 18:13:34 +01:00
IWirelessAccessPoint myWap;
double sqRange = Double.MAX_VALUE;
double myRange = Double.MAX_VALUE;
2014-02-03 05:33:50 +01:00
EntityPlayer myPlayer;
public ItemStack effectiveItem;
public double getRange()
{
return myRange;
}
2014-02-03 05:33:50 +01:00
public WirelessTerminalGuiObject(IWirelessTermHandler wh, ItemStack is, EntityPlayer ep, World w, int x, int y, int z) {
encryptionKey = wh.getEncryptionKey( is );
effectiveItem = is;
myPlayer = ep;
wth = wh;
Object obj = null;
try
{
long encKey = Long.parseLong( encryptionKey );
obj = AEApi.instance().registries().locateable().findLocateableBySerial( encKey );
}
catch (NumberFormatException err)
{
// :P
}
if ( obj instanceof IGridHost )
{
IGridNode n = ((IGridHost) obj).getGridNode( ForgeDirection.UNKNOWN );
if ( n != null )
{
targetGrid = n.getGrid();
if ( targetGrid != null )
{
sg = targetGrid.getCache( IStorageGrid.class );
if ( sg != null )
itemStorage = sg.getItemInventory();
}
}
}
}
public boolean rangeCheck()
{
sqRange = myRange = Double.MAX_VALUE;
if ( targetGrid != null && itemStorage != null )
{
if ( myWap != null )
{
2014-03-25 18:13:34 +01:00
if ( myWap.getGrid() == targetGrid )
{
if ( testWap( myWap ) )
return true;
}
return false;
}
IMachineSet tw = targetGrid.getMachines( TileWireless.class );
myWap = null;
for (IGridNode n : tw)
{
2014-03-25 18:13:34 +01:00
IWirelessAccessPoint wap = (IWirelessAccessPoint) n.getMachine();
if ( testWap( wap ) )
myWap = wap;
}
return myWap != null;
}
return false;
}
2014-03-25 18:13:34 +01:00
private boolean testWap(IWirelessAccessPoint wap)
{
double rangeLimit = wap.getRange();
rangeLimit *= rangeLimit;
2014-03-25 18:13:34 +01:00
DimensionalCoord dc = wap.getLocation();
if ( dc.getWorld() == myPlayer.worldObj )
{
2014-03-25 18:13:34 +01:00
double offX = (double) dc.x - myPlayer.posX;
double offY = (double) dc.y - myPlayer.posY;
double offZ = (double) dc.z - myPlayer.posZ;
double r = offX * offX + offY * offY + offZ * offZ;
if ( r < rangeLimit && sqRange > r )
{
2014-03-25 18:13:34 +01:00
if ( wap.isActive() )
{
sqRange = r;
myRange = Math.sqrt( r );
return true;
}
}
}
2014-02-03 05:33:50 +01:00
return false;
}
@Override
public IMEMonitor<IAEItemStack> getItemInventory()
{
if ( sg == null )
return null;
2014-02-03 05:33:50 +01:00
return sg.getItemInventory();
}
@Override
public IMEMonitor<IAEFluidStack> getFluidInventory()
{
if ( sg == null )
return null;
2014-02-03 05:33:50 +01:00
return sg.getFluidInventory();
}
@Override
public void addListener(IMEMonitorHandlerReceiver<IAEItemStack> l, Object verificationToken)
2014-02-03 05:33:50 +01:00
{
if ( itemStorage != null )
itemStorage.addListener( l, verificationToken );
2014-02-03 05:33:50 +01:00
}
@Override
public void removeListener(IMEMonitorHandlerReceiver<IAEItemStack> l)
2014-02-03 05:33:50 +01:00
{
if ( itemStorage != null )
itemStorage.removeListener( l );
2014-02-03 05:33:50 +01:00
}
@Override
public IItemList<IAEItemStack> getAvailableItems(IItemList out)
{
if ( itemStorage != null )
return itemStorage.getAvailableItems( out );
return out;
2014-02-03 05:33:50 +01:00
}
@Override
public IItemList<IAEItemStack> getStorageList()
{
if ( itemStorage != null )
return itemStorage.getStorageList();
return null;
2014-02-03 05:33:50 +01:00
}
@Override
public AccessRestriction getAccess()
{
if ( itemStorage != null )
return itemStorage.getAccess();
return AccessRestriction.NO_ACCESS;
2014-02-03 05:33:50 +01:00
}
@Override
public boolean isPrioritized(IAEItemStack input)
{
if ( itemStorage != null )
return itemStorage.isPrioritized( input );
return false;
2014-02-03 05:33:50 +01:00
}
@Override
public boolean canAccept(IAEItemStack input)
{
if ( itemStorage != null )
return itemStorage.canAccept( input );
return false;
2014-02-03 05:33:50 +01:00
}
@Override
public int getPriority()
{
if ( itemStorage != null )
return itemStorage.getPriority();
return 0;
2014-02-03 05:33:50 +01:00
}
@Override
public int getSlot()
{
if ( itemStorage != null )
return itemStorage.getSlot();
return 0;
2014-02-03 05:33:50 +01:00
}
@Override
public IAEItemStack injectItems(IAEItemStack input, Actionable type, BaseActionSource src)
{
if ( itemStorage != null )
return itemStorage.injectItems( input, type, src );
return input;
2014-02-03 05:33:50 +01:00
}
@Override
public IAEItemStack extractItems(IAEItemStack request, Actionable mode, BaseActionSource src)
{
if ( itemStorage != null )
return itemStorage.extractItems( request, mode, src );
return null;
2014-02-03 05:33:50 +01:00
}
@Override
public StorageChannel getChannel()
{
if ( itemStorage != null )
return itemStorage.getChannel();
return StorageChannel.ITEMS;
2014-02-03 05:33:50 +01:00
}
@Override
public double extractAEPower(double amt, Actionable mode, PowerMultiplier usePowerMultiplier)
{
if ( wth != null && effectiveItem != null )
{
if ( mode == Actionable.SIMULATE )
return wth.hasPower( myPlayer, amt, getItemStack() ) ? amt : 0;
return wth.usePower( myPlayer, amt, getItemStack() ) ? amt : 0;
}
return 0.0;
2014-02-03 05:33:50 +01:00
}
@Override
public ItemStack getItemStack()
{
return effectiveItem;
}
2014-02-17 01:50:25 +01:00
@Override
public IConfigManager getConfigManager()
{
return wth.getConfigManager( effectiveItem );
}
@Override
public IGridNode getGridNode(ForgeDirection dir)
{
return this.getActionableNode();
}
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
return AECableType.NONE;
}
@Override
public void securityBreak()
{
}
@Override
public IGridNode getActionableNode()
{
rangeCheck();
if ( myWap != null )
return myWap.getActionableNode();
return null;
}
@Override
public boolean validForPass(int i)
{
return itemStorage.validForPass( i );
}
2014-02-03 05:33:50 +01:00
}