Applied-Energistics-2-tiler.../src/main/java/appeng/parts/reporting/PartConversionMonitor.java

194 lines
6.2 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.parts.reporting;
import java.util.Collections;
2014-01-20 17:41:37 +01:00
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
2014-01-20 17:41:37 +01:00
import net.minecraft.tileentity.TileEntity;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
2015-12-24 02:07:03 +01:00
2014-01-20 17:41:37 +01:00
import appeng.api.networking.energy.IEnergySource;
import appeng.api.networking.security.PlayerSource;
import appeng.api.parts.IPartModel;
2014-01-20 17:41:37 +01:00
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEItemStack;
import appeng.core.AppEng;
import appeng.helpers.Reflected;
import appeng.items.parts.PartModels;
2014-01-20 17:41:37 +01:00
import appeng.me.GridAccessException;
import appeng.parts.PartModel;
2014-01-20 17:41:37 +01:00
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
public class PartConversionMonitor extends AbstractPartMonitor
{
@PartModels
public static final ResourceLocation MODEL_OFF = new ResourceLocation( AppEng.MOD_ID, "part/conversion_monitor_off" );
@PartModels
public static final ResourceLocation MODEL_ON = new ResourceLocation( AppEng.MOD_ID, "part/conversion_monitor_on" );
@PartModels
public static final ResourceLocation MODEL_LOCKED_OFF = new ResourceLocation( AppEng.MOD_ID, "part/conversion_monitor_locked_off" );
@PartModels
public static final ResourceLocation MODEL_LOCKED_ON = new ResourceLocation( AppEng.MOD_ID, "part/conversion_monitor_locked_on" );
public static final IPartModel MODELS_OFF = new PartModel( MODEL_BASE, MODEL_OFF, MODEL_STATUS_OFF );
public static final IPartModel MODELS_ON = new PartModel( MODEL_BASE, MODEL_ON, MODEL_STATUS_ON );
public static final IPartModel MODELS_HAS_CHANNEL = new PartModel( MODEL_BASE, MODEL_ON, MODEL_STATUS_HAS_CHANNEL );
public static final IPartModel MODELS_LOCKED_OFF = new PartModel( MODEL_BASE, MODEL_LOCKED_OFF, MODEL_STATUS_OFF );
public static final IPartModel MODELS_LOCKED_ON = new PartModel( MODEL_BASE, MODEL_LOCKED_ON, MODEL_STATUS_ON );
public static final IPartModel MODELS_LOCKED_HAS_CHANNEL = new PartModel( MODEL_BASE, MODEL_LOCKED_ON, MODEL_STATUS_HAS_CHANNEL );
@Reflected
2015-09-30 14:24:40 +02:00
public PartConversionMonitor( final ItemStack is )
{
super( is );
}
2014-01-20 17:41:37 +01:00
@Override
public boolean onPartShiftActivate( final EntityPlayer player, final EnumHand hand, final Vec3d pos )
2014-01-20 17:41:37 +01:00
{
if( Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
2014-01-20 17:41:37 +01:00
return true;
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
if( !this.getProxy().isActive() )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
if( !Platform.hasPermissions( this.getLocation(), player ) )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
boolean ModeB = false;
ItemStack item = player.getHeldItem( hand );
if( item == null && this.getDisplayed() != null )
2014-01-20 17:41:37 +01:00
{
ModeB = true;
item = ( (IAEItemStack) this.getDisplayed() ).getItemStack();
2014-01-20 17:41:37 +01:00
}
if( item != null )
2014-01-20 17:41:37 +01:00
{
try
{
if( !this.getProxy().isActive() )
2015-04-29 02:30:53 +02:00
{
2014-01-20 17:41:37 +01:00
return false;
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
final IEnergySource energy = this.getProxy().getEnergy();
final IMEMonitor<IAEItemStack> cell = this.getProxy().getStorage().getItemInventory();
final IAEItemStack input = AEItemStack.create( item );
2014-01-20 17:41:37 +01:00
if( ModeB )
2014-01-20 17:41:37 +01:00
{
for( int x = 0; x < player.inventory.getSizeInventory(); x++ )
2014-01-20 17:41:37 +01:00
{
final ItemStack targetStack = player.inventory.getStackInSlot( x );
if( input.equals( targetStack ) )
2014-01-20 17:41:37 +01:00
{
final IAEItemStack insertItem = input.copy();
2014-01-20 17:41:37 +01:00
insertItem.setStackSize( targetStack.stackSize );
final IAEItemStack failedToInsert = Platform.poweredInsert( energy, cell, insertItem, new PlayerSource( player, this ) );
2014-01-20 17:41:37 +01:00
player.inventory.setInventorySlotContents( x, failedToInsert == null ? null : failedToInsert.getItemStack() );
}
}
}
else
{
final IAEItemStack failedToInsert = Platform.poweredInsert( energy, cell, input, new PlayerSource( player, this ) );
2014-01-20 17:41:37 +01:00
player.inventory.setInventorySlotContents( player.inventory.currentItem, failedToInsert == null ? null : failedToInsert.getItemStack() );
}
}
catch( final GridAccessException e )
2014-01-20 17:41:37 +01:00
{
// :P
}
}
return true;
}
@Override
2015-09-30 14:24:40 +02:00
protected void extractItem( final EntityPlayer player )
2014-01-20 17:41:37 +01:00
{
final IAEItemStack input = (IAEItemStack) this.getDisplayed();
if( input != null )
2014-01-20 17:41:37 +01:00
{
try
{
if( !this.getProxy().isActive() )
2015-04-29 02:30:53 +02:00
{
2014-01-20 17:41:37 +01:00
return;
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
final IEnergySource energy = this.getProxy().getEnergy();
final IMEMonitor<IAEItemStack> cell = this.getProxy().getStorage().getItemInventory();
2014-01-20 17:41:37 +01:00
final ItemStack is = input.getItemStack();
2014-01-20 17:41:37 +01:00
input.setStackSize( is.getMaxStackSize() );
final IAEItemStack retrieved = Platform.poweredExtraction( energy, cell, input, new PlayerSource( player, this ) );
if( retrieved != null )
2014-01-20 17:41:37 +01:00
{
ItemStack newItems = retrieved.getItemStack();
final InventoryAdaptor adaptor = InventoryAdaptor.getAdaptor( player, EnumFacing.UP );
2014-01-20 17:41:37 +01:00
newItems = adaptor.addItems( newItems );
if( newItems != null )
2014-01-20 17:41:37 +01:00
{
final TileEntity te = this.getTile();
final List<ItemStack> list = Collections.singletonList( newItems );
Platform.spawnDrops( player.worldObj, te.getPos().offset( this.getSide().getFacing() ), list );
2014-01-20 17:41:37 +01:00
}
if( player.openContainer != null )
2015-04-29 02:30:53 +02:00
{
2014-01-20 17:41:37 +01:00
player.openContainer.detectAndSendChanges();
2015-04-29 02:30:53 +02:00
}
2014-01-20 17:41:37 +01:00
}
}
catch( final GridAccessException e )
2014-01-20 17:41:37 +01:00
{
// :P
}
}
}
@Override
public IPartModel getStaticModels()
{
return selectModel( MODELS_OFF, MODELS_ON, MODELS_HAS_CHANNEL,
MODELS_LOCKED_OFF, MODELS_LOCKED_ON, MODELS_LOCKED_HAS_CHANNEL );
}
}