Applied-Energistics-2-tiler.../src/main/java/appeng/parts/automation/PartImportBus.java

339 lines
10 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.automation;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.Vec3;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2013-12-28 22:04:51 +01:00
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
2013-12-28 22:04:51 +01:00
import appeng.api.config.PowerMultiplier;
import appeng.api.config.RedstoneMode;
import appeng.api.config.Settings;
import appeng.api.config.Upgrades;
2013-12-28 22:04:51 +01:00
import appeng.api.networking.IGridNode;
2014-01-05 09:43:49 +01:00
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.energy.IEnergySource;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.MachineSource;
2013-12-28 22:04:51 +01:00
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
2014-09-20 22:20:53 +02:00
import appeng.api.parts.IPartCollisionHelper;
import appeng.api.parts.IPartRenderHelper;
2013-12-28 22:04:51 +01:00
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEItemStack;
2015-06-22 18:16:25 +02:00
import appeng.client.render.ModelGenerator;
import appeng.client.texture.CableBusTextures;
2014-03-17 20:59:50 +01:00
import appeng.core.settings.TickRates;
import appeng.core.sync.GuiBridge;
import appeng.helpers.Reflected;
2013-12-28 22:04:51 +01:00
import appeng.me.GridAccessException;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.inv.IInventoryDestination;
import appeng.util.item.AEItemStack;
2014-10-04 08:08:28 +02:00
public class PartImportBus extends PartSharedItemBus implements IInventoryDestination
{
private final BaseActionSource source;
private IMEInventory<IAEItemStack> destination = null;
private IAEItemStack lastItemChecked = null;
private int itemToSend; // used in tickingRequest
private boolean worked; // used in tickingRequest
2014-01-05 09:43:49 +01:00
@Reflected
2015-09-30 14:24:40 +02:00
public PartImportBus( final ItemStack is )
{
super( is );
this.getConfigManager().registerSetting( Settings.REDSTONE_CONTROLLED, RedstoneMode.IGNORE );
this.getConfigManager().registerSetting( Settings.FUZZY_MODE, FuzzyMode.IGNORE_ALL );
this.source = new MachineSource( this );
}
2013-12-28 22:04:51 +01:00
@Override
2015-09-30 14:24:40 +02:00
public boolean canInsert( final ItemStack stack )
2013-12-28 22:04:51 +01:00
{
if( stack == null || stack.getItem() == null )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2015-02-03 12:04:13 +01:00
final IAEItemStack out = this.destination.injectItems( this.lastItemChecked = AEApi.instance().storage().createItemStack( stack ), Actionable.SIMULATE, this.source );
if( out == null )
2015-04-29 02:30:53 +02:00
{
2013-12-28 22:04:51 +01:00
return true;
2015-04-29 02:30:53 +02:00
}
2013-12-28 22:04:51 +01:00
return out.getStackSize() != stack.stackSize;
}
@Override
2015-09-30 14:24:40 +02:00
public void getBoxes( final IPartCollisionHelper bch )
{
bch.addBox( 6, 6, 11, 10, 10, 13 );
bch.addBox( 5, 5, 13, 11, 11, 14 );
bch.addBox( 4, 4, 14, 12, 12, 16 );
}
@Override
@SideOnly( Side.CLIENT )
2015-09-30 14:24:40 +02:00
public void renderInventory( final IPartRenderHelper rh, final ModelGenerator renderer )
{
2015-09-30 14:26:54 +02:00
rh.setTexture( CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(), renderer.getIcon( this.is ), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon() );
rh.setBounds( 3, 3, 15, 13, 13, 16 );
rh.renderInventoryBox( renderer );
rh.setBounds( 4, 4, 14, 12, 12, 15 );
rh.renderInventoryBox( renderer );
rh.setBounds( 5, 5, 13, 11, 11, 14 );
rh.renderInventoryBox( renderer );
}
@Override
@SideOnly( Side.CLIENT )
2015-09-30 14:24:40 +02:00
public void renderStatic( final BlockPos pos, final IPartRenderHelper rh, final ModelGenerator renderer )
{
2015-09-30 14:26:54 +02:00
rh.setTexture( CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(), renderer.getIcon( this.is ), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon() );
rh.setBounds( 4, 4, 14, 12, 12, 16 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
rh.setBounds( 5, 5, 13, 11, 11, 14 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
rh.setBounds( 6, 6, 12, 10, 10, 13 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
2015-09-30 14:26:54 +02:00
rh.setTexture( CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorBack.getIcon(), renderer.getIcon( this.is ), CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorSidesStatus.getIcon() );
rh.setBounds( 6, 6, 11, 10, 10, 12 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
2015-06-16 02:44:59 +02:00
this.renderLights( pos, rh, renderer );
}
@Override
public int cableConnectionRenderTo()
{
return 5;
}
2013-12-28 22:04:51 +01:00
@Override
2015-09-30 14:24:40 +02:00
public boolean onPartActivate( final EntityPlayer player, final Vec3 pos )
{
if( !player.isSneaking() )
{
if( Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
Platform.openGUI( player, this.getHost().getTile(), this.side, GuiBridge.GUI_BUS );
return true;
}
return false;
}
@Override
2015-09-30 14:24:40 +02:00
public TickingRequest getTickingRequest( final IGridNode node )
2013-12-28 22:04:51 +01:00
{
2014-12-29 15:13:47 +01:00
return new TickingRequest( TickRates.ImportBus.min, TickRates.ImportBus.max, this.getHandler() == null, false );
2013-12-28 22:04:51 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public TickRateModulation tickingRequest( final IGridNode node, final int ticksSinceLastCall )
{
return this.doBusWork();
}
@Override
protected TickRateModulation doBusWork()
2013-12-28 22:04:51 +01:00
{
if( !this.proxy.isActive() || !this.canDoBusWork() )
2015-04-29 02:30:53 +02:00
{
return TickRateModulation.IDLE;
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.worked = false;
2013-12-28 22:04:51 +01:00
final InventoryAdaptor myAdaptor = this.getHandler();
final FuzzyMode fzMode = (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE );
if( myAdaptor != null )
2013-12-28 22:04:51 +01:00
{
try
{
this.itemToSend = this.calculateItemsToSend();
this.itemToSend = Math.min( this.itemToSend, (int) ( 0.01 + this.proxy.getEnergy().extractAEPower( this.itemToSend, Actionable.SIMULATE, PowerMultiplier.CONFIG ) ) );
final IMEMonitor<IAEItemStack> inv = this.proxy.getStorage().getItemInventory();
final IEnergyGrid energy = this.proxy.getEnergy();
boolean Configured = false;
for( int x = 0; x < this.availableSlots(); x++ )
{
final IAEItemStack ais = this.config.getAEStackInSlot( x );
if( ais != null && this.itemToSend > 0 )
{
Configured = true;
while( this.itemToSend > 0 )
{
if( this.importStuff( myAdaptor, ais, inv, energy, fzMode ) )
2015-04-29 02:30:53 +02:00
{
break;
2015-04-29 02:30:53 +02:00
}
}
}
}
2013-12-28 22:04:51 +01:00
if( !Configured )
2013-12-28 22:04:51 +01:00
{
while( this.itemToSend > 0 )
{
if( this.importStuff( myAdaptor, null, inv, energy, fzMode ) )
2015-04-29 02:30:53 +02:00
{
break;
2015-04-29 02:30:53 +02:00
}
}
2013-12-28 22:04:51 +01:00
}
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2013-12-28 22:04:51 +01:00
{
// :3
}
}
else
2015-04-29 02:30:53 +02:00
{
2013-12-28 22:04:51 +01:00
return TickRateModulation.SLEEP;
2015-04-29 02:30:53 +02:00
}
2013-12-28 22:04:51 +01:00
2014-12-29 15:13:47 +01:00
return this.worked ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
2013-12-28 22:04:51 +01:00
}
2015-09-30 14:24:40 +02:00
private boolean importStuff( final InventoryAdaptor myAdaptor, final IAEItemStack whatToImport, final IMEMonitor<IAEItemStack> inv, final IEnergySource energy, final FuzzyMode fzMode )
{
final int toSend = this.calculateMaximumAmountToImport( myAdaptor, whatToImport, inv, fzMode );
final ItemStack newItems;
if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
newItems = myAdaptor.removeSimilarItems( toSend, whatToImport == null ? null : whatToImport.getItemStack(), fzMode, this.configDestination( inv ) );
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
newItems = myAdaptor.removeItems( toSend, whatToImport == null ? null : whatToImport.getItemStack(), this.configDestination( inv ) );
2015-04-29 02:30:53 +02:00
}
if( newItems != null )
{
newItems.stackSize = (int) ( Math.min( newItems.stackSize, energy.extractAEPower( newItems.stackSize, Actionable.SIMULATE, PowerMultiplier.CONFIG ) ) + 0.01 );
2014-12-29 15:13:47 +01:00
this.itemToSend -= newItems.stackSize;
if( this.lastItemChecked == null || !this.lastItemChecked.isSameType( newItems ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.lastItemChecked = AEApi.instance().storage().createItemStack( newItems );
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.lastItemChecked.setStackSize( newItems.stackSize );
2015-04-29 02:30:53 +02:00
}
final IAEItemStack failed = Platform.poweredInsert( energy, this.destination, this.lastItemChecked, this.source );
if( failed != null )
{
myAdaptor.addItems( failed.getItemStack() );
return true;
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.worked = true;
2015-04-29 02:30:53 +02:00
}
}
else
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
return false;
}
2015-09-30 14:24:40 +02:00
private int calculateMaximumAmountToImport( final InventoryAdaptor myAdaptor, final IAEItemStack whatToImport, final IMEMonitor<IAEItemStack> inv, final FuzzyMode fzMode )
{
final int toSend = Math.min( this.itemToSend, 64 );
final ItemStack itemStackToImport;
if( whatToImport == null )
{
itemStackToImport = null;
}
else
{
itemStackToImport = whatToImport.getItemStack();
}
2015-09-30 14:22:21 +02:00
final IAEItemStack itemAmountNotStorable;
final ItemStack simResult;
if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
{
simResult = myAdaptor.simulateSimilarRemove( toSend, itemStackToImport, fzMode, this.configDestination( inv ) );
itemAmountNotStorable = this.destination.injectItems( AEItemStack.create( simResult ), Actionable.SIMULATE, this.source );
}
else
{
simResult = myAdaptor.simulateRemove( toSend, itemStackToImport, this.configDestination( inv ) );
itemAmountNotStorable = this.destination.injectItems( AEItemStack.create( simResult ), Actionable.SIMULATE, this.source );
}
if( itemAmountNotStorable != null )
{
return (int) Math.min( simResult.stackSize - itemAmountNotStorable.getStackSize(), toSend );
}
return toSend;
}
2015-09-30 14:24:40 +02:00
private IInventoryDestination configDestination( final IMEMonitor<IAEItemStack> itemInventory )
{
this.destination = itemInventory;
return this;
}
@Override
protected boolean isSleeping()
{
return this.getHandler() == null || super.isSleeping();
}
@Override
public RedstoneMode getRSMode()
{
return (RedstoneMode) this.getConfigManager().getSetting( Settings.REDSTONE_CONTROLLED );
}
}