Applied-Energistics-2-tiler.../tile/misc/TileSecurity.java

318 lines
7.8 KiB
Java
Raw Normal View History

package appeng.tile.misc;
2014-02-09 02:34:52 +01:00
import io.netty.buffer.ByteBuf;
import java.io.IOException;
2014-02-01 06:37:27 +01:00
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
2014-02-01 06:37:27 +01:00
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2014-02-09 02:34:52 +01:00
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
2014-02-01 06:37:27 +01:00
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.AEApi;
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;
import appeng.api.events.LocatableEventAnnounce;
import appeng.api.events.LocatableEventAnnounce.LocatableEvent;
import appeng.api.features.ILocatable;
import appeng.api.features.IPlayerRegistry;
import appeng.api.implementations.items.IBiometricCard;
import appeng.api.networking.GridFlags;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.networking.events.MENetworkSecurityChange;
2014-05-13 06:15:46 +02:00
import appeng.api.networking.security.ISecurityProvider;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IMEMonitor;
2014-02-17 01:50:25 +01:00
import appeng.api.storage.ITerminalHost;
import appeng.api.storage.MEMonitorHandler;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
2014-02-17 01:50:25 +01:00
import appeng.api.util.IConfigManager;
2014-05-13 06:15:46 +02:00
import appeng.helpers.PlayerSecuirtyWrapper;
import appeng.me.GridAccessException;
import appeng.me.storage.SecurityInventory;
import appeng.tile.events.AETileEventHandler;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkTile;
2014-02-01 06:37:27 +01:00
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.InvOperation;
2014-02-17 01:50:25 +01:00
import appeng.util.ConfigManager;
import appeng.util.IConfigManagerHost;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
2014-05-13 06:15:46 +02:00
public class TileSecurity extends AENetworkTile implements ITerminalHost, IAEAppEngInventory, ILocatable, IConfigManagerHost, ISecurityProvider
{
private static int diffrence = 0;
2014-02-17 01:50:25 +01:00
private IConfigManager cm = new ConfigManager( this );
private SecurityInventory inventory = new SecurityInventory( this );
private MEMonitorHandler<IAEItemStack> securityMonitor = new MEMonitorHandler<IAEItemStack>( inventory );
private boolean isActive = false;
public long securityKey;
2014-02-01 06:37:27 +01:00
public AppEngInternalInventory configSlot = new AppEngInternalInventory( this, 1 );
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack)
{
}
@Override
public void getDrops(World w, int x, int y, int z, ArrayList<ItemStack> drops)
{
if ( !configSlot.isEmpty() )
drops.add( configSlot.getStackInSlot( 0 ) );
2014-02-01 06:37:27 +01:00
for (IAEItemStack ais : inventory.storedItems)
drops.add( ais.getItemStack() );
}
IMEInventoryHandler<IAEItemStack> getSecurityInventory()
{
return inventory;
}
@Override
public void onReady()
{
super.onReady();
if ( Platform.isServer() )
{
isActive = true;
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Register ) );
}
}
@Override
public void onChunkUnload()
{
super.onChunkUnload();
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Unregister ) );
isActive = false;
}
@Override
public void invalidate()
{
super.invalidate();
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Unregister ) );
isActive = false;
}
class SecurityHandler extends AETileEventHandler
{
public SecurityHandler() {
super( TileEventType.WORLD_NBT, TileEventType.NETWORK );
}
@Override
2014-02-09 02:34:52 +01:00
public boolean readFromStream(ByteBuf data) throws IOException
{
boolean wasActive = isActive;
isActive = data.readBoolean();
return wasActive != isActive;
}
@Override
2014-02-09 02:34:52 +01:00
public void writeToStream(ByteBuf data) throws IOException
{
data.writeBoolean( gridProxy.isActive() );
}
@Override
public void writeToNBT(NBTTagCompound data)
{
2014-02-17 01:50:25 +01:00
cm.writeToNBT( data );
;
data.setLong( "securityKey", securityKey );
2014-02-01 06:37:27 +01:00
configSlot.writeToNBT( data, "config" );
NBTTagCompound storedItems = new NBTTagCompound();
int offset = 0;
for (IAEItemStack ais : inventory.storedItems)
{
NBTTagCompound it = new NBTTagCompound();
ais.getItemStack().writeToNBT( it );
2014-02-09 02:34:52 +01:00
storedItems.setTag( "" + (offset++), it );
}
2014-02-09 02:34:52 +01:00
data.setTag( "storedItems", storedItems );
}
@Override
public void readFromNBT(NBTTagCompound data)
{
2014-02-17 01:50:25 +01:00
cm.readFromNBT( data );
securityKey = data.getLong( "securityKey" );
2014-02-01 06:37:27 +01:00
configSlot.readFromNBT( data, "config" );
NBTTagCompound storedItems = data.getCompoundTag( "storedItems" );
2014-02-09 02:34:52 +01:00
for (Object key : storedItems.func_150296_c())
{
2014-02-09 02:34:52 +01:00
NBTBase obj = storedItems.getTag( (String) key );
if ( obj instanceof NBTTagCompound )
{
inventory.storedItems.add( AEItemStack.create( ItemStack.loadItemStackFromNBT( (NBTTagCompound) obj ) ) );
}
}
}
};
public void inventoryChanged()
{
try
{
saveChanges();
gridProxy.getGrid().postEvent( new MENetworkSecurityChange() );
}
catch (GridAccessException e)
{
// :P
}
}
public void readPermissions(HashMap<Integer, EnumSet<SecurityPermissions>> playerPerms)
{
IPlayerRegistry pr = AEApi.instance().registries().players();
// read permissions
for (IAEItemStack ais : inventory.storedItems)
{
ItemStack is = ais.getItemStack();
Item i = is.getItem();
if ( i instanceof IBiometricCard )
{
IBiometricCard bc = (IBiometricCard) i;
2014-05-13 06:15:46 +02:00
bc.registerPermissions( new PlayerSecuirtyWrapper( playerPerms ), pr, is );
}
}
// make sure thea admin is Boss.
playerPerms.put( gridProxy.getNode().getPlayerID(), EnumSet.allOf( SecurityPermissions.class ) );
}
@MENetworkEventSubscribe
public void bootUpdate(MENetworkChannelsChanged changed)
{
markForUpdate();
}
@MENetworkEventSubscribe
public void powerUpdate(MENetworkPowerStatusChange changed)
{
markForUpdate();
}
public boolean isSecurityEnabled()
{
return isActive && gridProxy.isActive();
}
public void updateNodeCount(int nodes)
{
gridProxy.setIdlePowerUsage( 2.0 + ((double) nodes / 0.033) );
}
public TileSecurity() {
addNewHandler( new SecurityHandler() );
2014-01-31 22:12:15 +01:00
gridProxy.setFlags( GridFlags.REQUIRE_CHANNEL );
gridProxy.setIdlePowerUsage( 2.0 );
diffrence++;
securityKey = System.currentTimeMillis() * 10 + diffrence;
if ( diffrence > 10 )
diffrence = 0;
2014-02-17 01:50:25 +01:00
cm.registerSetting( Settings.SORT_BY, SortOrder.NAME );
cm.registerSetting( Settings.VIEW_MODE, ViewItems.ALL );
cm.registerSetting( Settings.SORT_DIRECTION, SortDir.ASCENDING );
}
public int getOwner()
{
return gridProxy.getNode().getPlayerID();
}
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
return AECableType.SMART;
}
@Override
public DimensionalCoord getLocation()
{
return new DimensionalCoord( this );
}
public boolean isActive()
{
return isActive;
}
@Override
public IMEMonitor<IAEItemStack> getItemInventory()
{
return securityMonitor;
}
@Override
public IMEMonitor<IAEFluidStack> getFluidInventory()
{
return null;
}
@Override
public long getLocatableSerial()
{
return securityKey;
}
public boolean isPowered()
{
return gridProxy.isActive();
}
2014-02-17 01:50:25 +01:00
@Override
public IConfigManager getConfigManager()
{
return cm;
}
@Override
public void updateSetting(IConfigManager manager, Enum settingName, Enum newValue)
{
}
2014-05-13 06:15:46 +02:00
@Override
public long getSecurityKey()
{
return securityKey;
}
}