Applied-Energistics-2-tiler.../src/main/java/appeng/parts/p2p/PartP2PItems.java

408 lines
9.1 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 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* 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>.
*/
2014-09-24 02:26:27 +02:00
package appeng.parts.p2p;
2014-09-24 02:26:27 +02:00
import java.util.LinkedList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
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;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
2014-09-24 02:26:27 +02:00
import appeng.api.networking.IGridNode;
import appeng.api.networking.events.MENetworkBootingStatusChange;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.core.settings.TickRates;
import appeng.items.parts.PartModels;
2014-09-24 02:26:27 +02:00
import appeng.me.GridAccessException;
import appeng.me.cache.helpers.TunnelCollection;
import appeng.tile.inventory.AppEngNullInventory;
import appeng.util.Platform;
import appeng.util.inv.WrapperChainedInventory;
import appeng.util.inv.WrapperMCISidedInventory;
// TODO: BC Integration
//@Interface( iface = "buildcraft.api.transport.IPipeConnection", iname = IntegrationType.BuildCraftTransport )
public class PartP2PItems extends PartP2PTunnel<PartP2PItems> implements /* IPipeConnection, */ISidedInventory, IGridTickable
2014-09-24 02:26:27 +02:00
{
private static final P2PModels MODELS = new P2PModels( "part/p2p/p2p_tunnel_items" );
@PartModels
public static List<ResourceLocation> getModels()
{
return MODELS.getModels();
}
private final LinkedList<IInventory> which = new LinkedList<IInventory>();
private int oldSize = 0;
private boolean requested;
private IInventory cachedInv;
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
public PartP2PItems( final ItemStack is )
{
super( is );
}
@Override
public void onNeighborChanged()
{
this.cachedInv = null;
2015-09-30 14:24:40 +02:00
final PartP2PItems input = this.getInput();
if( input != null && this.isOutput() )
2015-04-29 02:30:53 +02:00
{
input.onTunnelNetworkChange();
2015-04-29 02:30:53 +02:00
}
}
private IInventory getDestination()
{
this.requested = true;
if( this.cachedInv != null )
2015-04-29 02:30:53 +02:00
{
return this.cachedInv;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final List<IInventory> outs = new LinkedList<IInventory>();
final TunnelCollection<PartP2PItems> itemTunnels;
try
{
itemTunnels = this.getOutputs();
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
{
return new AppEngNullInventory();
}
2015-09-30 14:24:40 +02:00
for( final PartP2PItems t : itemTunnels )
{
2015-09-30 14:24:40 +02:00
final IInventory inv = t.getOutputInv();
if( inv != null )
{
if( Platform.getRandomInt() % 2 == 0 )
2015-04-29 02:30:53 +02:00
{
outs.add( inv );
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
outs.add( 0, inv );
2015-04-29 02:30:53 +02:00
}
}
}
return this.cachedInv = new WrapperChainedInventory( outs );
}
2014-09-24 02:26:27 +02:00
private IInventory getOutputInv()
2014-09-24 02:26:27 +02:00
{
IInventory output = null;
if( this.getProxy().isActive() )
2014-09-24 02:26:27 +02:00
{
final TileEntity te = this.getTile().getWorld().getTileEntity( this.getTile().getPos().offset( this.getSide().getFacing() ) );
2014-09-24 02:26:27 +02:00
if( this.which.contains( this ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.which.add( this );
2014-09-24 02:26:27 +02:00
if( output == null )
2014-09-24 02:26:27 +02:00
{
if( te instanceof TileEntityChest )
2014-09-24 02:26:27 +02:00
{
output = Platform.GetChestInv( te );
}
else if( te instanceof ISidedInventory )
2014-09-24 02:26:27 +02:00
{
output = new WrapperMCISidedInventory( (ISidedInventory) te, this.getSide().getFacing().getOpposite() );
2014-09-24 02:26:27 +02:00
}
else if( te instanceof IInventory )
2014-09-24 02:26:27 +02:00
{
output = (IInventory) te;
}
}
2014-12-29 15:13:47 +01:00
this.which.pop();
2014-09-24 02:26:27 +02:00
}
return output;
}
@Override
2015-09-30 14:24:40 +02:00
public TickingRequest getTickingRequest( final IGridNode node )
2014-09-24 02:26:27 +02:00
{
return new TickingRequest( TickRates.ItemTunnel.getMin(), TickRates.ItemTunnel.getMax(), false, false );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public TickRateModulation tickingRequest( final IGridNode node, final int ticksSinceLastCall )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final boolean wasReq = this.requested;
2014-09-24 02:26:27 +02:00
if( this.requested && this.cachedInv != null )
2015-04-29 02:30:53 +02:00
{
( (WrapperChainedInventory) this.cachedInv ).cycleOrder();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.requested = false;
2014-09-24 02:26:27 +02:00
return wasReq ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
}
@MENetworkEventSubscribe
2015-09-30 14:24:40 +02:00
public void changeStateA( final MENetworkBootingStatusChange bs )
2014-09-24 02:26:27 +02:00
{
if( !this.isOutput() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.cachedInv = null;
2015-09-30 14:24:40 +02:00
final int olderSize = this.oldSize;
2014-12-29 15:13:47 +01:00
this.oldSize = this.getDestination().getSizeInventory();
if( olderSize != this.oldSize )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getHost().notifyNeighbors();
2014-09-24 02:26:27 +02:00
}
}
}
@MENetworkEventSubscribe
2015-09-30 14:24:40 +02:00
public void changeStateB( final MENetworkChannelsChanged bs )
2014-09-24 02:26:27 +02:00
{
if( !this.isOutput() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.cachedInv = null;
2015-09-30 14:24:40 +02:00
final int olderSize = this.oldSize;
2014-12-29 15:13:47 +01:00
this.oldSize = this.getDestination().getSizeInventory();
if( olderSize != this.oldSize )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getHost().notifyNeighbors();
2014-09-24 02:26:27 +02:00
}
}
}
@MENetworkEventSubscribe
2015-09-30 14:24:40 +02:00
public void changeStateC( final MENetworkPowerStatusChange bs )
2014-09-24 02:26:27 +02:00
{
if( !this.isOutput() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.cachedInv = null;
2015-09-30 14:24:40 +02:00
final int olderSize = this.oldSize;
2014-12-29 15:13:47 +01:00
this.oldSize = this.getDestination().getSizeInventory();
if( olderSize != this.oldSize )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getHost().notifyNeighbors();
2014-09-24 02:26:27 +02:00
}
}
}
@Override
public void onTunnelNetworkChange()
{
if( !this.isOutput() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.cachedInv = null;
2015-09-30 14:24:40 +02:00
final int olderSize = this.oldSize;
2014-12-29 15:13:47 +01:00
this.oldSize = this.getDestination().getSizeInventory();
if( olderSize != this.oldSize )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getHost().notifyNeighbors();
2014-09-24 02:26:27 +02:00
}
}
else
{
2015-09-30 14:24:40 +02:00
final PartP2PItems input = this.getInput();
if( input != null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
input.getHost().notifyNeighbors();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
@Override
public int[] getSlotsForFace( final EnumFacing side )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final int[] slots = new int[this.getSizeInventory()];
for( int x = 0; x < this.getSizeInventory(); x++ )
2015-04-29 02:30:53 +02:00
{
slots[x] = x;
2015-04-29 02:30:53 +02:00
}
return slots;
2014-09-24 02:26:27 +02:00
}
@Override
public int getSizeInventory()
{
2014-12-29 15:13:47 +01:00
return this.getDestination().getSizeInventory();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack getStackInSlot( final int i )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.getDestination().getStackInSlot( i );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack decrStackSize( final int i, final int j )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.getDestination().decrStackSize( i, j );
2014-09-24 02:26:27 +02:00
}
@Override
public ItemStack removeStackFromSlot( final int i )
2014-09-24 02:26:27 +02:00
{
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public void setInventorySlotContents( final int i, final ItemStack itemstack )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getDestination().setInventorySlotContents( i, itemstack );
2014-09-24 02:26:27 +02:00
}
@Override
public String getName()
2014-09-24 02:26:27 +02:00
{
return null;
}
@Override
2015-06-16 02:44:59 +02:00
public boolean hasCustomName()
2014-09-24 02:26:27 +02:00
{
return false;
}
@Override
public int getInventoryStackLimit()
{
2014-12-29 15:13:47 +01:00
return this.getDestination().getInventoryStackLimit();
2014-09-24 02:26:27 +02:00
}
@Override
public void markDirty()
2014-09-24 02:26:27 +02:00
{
// eh?
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isUseableByPlayer( final EntityPlayer entityplayer )
2014-09-24 02:26:27 +02:00
{
return false;
2014-09-24 02:26:27 +02:00
}
@Override
public void openInventory( final EntityPlayer p )
2014-09-24 02:26:27 +02:00
{
}
@Override
public void closeInventory( final EntityPlayer p )
2014-09-24 02:26:27 +02:00
{
2014-09-28 00:50:06 +02:00
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public boolean isItemValidForSlot( final int i, final net.minecraft.item.ItemStack itemstack )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.getDestination().isItemValidForSlot( i, itemstack );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canInsertItem( final int i, final ItemStack itemstack, final EnumFacing j )
2014-09-24 02:26:27 +02:00
{
return this.getDestination().isItemValidForSlot( i, itemstack );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canExtractItem( final int i, final ItemStack itemstack, final EnumFacing j )
2014-09-24 02:26:27 +02:00
{
return false;
}
public float getPowerDrainPerTick()
2014-09-24 02:26:27 +02:00
{
return 2.0f;
2014-09-24 02:26:27 +02:00
}
2015-06-16 02:44:59 +02:00
@Override
2015-09-30 14:24:40 +02:00
public int getField( final int id )
2015-06-16 02:44:59 +02:00
{
return 0;
}
// @Override
// @Method( iname = IntegrationType.BuildCraftTransport )
// public ConnectOverride overridePipeConnection( PipeType type, ForgeDirection with )
// {
// return 0;
// }
2015-06-16 02:44:59 +02:00
@Override
public void setField( final int id, final int value )
2015-06-16 02:44:59 +02:00
{
2015-06-16 02:44:59 +02:00
}
@Override
public int getFieldCount()
{
return 0;
}
@Override
public void clear()
{
// probobly not...
}
@Override
public ITextComponent getDisplayName()
2015-06-16 02:44:59 +02:00
{
return null;
}
// TODO: BC Integration
// @Override
// @Method( iname = "BuildCraftTransport" )
// public ConnectOverride overridePipeConnection( PipeType type, ForgeDirection with )
// {
// return this.side == with && type == PipeType.ITEM ? ConnectOverride.CONNECT : ConnectOverride.DEFAULT;
// }
@Override
public List<ResourceLocation> getStaticModels()
{
return MODELS.getModel( isPowered(), isActive() );
}
2014-09-24 02:26:27 +02:00
}