Applied-Energistics-2-tiler.../util/item/AEStack.java

166 lines
3.2 KiB
Java
Raw Normal View History

package appeng.util.item;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import appeng.api.storage.data.IAEStack;
public abstract class AEStack<StackType extends IAEStack> implements IAEStack<StackType>
{
protected boolean isCraftable;
protected long stackSize;
protected long countRequestable;
@Override
public boolean isMeaninful()
{
return stackSize != 0 || getCountRequestable() > 0 || isCraftable();
}
@Override
public StackType reset()
{
stackSize = 0;
// priority = Integer.MIN_VALUE;
setCountRequestable( 0 );
setCraftable( false );
return (StackType) this;
}
@Override
public long getStackSize()
{
return stackSize;
}
@Override
public StackType setStackSize(long ss)
{
stackSize = ss;
return (StackType) this;
}
@Override
public long getCountRequestable()
{
return countRequestable;
}
@Override
public StackType setCountRequestable(long countRequestable)
{
this.countRequestable = countRequestable;
return (StackType) this;
}
@Override
public boolean isCraftable()
{
return isCraftable;
}
@Override
public StackType setCraftable(boolean isCraftable)
{
this.isCraftable = isCraftable;
return (StackType) this;
}
@Override
public void decStackSize(long i)
{
stackSize -= i;
}
@Override
public void incStackSize(long i)
{
stackSize += i;
}
@Override
public void decCountRequestable(long i)
{
countRequestable -= i;
}
@Override
public void incCountRequestable(long i)
{
countRequestable += i;
}
void putPacketValue(DataOutputStream tag, long num) throws IOException
{
if ( num <= 255 )
tag.writeByte( (byte) (num + (long) Byte.MIN_VALUE) );
else if ( num <= 65535 )
tag.writeShort( (short) (num + (long) Short.MIN_VALUE) );
else if ( num <= 4294967295L )
tag.writeInt( (int) (num + (long) Integer.MIN_VALUE) );
else
tag.writeLong( num );
}
static long getPacketValue(byte type, DataInputStream tag) throws IOException
{
if ( type == 0 )
{
long l = tag.readByte();
l -= (long) Byte.MIN_VALUE;
return l;
}
else if ( type == 1 )
{
long l = tag.readShort();
l -= (long) Short.MIN_VALUE;
return l;
}
else if ( type == 2 )
{
long l = tag.readInt();
l -= (long) Integer.MIN_VALUE;
return l;
}
return tag.readLong();
}
byte getType(long num)
{
if ( num <= 255 )
return 0;
else if ( num <= 65535 )
return 1;
else if ( num <= 4294967295L )
return 2;
else
return 3;
}
abstract void writeIdentity(DataOutputStream i) throws IOException;
abstract void readNBT(DataOutputStream i) throws IOException;
abstract boolean hasTagCompound();
@Override
public void writeToPacket(DataOutputStream i) throws IOException
{
byte mask = (byte) (getType( 0 ) | (getType( stackSize ) << 2) | (getType( getCountRequestable() ) << 4) | ((byte) (isCraftable ? 1 : 0) << 6) | (hasTagCompound() ? 1
: 0) << 7);
i.writeByte( mask );
writeIdentity( i );
readNBT( i );
// putPacketValue( i, priority );
putPacketValue( i, stackSize );
putPacketValue( i, getCountRequestable() );
}
}