2014-01-27 05:00:36 +01:00
|
|
|
package appeng.tile.misc;
|
|
|
|
|
2014-09-21 01:44:25 +02:00
|
|
|
import appeng.helpers.PlayerSecurityWrapper;
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
import java.io.IOException;
|
2014-02-01 06:37:27 +01:00
|
|
|
import java.util.ArrayList;
|
2014-01-27 05:00:36 +01:00
|
|
|
import java.util.EnumSet;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2014-07-21 05:45:08 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2014-02-01 06:37:27 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
2014-01-31 07:33:31 +01:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraft.nbt.NBTBase;
|
2014-01-27 05:00:36 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-02-01 06:37:27 +01:00
|
|
|
import net.minecraft.world.World;
|
2014-02-02 08:32:10 +01:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.api.AEApi;
|
2014-01-27 05:00:36 +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-02-02 08:32:10 +01:00
|
|
|
import appeng.api.events.LocatableEventAnnounce;
|
|
|
|
import appeng.api.events.LocatableEventAnnounce.LocatableEvent;
|
|
|
|
import appeng.api.features.ILocatable;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.api.features.IPlayerRegistry;
|
|
|
|
import appeng.api.implementations.items.IBiometricCard;
|
2014-07-17 06:44:02 +02:00
|
|
|
import appeng.api.implementations.tiles.IColorableTile;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.events.MENetworkChannelsChanged;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.api.networking.events.MENetworkSecurityChange;
|
2014-05-13 06:15:46 +02:00
|
|
|
import appeng.api.networking.security.ISecurityProvider;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
|
|
|
import appeng.api.storage.IMEMonitor;
|
2014-02-17 01:50:25 +01:00
|
|
|
import appeng.api.storage.ITerminalHost;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.api.storage.MEMonitorHandler;
|
|
|
|
import appeng.api.storage.data.IAEFluidStack;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.util.AECableType;
|
2014-07-17 06:44:02 +02:00
|
|
|
import appeng.api.util.AEColor;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.util.DimensionalCoord;
|
2014-02-17 01:50:25 +01:00
|
|
|
import appeng.api.util.IConfigManager;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.me.GridAccessException;
|
|
|
|
import appeng.me.storage.SecurityInventory;
|
2014-08-28 09:39:52 +02:00
|
|
|
import appeng.tile.TileEvent;
|
2014-01-27 05:00:36 +01:00
|
|
|
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;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.util.Platform;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.util.item.AEItemStack;
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-07-17 06:44:02 +02:00
|
|
|
public class TileSecurity extends AENetworkTile implements ITerminalHost, IAEAppEngInventory, ILocatable, IConfigManagerHost, ISecurityProvider, IColorableTile
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
|
|
|
|
2014-09-21 01:34:38 +02:00
|
|
|
private static int difference = 0;
|
2014-02-17 01:50:25 +01:00
|
|
|
private IConfigManager cm = new ConfigManager( this );
|
2014-01-31 07:33:31 +01:00
|
|
|
|
|
|
|
private SecurityInventory inventory = new SecurityInventory( this );
|
|
|
|
private MEMonitorHandler<IAEItemStack> securityMonitor = new MEMonitorHandler<IAEItemStack>( inventory );
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
private boolean isActive = false;
|
|
|
|
|
2014-07-17 06:44:02 +02:00
|
|
|
AEColor paintedColor = AEColor.Transparent;
|
2014-01-27 05:00:36 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-02-02 08:32:10 +01:00
|
|
|
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() );
|
|
|
|
}
|
|
|
|
|
2014-01-31 07:33:31 +01:00
|
|
|
IMEInventoryHandler<IAEItemStack> getSecurityInventory()
|
|
|
|
{
|
|
|
|
return inventory;
|
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
@Override
|
|
|
|
public void onReady()
|
|
|
|
{
|
|
|
|
super.onReady();
|
|
|
|
if ( Platform.isServer() )
|
2014-02-02 08:32:10 +01:00
|
|
|
{
|
2014-01-27 05:00:36 +01:00
|
|
|
isActive = true;
|
2014-02-02 08:32:10 +01:00
|
|
|
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Register ) );
|
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChunkUnload()
|
|
|
|
{
|
|
|
|
super.onChunkUnload();
|
2014-02-02 08:32:10 +01:00
|
|
|
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Unregister ) );
|
2014-01-27 05:00:36 +01:00
|
|
|
isActive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void invalidate()
|
|
|
|
{
|
|
|
|
super.invalidate();
|
2014-02-02 08:32:10 +01:00
|
|
|
MinecraftForge.EVENT_BUS.post( new LocatableEventAnnounce( this, LocatableEvent.Unregister ) );
|
2014-01-27 05:00:36 +01:00
|
|
|
isActive = false;
|
|
|
|
}
|
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
@TileEvent(TileEventType.NETWORK_READ)
|
|
|
|
public boolean readFromStream_TileSecurity(ByteBuf data) throws IOException
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2014-08-28 09:39:52 +02:00
|
|
|
boolean wasActive = isActive;
|
|
|
|
isActive = data.readBoolean();
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
AEColor oldPaintedColor = paintedColor;
|
|
|
|
paintedColor = AEColor.values()[data.readByte()];
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
return oldPaintedColor != paintedColor || wasActive != isActive;
|
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
@TileEvent(TileEventType.NETWORK_WRITE)
|
|
|
|
public void writeToStream_TileSecurity(ByteBuf data) throws IOException
|
|
|
|
{
|
|
|
|
data.writeBoolean( gridProxy.isActive() );
|
|
|
|
data.writeByte( paintedColor.ordinal() );
|
|
|
|
}
|
2014-07-17 06:44:02 +02:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
@TileEvent(TileEventType.WORLD_NBT_WRITE)
|
|
|
|
public void writeToNBT_TileSecurity(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
cm.writeToNBT( data );
|
|
|
|
data.setByte( "paintedColor", (byte) paintedColor.ordinal() );
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
data.setLong( "securityKey", securityKey );
|
|
|
|
configSlot.writeToNBT( data, "config" );
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
NBTTagCompound storedItems = new NBTTagCompound();
|
2014-02-17 01:50:25 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
int offset = 0;
|
|
|
|
for (IAEItemStack ais : inventory.storedItems)
|
|
|
|
{
|
|
|
|
NBTTagCompound it = new NBTTagCompound();
|
|
|
|
ais.getItemStack().writeToNBT( it );
|
|
|
|
storedItems.setTag( "" + (offset++), it );
|
|
|
|
}
|
2014-02-01 06:37:27 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
data.setTag( "storedItems", storedItems );
|
|
|
|
}
|
2014-01-31 07:33:31 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
@TileEvent(TileEventType.WORLD_NBT_READ)
|
|
|
|
public void readFromNBT_TileSecurity(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
cm.readFromNBT( data );
|
|
|
|
if ( data.hasKey( "paintedColor" ) )
|
|
|
|
paintedColor = AEColor.values()[data.getByte( "paintedColor" )];
|
2014-01-31 07:33:31 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
securityKey = data.getLong( "securityKey" );
|
|
|
|
configSlot.readFromNBT( data, "config" );
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-08-28 09:39:52 +02:00
|
|
|
NBTTagCompound storedItems = data.getCompoundTag( "storedItems" );
|
|
|
|
for (Object key : storedItems.func_150296_c())
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2014-08-28 09:39:52 +02:00
|
|
|
NBTBase obj = storedItems.getTag( (String) key );
|
|
|
|
if ( obj instanceof NBTTagCompound )
|
2014-01-31 07:33:31 +01:00
|
|
|
{
|
2014-08-28 09:39:52 +02:00
|
|
|
inventory.storedItems.add( AEItemStack.create( ItemStack.loadItemStackFromNBT( (NBTTagCompound) obj ) ) );
|
2014-01-31 07:33:31 +01:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
2014-08-28 09:39:52 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-01-31 07:33:31 +01:00
|
|
|
public void inventoryChanged()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
saveChanges();
|
|
|
|
gridProxy.getGrid().postEvent( new MENetworkSecurityChange() );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
public void readPermissions(HashMap<Integer, EnumSet<SecurityPermissions>> playerPerms)
|
|
|
|
{
|
2014-01-31 07:33:31 +01:00
|
|
|
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-09-21 01:44:25 +02:00
|
|
|
bc.registerPermissions( new PlayerSecurityWrapper( playerPerms ), pr, is );
|
2014-01-31 07:33:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure thea admin is Boss.
|
2014-01-27 05:00:36 +01:00
|
|
|
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 TileSecurity() {
|
2014-01-31 22:12:15 +01:00
|
|
|
gridProxy.setFlags( GridFlags.REQUIRE_CHANNEL );
|
2014-01-27 05:00:36 +01:00
|
|
|
gridProxy.setIdlePowerUsage( 2.0 );
|
2014-09-21 01:34:38 +02:00
|
|
|
difference++;
|
2014-08-26 03:22:00 +02:00
|
|
|
|
2014-09-21 01:34:38 +02:00
|
|
|
securityKey = System.currentTimeMillis() * 10 + difference;
|
|
|
|
if ( difference > 10 )
|
|
|
|
difference = 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 );
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-31 07:33:31 +01:00
|
|
|
@Override
|
|
|
|
public IMEMonitor<IAEItemStack> getItemInventory()
|
|
|
|
{
|
|
|
|
return securityMonitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IMEMonitor<IAEFluidStack> getFluidInventory()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-02 08:32:10 +01:00
|
|
|
@Override
|
|
|
|
public long getLocatableSerial()
|
|
|
|
{
|
|
|
|
return securityKey;
|
|
|
|
}
|
|
|
|
|
2014-02-02 20:00:57 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-17 06:44:02 +02:00
|
|
|
public AEColor getColor()
|
|
|
|
{
|
|
|
|
return paintedColor;
|
|
|
|
}
|
|
|
|
|
2014-07-21 05:45:08 +02:00
|
|
|
@Override
|
|
|
|
public boolean recolourBlock(ForgeDirection side, AEColor newPaintedColor, EntityPlayer who)
|
2014-07-17 06:44:02 +02:00
|
|
|
{
|
2014-07-21 05:45:08 +02:00
|
|
|
if ( paintedColor == newPaintedColor )
|
|
|
|
return false;
|
|
|
|
|
2014-07-17 06:44:02 +02:00
|
|
|
paintedColor = newPaintedColor;
|
|
|
|
markDirty();
|
|
|
|
markForUpdate();
|
2014-07-21 05:45:08 +02:00
|
|
|
return true;
|
2014-07-17 06:44:02 +02:00
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|