2013-12-27 23:59:59 +01:00
|
|
|
package appeng.parts.automation;
|
|
|
|
|
|
|
|
import net.minecraft.client.renderer.RenderBlocks;
|
2013-12-30 06:55:02 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2013-12-30 06:55:02 +01:00
|
|
|
import net.minecraft.util.Vec3;
|
2013-12-28 22:04:51 +01:00
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.config.Actionable;
|
2013-12-30 06:55:02 +01:00
|
|
|
import appeng.api.config.FuzzyMode;
|
2013-12-28 22:04:51 +01:00
|
|
|
import appeng.api.config.PowerMultiplier;
|
2013-12-30 06:55:02 +01:00
|
|
|
import appeng.api.config.RedstoneMode;
|
|
|
|
import appeng.api.config.Settings;
|
2014-01-02 06:51:41 +01:00
|
|
|
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.IGridTickable;
|
|
|
|
import appeng.api.networking.ticking.TickRateModulation;
|
|
|
|
import appeng.api.networking.ticking.TickingRequest;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.parts.IPartCollsionHelper;
|
|
|
|
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;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.client.texture.CableBusTextures;
|
2014-03-17 20:59:50 +01:00
|
|
|
import appeng.core.settings.TickRates;
|
2013-12-30 06:55:02 +01:00
|
|
|
import appeng.core.sync.GuiBridge;
|
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;
|
2014-02-07 06:51:19 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
public class PartImportBus extends PartSharedItemBus implements IGridTickable, IInventoryDestination
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
BaseActionSource mySrc;
|
|
|
|
IMEInventory<IAEItemStack> destination = null;
|
|
|
|
IAEItemStack lastItemChecked = null;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public PartImportBus(ItemStack is) {
|
|
|
|
super( PartImportBus.class, is );
|
2014-01-05 09:43:49 +01:00
|
|
|
settings.registerSetting( Settings.REDSTONE_CONTROLLED, RedstoneMode.IGNORE );
|
2013-12-30 06:55:02 +01:00
|
|
|
settings.registerSetting( Settings.FUZZY_MODE, FuzzyMode.IGNORE_ALL );
|
2014-01-05 09:43:49 +01:00
|
|
|
mySrc = new MachineSource( this );
|
2013-12-30 06:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-20 06:18:49 +01:00
|
|
|
public boolean onPartActivate(EntityPlayer player, Vec3 pos)
|
2013-12-30 06:55:02 +01:00
|
|
|
{
|
|
|
|
if ( !player.isSneaking() )
|
|
|
|
{
|
|
|
|
if ( Platform.isClient() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Platform.openGUI( player, getHost().getTile(), side, GuiBridge.GUI_BUS );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2013-12-28 22:04:51 +01:00
|
|
|
@Override
|
|
|
|
public boolean canInsert(ItemStack stack)
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
IAEItemStack out = destination.injectItems( lastItemChecked = AEApi.instance().storage().createItemStack( stack ), Actionable.SIMULATE, mySrc );
|
2013-12-28 22:04:51 +01:00
|
|
|
if ( out == null )
|
|
|
|
return true;
|
|
|
|
return out.getStackSize() != stack.stackSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IInventoryDestination configDest(IMEMonitor<IAEItemStack> itemInventory)
|
|
|
|
{
|
|
|
|
destination = itemInventory;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
2014-02-07 06:51:19 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2013-12-27 23:59:59 +01:00
|
|
|
public void renderInventory(IPartRenderHelper rh, RenderBlocks renderer)
|
|
|
|
{
|
2014-04-04 06:31:10 +02:00
|
|
|
rh.setTexture( CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(),
|
|
|
|
is.getIconIndex(), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon() );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
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
|
2014-02-07 06:51:19 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2013-12-27 23:59:59 +01:00
|
|
|
public void renderStatic(int x, int y, int z, IPartRenderHelper rh, RenderBlocks renderer)
|
|
|
|
{
|
2014-02-15 07:30:06 +01:00
|
|
|
renderCache = rh.useSimpliedRendering( x, y, z, this, renderCache );
|
2014-04-04 06:31:10 +02:00
|
|
|
rh.setTexture( CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(),
|
|
|
|
is.getIconIndex(), CableBusTextures.PartImportSides.getIcon(), CableBusTextures.PartImportSides.getIcon() );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
rh.setBounds( 4, 4, 14, 12, 12, 16 );
|
|
|
|
rh.renderBlock( x, y, z, renderer );
|
|
|
|
|
|
|
|
rh.setBounds( 5, 5, 13, 11, 11, 14 );
|
|
|
|
rh.renderBlock( x, y, z, renderer );
|
|
|
|
|
|
|
|
rh.setBounds( 6, 6, 12, 10, 10, 13 );
|
|
|
|
rh.renderBlock( x, y, z, renderer );
|
|
|
|
rh.setTexture( CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorSidesStatus.getIcon(),
|
|
|
|
CableBusTextures.PartMonitorBack.getIcon(), is.getIconIndex(), CableBusTextures.PartMonitorSidesStatus.getIcon(),
|
|
|
|
CableBusTextures.PartMonitorSidesStatus.getIcon() );
|
|
|
|
|
|
|
|
rh.setBounds( 6, 6, 11, 10, 10, 12 );
|
|
|
|
rh.renderBlock( x, y, z, renderer );
|
|
|
|
|
|
|
|
renderLights( x, y, z, rh, renderer );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getBoxes(IPartCollsionHelper bch)
|
|
|
|
{
|
|
|
|
bch.addBox( 6, 6, 11, 10, 10, 12 );
|
|
|
|
bch.addBox( 6, 6, 12, 10, 10, 13 );
|
|
|
|
bch.addBox( 5, 5, 13, 11, 11, 14 );
|
|
|
|
bch.addBox( 4, 4, 14, 12, 12, 16 );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int cableConnectionRenderTo()
|
|
|
|
{
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2013-12-28 22:04:51 +01:00
|
|
|
@Override
|
|
|
|
public TickingRequest getTickingRequest(IGridNode node)
|
|
|
|
{
|
2014-03-17 20:59:50 +01:00
|
|
|
return new TickingRequest( TickRates.ImportBus.min, TickRates.ImportBus.max, getHandler() == null, false );
|
2013-12-28 22:04:51 +01:00
|
|
|
}
|
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
private int itemToSend; // used in tickingRequest
|
|
|
|
private boolean worked; // used in tickingRequest
|
|
|
|
|
|
|
|
TickRateModulation doBusWork()
|
2013-12-28 22:04:51 +01:00
|
|
|
{
|
2014-02-03 03:30:52 +01:00
|
|
|
if ( !proxy.isActive() )
|
|
|
|
return TickRateModulation.IDLE;
|
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
worked = false;
|
2013-12-28 22:04:51 +01:00
|
|
|
|
|
|
|
InventoryAdaptor myAdaptor = getHandler();
|
2014-01-06 07:56:16 +01:00
|
|
|
FuzzyMode fzMode = (FuzzyMode) getConfigManager().getSetting( Settings.FUZZY_MODE );
|
|
|
|
|
2013-12-28 22:04:51 +01:00
|
|
|
if ( myAdaptor != null )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-01-02 06:51:41 +01:00
|
|
|
switch (getInstalledUpgrades( Upgrades.SPEED ))
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
itemToSend = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
itemToSend = 8;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
itemToSend = 32;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
itemToSend = 64;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
itemToSend = 96;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemToSend = Math.min( itemToSend, (int) (0.01 + proxy.getEnergy().extractAEPower( itemToSend, Actionable.SIMULATE, PowerMultiplier.CONFIG )) );
|
|
|
|
IMEMonitor<IAEItemStack> inv = proxy.getStorage().getItemInventory();
|
2014-01-05 09:43:49 +01:00
|
|
|
IEnergyGrid energy = proxy.getEnergy();
|
2014-01-02 06:51:41 +01:00
|
|
|
|
|
|
|
boolean Configured = false;
|
|
|
|
for (int x = 0; x < availableSlots(); x++)
|
|
|
|
{
|
|
|
|
IAEItemStack ais = config.getAEStackInSlot( x );
|
|
|
|
if ( ais != null && itemToSend > 0 )
|
|
|
|
{
|
|
|
|
Configured = true;
|
|
|
|
while (itemToSend > 0)
|
|
|
|
{
|
2014-01-06 07:56:16 +01:00
|
|
|
if ( importStuff( myAdaptor, ais, inv, energy, fzMode ) )
|
2014-01-02 06:51:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-28 22:04:51 +01:00
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
if ( !Configured )
|
2013-12-28 22:04:51 +01:00
|
|
|
{
|
2014-01-02 06:51:41 +01:00
|
|
|
while (itemToSend > 0)
|
|
|
|
{
|
2014-01-06 07:56:16 +01:00
|
|
|
if ( importStuff( myAdaptor, null, inv, energy, fzMode ) )
|
2014-01-02 06:51:41 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-12-28 22:04:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return TickRateModulation.SLEEP;
|
|
|
|
|
|
|
|
return worked ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
|
|
|
|
}
|
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
@Override
|
|
|
|
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
|
|
|
|
{
|
|
|
|
return doBusWork();
|
|
|
|
}
|
|
|
|
|
2014-01-06 07:56:16 +01:00
|
|
|
private boolean importStuff(InventoryAdaptor myAdaptor, IAEItemStack whatToImport, IMEMonitor<IAEItemStack> inv, IEnergySource energy, FuzzyMode fzMode)
|
2014-01-02 06:51:41 +01:00
|
|
|
{
|
|
|
|
if ( itemToSend > 64 )
|
|
|
|
itemToSend = 64;
|
|
|
|
|
2014-01-06 07:56:16 +01:00
|
|
|
ItemStack newItems;
|
|
|
|
if ( getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
|
|
|
|
newItems = myAdaptor.removeSimilarItems( itemToSend, whatToImport == null ? null : whatToImport.getItemStack(), fzMode, configDest( inv ) );
|
|
|
|
else
|
|
|
|
newItems = myAdaptor.removeItems( itemToSend, whatToImport == null ? null : whatToImport.getItemStack(), configDest( inv ) );
|
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
if ( newItems != null )
|
|
|
|
{
|
2014-01-05 09:43:49 +01:00
|
|
|
newItems.stackSize = (int) (Math.min( newItems.stackSize, energy.extractAEPower( newItems.stackSize, Actionable.SIMULATE, PowerMultiplier.CONFIG ) ) + 0.01);
|
2014-01-02 06:51:41 +01:00
|
|
|
itemToSend -= newItems.stackSize;
|
|
|
|
|
|
|
|
if ( lastItemChecked == null || !lastItemChecked.isSameType( newItems ) )
|
|
|
|
lastItemChecked = AEApi.instance().storage().createItemStack( newItems );
|
|
|
|
else
|
|
|
|
lastItemChecked.setStackSize( newItems.stackSize );
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
IAEItemStack failed = Platform.poweredInsert( energy, destination, lastItemChecked, mySrc );
|
|
|
|
// destination.injectItems( lastItemChecked, Actionable.MODULATE );
|
2014-01-02 06:51:41 +01:00
|
|
|
if ( failed != null )
|
|
|
|
{
|
|
|
|
myAdaptor.addItems( failed.getItemStack() );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
worked = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RedstoneMode getRSMode()
|
|
|
|
{
|
2014-01-05 09:43:49 +01:00
|
|
|
return (RedstoneMode) settings.getSetting( Settings.REDSTONE_CONTROLLED );
|
2014-01-02 06:51:41 +01:00
|
|
|
}
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
@Override
|
|
|
|
protected boolean isSleeping()
|
|
|
|
{
|
2014-01-02 06:51:41 +01:00
|
|
|
return getHandler() == null || super.isSleeping();
|
2013-12-30 06:55:02 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|