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

192 lines
5.7 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;
2014-02-01 06:37:27 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.inventory.IInventory;
2014-02-01 06:37:27 +01:00
import net.minecraft.item.ItemStack;
2015-12-24 02:07:03 +01:00
import appeng.api.AEApi;
import appeng.api.config.SecurityPermissions;
2014-02-24 19:01:16 +01:00
import appeng.api.features.INetworkEncodable;
import appeng.api.features.IWirelessTermHandler;
2014-02-01 06:37:27 +01:00
import appeng.api.implementations.items.IBiometricCard;
2014-02-17 01:50:25 +01:00
import appeng.api.storage.ITerminalHost;
import appeng.container.guisync.GuiSync;
import appeng.container.slot.SlotOutput;
import appeng.container.slot.SlotRestrictedInput;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.InvOperation;
import appeng.tile.misc.TileSecurityStation;
public class ContainerSecurityStation extends ContainerMEMonitorable implements IAEAppEngInventory
{
private final SlotRestrictedInput configSlot;
private final AppEngInternalInventory wirelessEncoder = new AppEngInternalInventory( this, 2 );
private final SlotRestrictedInput wirelessIn;
private final SlotOutput wirelessOut;
private final TileSecurityStation securityBox;
@GuiSync( 0 )
public int permissionMode = 0;
2014-02-01 06:37:27 +01:00
public ContainerSecurityStation( final InventoryPlayer ip, final ITerminalHost monitorable )
{
2014-09-21 01:06:22 +02:00
super( ip, monitorable, false );
2014-02-01 06:37:27 +01:00
this.securityBox = (TileSecurityStation) monitorable;
this.addSlotToContainer( this.configSlot = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.BIOMETRIC_CARD, this.securityBox.getConfigSlot(), 0, 37, -33, ip ) );
2014-12-29 15:13:47 +01:00
this.addSlotToContainer( this.wirelessIn = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.ENCODABLE_ITEM, this.wirelessEncoder, 0, 212, 10, ip ) );
this.addSlotToContainer( this.wirelessOut = new SlotOutput( this.wirelessEncoder, 1, 212, 68, -1 ) );
2014-02-01 06:37:27 +01:00
2014-12-29 15:13:47 +01:00
this.bindPlayerInventory( ip, 0, 0 );
2014-02-01 06:37:27 +01:00
}
2015-09-30 14:24:40 +02:00
public void toggleSetting( final String value, final EntityPlayer player )
2014-02-01 06:37:27 +01:00
{
try
{
2015-09-30 14:24:40 +02:00
final SecurityPermissions permission = SecurityPermissions.valueOf( value );
2014-02-01 06:37:27 +01:00
2015-09-30 14:24:40 +02:00
final ItemStack a = this.configSlot.getStack();
if( a != null && a.getItem() instanceof IBiometricCard )
2014-02-01 06:37:27 +01:00
{
2015-09-30 14:24:40 +02:00
final IBiometricCard bc = (IBiometricCard) a.getItem();
if( bc.hasPermission( a, permission ) )
2015-04-29 02:30:53 +02:00
{
2014-02-01 06:37:27 +01:00
bc.removePermission( a, permission );
2015-04-29 02:30:53 +02:00
}
2014-02-01 06:37:27 +01:00
else
2015-04-29 02:30:53 +02:00
{
2014-02-01 06:37:27 +01:00
bc.addPermission( a, permission );
2015-04-29 02:30:53 +02:00
}
2014-02-01 06:37:27 +01:00
}
}
2015-09-30 14:24:40 +02:00
catch( final EnumConstantNotPresentException ex )
2014-02-01 06:37:27 +01:00
{
// :(
}
}
@Override
public void detectAndSendChanges()
{
2014-12-29 15:13:47 +01:00
this.verifyPermissions( SecurityPermissions.SECURITY, false );
this.setPermissionMode( 0 );
2014-02-01 06:37:27 +01:00
2015-09-30 14:24:40 +02:00
final ItemStack a = this.configSlot.getStack();
if( a != null && a.getItem() instanceof IBiometricCard )
2014-02-01 06:37:27 +01:00
{
2015-09-30 14:24:40 +02:00
final IBiometricCard bc = (IBiometricCard) a.getItem();
2014-02-01 06:37:27 +01:00
2015-09-30 14:24:40 +02:00
for( final SecurityPermissions sp : bc.getPermissions( a ) )
2015-04-29 02:30:53 +02:00
{
this.setPermissionMode( this.getPermissionMode() | ( 1 << sp.ordinal() ) );
2015-04-29 02:30:53 +02:00
}
2014-02-01 06:37:27 +01:00
}
2014-12-29 15:13:47 +01:00
this.updatePowerStatus();
super.detectAndSendChanges();
}
2014-02-01 06:37:27 +01:00
@Override
2015-09-30 14:24:40 +02:00
public void onContainerClosed( final EntityPlayer player )
{
super.onContainerClosed( player );
if( this.wirelessIn.getHasStack() )
2015-04-29 02:30:53 +02:00
{
player.dropItem( this.wirelessIn.getStack(), false );
2015-04-29 02:30:53 +02:00
}
if( this.wirelessOut.getHasStack() )
2015-04-29 02:30:53 +02:00
{
player.dropItem( this.wirelessOut.getStack(), false );
2015-04-29 02:30:53 +02:00
}
}
@Override
public void saveChanges()
{
// :P
}
@Override
2015-09-30 14:24:40 +02:00
public void onChangeInventory( final IInventory inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack )
{
if( !this.wirelessOut.getHasStack() )
{
if( this.wirelessIn.getHasStack() )
{
2015-09-30 14:24:40 +02:00
final ItemStack term = this.wirelessIn.getStack().copy();
INetworkEncodable networkEncodable = null;
if( term.getItem() instanceof INetworkEncodable )
2015-04-29 02:30:53 +02:00
{
networkEncodable = (INetworkEncodable) term.getItem();
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final IWirelessTermHandler wTermHandler = AEApi.instance().registries().wireless().getWirelessTerminalHandler( term );
if( wTermHandler != null )
2015-04-29 02:30:53 +02:00
{
networkEncodable = wTermHandler;
2015-04-29 02:30:53 +02:00
}
if( networkEncodable != null )
{
networkEncodable.setEncryptionKey( term, String.valueOf( this.securityBox.getSecurityKey() ), "" );
2014-12-29 15:13:47 +01:00
this.wirelessIn.putStack( null );
this.wirelessOut.putStack( term );
// update the two slots in question...
for( final Object crafter : this.listeners )
{
final IContainerListener IContainerListener = (IContainerListener) crafter;
IContainerListener.sendSlotContents( this, this.wirelessIn.slotNumber, this.wirelessIn.getStack() );
IContainerListener.sendSlotContents( this, this.wirelessOut.slotNumber, this.wirelessOut.getStack() );
}
}
}
}
}
public int getPermissionMode()
{
return this.permissionMode;
}
private void setPermissionMode( final int permissionMode )
{
this.permissionMode = permissionMode;
}
}