Applied-Energistics-2-tiler.../parts/p2p/PartP2PItems.java

363 lines
8.2 KiB
Java
Raw Normal View History

2014-01-20 17:41:37 +01:00
package appeng.parts.p2p;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
2014-02-09 02:34:52 +01:00
import net.minecraft.init.Blocks;
2014-01-20 17:41:37 +01:00
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
2014-02-09 02:34:52 +01:00
import net.minecraft.util.IIcon;
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.config.TunnelType;
2014-06-09 01:54:38 +02:00
import appeng.api.networking.IGridNode;
2014-01-31 04:06:54 +01:00
import appeng.api.networking.events.MENetworkBootingStatusChange;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
2014-06-09 01:54:38 +02:00
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
2014-01-20 17:41:37 +01:00
import appeng.core.AppEng;
2014-06-09 01:54:38 +02:00
import appeng.core.settings.TickRates;
2014-07-24 00:26:23 +02:00
import appeng.integration.IntegrationType;
2014-01-20 17:41:37 +01:00
import appeng.integration.abstraction.IBC;
import appeng.me.GridAccessException;
import appeng.me.cache.helpers.TunnelCollection;
import appeng.tile.inventory.AppEngNullInventory;
import appeng.transformer.annotations.integration.Interface;
import appeng.transformer.annotations.integration.Method;
2014-01-20 17:41:37 +01:00
import appeng.util.Platform;
import appeng.util.inv.WrapperBCPipe;
import appeng.util.inv.WrapperChainedInventory;
import appeng.util.inv.WrapperMCISidedInventory;
import buildcraft.api.transport.IPipeConnection;
import buildcraft.api.transport.IPipeTile.PipeType;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@Interface(iface = "buildcraft.api.transport.IPipeConnection", iname = "BC")
2014-06-09 01:54:38 +02:00
public class PartP2PItems extends PartP2PTunnel<PartP2PItems> implements IPipeConnection, IInventory, ISidedInventory, IGridTickable
2014-01-20 17:41:37 +01:00
{
public TunnelType getTunnelType()
{
return TunnelType.ITEM;
}
2014-01-20 17:41:37 +01:00
public PartP2PItems(ItemStack is) {
super( is );
}
int oldSize = 0;
2014-06-09 01:54:38 +02:00
boolean requsted;
2014-01-20 17:41:37 +01:00
IInventory cachedInv;
LinkedList<IInventory> which = new LinkedList<IInventory>();
IInventory getOutputInv()
{
IInventory output = null;
if ( proxy.isActive() )
{
2014-02-09 02:34:52 +01:00
TileEntity te = tile.getWorldObj().getTileEntity( tile.xCoord + side.offsetX, tile.yCoord + side.offsetY, tile.zCoord + side.offsetZ );
2014-01-20 17:41:37 +01:00
if ( which.contains( this ) )
return null;
which.add( this );
2014-07-24 00:26:23 +02:00
if ( AppEng.instance.isIntegrationEnabled( IntegrationType.BC ) )
2014-01-20 17:41:37 +01:00
{
2014-07-24 00:26:23 +02:00
IBC buildcraft = (IBC) AppEng.instance.getIntegration( IntegrationType.BC );
2014-01-20 17:41:37 +01:00
if ( buildcraft != null )
{
if ( buildcraft.isPipe( te, side.getOpposite() ) )
{
try
{
output = new WrapperBCPipe( te, side.getOpposite() );
}
catch (Throwable _)
{
}
}
}
}
2014-02-02 03:52:37 +01:00
/*
* if ( AppEng.instance.isIntegrationEnabled( "TE" ) ) { ITE thermal = (ITE) AppEng.instance.getIntegration(
* "TE" ); if ( thermal != null ) { if ( thermal.isPipe( te, side.getOpposite() ) ) { try { output = new
* WrapperTEPipe( te, side.getOpposite() ); } catch (Throwable _) { } } } }
*/
2014-01-20 17:41:37 +01:00
if ( output == null )
{
if ( te instanceof TileEntityChest )
{
output = Platform.GetChestInv( te );
}
else if ( te instanceof ISidedInventory )
{
output = new WrapperMCISidedInventory( (ISidedInventory) te, side.getOpposite() );
}
else if ( te instanceof IInventory )
{
output = (IInventory) te;
}
}
which.pop();
}
return output;
}
@Override
public void onNeighborChanged()
{
cachedInv = null;
PartP2PItems input = getInput();
2014-01-31 18:39:09 +01:00
if ( input != null && output )
input.onTunnelNetworkChange();
2014-01-20 17:41:37 +01:00
}
2014-06-09 01:54:38 +02:00
@Override
public TickingRequest getTickingRequest(IGridNode node)
{
return new TickingRequest( TickRates.ItemTunnel.min, TickRates.ItemTunnel.max, false, false );
}
@Override
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
{
boolean wasReq = requsted;
if ( requsted && cachedInv != null )
((WrapperChainedInventory) cachedInv).cycleOrder();
requsted = false;
return wasReq ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
}
2014-01-20 17:41:37 +01:00
IInventory getDest()
{
2014-06-09 01:54:38 +02:00
requsted = true;
2014-01-20 17:41:37 +01:00
if ( cachedInv != null )
return cachedInv;
List<IInventory> outs = new LinkedList<IInventory>();
TunnelCollection<PartP2PItems> itemTunnels;
try
{
itemTunnels = getOutputs();
}
catch (GridAccessException e)
{
return new AppEngNullInventory();
}
for (PartP2PItems t : itemTunnels)
{
IInventory inv = t.getOutputInv();
if ( inv != null )
{
if ( Platform.getRandomInt() % 2 == 0 )
outs.add( inv );
else
outs.add( 0, inv );
}
}
return cachedInv = new WrapperChainedInventory( outs );
}
2014-01-31 04:06:54 +01:00
@MENetworkEventSubscribe
public void changeStateA(MENetworkBootingStatusChange bs)
{
if ( !output )
{
cachedInv = null;
int olderSize = oldSize;
oldSize = getDest().getSizeInventory();
if ( olderSize != oldSize )
{
getHost().notifyNeighbors();
2014-01-31 04:06:54 +01:00
}
}
}
@MENetworkEventSubscribe
public void changeStateB(MENetworkChannelsChanged bs)
{
if ( !output )
{
cachedInv = null;
int olderSize = oldSize;
oldSize = getDest().getSizeInventory();
if ( olderSize != oldSize )
{
getHost().notifyNeighbors();
2014-01-31 04:06:54 +01:00
}
}
}
@MENetworkEventSubscribe
public void changeStateC(MENetworkPowerStatusChange bs)
{
if ( !output )
{
cachedInv = null;
int olderSize = oldSize;
oldSize = getDest().getSizeInventory();
if ( olderSize != oldSize )
{
2014-06-09 01:54:38 +02:00
getHost().notifyNeighbors();
2014-01-31 04:06:54 +01:00
}
}
}
2014-01-20 17:41:37 +01:00
@Override
public void onTunnelNetworkChange()
2014-01-20 17:41:37 +01:00
{
2014-01-31 04:06:54 +01:00
if ( !output )
{
cachedInv = null;
int olderSize = oldSize;
oldSize = getDest().getSizeInventory();
if ( olderSize != oldSize )
{
2014-06-09 01:54:38 +02:00
getHost().notifyNeighbors();
2014-01-31 04:06:54 +01:00
}
}
else
2014-01-20 17:41:37 +01:00
{
2014-01-31 04:06:54 +01:00
PartP2PItems input = getInput();
if ( input != null )
input.getHost().notifyNeighbors();
2014-01-20 17:41:37 +01:00
}
}
@SideOnly(Side.CLIENT)
2014-02-09 02:34:52 +01:00
public IIcon getTypeTexture()
2014-01-20 17:41:37 +01:00
{
2014-02-09 02:34:52 +01:00
return Blocks.hopper.getBlockTextureFromSide( 0 );
2014-01-20 17:41:37 +01:00
}
@Override
public int getSizeInventory()
{
return getDest().getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int i)
{
return getDest().getStackInSlot( i );
}
@Override
public ItemStack decrStackSize(int i, int j)
{
return getDest().decrStackSize( i, j );
}
@Override
public ItemStack getStackInSlotOnClosing(int i)
{
return null;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
getDest().setInventorySlotContents( i, itemstack );
}
@Override
2014-02-09 02:34:52 +01:00
public String getInventoryName()
2014-01-20 17:41:37 +01:00
{
return null;
}
@Override
2014-02-09 02:34:52 +01:00
public boolean hasCustomInventoryName()
2014-01-20 17:41:37 +01:00
{
return false;
}
@Override
public int getInventoryStackLimit()
{
return getDest().getInventoryStackLimit();
}
@Override
2014-02-09 02:34:52 +01:00
public void openInventory()
2014-01-20 17:41:37 +01:00
{
}
@Override
2014-02-09 02:34:52 +01:00
public void closeInventory()
2014-01-20 17:41:37 +01:00
{
}
@Override
public boolean isItemValidForSlot(int i, net.minecraft.item.ItemStack itemstack)
{
return getDest().isItemValidForSlot( i, itemstack );
}
@Override
public int[] getAccessibleSlotsFromSide(int var1)
{
int[] slots = new int[getSizeInventory()];
for (int x = 0; x < getSizeInventory(); x++)
slots[x] = x;
return slots;
}
public float getPowerDrainPerTick()
{
return 2.0f;
};
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j)
{
return getDest().isItemValidForSlot( i, itemstack );
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return false;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return false;
}
@Override
@Method(iname = "BC")
2014-01-20 17:41:37 +01:00
public ConnectOverride overridePipeConnection(PipeType type, ForgeDirection with)
{
return side.equals( with ) && type == PipeType.ITEM ? ConnectOverride.CONNECT : ConnectOverride.DEFAULT;
}
@Override
2014-02-09 02:34:52 +01:00
public void markDirty()
2014-01-20 17:41:37 +01:00
{
// eh?
}
}