Relocate Source to proper directory.
This commit is contained in:
@@ -0,0 +1,341 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.api.storage.StorageChannel;
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.api.storage.data.IAETagCompound;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFluidStack, Comparable<AEFluidStack>
|
||||
{
|
||||
|
||||
public int myHash;
|
||||
Fluid fluid;
|
||||
protected IAETagCompound tagCompound;
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return getFluidStack().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAETagCompound getTagCompound()
|
||||
{
|
||||
return tagCompound;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(IAEFluidStack option)
|
||||
{
|
||||
if ( option == null )
|
||||
return;
|
||||
|
||||
// if ( priority < ((AEFluidStack) option).priority )
|
||||
// priority = ((AEFluidStack) option).priority;
|
||||
|
||||
incStackSize( option.getStackSize() );
|
||||
setCountRequestable( getCountRequestable() + option.getCountRequestable() );
|
||||
setCraftable( isCraftable() || option.isCraftable() );
|
||||
}
|
||||
|
||||
private AEFluidStack(AEFluidStack is) {
|
||||
|
||||
fluid = is.fluid;
|
||||
stackSize = is.stackSize;
|
||||
|
||||
// priority = is.priority;
|
||||
setCraftable( is.isCraftable() );
|
||||
setCountRequestable( is.getCountRequestable() );
|
||||
|
||||
this.myHash = is.myHash;
|
||||
}
|
||||
|
||||
protected AEFluidStack(FluidStack is) {
|
||||
if ( is == null )
|
||||
throw new RuntimeException( "Invalid Itemstack." );
|
||||
|
||||
fluid = is.getFluid();
|
||||
|
||||
if ( fluid == null )
|
||||
throw new RuntimeException( "Fluid is null." );
|
||||
|
||||
stackSize = is.amount;
|
||||
setCraftable( false );
|
||||
setCountRequestable( 0 );
|
||||
|
||||
myHash = fluid.hashCode() ^ (tagCompound == null ? 0 : System.identityHashCode( tagCompound ));
|
||||
}
|
||||
|
||||
public static AEFluidStack create(Object a)
|
||||
{
|
||||
if ( a == null )
|
||||
return null;
|
||||
if ( a instanceof AEFluidStack )
|
||||
((AEFluidStack) a).copy();
|
||||
if ( a instanceof FluidStack )
|
||||
return new AEFluidStack( (FluidStack) a );
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object ia)
|
||||
{
|
||||
if ( ia instanceof AEFluidStack )
|
||||
{
|
||||
return ((AEFluidStack) ia).fluid == fluid && tagCompound == ((AEFluidStack) ia).tagCompound;
|
||||
}
|
||||
else if ( ia instanceof FluidStack )
|
||||
{
|
||||
FluidStack is = (FluidStack) ia;
|
||||
|
||||
if ( is.fluidID == this.fluid.getID() )
|
||||
{
|
||||
NBTTagCompound ta = (NBTTagCompound) tagCompound;
|
||||
NBTTagCompound tb = is.tag;
|
||||
if ( ta == tb )
|
||||
return true;
|
||||
|
||||
if ( (ta == null && tb == null) || (ta != null && ta.hasNoTags() && tb == null) || (tb != null && tb.hasNoTags() && ta == null)
|
||||
|| (ta != null && ta.hasNoTags() && tb != null && tb.hasNoTags()) )
|
||||
return true;
|
||||
|
||||
if ( (ta == null && tb != null) || (ta != null && tb == null) )
|
||||
return false;
|
||||
|
||||
if ( AESharedNBT.isShared( tb ) )
|
||||
return ta == tb;
|
||||
|
||||
return Platform.NBTEqualityTest( ta, tb );
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack getFluidStack()
|
||||
{
|
||||
FluidStack is = new FluidStack( fluid, (int) Math.min( Integer.MAX_VALUE, stackSize ) );
|
||||
if ( tagCompound != null )
|
||||
is.tag = tagCompound.getNBTTagCompoundCopy();
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack copy()
|
||||
{
|
||||
return new AEFluidStack( this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound i)
|
||||
{
|
||||
/*
|
||||
* Mojang Fucked this over ; GC Optimization - Ugly Yes, but it saves a lot in the memory department.
|
||||
*/
|
||||
|
||||
/*
|
||||
* NBTBase FluidName = i.getTag( "FluidName" ); NBTBase Count = i.getTag( "Count" ); NBTBase Cnt = i.getTag(
|
||||
* "Cnt" ); NBTBase Req = i.getTag( "Req" ); NBTBase Craft = i.getTag( "Craft" );
|
||||
*/
|
||||
|
||||
/*
|
||||
* if ( FluidName != null && FluidName instanceof NBTTagString ) ((NBTTagString) FluidName).data = (String)
|
||||
* this.fluid.getName(); else
|
||||
*/
|
||||
i.setString( "FluidName", (String) this.fluid.getName() );
|
||||
|
||||
/*
|
||||
* if ( Count != null && Count instanceof NBTTagByte ) ((NBTTagByte) Count).data = (byte) 0; else
|
||||
*/
|
||||
i.setByte( "Count", (byte) 0 );
|
||||
|
||||
/*
|
||||
* if ( Cnt != null && Cnt instanceof NBTTagLong ) ((NBTTagLong) Cnt).data = this.stackSize; else
|
||||
*/
|
||||
i.setLong( "Cnt", this.stackSize );
|
||||
|
||||
/*
|
||||
* if ( Req != null && Req instanceof NBTTagLong ) ((NBTTagLong) Req).data = this.stackSize; else
|
||||
*/
|
||||
i.setLong( "Req", this.getCountRequestable() );
|
||||
|
||||
/*
|
||||
* if ( Craft != null && Craft instanceof NBTTagByte ) ((NBTTagByte) Craft).data = (byte) (this.isCraftable() ?
|
||||
* 1 : 0); else
|
||||
*/i.setBoolean( "Craft", this.isCraftable() );
|
||||
|
||||
if ( tagCompound != null )
|
||||
i.setTag( "tag", (NBTTagCompound) tagCompound );
|
||||
else
|
||||
i.removeTag( "tag" );
|
||||
|
||||
}
|
||||
|
||||
public static IAEFluidStack loadFluidStackFromNBT(NBTTagCompound i)
|
||||
{
|
||||
ItemStack itemstack = ItemStack.loadItemStackFromNBT( i );
|
||||
if ( itemstack == null )
|
||||
return null;
|
||||
AEFluidStack aeis = AEFluidStack.create( itemstack );
|
||||
// aeis.priority = i.getInteger( "Priority" );
|
||||
aeis.stackSize = i.getLong( "Cnt" );
|
||||
aeis.setCountRequestable( i.getLong( "Req" ) );
|
||||
aeis.setCraftable( i.getBoolean( "Craft" ) );
|
||||
return aeis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTagCompound()
|
||||
{
|
||||
return tagCompound != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return myHash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(AEFluidStack b)
|
||||
{
|
||||
int diff = hashCode() - b.hashCode();
|
||||
return diff > 0 ? 1 : (diff < 0 ? -1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
void writeIdentity(ByteBuf i) throws IOException
|
||||
{
|
||||
byte[] name = fluid.getName().getBytes( "UTF-8" );
|
||||
i.writeByte( (byte) name.length );
|
||||
i.writeBytes( name );
|
||||
}
|
||||
|
||||
@Override
|
||||
void readNBT(ByteBuf i) throws IOException
|
||||
{
|
||||
if ( hasTagCompound() )
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream( bytes );
|
||||
|
||||
CompressedStreamTools.write( (NBTTagCompound) getTagCompound(), data );
|
||||
|
||||
byte[] tagBytes = bytes.toByteArray();
|
||||
int size = tagBytes.length;
|
||||
|
||||
i.writeInt( size );
|
||||
i.writeBytes( tagBytes );
|
||||
}
|
||||
}
|
||||
|
||||
public static IAEFluidStack loadFluidStackFromPacket(ByteBuf data) throws IOException
|
||||
{
|
||||
byte mask = data.readByte();
|
||||
// byte PriorityType = (byte) (mask & 0x03);
|
||||
byte StackType = (byte) ((mask & 0x0C) >> 2);
|
||||
byte CountReqType = (byte) ((mask & 0x30) >> 4);
|
||||
boolean isCraftable = (mask & 0x40) > 0;
|
||||
boolean hasTagCompound = (mask & 0x80) > 0;
|
||||
|
||||
// don't send this...
|
||||
NBTTagCompound d = new NBTTagCompound();
|
||||
|
||||
byte len2 = data.readByte();
|
||||
byte name[] = new byte[len2];
|
||||
data.readBytes( name, 0, len2 );
|
||||
|
||||
d.setString( "FluidName", new String( name, "UTF-8" ) );
|
||||
d.setByte( "Count", (byte) 0 );
|
||||
|
||||
if ( hasTagCompound )
|
||||
{
|
||||
int len = data.readInt();
|
||||
|
||||
byte[] bd = new byte[len];
|
||||
data.readBytes( bd );
|
||||
|
||||
DataInputStream di = new DataInputStream( new ByteArrayInputStream( bd ) );
|
||||
d.setTag( "tag", CompressedStreamTools.read( di ) );
|
||||
}
|
||||
|
||||
// long priority = getPacketValue( PriorityType, data );
|
||||
long stackSize = getPacketValue( StackType, data );
|
||||
long countRequestable = getPacketValue( CountReqType, data );
|
||||
|
||||
FluidStack fluidStack = FluidStack.loadFluidStackFromNBT( d );
|
||||
if ( fluidStack == null )
|
||||
return null;
|
||||
|
||||
AEFluidStack aeis = AEFluidStack.create( fluidStack );
|
||||
// aeis.priority = (int) priority;
|
||||
aeis.stackSize = stackSize;
|
||||
aeis.setCountRequestable( countRequestable );
|
||||
aeis.setCraftable( isCraftable );
|
||||
return aeis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fluid getFluid()
|
||||
{
|
||||
return fluid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack empty()
|
||||
{
|
||||
IAEFluidStack dup = copy();
|
||||
dup.reset();
|
||||
return dup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fuzzyComparison(Object st, FuzzyMode mode)
|
||||
{
|
||||
if ( st instanceof FluidStack )
|
||||
{
|
||||
return ((FluidStack) st).getFluid() == getFluid();
|
||||
}
|
||||
|
||||
if ( st instanceof IAEFluidStack )
|
||||
{
|
||||
return ((IAEFluidStack) st).getFluid() == getFluid();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItem()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFluid()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageChannel getChannel()
|
||||
{
|
||||
return StorageChannel.FLUIDS;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import appeng.util.Platform;
|
||||
import cpw.mods.fml.common.registry.GameRegistry.UniqueIdentifier;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public class AEItemDef
|
||||
{
|
||||
|
||||
public int myHash;
|
||||
|
||||
public int def;
|
||||
|
||||
private int itemID;
|
||||
public Item item;
|
||||
public int damageValue;
|
||||
|
||||
public int dspDamage;
|
||||
public int maxDamage;
|
||||
|
||||
public AESharedNBT tagCompound;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public String displayName;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public List tooltip;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public UniqueIdentifier uniqueID;
|
||||
|
||||
public OreReference isOre;
|
||||
|
||||
static AESharedNBT lowTag = new AESharedNBT( Integer.MIN_VALUE );
|
||||
static AESharedNBT highTag = new AESharedNBT( Integer.MAX_VALUE );
|
||||
|
||||
public AEItemDef(Item it) {
|
||||
item = it;
|
||||
itemID = System.identityHashCode( item );
|
||||
}
|
||||
|
||||
public AEItemDef copy()
|
||||
{
|
||||
AEItemDef t = new AEItemDef( item );
|
||||
t.def = def;
|
||||
t.damageValue = damageValue;
|
||||
t.dspDamage = dspDamage;
|
||||
t.maxDamage = maxDamage;
|
||||
t.tagCompound = tagCompound;
|
||||
t.isOre = isOre;
|
||||
return t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
AEItemDef def = (AEItemDef) obj;
|
||||
return def.damageValue == damageValue && def.item == item && tagCompound == def.tagCompound;
|
||||
}
|
||||
|
||||
public int getDamageValueHack(ItemStack is)
|
||||
{
|
||||
return Items.blaze_rod.getDamage( is );
|
||||
}
|
||||
|
||||
public boolean isItem(ItemStack otherStack)
|
||||
{
|
||||
// hackery!
|
||||
int dmg = getDamageValueHack( otherStack );
|
||||
|
||||
if ( item == otherStack.getItem() && dmg == damageValue )
|
||||
{
|
||||
if ( (tagCompound != null) == otherStack.hasTagCompound() )
|
||||
return true;
|
||||
|
||||
if ( tagCompound != null && otherStack.hasTagCompound() )
|
||||
return Platform.NBTEqualityTest( (NBTBase) tagCompound, otherStack.getTagCompound() );
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void reHash()
|
||||
{
|
||||
def = itemID << Platform.DEF_OFFSET | damageValue;
|
||||
myHash = def ^ (tagCompound == null ? 0 : System.identityHashCode( tagCompound ));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,630 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.api.storage.StorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IAETagCompound;
|
||||
import appeng.util.Platform;
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
import cpw.mods.fml.common.registry.GameRegistry.UniqueIdentifier;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemStack, Comparable<AEItemStack>
|
||||
{
|
||||
|
||||
AEItemDef def;
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return getItemStack().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(IAEItemStack option)
|
||||
{
|
||||
if ( option == null )
|
||||
return;
|
||||
|
||||
// if ( priority < ((AEItemStack) option).priority )
|
||||
// priority = ((AEItemStack) option).priority;
|
||||
|
||||
incStackSize( option.getStackSize() );
|
||||
setCountRequestable( getCountRequestable() + option.getCountRequestable() );
|
||||
setCraftable( isCraftable() || option.isCraftable() );
|
||||
}
|
||||
|
||||
private AEItemStack(AEItemStack is) {
|
||||
def = is.def;
|
||||
stackSize = is.stackSize;
|
||||
setCraftable( is.isCraftable() );
|
||||
setCountRequestable( is.getCountRequestable() );
|
||||
}
|
||||
|
||||
protected AEItemStack(ItemStack is) {
|
||||
if ( is == null )
|
||||
throw new RuntimeException( "Invalid Itemstack." );
|
||||
|
||||
def = new AEItemDef( is.getItem() );
|
||||
|
||||
if ( def.item == null )
|
||||
throw new RuntimeException( "This ItemStack is bad, it has a null item." );
|
||||
|
||||
/*
|
||||
* Prevent an Item from changing the damage value on me... Either, this or a core mod.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Super Hacky.
|
||||
*
|
||||
* is.itemID = appeng.api.Materials.matQuartz.itemID; damageValue = is.getItemDamage(); is.itemID = itemID;
|
||||
*/
|
||||
|
||||
/*
|
||||
* Kinda Hacky
|
||||
*/
|
||||
def.damageValue = def.getDamageValueHack( is );
|
||||
def.dspDamage = is.getItemDamageForDisplay();
|
||||
def.maxDamage = is.getMaxDamage();
|
||||
|
||||
NBTTagCompound tagCompound = is.getTagCompound();
|
||||
if ( tagCompound != null )
|
||||
def.tagCompound = (AESharedNBT) AESharedNBT.getSharedTagCompound( tagCompound, is );
|
||||
|
||||
stackSize = is.stackSize;
|
||||
setCraftable( false );
|
||||
setCountRequestable( 0 );
|
||||
|
||||
def.reHash();
|
||||
def.isOre = OreHelper.instance.isOre( is );
|
||||
}
|
||||
|
||||
public static AEItemStack create(Object a)
|
||||
{
|
||||
if ( a instanceof ItemStack )
|
||||
return new AEItemStack( (ItemStack) a );
|
||||
if ( a instanceof IAEItemStack )
|
||||
((IAEItemStack) a).copy();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item getItem()
|
||||
{
|
||||
return def.item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object ia)
|
||||
{
|
||||
if ( ia instanceof AEItemStack )
|
||||
{
|
||||
return ((AEItemStack) ia).def.equals( def );// && def.tagCompound == ((AEItemStack) ia).def.tagCompound;
|
||||
}
|
||||
else if ( ia instanceof ItemStack )
|
||||
{
|
||||
ItemStack is = (ItemStack) ia;
|
||||
|
||||
if ( is.getItem() == def.item && is.getItemDamage() == this.def.damageValue )
|
||||
{
|
||||
NBTTagCompound ta = (NBTTagCompound) def.tagCompound;
|
||||
NBTTagCompound tb = is.getTagCompound();
|
||||
if ( ta == tb )
|
||||
return true;
|
||||
|
||||
if ( (ta == null && tb == null) || (ta != null && ta.hasNoTags() && tb == null) || (tb != null && tb.hasNoTags() && ta == null)
|
||||
|| (ta != null && ta.hasNoTags() && tb != null && tb.hasNoTags()) )
|
||||
return true;
|
||||
|
||||
if ( (ta == null && tb != null) || (ta != null && tb == null) )
|
||||
return false;
|
||||
|
||||
if ( AESharedNBT.isShared( tb ) )
|
||||
return ta == tb;
|
||||
|
||||
return Platform.NBTEqualityTest( ta, tb );
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemStack()
|
||||
{
|
||||
ItemStack is = new ItemStack( def.item, (int) Math.min( Integer.MAX_VALUE, stackSize ), def.damageValue );
|
||||
if ( def.tagCompound != null )
|
||||
is.setTagCompound( def.tagCompound.getNBTTagCompoundCopy() );
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack copy()
|
||||
{
|
||||
return new AEItemStack( this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound i)
|
||||
{
|
||||
/*
|
||||
* Mojang Fucked this over ; GC Optimization - Ugly Yes, but it saves a lot in the memory department.
|
||||
*/
|
||||
|
||||
/*
|
||||
* NBTBase id = i.getTag( "id" ); NBTBase Count = i.getTag( "Count" ); NBTBase Cnt = i.getTag( "Cnt" ); NBTBase
|
||||
* Req = i.getTag( "Req" ); NBTBase Craft = i.getTag( "Craft" ); NBTBase Damage = i.getTag( "Damage" );
|
||||
*/
|
||||
|
||||
/*
|
||||
* if ( id != null && id instanceof NBTTagShort ) ((NBTTagShort) id).data = (short) this.def.item.itemID; else
|
||||
*/
|
||||
i.setShort( "id", (short) Item.itemRegistry.getIDForObject( this.def.item ) );
|
||||
|
||||
/*
|
||||
* if ( Count != null && Count instanceof NBTTagByte ) ((NBTTagByte) Count).data = (byte) 0; else
|
||||
*/
|
||||
i.setByte( "Count", (byte) 0 );
|
||||
|
||||
/*
|
||||
* if ( Cnt != null && Cnt instanceof NBTTagLong ) ((NBTTagLong) Cnt).data = this.stackSize; else
|
||||
*/
|
||||
i.setLong( "Cnt", this.stackSize );
|
||||
|
||||
/*
|
||||
* if ( Req != null && Req instanceof NBTTagLong ) ((NBTTagLong) Req).data = this.stackSize; else
|
||||
*/
|
||||
i.setLong( "Req", this.getCountRequestable() );
|
||||
|
||||
/*
|
||||
* if ( Craft != null && Craft instanceof NBTTagByte ) ((NBTTagByte) Craft).data = (byte) (this.isCraftable() ?
|
||||
* 1 : 0); else
|
||||
*/
|
||||
i.setBoolean( "Craft", this.isCraftable() );
|
||||
|
||||
/*
|
||||
* if ( Damage != null && Damage instanceof NBTTagShort ) ((NBTTagShort) Damage).data = (short)
|
||||
* this.def.damageValue; else
|
||||
*/
|
||||
i.setShort( "Damage", (short) this.def.damageValue );
|
||||
|
||||
if ( def.tagCompound != null )
|
||||
i.setTag( "tag", (NBTTagCompound) def.tagCompound );
|
||||
else
|
||||
i.removeTag( "tag" );
|
||||
|
||||
}
|
||||
|
||||
public static IAEItemStack loadItemStackFromNBT(NBTTagCompound i)
|
||||
{
|
||||
if ( i == null )
|
||||
return null;
|
||||
|
||||
ItemStack itemstack = ItemStack.loadItemStackFromNBT( i );
|
||||
if ( itemstack == null )
|
||||
return null;
|
||||
|
||||
AEItemStack aeis = AEItemStack.create( itemstack );
|
||||
// aeis.priority = i.getInteger( "Priority" );
|
||||
aeis.stackSize = i.getLong( "Cnt" );
|
||||
aeis.setCountRequestable( i.getLong( "Req" ) );
|
||||
aeis.setCraftable( i.getBoolean( "Craft" ) );
|
||||
return aeis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTagCompound()
|
||||
{
|
||||
return def.tagCompound != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return def.myHash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(AEItemStack b)
|
||||
{
|
||||
int id = compare( def.item.hashCode(), b.def.item.hashCode() );
|
||||
int dv = compare( def.damageValue, b.def.damageValue );
|
||||
int dspv = compare( def.dspDamage, b.def.dspDamage );
|
||||
// AELog.info( "NBT: " + nbt );
|
||||
return id == 0 ? (dv == 0 ? (dspv == 0 ? compareNBT( b.def ) : dspv) : dv) : id;
|
||||
}
|
||||
|
||||
private int compareNBT(AEItemDef b)
|
||||
{
|
||||
int nbt = compare( (def.tagCompound == null ? 0 : def.tagCompound.getHash()), (b.tagCompound == null ? 0 : b.tagCompound.getHash()) );
|
||||
if ( nbt == 0 )
|
||||
return compare( System.identityHashCode( def.tagCompound ), System.identityHashCode( b.tagCompound ) );
|
||||
return nbt;
|
||||
}
|
||||
|
||||
private int compare(int l, long m)
|
||||
{
|
||||
return l < m ? -1 : (l > m ? 1 : 0);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public List getToolTip()
|
||||
{
|
||||
if ( def.tooltip != null )
|
||||
return def.tooltip;
|
||||
|
||||
return def.tooltip = Platform.getTooltip( getItemStack() );
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public String getDisplayName()
|
||||
{
|
||||
if ( def.displayName != null )
|
||||
return def.displayName;
|
||||
|
||||
return def.displayName = Platform.getItemDisplayName( getItemStack() );
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public String getModID()
|
||||
{
|
||||
if ( def.uniqueID != null )
|
||||
return getModName( def.uniqueID );
|
||||
|
||||
return getModName( def.uniqueID = GameRegistry.findUniqueIdentifierFor( def.item ) );
|
||||
}
|
||||
|
||||
private String getModName(UniqueIdentifier uniqueIdentifier)
|
||||
{
|
||||
if ( uniqueIdentifier == null )
|
||||
return "** Null";
|
||||
|
||||
return uniqueIdentifier.modId == null ? "** Null" : uniqueIdentifier.modId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack empty()
|
||||
{
|
||||
IAEItemStack dup = copy();
|
||||
dup.reset();
|
||||
return dup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemDamage()
|
||||
{
|
||||
return def.damageValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
void writeIdentity(ByteBuf i) throws IOException
|
||||
{
|
||||
i.writeShort( Item.itemRegistry.getIDForObject( def.item ) );
|
||||
i.writeShort( getItemDamage() );
|
||||
}
|
||||
|
||||
@Override
|
||||
void readNBT(ByteBuf i) throws IOException
|
||||
{
|
||||
if ( hasTagCompound() )
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream( bytes );
|
||||
|
||||
CompressedStreamTools.write( (NBTTagCompound) getTagCompound(), data );
|
||||
|
||||
byte[] tagBytes = bytes.toByteArray();
|
||||
int size = tagBytes.length;
|
||||
|
||||
i.writeInt( size );
|
||||
i.writeBytes( tagBytes );
|
||||
}
|
||||
}
|
||||
|
||||
public static IAEItemStack loadItemStackFromPacket(ByteBuf data) throws IOException
|
||||
{
|
||||
byte mask = data.readByte();
|
||||
// byte PriorityType = (byte) (mask & 0x03);
|
||||
byte StackType = (byte) ((mask & 0x0C) >> 2);
|
||||
byte CountReqType = (byte) ((mask & 0x30) >> 4);
|
||||
boolean isCraftable = (mask & 0x40) > 0;
|
||||
boolean hasTagCompound = (mask & 0x80) > 0;
|
||||
|
||||
// don't send this...
|
||||
NBTTagCompound d = new NBTTagCompound();
|
||||
|
||||
d.setShort( "id", data.readShort() );
|
||||
d.setShort( "Damage", data.readShort() );
|
||||
d.setByte( "Count", (byte) 0 );
|
||||
|
||||
if ( hasTagCompound )
|
||||
{
|
||||
int len = data.readInt();
|
||||
|
||||
byte[] bd = new byte[len];
|
||||
data.readBytes( bd );
|
||||
|
||||
ByteArrayInputStream di = new ByteArrayInputStream( bd );
|
||||
d.setTag( "tag", CompressedStreamTools.read( new DataInputStream( di ) ) );
|
||||
}
|
||||
|
||||
// long priority = getPacketValue( PriorityType, data );
|
||||
long stackSize = getPacketValue( StackType, data );
|
||||
long countRequestable = getPacketValue( CountReqType, data );
|
||||
|
||||
ItemStack itemstack = ItemStack.loadItemStackFromNBT( d );
|
||||
if ( itemstack == null )
|
||||
return null;
|
||||
|
||||
AEItemStack aeis = AEItemStack.create( itemstack );
|
||||
// aeis.priority = (int) priority;
|
||||
aeis.stackSize = stackSize;
|
||||
aeis.setCountRequestable( countRequestable );
|
||||
aeis.setCraftable( isCraftable );
|
||||
return aeis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sameOre(IAEItemStack is)
|
||||
{
|
||||
return OreHelper.instance.sameOre( this, is );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fuzzyComparison(Object st, FuzzyMode Mode)
|
||||
{
|
||||
if ( st instanceof IAEItemStack )
|
||||
{
|
||||
IAEItemStack o = (IAEItemStack) st;
|
||||
|
||||
if ( sameOre( o ) )
|
||||
return true;
|
||||
|
||||
if ( o.getItem() == getItem() )
|
||||
{
|
||||
if ( def.item.isDamageable() )
|
||||
{
|
||||
ItemStack a = getItemStack();
|
||||
ItemStack b = o.getItemStack();
|
||||
|
||||
try
|
||||
{
|
||||
if ( Mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if ( Mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return (a.getItemDamageForDisplay() > 1) == (b.getItemDamageForDisplay() > 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
float APercentDamaged = 1.0f - (float) a.getItemDamageForDisplay() / (float) a.getMaxDamage();
|
||||
float BPercentDamaged = 1.0f - (float) b.getItemDamageForDisplay() / (float) b.getMaxDamage();
|
||||
|
||||
return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint);
|
||||
}
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
if ( Mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if ( Mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return (a.getItemDamage() > 1) == (b.getItemDamage() > 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
float APercentDamaged = (float) a.getItemDamage() / (float) a.getMaxDamage();
|
||||
float BPercentDamaged = (float) b.getItemDamage() / (float) b.getMaxDamage();
|
||||
|
||||
return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.getItemDamage() == o.getItemDamage();
|
||||
}
|
||||
}
|
||||
|
||||
if ( st instanceof ItemStack )
|
||||
{
|
||||
ItemStack o = (ItemStack) st;
|
||||
|
||||
OreHelper.instance.sameOre( this, o );
|
||||
|
||||
if ( o.getItem() == getItem() )
|
||||
{
|
||||
if ( def.item.isDamageable() )
|
||||
{
|
||||
ItemStack a = getItemStack();
|
||||
ItemStack b = o;
|
||||
|
||||
try
|
||||
{
|
||||
if ( Mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if ( Mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return (a.getItemDamageForDisplay() > 1) == (b.getItemDamageForDisplay() > 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
float APercentDamaged = 1.0f - (float) a.getItemDamageForDisplay() / (float) a.getMaxDamage();
|
||||
float BPercentDamaged = 1.0f - (float) b.getItemDamageForDisplay() / (float) b.getMaxDamage();
|
||||
|
||||
return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint);
|
||||
}
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
if ( Mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if ( Mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return (a.getItemDamage() > 1) == (b.getItemDamage() > 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
float APercentDamaged = (float) a.getItemDamage() / (float) a.getMaxDamage();
|
||||
float BPercentDamaged = (float) b.getItemDamage() / (float) b.getMaxDamage();
|
||||
|
||||
return (APercentDamaged > Mode.breakPoint) == (BPercentDamaged > Mode.breakPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.getItemDamage() == o.getItemDamage();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public IAEItemStack getLow(FuzzyMode fuzzy, boolean ignoreMeta)
|
||||
{
|
||||
AEItemStack bottom = new AEItemStack( this );
|
||||
AEItemDef newDef = bottom.def = bottom.def.copy();
|
||||
|
||||
if ( ignoreMeta )
|
||||
{
|
||||
newDef.dspDamage = newDef.damageValue = 0;
|
||||
newDef.reHash();
|
||||
return bottom;
|
||||
}
|
||||
|
||||
if ( newDef.item.isDamageable() )
|
||||
{
|
||||
if ( fuzzy == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
newDef.dspDamage = 0;
|
||||
}
|
||||
else if ( fuzzy == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
if ( def.damageValue == 0 )
|
||||
newDef.dspDamage = 0;
|
||||
else
|
||||
newDef.dspDamage = 1;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
int breakpoint = fuzzy.calculateBreakPoint( def.maxDamage );
|
||||
newDef.dspDamage = breakpoint <= def.dspDamage ? breakpoint : 0;
|
||||
}
|
||||
|
||||
newDef.damageValue = newDef.dspDamage;
|
||||
}
|
||||
|
||||
newDef.tagCompound = AEItemDef.lowTag;
|
||||
newDef.reHash();
|
||||
return bottom;
|
||||
}
|
||||
|
||||
public IAEItemStack getHigh(FuzzyMode fuzzy, boolean ignoreMeta)
|
||||
{
|
||||
AEItemStack top = new AEItemStack( this );
|
||||
AEItemDef newDef = top.def = top.def.copy();
|
||||
|
||||
if ( ignoreMeta )
|
||||
{
|
||||
newDef.dspDamage = newDef.damageValue = Integer.MAX_VALUE;
|
||||
newDef.reHash();
|
||||
return top;
|
||||
}
|
||||
|
||||
if ( newDef.item.isDamageable() )
|
||||
{
|
||||
if ( fuzzy == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
newDef.dspDamage = def.maxDamage + 1;
|
||||
}
|
||||
else if ( fuzzy == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
if ( def.damageValue == 0 )
|
||||
newDef.dspDamage = 0;
|
||||
else
|
||||
newDef.dspDamage = def.maxDamage + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int breakpoint = fuzzy.calculateBreakPoint( def.maxDamage );
|
||||
newDef.dspDamage = def.dspDamage < breakpoint ? breakpoint - 1 : def.maxDamage + 1;
|
||||
}
|
||||
|
||||
newDef.damageValue = newDef.dspDamage;
|
||||
}
|
||||
|
||||
newDef.tagCompound = AEItemDef.highTag;
|
||||
newDef.reHash();
|
||||
return top;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAETagCompound getTagCompound()
|
||||
{
|
||||
return def.tagCompound;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameType(IAEItemStack otherStack)
|
||||
{
|
||||
if ( otherStack == null )
|
||||
return false;
|
||||
|
||||
return def.equals( ((AEItemStack) otherStack).def );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameType(ItemStack otherStack)
|
||||
{
|
||||
if ( otherStack == null )
|
||||
return false;
|
||||
|
||||
return def.isItem( otherStack );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItem()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFluid()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageChannel getChannel()
|
||||
{
|
||||
return StorageChannel.ITEMS;
|
||||
}
|
||||
|
||||
public boolean isOre()
|
||||
{
|
||||
return def.isOre != null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Iterator;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.features.IItemComparison;
|
||||
import appeng.api.storage.data.IAETagCompound;
|
||||
import appeng.util.Platform;
|
||||
|
||||
/*
|
||||
* this is used for the shared NBT Cache.
|
||||
*/
|
||||
public class AESharedNBT extends NBTTagCompound implements IAETagCompound
|
||||
{
|
||||
|
||||
private Item item;
|
||||
private int meta, hash;
|
||||
public SharedSearchObject sso;
|
||||
private IItemComparison comp;
|
||||
|
||||
public int getHash()
|
||||
{
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemComparison getSpecialComparison()
|
||||
{
|
||||
return comp;
|
||||
}
|
||||
|
||||
private AESharedNBT(Item itemID, int damageValue) {
|
||||
super();
|
||||
item = itemID;
|
||||
meta = damageValue;
|
||||
}
|
||||
|
||||
public AESharedNBT(int fakeValue) {
|
||||
super();
|
||||
item = null;
|
||||
meta = 0;
|
||||
hash = fakeValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound getNBTTagCompoundCopy()
|
||||
{
|
||||
return (NBTTagCompound) copy();
|
||||
}
|
||||
|
||||
public static AESharedNBT createFromCompound(Item itemID, int damageValue, NBTTagCompound c)
|
||||
{
|
||||
AESharedNBT x = new AESharedNBT( itemID, damageValue );
|
||||
|
||||
// c.getTags()
|
||||
Iterator var2 = c.func_150296_c().iterator();
|
||||
|
||||
while (var2.hasNext())
|
||||
{
|
||||
String name = (String) var2.next();
|
||||
x.setTag( name, c.getTag( name ).copy() );
|
||||
}
|
||||
|
||||
x.hash = Platform.NBTOrderlessHash( c );
|
||||
|
||||
ItemStack isc = new ItemStack( itemID, 1, damageValue );
|
||||
isc.setTagCompound( c );
|
||||
x.comp = AEApi.instance().registries().specialComparison().getSpecialComparison( isc );
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object par1Obj)
|
||||
{
|
||||
if ( par1Obj instanceof AESharedNBT )
|
||||
return this == par1Obj;
|
||||
return super.equals( par1Obj );
|
||||
}
|
||||
|
||||
public boolean matches(Item itemid2, int meta2, int orderlessHash)
|
||||
{
|
||||
return itemid2 == item && meta == meta2 && hash == orderlessHash;
|
||||
}
|
||||
|
||||
public boolean comparePreciseWithRegistry(AESharedNBT tagCompound)
|
||||
{
|
||||
if ( this == tagCompound )
|
||||
return true;
|
||||
|
||||
if ( comp != null && tagCompound.comp != null )
|
||||
{
|
||||
return comp.sameAsPrecise( tagCompound.comp );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean compareFuzzyWithRegistry(AESharedNBT tagCompound)
|
||||
{
|
||||
if ( this == tagCompound )
|
||||
return true;
|
||||
if ( tagCompound == null )
|
||||
return false;
|
||||
|
||||
if ( comp == tagCompound.comp )
|
||||
return true;
|
||||
|
||||
if ( comp != null )
|
||||
{
|
||||
return comp.sameAsFuzzy( tagCompound.comp );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Shared Tag Compound Cache.
|
||||
*/
|
||||
private static WeakHashMap<SharedSearchObject, WeakReference<SharedSearchObject>> sharedTagCompounds = new WeakHashMap();
|
||||
|
||||
/*
|
||||
* Debug purposes.
|
||||
*/
|
||||
public static int sharedTagLoad()
|
||||
{
|
||||
return sharedTagCompounds.size();
|
||||
}
|
||||
|
||||
/*
|
||||
* returns true if the compound is part of the shared compound system ( and can thus be compared directly ).
|
||||
*/
|
||||
public static boolean isShared(NBTTagCompound ta)
|
||||
{
|
||||
return ta instanceof AESharedNBT;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns an NBT Compound that is used for accelerating comparisons.
|
||||
*/
|
||||
synchronized public static NBTTagCompound getSharedTagCompound(NBTTagCompound tagCompound, ItemStack s)
|
||||
{
|
||||
if ( tagCompound.hasNoTags() )
|
||||
return null;
|
||||
|
||||
Item itemid = s.getItem();
|
||||
int meta = -1;
|
||||
if ( s.getItem() != null && s.isItemStackDamageable() && s.getHasSubtypes() )
|
||||
meta = s.getItemDamage();
|
||||
|
||||
if ( isShared( tagCompound ) )
|
||||
return tagCompound;
|
||||
|
||||
SharedSearchObject sso = new SharedSearchObject( itemid, meta, tagCompound );
|
||||
|
||||
WeakReference<SharedSearchObject> c = sharedTagCompounds.get( sso );
|
||||
if ( c != null )
|
||||
{
|
||||
SharedSearchObject cg = c.get();
|
||||
if ( cg != null )
|
||||
return cg.shared; // I don't think I really need to check this
|
||||
// as its already certain to exist..
|
||||
}
|
||||
|
||||
AESharedNBT clone = AESharedNBT.createFromCompound( itemid, meta, tagCompound );
|
||||
sso.compound = (NBTTagCompound) sso.compound.copy(); // prevent
|
||||
// modification
|
||||
// of data based
|
||||
// on original
|
||||
// item.
|
||||
sso.shared = clone;
|
||||
clone.sso = sso;
|
||||
|
||||
sharedTagCompounds.put( sso, new WeakReference<SharedSearchObject>( sso ) );
|
||||
return clone;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
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 isMeaningful()
|
||||
{
|
||||
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(ByteBuf 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, ByteBuf 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(ByteBuf i) throws IOException;
|
||||
|
||||
abstract void readNBT(ByteBuf i) throws IOException;
|
||||
|
||||
abstract boolean hasTagCompound();
|
||||
|
||||
@Override
|
||||
public void writeToPacket(ByteBuf 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() );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
|
||||
public final class ItemList<StackType extends IAEStack> implements IItemList<StackType>
|
||||
{
|
||||
|
||||
private final TreeMap<StackType, StackType> records = new TreeMap();
|
||||
private final Class<? extends IAEStack> clz;
|
||||
|
||||
// private int currentPriority = Integer.MIN_VALUE;
|
||||
|
||||
int iteration = Integer.MIN_VALUE;
|
||||
public Throwable stacktrace;
|
||||
|
||||
public ItemList(Class<? extends IAEStack> cla) {
|
||||
clz = cla;
|
||||
}
|
||||
|
||||
private boolean checkStackType(StackType st)
|
||||
{
|
||||
if ( st == null )
|
||||
return true;
|
||||
|
||||
if ( !clz.isInstance( st ) )
|
||||
throw new RuntimeException( "WRONG TYPE - got " + st.getClass().getName() + " expected " + clz.getName() );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public void add(StackType option)
|
||||
{
|
||||
if ( checkStackType( option ) )
|
||||
return;
|
||||
|
||||
StackType st = records.get( option );
|
||||
|
||||
if ( st != null )
|
||||
{
|
||||
// st.setPriority( currentPriority );
|
||||
st.add( option );
|
||||
return;
|
||||
}
|
||||
|
||||
StackType opt = (StackType) option.copy();
|
||||
// opt.setPriority( currentPriority );
|
||||
records.put( opt, opt );
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public void addStorage(StackType option) // adds a stack as
|
||||
// stored.
|
||||
{
|
||||
if ( checkStackType( option ) )
|
||||
return;
|
||||
|
||||
StackType st = records.get( option );
|
||||
|
||||
if ( st != null )
|
||||
{
|
||||
// st.setPriority( currentPriority );
|
||||
st.incStackSize( option.getStackSize() );
|
||||
return;
|
||||
}
|
||||
|
||||
StackType opt = (StackType) option.copy();
|
||||
// opt.setPriority( currentPriority );
|
||||
records.put( opt, opt );
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public void addCrafting(StackType option) // adds a stack as
|
||||
// craftable.
|
||||
{
|
||||
if ( checkStackType( option ) )
|
||||
return;
|
||||
|
||||
StackType st = records.get( option );
|
||||
|
||||
if ( st != null )
|
||||
{
|
||||
// st.setPriority( currentPriority );
|
||||
st.setCraftable( true );
|
||||
return;
|
||||
}
|
||||
|
||||
StackType opt = (StackType) option.copy();
|
||||
// opt.setPriority( currentPriority );
|
||||
opt.setStackSize( 0 );
|
||||
opt.setCraftable( true );
|
||||
|
||||
records.put( opt, opt );
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public void addRequestable(StackType option) // adds a stack
|
||||
// as
|
||||
// requestable.
|
||||
{
|
||||
if ( checkStackType( option ) )
|
||||
return;
|
||||
|
||||
StackType st = records.get( option );
|
||||
|
||||
if ( st != null )
|
||||
{
|
||||
// st.setPriority( currentPriority );
|
||||
((IAEItemStack) st).setCountRequestable( ((IAEItemStack) st).getCountRequestable() + ((IAEItemStack) option).getCountRequestable() );
|
||||
return;
|
||||
}
|
||||
|
||||
StackType opt = (StackType) option.copy();
|
||||
// opt.setPriority( currentPriority );
|
||||
opt.setStackSize( 0 );
|
||||
opt.setCraftable( false );
|
||||
opt.setCountRequestable( opt.getCountRequestable() );
|
||||
|
||||
records.put( opt, opt );
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public StackType getFirstItem()
|
||||
{
|
||||
Iterator<StackType> i = this.iterator();
|
||||
while (i.hasNext())
|
||||
return i.next();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public void resetStatus()
|
||||
{
|
||||
for (StackType i : this)
|
||||
i.reset();
|
||||
}
|
||||
|
||||
/*
|
||||
* synchronized public void clean() { Iterator<StackType> i = iterator(); while (i.hasNext()) { StackType AEI =
|
||||
* i.next(); if ( !AEI.isMeaningful() ) i.remove(); } }
|
||||
*/
|
||||
|
||||
@Override
|
||||
synchronized public Iterator iterator()
|
||||
{
|
||||
return new MeaningfulIterator( records.values().iterator() );
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public StackType findPrecise(StackType i)
|
||||
{
|
||||
if ( checkStackType( i ) )
|
||||
return null;
|
||||
|
||||
StackType is = records.get( i );
|
||||
if ( is != null )
|
||||
{
|
||||
return is;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public int size()
|
||||
{
|
||||
return records.values().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return !iterator().hasNext();
|
||||
}
|
||||
|
||||
public Collection<StackType> findFuzzyDamage(AEItemStack filter, FuzzyMode fuzzy, boolean ignoreMeta)
|
||||
{
|
||||
StackType low = (StackType) filter.getLow( fuzzy, ignoreMeta );
|
||||
StackType high = (StackType) filter.getHigh( fuzzy, ignoreMeta );
|
||||
return records.subMap( low, true, high, true ).descendingMap().values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<StackType> findFuzzy(StackType filter, FuzzyMode fuzzy)
|
||||
{
|
||||
if ( checkStackType( filter ) )
|
||||
return new ArrayList();
|
||||
|
||||
if ( filter instanceof IAEFluidStack )
|
||||
return filter.equals( this ) ? (List<StackType>) Arrays.asList( new IAEFluidStack[] { (IAEFluidStack) filter } ) : (List<StackType>) Arrays
|
||||
.asList( new IAEFluidStack[] {} );
|
||||
|
||||
AEItemStack ais = (AEItemStack) filter;
|
||||
if ( ais.isOre() )
|
||||
{
|
||||
OreReference or = ais.def.isOre;
|
||||
if ( or.getAEEquivalents().size() == 1 )
|
||||
{
|
||||
IAEItemStack is = or.getAEEquivalents().get( 0 );
|
||||
return findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
Collection<StackType> output = new LinkedList();
|
||||
|
||||
for (IAEItemStack is : or.getAEEquivalents())
|
||||
output.addAll( findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ) );
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
return findFuzzyDamage( ais, fuzzy, false );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemContainer;
|
||||
|
||||
public class ItemModList implements IItemContainer<IAEItemStack>
|
||||
{
|
||||
|
||||
final IItemContainer<IAEItemStack> backingStore;
|
||||
final IItemContainer<IAEItemStack> overrides = AEApi.instance().storage().createItemList();
|
||||
|
||||
public ItemModList(IItemContainer<IAEItemStack> backend) {
|
||||
backingStore = backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(IAEItemStack option)
|
||||
{
|
||||
IAEItemStack over = overrides.findPrecise( option );
|
||||
if ( over == null )
|
||||
{
|
||||
over = backingStore.findPrecise( option );
|
||||
if ( over == null )
|
||||
overrides.add( option );
|
||||
else
|
||||
{
|
||||
option.add( over );
|
||||
overrides.add( option );
|
||||
}
|
||||
}
|
||||
else
|
||||
overrides.add( option );
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack findPrecise(IAEItemStack i)
|
||||
{
|
||||
IAEItemStack over = overrides.findPrecise( i );
|
||||
if ( over == null )
|
||||
return backingStore.findPrecise( i );
|
||||
return over;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<IAEItemStack> findFuzzy(IAEItemStack input, FuzzyMode fuzzy)
|
||||
{
|
||||
return overrides.findFuzzy( input, fuzzy );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return overrides.isEmpty() && backingStore.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
|
||||
public class MeaningfulIterator<StackType extends IAEStack> implements Iterator
|
||||
{
|
||||
|
||||
final Iterator<StackType> parent;
|
||||
private StackType next;
|
||||
|
||||
public MeaningfulIterator(Iterator<StackType> iterator) {
|
||||
parent = iterator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
while (parent.hasNext())
|
||||
{
|
||||
next = parent.next();
|
||||
if ( next.isMeaningful() )
|
||||
return true;
|
||||
else
|
||||
parent.remove(); // self cleaning :3
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object next()
|
||||
{
|
||||
return next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove()
|
||||
{
|
||||
parent.remove();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
|
||||
public class OreHelper
|
||||
{
|
||||
|
||||
public static OreHelper instance = new OreHelper();
|
||||
|
||||
class ItemRef
|
||||
{
|
||||
|
||||
ItemRef(ItemStack stack) {
|
||||
ref = stack.getItem();
|
||||
|
||||
if ( stack.getItem().isDamageable() )
|
||||
damage = 0; // IGNORED
|
||||
else
|
||||
damage = stack.getItemDamage(); // might be important...
|
||||
|
||||
hash = ref.hashCode() ^ damage;
|
||||
}
|
||||
|
||||
Item ref;
|
||||
int damage;
|
||||
int hash;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
ItemRef obj = (ItemRef) o;
|
||||
return damage == obj.damage && ref == obj.ref;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return hash;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class OreResult
|
||||
{
|
||||
|
||||
public OreReference oreValue = null;
|
||||
|
||||
};
|
||||
|
||||
HashMap<ItemRef, OreResult> references = new HashMap();
|
||||
|
||||
public OreReference isOre(ItemStack ItemStack)
|
||||
{
|
||||
ItemRef ir = new ItemRef( ItemStack );
|
||||
OreResult or = references.get( ir );
|
||||
|
||||
if ( or == null )
|
||||
{
|
||||
or = new OreResult();
|
||||
references.put( ir, or );
|
||||
|
||||
OreReference ref = new OreReference();
|
||||
Collection<Integer> ores = ref.getOres();
|
||||
Collection<ItemStack> set = ref.getEquivalents();
|
||||
|
||||
for (String ore : OreDictionary.getOreNames())
|
||||
{
|
||||
boolean add = false;
|
||||
|
||||
for (ItemStack oreItem : OreDictionary.getOres( ore ))
|
||||
{
|
||||
if ( OreDictionary.itemMatches( oreItem, ItemStack, false ) )
|
||||
{
|
||||
add = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( add )
|
||||
{
|
||||
for (ItemStack oreItem : OreDictionary.getOres( ore ))
|
||||
set.add( oreItem.copy() );
|
||||
|
||||
ores.add( OreDictionary.getOreID( ore ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( !set.isEmpty() )
|
||||
or.oreValue = ref;
|
||||
}
|
||||
|
||||
return or.oreValue;
|
||||
}
|
||||
|
||||
public boolean sameOre(AEItemStack aeItemStack, IAEItemStack is)
|
||||
{
|
||||
OreReference a = aeItemStack.def.isOre;
|
||||
OreReference b = ((AEItemStack) aeItemStack).def.isOre;
|
||||
|
||||
if ( a == b )
|
||||
return true;
|
||||
|
||||
if ( a == null || b == null )
|
||||
return false;
|
||||
|
||||
Collection<Integer> bOres = b.getOres();
|
||||
for (Integer ore : a.getOres())
|
||||
{
|
||||
if ( bOres.contains( ore ) )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean sameOre(OreReference a, OreReference b)
|
||||
{
|
||||
if ( a == null || b == null )
|
||||
return false;
|
||||
|
||||
if ( a == b )
|
||||
return true;
|
||||
|
||||
Collection<Integer> bOres = b.getOres();
|
||||
for (Integer ore : a.getOres())
|
||||
{
|
||||
if ( bOres.contains( ore ) )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean sameOre(AEItemStack aeItemStack, ItemStack o)
|
||||
{
|
||||
OreReference a = aeItemStack.def.isOre;
|
||||
if ( a == null )
|
||||
return false;
|
||||
|
||||
for (ItemStack oreItem : a.getEquivalents())
|
||||
{
|
||||
if ( OreDictionary.itemMatches( oreItem, o, false ) )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
|
||||
public class OreReference
|
||||
{
|
||||
|
||||
private LinkedList<ItemStack> otherOptions = new LinkedList();
|
||||
private ArrayList aeotherOptions = null;
|
||||
private HashSet<Integer> ores = new HashSet<Integer>();
|
||||
|
||||
public Collection<ItemStack> getEquivalents()
|
||||
{
|
||||
return otherOptions;
|
||||
}
|
||||
|
||||
public List<IAEItemStack> getAEEquivalents()
|
||||
{
|
||||
if ( aeotherOptions == null )
|
||||
{
|
||||
aeotherOptions = new ArrayList( otherOptions.size() );
|
||||
|
||||
// SUMMON AE STACKS!
|
||||
for (ItemStack is : otherOptions)
|
||||
if ( is.getItem() != null )
|
||||
aeotherOptions.add( AEItemStack.create( is ) );
|
||||
}
|
||||
|
||||
return aeotherOptions;
|
||||
}
|
||||
|
||||
public Collection<Integer> getOres()
|
||||
{
|
||||
return ores;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package appeng.util.item;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class SharedSearchObject
|
||||
{
|
||||
|
||||
int def;
|
||||
int hash;
|
||||
NBTTagCompound compound;
|
||||
public AESharedNBT shared;
|
||||
|
||||
public SharedSearchObject(Item itemID, int damageValue, NBTTagCompound tagCompound) {
|
||||
def = (damageValue << Platform.DEF_OFFSET) | Item.itemRegistry.getIDForObject( itemID );
|
||||
hash = Platform.NBTOrderlessHash( tagCompound );
|
||||
compound = tagCompound;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if ( obj == null )
|
||||
return false;
|
||||
SharedSearchObject b = (SharedSearchObject) obj;
|
||||
if ( def == b.def && hash == b.hash )
|
||||
{
|
||||
return Platform.NBTEqualityTest( compound, b.compound );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return def ^ hash;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user