Refactored AEStacks & ItemList (#4406)
Removed custom VarInt/VarLong solution. Moved itemstack and fluidstack serialisation to their own NBT tag to avoid interference. Removed now useless NBT sorting Added NullItemList
This commit is contained in:
@@ -19,7 +19,6 @@
|
|||||||
package appeng.fluids.util;
|
package appeng.fluids.util;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import net.minecraft.fluid.Fluid;
|
import net.minecraft.fluid.Fluid;
|
||||||
@@ -40,6 +39,10 @@ import appeng.util.item.AEStack;
|
|||||||
|
|
||||||
public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFluidStack, Comparable<AEFluidStack>
|
public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFluidStack, Comparable<AEFluidStack>
|
||||||
{
|
{
|
||||||
|
private static final String NBT_STACKSIZE = "cnt";
|
||||||
|
private static final String NBT_REQUESTABLE = "req";
|
||||||
|
private static final String NBT_CRAFTABLE = "craft";
|
||||||
|
private static final String NBT_FLUIDSTACK = "fs";
|
||||||
|
|
||||||
private final Fluid fluid;
|
private final Fluid fluid;
|
||||||
private CompoundNBT tagCompound;
|
private CompoundNBT tagCompound;
|
||||||
@@ -90,7 +93,7 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
|
|||||||
|
|
||||||
public static IAEFluidStack fromNBT( final CompoundNBT data )
|
public static IAEFluidStack fromNBT( final CompoundNBT data )
|
||||||
{
|
{
|
||||||
final FluidStack fluidStack = FluidStack.loadFluidStackFromNBT( data );
|
final FluidStack fluidStack = FluidStack.loadFluidStackFromNBT( data.getCompound( NBT_FLUIDSTACK ) );
|
||||||
|
|
||||||
if( fluidStack == null )
|
if( fluidStack == null )
|
||||||
{
|
{
|
||||||
@@ -98,9 +101,9 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
|
|||||||
}
|
}
|
||||||
|
|
||||||
final AEFluidStack fluid = AEFluidStack.fromFluidStack( fluidStack );
|
final AEFluidStack fluid = AEFluidStack.fromFluidStack( fluidStack );
|
||||||
fluid.setStackSize( data.getLong( "Cnt" ) );
|
fluid.setStackSize( data.getLong( NBT_STACKSIZE ) );
|
||||||
fluid.setCountRequestable( data.getLong( "Req" ) );
|
fluid.setCountRequestable( data.getLong( NBT_REQUESTABLE ) );
|
||||||
fluid.setCraftable( data.getBoolean( "Craft" ) );
|
fluid.setCraftable( data.getBoolean( NBT_CRAFTABLE ) );
|
||||||
|
|
||||||
if( fluid.hasTagCompound() )
|
if( fluid.hasTagCompound() )
|
||||||
{
|
{
|
||||||
@@ -125,20 +128,20 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
|
|||||||
@Override
|
@Override
|
||||||
public void writeToNBT( final CompoundNBT data )
|
public void writeToNBT( final CompoundNBT data )
|
||||||
{
|
{
|
||||||
data.putString( "FluidName", this.fluid.getRegistryName().toString() );
|
data.getCompound( NBT_FLUIDSTACK ).putString( "FluidName", this.fluid.getRegistryName().toString() );
|
||||||
data.putByte( "Count", (byte) 0 );
|
data.getCompound( NBT_FLUIDSTACK ).putInt( "Amount", 0 );
|
||||||
data.putLong( "Cnt", this.getStackSize() );
|
|
||||||
data.putLong( "Req", this.getCountRequestable() );
|
|
||||||
data.putBoolean( "Craft", this.isCraftable() );
|
|
||||||
|
|
||||||
if( this.hasTagCompound() )
|
if( this.hasTagCompound() )
|
||||||
{
|
{
|
||||||
data.put( "Tag", this.tagCompound );
|
data.getCompound( NBT_FLUIDSTACK ).put( "Tag", this.tagCompound );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
data.remove( "Tag" );
|
data.getCompound( NBT_FLUIDSTACK ).remove( "Tag" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data.putLong( NBT_STACKSIZE, this.getStackSize() );
|
||||||
|
data.putLong( NBT_REQUESTABLE, this.getCountRequestable() );
|
||||||
|
data.putBoolean( NBT_CRAFTABLE, this.isCraftable() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -264,27 +267,11 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
|
|||||||
|
|
||||||
public static IAEFluidStack fromPacket( final PacketBuffer buffer )
|
public static IAEFluidStack fromPacket( final PacketBuffer buffer )
|
||||||
{
|
{
|
||||||
final byte mask = buffer.readByte();
|
final boolean isCraftable = buffer.readBoolean();
|
||||||
final byte stackType = (byte) ( ( mask & 0x0C ) >> 2 );
|
final FluidStack fluidStack = buffer.readFluidStack();
|
||||||
final byte countReqType = (byte) ( ( mask & 0x30 ) >> 4 );
|
|
||||||
final boolean isCraftable = ( mask & 0x40 ) > 0;
|
|
||||||
final boolean hasTagCompound = ( mask & 0x80 ) > 0;
|
|
||||||
|
|
||||||
// don't send this...
|
final long stackSize = buffer.readVarLong();
|
||||||
final CompoundNBT d = new CompoundNBT();
|
final long countRequestable = buffer.readVarLong();
|
||||||
|
|
||||||
d.putString( "FluidName", buffer.readString() );
|
|
||||||
d.putByte( "Amount", (byte) 0 );
|
|
||||||
|
|
||||||
if( hasTagCompound )
|
|
||||||
{
|
|
||||||
d.put( "Tag", buffer.readCompoundTag() );
|
|
||||||
}
|
|
||||||
|
|
||||||
final long stackSize = getPacketValue( stackType, buffer );
|
|
||||||
final long countRequestable = getPacketValue( countReqType, buffer );
|
|
||||||
|
|
||||||
final FluidStack fluidStack = FluidStack.loadFluidStackFromNBT( d );
|
|
||||||
|
|
||||||
if( fluidStack == null )
|
if( fluidStack == null )
|
||||||
{
|
{
|
||||||
@@ -292,7 +279,6 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
|
|||||||
}
|
}
|
||||||
|
|
||||||
final AEFluidStack fluid = AEFluidStack.fromFluidStack( fluidStack );
|
final AEFluidStack fluid = AEFluidStack.fromFluidStack( fluidStack );
|
||||||
// fluid.priority = (int) priority;
|
|
||||||
fluid.setStackSize( stackSize );
|
fluid.setStackSize( stackSize );
|
||||||
fluid.setCountRequestable( countRequestable );
|
fluid.setCountRequestable( countRequestable );
|
||||||
fluid.setCraftable( isCraftable );
|
fluid.setCraftable( isCraftable );
|
||||||
@@ -302,23 +288,9 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
|
|||||||
@Override
|
@Override
|
||||||
public void writeToPacket( final PacketBuffer buffer )
|
public void writeToPacket( final PacketBuffer buffer )
|
||||||
{
|
{
|
||||||
final byte mask = (byte) ( ( this.getType( this.getStackSize() ) << 2 ) | ( this
|
buffer.writeBoolean( this.isCraftable() );
|
||||||
.getType( this.getCountRequestable() ) << 4 ) | ( (byte) ( this.isCraftable() ? 1 : 0 ) << 6 ) | ( this.hasTagCompound() ? 1 : 0 ) << 7 );
|
buffer.writeFluidStack( this.getFluidStack() );
|
||||||
|
buffer.writeVarLong( this.getStackSize() );
|
||||||
buffer.writeByte( mask );
|
buffer.writeVarLong( this.getCountRequestable() );
|
||||||
|
|
||||||
this.writeToStream( buffer );
|
|
||||||
|
|
||||||
this.putPacketValue( buffer, this.getStackSize() );
|
|
||||||
this.putPacketValue( buffer, this.getCountRequestable() );
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeToStream( final PacketBuffer buffer )
|
|
||||||
{
|
|
||||||
buffer.writeString( fluid.getRegistryName().toString() );
|
|
||||||
if( this.hasTagCompound() )
|
|
||||||
{
|
|
||||||
buffer.writeCompoundTag( tagCompound );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of Applied Energistics 2.
|
* This file is part of Applied Energistics 2.
|
||||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
|
||||||
*
|
*
|
||||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -19,19 +19,16 @@
|
|||||||
package appeng.util.item;
|
package appeng.util.item;
|
||||||
|
|
||||||
|
|
||||||
import java.util.EnumSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.CompoundNBT;
|
import net.minecraft.nbt.CompoundNBT;
|
||||||
import net.minecraft.network.PacketBuffer;
|
import net.minecraft.network.PacketBuffer;
|
||||||
import net.minecraft.util.ResourceLocation;
|
|
||||||
import net.minecraft.util.text.ITextComponent;
|
import net.minecraft.util.text.ITextComponent;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
@@ -47,14 +44,17 @@ import appeng.util.Platform;
|
|||||||
|
|
||||||
public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemStack
|
public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemStack
|
||||||
{
|
{
|
||||||
private AESharedItemStack sharedStack;
|
private static final String NBT_STACKSIZE = "cnt";
|
||||||
|
private static final String NBT_REQUESTABLE = "req";
|
||||||
|
private static final String NBT_CRAFTABLE = "craft";
|
||||||
|
private static final String NBT_ITEMSTACK = "is";
|
||||||
|
|
||||||
|
private final AESharedItemStack sharedStack;
|
||||||
|
|
||||||
@OnlyIn( Dist.CLIENT )
|
@OnlyIn( Dist.CLIENT )
|
||||||
private ITextComponent displayName;
|
private ITextComponent displayName;
|
||||||
@OnlyIn( Dist.CLIENT )
|
@OnlyIn( Dist.CLIENT )
|
||||||
private List<ITextComponent> tooltip;
|
private List<ITextComponent> tooltip;
|
||||||
@OnlyIn( Dist.CLIENT )
|
|
||||||
private ResourceLocation uniqueID;
|
|
||||||
|
|
||||||
private AEItemStack( final AEItemStack is )
|
private AEItemStack( final AEItemStack is )
|
||||||
{
|
{
|
||||||
@@ -90,38 +90,37 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ItemStack itemstack = ItemStack.read( i );
|
final ItemStack itemstack = ItemStack.read( i.getCompound( NBT_ITEMSTACK ) );
|
||||||
if( itemstack.isEmpty() )
|
if( itemstack.isEmpty() )
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final AEItemStack item = AEItemStack.fromItemStack( itemstack );
|
final AEItemStack item = AEItemStack.fromItemStack( itemstack );
|
||||||
item.setStackSize( i.getLong( "Cnt" ) );
|
item.setStackSize( i.getLong( NBT_STACKSIZE ) );
|
||||||
item.setCountRequestable( i.getLong( "Req" ) );
|
item.setCountRequestable( i.getLong( NBT_REQUESTABLE ) );
|
||||||
item.setCraftable( i.getBoolean( "Craft" ) );
|
item.setCraftable( i.getBoolean( NBT_CRAFTABLE ) );
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeToNBT( final CompoundNBT i )
|
public void writeToNBT( final CompoundNBT i )
|
||||||
{
|
{
|
||||||
this.getDefinition().write( i );
|
final CompoundNBT itemStack = new CompoundNBT();
|
||||||
i.putLong( "Cnt", this.getStackSize() );
|
this.getDefinition().write( itemStack );
|
||||||
i.putLong( "Req", this.getCountRequestable() );
|
|
||||||
i.putBoolean( "Craft", this.isCraftable() );
|
i.put( NBT_ITEMSTACK, itemStack );
|
||||||
|
i.putLong( NBT_STACKSIZE, this.getStackSize() );
|
||||||
|
i.putLong( NBT_REQUESTABLE, this.getCountRequestable() );
|
||||||
|
i.putBoolean( NBT_CRAFTABLE, this.isCraftable() );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AEItemStack fromPacket( final PacketBuffer data )
|
public static AEItemStack fromPacket( final PacketBuffer buffer )
|
||||||
{
|
{
|
||||||
final byte mask = data.readByte();
|
final boolean isCraftable = buffer.readBoolean();
|
||||||
final byte stackType = (byte) ( ( mask & 0x0C ) >> 2 );
|
final long stackSize = buffer.readVarLong();
|
||||||
final byte countReqType = (byte) ( ( mask & 0x30 ) >> 4 );
|
final long countRequestable = buffer.readVarLong();
|
||||||
final boolean isCraftable = ( mask & 0x40 ) > 0;
|
final ItemStack itemstack = buffer.readItemStack();
|
||||||
|
|
||||||
final ItemStack itemstack = data.readItemStack();
|
|
||||||
final long stackSize = getPacketValue( stackType, data );
|
|
||||||
final long countRequestable = getPacketValue( countReqType, data );
|
|
||||||
|
|
||||||
if( itemstack.isEmpty() )
|
if( itemstack.isEmpty() )
|
||||||
{
|
{
|
||||||
@@ -135,15 +134,12 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeToPacket( final PacketBuffer i )
|
public void writeToPacket( final PacketBuffer buffer )
|
||||||
{
|
{
|
||||||
final byte mask = (byte) ( ( this.getType( this.getStackSize() ) << 2 ) | ( this
|
buffer.writeBoolean( this.isCraftable() );
|
||||||
.getType( this.getCountRequestable() ) << 4 ) | ( (byte) ( this.isCraftable() ? 1 : 0 ) << 6 ) );
|
buffer.writeVarLong( this.getStackSize() );
|
||||||
|
buffer.writeVarLong( this.getCountRequestable() );
|
||||||
i.writeByte( mask );
|
buffer.writeItemStack( getDefinition() );
|
||||||
i.writeItemStack(getDefinition());
|
|
||||||
this.putPacketValue( i, this.getStackSize() );
|
|
||||||
this.putPacketValue( i, this.getCountRequestable() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -32,9 +32,6 @@ import appeng.api.config.FuzzyMode;
|
|||||||
|
|
||||||
final class AESharedItemStack implements Comparable<AESharedItemStack>
|
final class AESharedItemStack implements Comparable<AESharedItemStack>
|
||||||
{
|
{
|
||||||
private static final CompoundNBT LOW_TAG = new CompoundNBT();
|
|
||||||
private static final CompoundNBT HIGH_TAG = new CompoundNBT();
|
|
||||||
|
|
||||||
private final ItemStack itemStack;
|
private final ItemStack itemStack;
|
||||||
private final int itemId;
|
private final int itemId;
|
||||||
private final int itemDamage;
|
private final int itemDamage;
|
||||||
@@ -48,9 +45,9 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
|
|||||||
this.hashCode = this.makeHashCode();
|
this.hashCode = this.makeHashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
Bounds getBounds( final FuzzyMode fuzzy, final boolean ignoreMeta )
|
Bounds getBounds( final FuzzyMode fuzzy )
|
||||||
{
|
{
|
||||||
return new Bounds( this.itemStack, fuzzy, ignoreMeta );
|
return new Bounds( this.itemStack, fuzzy );
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack getDefinition()
|
ItemStack getDefinition()
|
||||||
@@ -116,36 +113,9 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
|
|||||||
return damageValue;
|
return damageValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final int nbt = this.compareNBT( b.getDefinition() );
|
|
||||||
if( nbt != 0 )
|
|
||||||
{
|
|
||||||
return nbt;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( !this.itemStack.areCapsCompatible( b.getDefinition() ) )
|
|
||||||
{
|
|
||||||
return System.identityHashCode( this.itemStack ) - System.identityHashCode( b.getDefinition() );
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int compareNBT( final ItemStack b )
|
|
||||||
{
|
|
||||||
if( this.itemStack.getTag() == b.getTag() )
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if( this.itemStack.getTag() == LOW_TAG || b.getTag() == HIGH_TAG )
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if( this.itemStack.getTag() == HIGH_TAG || b.getTag() == LOW_TAG )
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return System.identityHashCode( this.itemStack.getTag() ) - System.identityHashCode( b.getTag() );
|
|
||||||
}
|
|
||||||
|
|
||||||
private int makeHashCode()
|
private int makeHashCode()
|
||||||
{
|
{
|
||||||
return Objects.hash( this.itemId, this.itemDamage, this.itemStack.hasTag() ? this.itemStack.getTag() : 0 );
|
return Objects.hash( this.itemId, this.itemDamage, this.itemStack.hasTag() ? this.itemStack.getTag() : 0 );
|
||||||
@@ -160,20 +130,20 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
|
|||||||
* Bounds enforced by {@link ItemStack#isEmpty()}
|
* Bounds enforced by {@link ItemStack#isEmpty()}
|
||||||
*/
|
*/
|
||||||
private static final int MIN_DAMAGE_VALUE = 0;
|
private static final int MIN_DAMAGE_VALUE = 0;
|
||||||
private static final int MAX_DAMAGE_VALUE = 65535;
|
private static final int MAX_DAMAGE_VALUE = Short.MAX_VALUE;
|
||||||
|
|
||||||
private final AESharedItemStack lower;
|
private final AESharedItemStack lower;
|
||||||
private final AESharedItemStack upper;
|
private final AESharedItemStack upper;
|
||||||
|
|
||||||
public Bounds( final ItemStack stack, final FuzzyMode fuzzy, final boolean ignoreMeta )
|
public Bounds( final ItemStack stack, final FuzzyMode fuzzy )
|
||||||
{
|
{
|
||||||
Preconditions.checkState( !stack.isEmpty(), "ItemStack#isEmpty() has to be false" );
|
Preconditions.checkState( !stack.isEmpty(), "ItemStack#isEmpty() has to be false" );
|
||||||
Preconditions.checkState( stack.getCount() == 1, "ItemStack#getCount() has to be 1" );
|
Preconditions.checkState( stack.getCount() == 1, "ItemStack#getCount() has to be 1" );
|
||||||
|
|
||||||
final CompoundNBT tag = stack.hasTag() ? stack.getTag() : null;
|
final CompoundNBT tag = stack.hasTag() ? stack.getTag() : null;
|
||||||
|
|
||||||
this.lower = this.makeLowerBound( stack, tag, fuzzy, ignoreMeta );
|
this.lower = this.makeLowerBound( stack, tag, fuzzy );
|
||||||
this.upper = this.makeUpperBound( stack, tag, fuzzy, ignoreMeta );
|
this.upper = this.makeUpperBound( stack, tag, fuzzy );
|
||||||
}
|
}
|
||||||
|
|
||||||
public AESharedItemStack lower()
|
public AESharedItemStack lower()
|
||||||
@@ -186,83 +156,65 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
|
|||||||
return this.upper;
|
return this.upper;
|
||||||
}
|
}
|
||||||
|
|
||||||
private AESharedItemStack makeLowerBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy, final boolean ignoreMeta )
|
private AESharedItemStack makeLowerBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy )
|
||||||
{
|
{
|
||||||
final ItemStack newDef = itemStack.copy();
|
final ItemStack newDef = itemStack.copy();
|
||||||
|
|
||||||
if( ignoreMeta )
|
if( newDef.getItem().isDamageable() )
|
||||||
{
|
{
|
||||||
newDef.setDamage( MIN_DAMAGE_VALUE );
|
if( fuzzy == FuzzyMode.IGNORE_ALL )
|
||||||
newDef.setTag( tag );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( newDef.getItem().isDamageable() )
|
|
||||||
{
|
{
|
||||||
if( fuzzy == FuzzyMode.IGNORE_ALL )
|
newDef.setDamage( MIN_DAMAGE_VALUE );
|
||||||
|
}
|
||||||
|
else if( fuzzy == FuzzyMode.PERCENT_99 )
|
||||||
|
{
|
||||||
|
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
|
||||||
{
|
{
|
||||||
newDef.setDamage( MIN_DAMAGE_VALUE );
|
newDef.setDamage( MIN_DAMAGE_VALUE );
|
||||||
}
|
}
|
||||||
else if( fuzzy == FuzzyMode.PERCENT_99 )
|
|
||||||
{
|
|
||||||
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
|
|
||||||
{
|
|
||||||
newDef.setDamage( MIN_DAMAGE_VALUE );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
newDef.setDamage( MIN_DAMAGE_VALUE + 1 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
|
newDef.setDamage( MIN_DAMAGE_VALUE + 1 );
|
||||||
final int damage = breakpoint <= itemStack.getDamage() ? breakpoint : 0;
|
|
||||||
newDef.setDamage( damage );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newDef.setTag( LOW_TAG );
|
else
|
||||||
|
{
|
||||||
|
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
|
||||||
|
final int damage = breakpoint <= itemStack.getDamage() ? breakpoint : 0;
|
||||||
|
newDef.setDamage( damage );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new AESharedItemStack( newDef );
|
return new AESharedItemStack( newDef );
|
||||||
}
|
}
|
||||||
|
|
||||||
private AESharedItemStack makeUpperBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy, final boolean ignoreMeta )
|
private AESharedItemStack makeUpperBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy )
|
||||||
{
|
{
|
||||||
final ItemStack newDef = itemStack.copy();
|
final ItemStack newDef = itemStack.copy();
|
||||||
|
|
||||||
if( ignoreMeta )
|
if( newDef.getItem().isDamageable() )
|
||||||
{
|
{
|
||||||
newDef.setDamage( MAX_DAMAGE_VALUE );
|
if( fuzzy == FuzzyMode.IGNORE_ALL )
|
||||||
newDef.setTag( tag );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( newDef.getItem().isDamageable() )
|
|
||||||
{
|
{
|
||||||
if( fuzzy == FuzzyMode.IGNORE_ALL )
|
newDef.setDamage( itemStack.getMaxDamage() + 1 );
|
||||||
|
}
|
||||||
|
else if( fuzzy == FuzzyMode.PERCENT_99 )
|
||||||
|
{
|
||||||
|
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
|
||||||
{
|
{
|
||||||
newDef.setDamage( itemStack.getMaxDamage() + 1 );
|
newDef.setDamage( MIN_DAMAGE_VALUE );
|
||||||
}
|
|
||||||
else if( fuzzy == FuzzyMode.PERCENT_99 )
|
|
||||||
{
|
|
||||||
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
|
|
||||||
{
|
|
||||||
newDef.setDamage( MIN_DAMAGE_VALUE );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
newDef.setDamage( itemStack.getMaxDamage() + 1 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
|
newDef.setDamage( itemStack.getMaxDamage() + 1 );
|
||||||
final int damage = itemStack.getDamage() < breakpoint ? breakpoint - 1 : itemStack.getMaxDamage() + 1;
|
|
||||||
newDef.setDamage( damage );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newDef.setTag( HIGH_TAG );
|
else
|
||||||
|
{
|
||||||
|
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
|
||||||
|
final int damage = itemStack.getDamage() < breakpoint ? breakpoint - 1 : itemStack.getMaxDamage() + 1;
|
||||||
|
newDef.setDamage( damage );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new AESharedItemStack( newDef );
|
return new AESharedItemStack( newDef );
|
||||||
|
|||||||
@@ -19,42 +19,16 @@
|
|||||||
package appeng.util.item;
|
package appeng.util.item;
|
||||||
|
|
||||||
|
|
||||||
import net.minecraft.network.PacketBuffer;
|
|
||||||
|
|
||||||
import appeng.api.storage.data.IAEStack;
|
import appeng.api.storage.data.IAEStack;
|
||||||
|
|
||||||
|
|
||||||
public abstract class AEStack<StackType extends IAEStack<StackType>> implements IAEStack<StackType>
|
public abstract class AEStack<T extends IAEStack<T>> implements IAEStack<T>
|
||||||
{
|
{
|
||||||
|
|
||||||
private boolean isCraftable;
|
private boolean isCraftable;
|
||||||
private long stackSize;
|
private long stackSize;
|
||||||
private long countRequestable;
|
private long countRequestable;
|
||||||
|
|
||||||
protected static long getPacketValue( final byte type, final PacketBuffer tag )
|
|
||||||
{
|
|
||||||
if( type == 0 )
|
|
||||||
{
|
|
||||||
long l = tag.readByte();
|
|
||||||
l -= Byte.MIN_VALUE;
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
else if( type == 1 )
|
|
||||||
{
|
|
||||||
long l = tag.readShort();
|
|
||||||
l -= Short.MIN_VALUE;
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
else if( type == 2 )
|
|
||||||
{
|
|
||||||
long l = tag.readInt();
|
|
||||||
l -= Integer.MIN_VALUE;
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
return tag.readLong();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getStackSize()
|
public long getStackSize()
|
||||||
{
|
{
|
||||||
@@ -62,10 +36,10 @@ public abstract class AEStack<StackType extends IAEStack<StackType>> implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackType setStackSize( final long ss )
|
public T setStackSize( final long ss )
|
||||||
{
|
{
|
||||||
this.stackSize = ss;
|
this.stackSize = ss;
|
||||||
return (StackType) this;
|
return (T) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -75,10 +49,10 @@ public abstract class AEStack<StackType extends IAEStack<StackType>> implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackType setCountRequestable( final long countRequestable )
|
public T setCountRequestable( final long countRequestable )
|
||||||
{
|
{
|
||||||
this.countRequestable = countRequestable;
|
this.countRequestable = countRequestable;
|
||||||
return (StackType) this;
|
return (T) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,26 +62,25 @@ public abstract class AEStack<StackType extends IAEStack<StackType>> implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackType setCraftable( final boolean isCraftable )
|
public T setCraftable( final boolean isCraftable )
|
||||||
{
|
{
|
||||||
this.isCraftable = isCraftable;
|
this.isCraftable = isCraftable;
|
||||||
return (StackType) this;
|
return (T) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackType reset()
|
public T reset()
|
||||||
{
|
{
|
||||||
this.stackSize = 0;
|
this.stackSize = 0;
|
||||||
// priority = Integer.MIN_VALUE;
|
|
||||||
this.setCountRequestable( 0 );
|
this.setCountRequestable( 0 );
|
||||||
this.setCraftable( false );
|
this.setCraftable( false );
|
||||||
return (StackType) this;
|
return (T) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackType empty()
|
public T empty()
|
||||||
{
|
{
|
||||||
final StackType dup = this.copy();
|
final T dup = this.copy();
|
||||||
dup.reset();
|
dup.reset();
|
||||||
return dup;
|
return dup;
|
||||||
}
|
}
|
||||||
@@ -142,45 +115,5 @@ public abstract class AEStack<StackType extends IAEStack<StackType>> implements
|
|||||||
this.countRequestable -= i;
|
this.countRequestable -= i;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected byte getType( final long num )
|
|
||||||
{
|
|
||||||
if( num <= 255 )
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if( num <= 65535 )
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if( num <= 4294967295L )
|
|
||||||
{
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract boolean hasTagCompound();
|
protected abstract boolean hasTagCompound();
|
||||||
|
|
||||||
protected void putPacketValue( final PacketBuffer tag, final long num )
|
|
||||||
{
|
|
||||||
if( num <= 255 )
|
|
||||||
{
|
|
||||||
tag.writeByte( (byte) ( num + Byte.MIN_VALUE ) );
|
|
||||||
}
|
|
||||||
else if( num <= 65535 )
|
|
||||||
{
|
|
||||||
tag.writeShort( (short) ( num + Short.MIN_VALUE ) );
|
|
||||||
}
|
|
||||||
else if( num <= 4294967295L )
|
|
||||||
{
|
|
||||||
tag.writeInt( (int) ( num + Integer.MIN_VALUE ) );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tag.writeLong( num );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,200 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Applied Energistics 2.
|
||||||
|
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
|
||||||
|
*
|
||||||
|
* 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>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package appeng.util.item;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import appeng.api.config.FuzzyMode;
|
||||||
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
|
import appeng.api.storage.data.IItemList;
|
||||||
|
|
||||||
|
|
||||||
|
abstract class AbstractItemList implements IItemList<IAEItemStack>
|
||||||
|
{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add( final IAEItemStack option )
|
||||||
|
{
|
||||||
|
if( option == null )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack st = this.getRecords().get( ( (AEItemStack) option ).getSharedStack() );
|
||||||
|
|
||||||
|
if( st != null )
|
||||||
|
{
|
||||||
|
st.add( option );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack opt = option.copy();
|
||||||
|
|
||||||
|
this.putItemRecord( opt );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IAEItemStack findPrecise( final IAEItemStack itemStack )
|
||||||
|
{
|
||||||
|
if( itemStack == null )
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.getRecords().get( ( (AEItemStack) itemStack ).getSharedStack() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<IAEItemStack> findFuzzy( final IAEItemStack filter, final FuzzyMode fuzzy )
|
||||||
|
{
|
||||||
|
if( filter == null )
|
||||||
|
{
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.getRecords().values();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty()
|
||||||
|
{
|
||||||
|
return !this.iterator().hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addStorage( final IAEItemStack option )
|
||||||
|
{
|
||||||
|
if( option == null )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack st = this.getRecords().get( ( (AEItemStack) option ).getSharedStack() );
|
||||||
|
|
||||||
|
if( st != null )
|
||||||
|
{
|
||||||
|
st.incStackSize( option.getStackSize() );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack opt = option.copy();
|
||||||
|
|
||||||
|
this.putItemRecord( opt );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCrafting( final IAEItemStack option )
|
||||||
|
{
|
||||||
|
if( option == null )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack st = this.getRecords().get( ( (AEItemStack) option ).getSharedStack() );
|
||||||
|
|
||||||
|
if( st != null )
|
||||||
|
{
|
||||||
|
st.setCraftable( true );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack opt = option.copy();
|
||||||
|
opt.setStackSize( 0 );
|
||||||
|
opt.setCraftable( true );
|
||||||
|
|
||||||
|
this.putItemRecord( opt );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addRequestable( final IAEItemStack option )
|
||||||
|
{
|
||||||
|
if( option == null )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack st = this.getRecords().get( ( (AEItemStack) option ).getSharedStack() );
|
||||||
|
|
||||||
|
if( st != null )
|
||||||
|
{
|
||||||
|
st.setCountRequestable( st.getCountRequestable() + option.getCountRequestable() );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final IAEItemStack opt = option.copy();
|
||||||
|
opt.setStackSize( 0 );
|
||||||
|
opt.setCraftable( false );
|
||||||
|
opt.setCountRequestable( option.getCountRequestable() );
|
||||||
|
|
||||||
|
this.putItemRecord( opt );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IAEItemStack getFirstItem()
|
||||||
|
{
|
||||||
|
for( final IAEItemStack stackType : this )
|
||||||
|
{
|
||||||
|
return stackType;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size()
|
||||||
|
{
|
||||||
|
int size = 0;
|
||||||
|
for( IAEItemStack entry : getRecords().values() )
|
||||||
|
{
|
||||||
|
if( entry.isMeaningful() )
|
||||||
|
{
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<IAEItemStack> iterator()
|
||||||
|
{
|
||||||
|
return new MeaningfulItemIterator<>( this.getRecords().values() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void resetStatus()
|
||||||
|
{
|
||||||
|
for( final IAEItemStack i : this )
|
||||||
|
{
|
||||||
|
i.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract Map<AESharedItemStack, IAEItemStack> getRecords();
|
||||||
|
|
||||||
|
private IAEItemStack putItemRecord( final IAEItemStack itemStack )
|
||||||
|
{
|
||||||
|
return this.getRecords().put( ( (AEItemStack) itemStack ).getSharedStack(), itemStack );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Applied Energistics 2.
|
||||||
|
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
|
||||||
|
*
|
||||||
|
* 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>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package appeng.util.item;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.NavigableMap;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import appeng.api.config.FuzzyMode;
|
||||||
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
|
import appeng.util.item.AESharedItemStack.Bounds;
|
||||||
|
|
||||||
|
|
||||||
|
class FuzzyItemList extends AbstractItemList
|
||||||
|
{
|
||||||
|
private final NavigableMap<AESharedItemStack, IAEItemStack> records = new TreeMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<IAEItemStack> findFuzzy( final IAEItemStack filter, final FuzzyMode fuzzy )
|
||||||
|
{
|
||||||
|
if( filter == null )
|
||||||
|
{
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.findFuzzyDamage( filter, fuzzy );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Map<AESharedItemStack, IAEItemStack> getRecords()
|
||||||
|
{
|
||||||
|
return this.records;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Collection<IAEItemStack> findFuzzyDamage( final IAEItemStack filter, final FuzzyMode fuzzy )
|
||||||
|
{
|
||||||
|
final AEItemStack itemStack = (AEItemStack) filter;
|
||||||
|
final Bounds bounds = itemStack.getSharedStack().getBounds( fuzzy );
|
||||||
|
|
||||||
|
return this.records.subMap( bounds.lower(), true, bounds.upper(), true ).descendingMap().values();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of Applied Energistics 2.
|
* This file is part of Applied Energistics 2.
|
||||||
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
|
||||||
*
|
*
|
||||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -21,41 +21,24 @@ package appeng.util.item;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.IdentityHashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.NavigableMap;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentSkipListMap;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
|
||||||
import appeng.api.config.FuzzyMode;
|
import appeng.api.config.FuzzyMode;
|
||||||
import appeng.api.storage.data.IAEItemStack;
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
import appeng.api.storage.data.IItemList;
|
import appeng.api.storage.data.IItemList;
|
||||||
import appeng.util.item.AESharedItemStack.Bounds;
|
|
||||||
|
|
||||||
|
|
||||||
public final class ItemList implements IItemList<IAEItemStack>
|
public final class ItemList implements IItemList<IAEItemStack>
|
||||||
{
|
{
|
||||||
|
|
||||||
private final NavigableMap<AESharedItemStack, IAEItemStack> records = new ConcurrentSkipListMap<>();
|
private final static IItemList<IAEItemStack> NULL_ITEMLIST = new NullItemList();
|
||||||
|
|
||||||
@Override
|
private final Map<Item, IItemList<IAEItemStack>> records = new IdentityHashMap<>();
|
||||||
public void add( final IAEItemStack option )
|
|
||||||
{
|
|
||||||
if( option == null )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
|
|
||||||
|
|
||||||
if( st != null )
|
|
||||||
{
|
|
||||||
st.add( option );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final IAEItemStack opt = option.copy();
|
|
||||||
|
|
||||||
this.putItemRecord( opt );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IAEItemStack findPrecise( final IAEItemStack itemStack )
|
public IAEItemStack findPrecise( final IAEItemStack itemStack )
|
||||||
@@ -65,7 +48,7 @@ public final class ItemList implements IItemList<IAEItemStack>
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.records.get( ( (AEItemStack) itemStack ).getSharedStack() );
|
return this.getRecord( itemStack.getItem() ).findPrecise( itemStack );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -76,9 +59,7 @@ public final class ItemList implements IItemList<IAEItemStack>
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
final AEItemStack ais = (AEItemStack) filter;
|
return this.getRecord( filter.getItem() ).findFuzzy( filter, fuzzy );
|
||||||
|
|
||||||
return this.findFuzzyDamage( ais, fuzzy, false );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,76 +69,47 @@ public final class ItemList implements IItemList<IAEItemStack>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addStorage( final IAEItemStack option )
|
public void add( final IAEItemStack itemStack )
|
||||||
{
|
{
|
||||||
if( option == null )
|
if( itemStack == null )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
|
this.getOrCreateRecord( itemStack.getItem() ).add( itemStack );
|
||||||
|
|
||||||
if( st != null )
|
|
||||||
{
|
|
||||||
st.incStackSize( option.getStackSize() );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final IAEItemStack opt = option.copy();
|
|
||||||
|
|
||||||
this.putItemRecord( opt );
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* public void clean() { Iterator<StackType> i = iterator(); while (i.hasNext()) { StackType AEI =
|
|
||||||
* i.next(); if ( !AEI.isMeaningful() ) i.remove(); } }
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addCrafting( final IAEItemStack option )
|
|
||||||
{
|
|
||||||
if( option == null )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
|
|
||||||
|
|
||||||
if( st != null )
|
|
||||||
{
|
|
||||||
st.setCraftable( true );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final IAEItemStack opt = option.copy();
|
|
||||||
opt.setStackSize( 0 );
|
|
||||||
opt.setCraftable( true );
|
|
||||||
|
|
||||||
this.putItemRecord( opt );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addRequestable( final IAEItemStack option )
|
public void addStorage( final IAEItemStack itemStack )
|
||||||
{
|
{
|
||||||
if( option == null )
|
if( itemStack == null )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
|
this.getOrCreateRecord( itemStack.getItem() ).addStorage( itemStack );
|
||||||
|
}
|
||||||
|
|
||||||
if( st != null )
|
@Override
|
||||||
|
public void addCrafting( final IAEItemStack itemStack )
|
||||||
|
{
|
||||||
|
if( itemStack == null )
|
||||||
{
|
{
|
||||||
st.setCountRequestable( st.getCountRequestable() + option.getCountRequestable() );
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final IAEItemStack opt = option.copy();
|
this.getOrCreateRecord( itemStack.getItem() ).addCrafting( itemStack );
|
||||||
opt.setStackSize( 0 );
|
}
|
||||||
opt.setCraftable( false );
|
|
||||||
opt.setCountRequestable( option.getCountRequestable() );
|
|
||||||
|
|
||||||
this.putItemRecord( opt );
|
@Override
|
||||||
|
public void addRequestable( final IAEItemStack itemStack )
|
||||||
|
{
|
||||||
|
if( itemStack == null )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.getOrCreateRecord( itemStack.getItem() ).addRequestable( itemStack );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -174,13 +126,19 @@ public final class ItemList implements IItemList<IAEItemStack>
|
|||||||
@Override
|
@Override
|
||||||
public int size()
|
public int size()
|
||||||
{
|
{
|
||||||
return this.records.size();
|
int size = 0;
|
||||||
|
for( IItemList<IAEItemStack> entry : records.values() )
|
||||||
|
{
|
||||||
|
size += entry.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<IAEItemStack> iterator()
|
public Iterator<IAEItemStack> iterator()
|
||||||
{
|
{
|
||||||
return new MeaningfulItemIterator<>( this.records.values().iterator() );
|
return new ChainedIterator( this.records.values().iterator() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -192,16 +150,76 @@ public final class ItemList implements IItemList<IAEItemStack>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IAEItemStack putItemRecord( final IAEItemStack itemStack )
|
private IItemList<IAEItemStack> getRecord( Item item )
|
||||||
{
|
{
|
||||||
return this.records.put( ( (AEItemStack) itemStack ).getSharedStack(), itemStack );
|
return this.records.getOrDefault( item, NULL_ITEMLIST );
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<IAEItemStack> findFuzzyDamage( final IAEItemStack filter, final FuzzyMode fuzzy, final boolean ignoreMeta )
|
private IItemList<IAEItemStack> getOrCreateRecord( Item item )
|
||||||
{
|
{
|
||||||
final AEItemStack itemStack = (AEItemStack) filter;
|
return this.records.computeIfAbsent( item, this::makeRecordMap );
|
||||||
final Bounds bounds = itemStack.getSharedStack().getBounds( fuzzy, ignoreMeta );
|
}
|
||||||
|
|
||||||
|
private IItemList<IAEItemStack> makeRecordMap( Item item )
|
||||||
|
{
|
||||||
|
if( item.isDamageable() )
|
||||||
|
{
|
||||||
|
return new FuzzyItemList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new StrictItemList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ChainedIterator implements Iterator<IAEItemStack>
|
||||||
|
{
|
||||||
|
|
||||||
|
private final Iterator<IItemList<IAEItemStack>> parent;
|
||||||
|
private Iterator<IAEItemStack> next;
|
||||||
|
|
||||||
|
public ChainedIterator( Iterator<IItemList<IAEItemStack>> iterator )
|
||||||
|
{
|
||||||
|
this.parent = iterator;
|
||||||
|
if( this.parent.hasNext() )
|
||||||
|
{
|
||||||
|
this.next = this.parent.next().iterator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext()
|
||||||
|
{
|
||||||
|
while( this.next != null )
|
||||||
|
{
|
||||||
|
if( this.next.hasNext() )
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( this.parent.hasNext() )
|
||||||
|
{
|
||||||
|
this.next = this.parent.next().iterator();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.next = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IAEItemStack next()
|
||||||
|
{
|
||||||
|
if( this.next == null )
|
||||||
|
{
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.next.next();
|
||||||
|
}
|
||||||
|
|
||||||
return this.records.subMap( bounds.lower(), true, bounds.upper(), true ).descendingMap().values();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of Applied Energistics 2.
|
* This file is part of Applied Energistics 2.
|
||||||
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
|
||||||
*
|
*
|
||||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -19,6 +19,8 @@
|
|||||||
package appeng.util.item;
|
package appeng.util.item;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
@@ -28,12 +30,15 @@ import appeng.api.storage.data.IAEItemStack;
|
|||||||
public class MeaningfulItemIterator<T extends IAEItemStack> implements Iterator<T>
|
public class MeaningfulItemIterator<T extends IAEItemStack> implements Iterator<T>
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private final Collection<T> collection;
|
||||||
private final Iterator<T> parent;
|
private final Iterator<T> parent;
|
||||||
private T next;
|
private T next;
|
||||||
|
private final Collection<T> toRemove = new ArrayList<>();
|
||||||
|
|
||||||
public MeaningfulItemIterator( final Iterator<T> iterator )
|
public MeaningfulItemIterator( final Collection<T> collection )
|
||||||
{
|
{
|
||||||
this.parent = iterator;
|
this.collection = collection;
|
||||||
|
this.parent = collection.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -49,10 +54,15 @@ public class MeaningfulItemIterator<T extends IAEItemStack> implements Iterator<
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.parent.remove(); // self cleaning :3
|
// TODO: Avoid if possible
|
||||||
|
this.toRemove.add( this.next );
|
||||||
|
// this.parent.remove(); // self cleaning :3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cleanup afterwards to avoid CMEs
|
||||||
|
this.toRemove.forEach( entry -> this.collection.remove( entry ) );
|
||||||
|
|
||||||
this.next = null;
|
this.next = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Applied Energistics 2.
|
||||||
|
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
|
||||||
|
*
|
||||||
|
* 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>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package appeng.util.item;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import appeng.api.config.FuzzyMode;
|
||||||
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
|
import appeng.api.storage.data.IItemList;
|
||||||
|
|
||||||
|
|
||||||
|
public class NullItemList implements IItemList<IAEItemStack>
|
||||||
|
{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add( IAEItemStack option )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IAEItemStack findPrecise( IAEItemStack i )
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<IAEItemStack> findFuzzy( IAEItemStack input, FuzzyMode fuzzy )
|
||||||
|
{
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addStorage( IAEItemStack option )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCrafting( IAEItemStack option )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addRequestable( IAEItemStack option )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IAEItemStack getFirstItem()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<IAEItemStack> iterator()
|
||||||
|
{
|
||||||
|
return Collections.emptyIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void resetStatus()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Applied Energistics 2.
|
||||||
|
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
|
||||||
|
*
|
||||||
|
* 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>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package appeng.util.item;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.IdentityHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
|
|
||||||
|
|
||||||
|
class StrictItemList extends AbstractItemList
|
||||||
|
{
|
||||||
|
|
||||||
|
private final Map<AESharedItemStack, IAEItemStack> records = new IdentityHashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Map<AESharedItemStack, IAEItemStack> getRecords()
|
||||||
|
{
|
||||||
|
return this.records;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user