AE2 First Commit, the dawn of 1.7 hast arrived.

This commit is contained in:
algorithmx2
2013-12-27 16:59:59 -06:00
commit e9acdcbee2
468 changed files with 47440 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
package appeng.util;
import java.util.HashMap;
import java.util.Set;
import net.minecraft.nbt.NBTTagCompound;
import appeng.api.util.IConfigManager;
public class ConfigManager implements IConfigManager
{
HashMap<Enum, Enum> Settings = new HashMap();
IConfigManagerHost target;
public ConfigManager(IConfigManagerHost tile) {
target = tile;
}
/**
* read all settings using config manager.
*
* @param tagCompound
*/
@SuppressWarnings("static-access")
@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
for (Enum key : Settings.keySet())
{
String value = tagCompound.getString( key.name() );
Enum oldValue = Settings.get( key );
Enum newValue = oldValue.valueOf( oldValue.getClass(), value );
putSetting( key, newValue );
}
}
/**
* save all settings using config manager.
*
* @param tagCompound
*/
@Override
public void writeToNBT(NBTTagCompound tagCompound)
{
for (Enum e : Settings.keySet())
{
tagCompound.setString( e.name(), e.name() );
}
}
@Override
public Set<Enum> getSettings()
{
return Settings.keySet();
}
@Override
public void registerSetting(Enum settingName, Enum defaultValue)
{
Settings.put( settingName, defaultValue );
}
@Override
public Enum getSetting(Enum settingName)
{
Enum oldValue = Settings.get( settingName );
if ( oldValue != null )
return oldValue;
throw new RuntimeException( "Invalid Config setting" );
}
@Override
public Enum putSetting(Enum settingName, Enum newValue)
{
Enum oldValue = getSetting( settingName );
Settings.put( settingName, newValue );
target.updateSetting( settingName, newValue );
return oldValue;
}
}
+7
View File
@@ -0,0 +1,7 @@
package appeng.util;
public interface IConfigManagerHost {
void updateSetting(Enum settingName, Enum newValue);
}
+56
View File
@@ -0,0 +1,56 @@
package appeng.util;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
public class InWorldToolOperationResult
{
public ItemStack BlockItem;
public List<ItemStack> Drops;
public static InWorldToolOperationResult getBlockOperationResult(ItemStack[] items)
{
List<ItemStack> temp = new ArrayList<ItemStack>();
ItemStack b = null;
for (ItemStack l : items)
{
if ( b == null )
{
if ( Block.blocksList.length > l.itemID )
{
Block bl = Block.blocksList[l.itemID];
if ( bl.blockID > 0 )
{
b = l;
continue;
}
}
}
temp.add( l );
}
return new InWorldToolOperationResult( b, temp );
}
public InWorldToolOperationResult() {
BlockItem = null;
Drops = null;
}
public InWorldToolOperationResult(ItemStack block, List<ItemStack> drops) {
BlockItem = block;
Drops = drops;
}
public InWorldToolOperationResult(ItemStack block) {
BlockItem = block;
Drops = null;
}
}
+98
View File
@@ -0,0 +1,98 @@
package appeng.util;
import java.util.ArrayList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraftforge.common.ForgeDirection;
import appeng.api.config.FuzzyMode;
import appeng.integration.modules.BS;
import appeng.util.inv.AdaptorIInventory;
import appeng.util.inv.AdaptorISpecialInventory;
import appeng.util.inv.AdaptorList;
import appeng.util.inv.AdaptorPlayerInventory;
import appeng.util.inv.IInventoryDestination;
import appeng.util.inv.ItemSlot;
import appeng.util.inv.WrapperMCISidedInventory;
import buildcraft.api.inventory.ISpecialInventory;
import cpw.mods.fml.common.network.Player;
public abstract class InventoryAdaptor implements Iterable<ItemSlot>
{
// return what was extracted.
public abstract ItemStack removeItems(int how_many, ItemStack Filter, IInventoryDestination destination);
public abstract ItemStack simulateRemove(int how_many, ItemStack Filter, IInventoryDestination destination);
// return what was extracted.
public abstract ItemStack removeSimilarItems(int amount, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination);
public abstract ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination);
// return what isn't used...
public abstract ItemStack addItems(ItemStack A);
public abstract ItemStack simulateAdd(ItemStack A);
public abstract boolean containsItems();
// returns an appropriate adaptor, or null
public static InventoryAdaptor getAdaptor(Object te, ForgeDirection d)
{
if ( te == null )
return null;
if ( te instanceof Player )
{
return new AdaptorIInventory( new AdaptorPlayerInventory( ((EntityPlayer) te).inventory ) );
}
else if ( te instanceof ArrayList )
{
return new AdaptorList( (ArrayList) te );
}
else if ( BS.instance != null && BS.instance.isStorageCrate( te ) )
{
return BS.instance.getAdaptor( te, d );
}
else if ( te instanceof TileEntityChest )
{
return new AdaptorIInventory( (IInventory) Platform.GetChestInv( te ) );
}
else if ( isSpecialInventory( te ) )
{
return new AdaptorISpecialInventory( (ISpecialInventory) te, d );
}
else if ( te instanceof ISidedInventory )
{
return new AdaptorIInventory( new WrapperMCISidedInventory( (ISidedInventory) te, d ) );
}
else if ( te instanceof IInventory )
{
return new AdaptorIInventory( (IInventory) te );
}
return null;
}
private static boolean canBeSpecial = true;
private static boolean isSpecialInventory(Object a)
{
if ( canBeSpecial )
{
try
{
return a instanceof ISpecialInventory;
}
catch (Throwable e)
{
canBeSpecial = false;
}
}
return false;
}
}
+84
View File
@@ -0,0 +1,84 @@
package appeng.util;
import java.util.Comparator;
import appeng.api.config.SortDir;
import appeng.api.storage.data.IAEItemStack;
public class ItemSorters
{
public static SortDir Direction = SortDir.ASCENDING;
public static int compareInt(int a, int b)
{
if ( a == b )
return 0;
if ( a < b )
return -1;
return 1;
}
public static int compareLong(long a, long b)
{
if ( a == b )
return 0;
if ( a < b )
return -1;
return 1;
}
public static Comparator<IAEItemStack> ConfigBased_SortByName = new Comparator<IAEItemStack>() {
@Override
public int compare(IAEItemStack o1, IAEItemStack o2)
{
if ( Direction == SortDir.ASCENDING )
return Platform.getItemDisplayName( o1 ).toLowerCase().compareTo( Platform.getItemDisplayName( o2 ).toLowerCase() );
return Platform.getItemDisplayName( o2 ).toLowerCase().compareTo( Platform.getItemDisplayName( o1 ).toLowerCase() );
}
};
public static Comparator<IAEItemStack> ConfigBased_SortByID = new Comparator<IAEItemStack>() {
@Override
public int compare(IAEItemStack o1, IAEItemStack o2)
{
if ( Direction == SortDir.ASCENDING )
{
int comp = compareInt( o1.getItem().itemID, o2.getItem().itemID );
if ( comp == 0 )
comp = compareInt( o1.getItemDamage(), o2.getItemDamage() );
return comp;
}
int comp = -compareInt( o1.getItem().itemID, o2.getItem().itemID );
if ( comp == 0 )
comp = -compareInt( o1.getItemDamage(), o2.getItemDamage() );
return comp;
}
};
public static Comparator<IAEItemStack> ConfigBased_SortByMod = new Comparator<IAEItemStack>() {
@Override
public int compare(IAEItemStack o1, IAEItemStack o2)
{
if ( Direction == SortDir.ASCENDING )
return Platform.getMod( o1.getItemStack() ).compareTo( Platform.getMod( o2.getItemStack() ) );
return Platform.getMod( o2.getItemStack() ).compareTo( Platform.getMod( o1.getItemStack() ) );
}
};
public static Comparator<IAEItemStack> ConfigBased_SortBySize = new Comparator<IAEItemStack>() {
@Override
public int compare(IAEItemStack o1, IAEItemStack o2)
{
if ( Direction == SortDir.ASCENDING )
return compareLong( o2.getStackSize(), o1.getStackSize() );
return compareLong( o1.getStackSize(), o2.getStackSize() );
}
};
}
+15
View File
@@ -0,0 +1,15 @@
package appeng.util;
import net.minecraft.util.Vec3;
public class LookDirection
{
public final Vec3 a;
public final Vec3 b;
public LookDirection(Vec3 a, Vec3 b) {
this.a = a;
this.b = b;
}
}
+1250
View File
File diff suppressed because it is too large Load Diff
+41
View File
@@ -0,0 +1,41 @@
package appeng.util;
import java.util.Collection;
import java.util.Iterator;
import appeng.api.util.IReadOnlyCollection;
public class ReadOnlyCollection<T> implements IReadOnlyCollection<T>
{
private final Collection<T> c;
public ReadOnlyCollection(Collection<T> in) {
c = in;
}
@Override
public Iterator iterator()
{
return c.iterator();
}
@Override
public int size()
{
return c.size();
}
@Override
public boolean isEmpty()
{
return c.isEmpty();
}
@Override
public boolean contains(Object node)
{
return c.contains( node );
}
}
+10
View File
@@ -0,0 +1,10 @@
package appeng.util;
public enum SettingsFrom
{
// moved the item, and replaced it.
DISMANTLE_ITEM,
// used memory card?
MEMORY_CARD
}
+195
View File
@@ -0,0 +1,195 @@
package appeng.util;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class SortedList<T> implements Iterable<T>, List<T>, Cloneable {
private boolean sorted = true;
private final Comparator<T> comp;
private final LinkedList<T> storage = new LinkedList<T>();
private void makeSorted() {
if (!sorted) {
sorted = true;
Collections.sort(storage, comp);
}
}
public SortedList(Comparator<T> comp) {
this.comp = comp;
}
@Override
public boolean add(T input) {
sorted = false;
return storage.add(input);
}
@Override
public boolean addAll(Collection<? extends T> input) {
if (!input.isEmpty())
sorted = false;
return storage.addAll(input);
}
@Override
public void clear() {
sorted = true;
storage.clear();
}
@Override
public boolean contains(Object input) {
return storage.contains(input);
}
@Override
public boolean containsAll(Collection<?> input) {
return storage.containsAll(input);
}
@Override
public boolean isEmpty() {
return isEmpty();
}
@Override
public Iterator<T> iterator() {
makeSorted();
return storage.iterator();
}
public Iterator<T> reverseIterator() {
makeSorted();
final ListIterator<T> listIterator = listIterator(size());
return new Iterator<T>() {
public boolean hasNext() {
return listIterator.hasPrevious();
}
public T next() {
return listIterator.previous();
}
public void remove() {
listIterator.remove();
}
};
}
@Override
public boolean remove(Object input) {
return storage.remove(input);
}
@Override
public boolean removeAll(Collection<?> input) {
return storage.removeAll(input);
}
@Override
public boolean retainAll(Collection<?> input) {
return storage.retainAll(input);
}
@Override
public int size() {
return storage.size();
}
@Override
public Object[] toArray() {
return storage.toArray();
}
@Override
public <X> X[] toArray(X[] input) {
return storage.toArray(input);
}
public Comparator<? super T> comparator() {
return comp;
}
public T first() {
makeSorted();
return storage.peekFirst();
}
public T last() {
makeSorted();
return storage.peekLast();
}
@Override
public void add(int index, T element) {
makeSorted();
sorted = false;
add(index, element);
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
sorted = false;
return addAll(index, c);
}
@Override
public T get(int index) {
makeSorted();
return get(index);
}
@Override
public int indexOf(Object o) {
makeSorted();
return indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
makeSorted();
return lastIndexOf(o);
}
@Override
public ListIterator<T> listIterator() {
makeSorted();
return listIterator();
}
@Override
public ListIterator<T> listIterator(int index) {
makeSorted();
return listIterator(index);
}
@Override
public T remove(int index) {
makeSorted();
return remove(index);
}
@Override
public T set(int index, T element) {
makeSorted();
sorted = false;
return set(index, element);
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
makeSorted();
return storage.subList(fromIndex, toIndex);
}
}
+89
View File
@@ -0,0 +1,89 @@
package appeng.util.inv;
import java.util.Iterator;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import appeng.api.config.FuzzyMode;
import appeng.integration.modules.BC;
import appeng.util.InventoryAdaptor;
import appeng.util.iterators.NullIterator;
public class AdaptorBCPipe extends InventoryAdaptor
{
private TileEntity i;
private ForgeDirection d;
public AdaptorBCPipe(TileEntity s, ForgeDirection dd) {
if ( BC.instance != null )
{
if ( BC.instance.isPipe( s, dd ) )
{
i = s;
d = dd;
}
}
}
@Override
public ItemStack removeSimilarItems(int amount, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination)
{
return null;
}
@Override
public ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination)
{
return null;
}
@Override
public ItemStack removeItems(int how_many, ItemStack filter, IInventoryDestination destination)
{
return null;
}
@Override
public ItemStack simulateRemove(int how_many, ItemStack filter, IInventoryDestination destination)
{
return null;
}
@Override
public ItemStack addItems(ItemStack A)
{
if ( i == null )
return A;
if ( A == null )
return null;
if ( A.stackSize == 0 )
return null;
if ( BC.instance.addItemsToPipe( i, A, d ) )
return null;
return A;
}
@Override
public ItemStack simulateAdd(ItemStack A)
{
if ( i == null )
return A;
return null;
}
@Override
public boolean containsItems()
{
return false;
}
@Override
public Iterator<ItemSlot> iterator()
{
return new NullIterator();
}
}
+353
View File
@@ -0,0 +1,353 @@
package appeng.util.inv;
import java.util.Iterator;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import appeng.api.config.FuzzyMode;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
public class AdaptorIInventory extends InventoryAdaptor
{
private IInventory i;
private boolean wrapperEnabled;
public AdaptorIInventory(IInventory s) {
i = s;
wrapperEnabled = s instanceof IInventoryWrapper;
}
boolean canRemoveStackFromSlot(int x, ItemStack is)
{
if ( wrapperEnabled )
return ((IInventoryWrapper) i).canRemoveItemFromSlot( x, is );
return true;
}
@Override
public boolean containsItems()
{
int s = i.getSizeInventory();
for (int x = 0; x < s; x++)
{
if ( i.getStackInSlot( x ) != null )
return true;
}
return false;
}
@Override
public ItemStack removeSimilarItems(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination)
{
int s = i.getSizeInventory();
for (int x = 0; x < s; x++)
{
ItemStack is = i.getStackInSlot( x );
if ( is != null && canRemoveStackFromSlot( x, is ) && (filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode )) )
{
int lhow_many = how_many;
if ( lhow_many > is.stackSize )
lhow_many = is.stackSize;
if ( destination != null && !destination.canInsert( is ) )
lhow_many = 0;
ItemStack rv = null;
if ( lhow_many > 0 )
{
rv = is.copy();
rv.stackSize = lhow_many;
if ( is.stackSize == rv.stackSize )
i.setInventorySlotContents( x, null );
else
{
ItemStack po = is.copy();
po.stackSize -= rv.stackSize;
i.setInventorySlotContents( x, po );
}
}
if ( rv != null )
{
i.onInventoryChanged();
return rv;
}
}
}
return null;
}
@Override
public ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination)
{
int s = i.getSizeInventory();
for (int x = 0; x < s; x++)
{
ItemStack is = i.getStackInSlot( x );
if ( is != null && canRemoveStackFromSlot( x, is ) && (filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode )) )
{
int lhow_many = how_many;
if ( lhow_many > is.stackSize )
lhow_many = is.stackSize;
if ( destination != null && !destination.canInsert( is ) )
lhow_many = 0;
if ( lhow_many > 0 )
{
ItemStack rv = is.copy();
rv.stackSize = lhow_many;
return rv;
}
}
}
return null;
}
@Override
public ItemStack removeItems(int how_many, ItemStack filter, IInventoryDestination destination)
{
int s = i.getSizeInventory();
ItemStack rv = null;
for (int x = 0; x < s && how_many > 0; x++)
{
ItemStack is = i.getStackInSlot( x );
if ( is != null && canRemoveStackFromSlot( x, is ) && (filter == null || Platform.isSameItemPrecise( is, filter )) )
{
int lhow_many = how_many;
if ( lhow_many > is.stackSize )
lhow_many = is.stackSize;
if ( destination != null && !destination.canInsert( is ) )
lhow_many = 0;
if ( lhow_many > 0 )
{
if ( rv == null )
{
rv = is.copy();
rv.stackSize = lhow_many;
how_many -= lhow_many;
}
else
{
rv.stackSize += lhow_many;
how_many -= lhow_many;
}
if ( is.stackSize == lhow_many )
i.setInventorySlotContents( x, null );
else
{
ItemStack po = is.copy();
po.stackSize -= lhow_many;
i.setInventorySlotContents( x, po );
}
}
}
}
if ( rv != null )
i.onInventoryChanged();
return rv;
}
@Override
public ItemStack simulateRemove(int how_many, ItemStack filter, IInventoryDestination destination)
{
int s = i.getSizeInventory();
ItemStack rv = null;
for (int x = 0; x < s && how_many > 0; x++)
{
ItemStack is = i.getStackInSlot( x );
if ( is != null && canRemoveStackFromSlot( x, is ) && (filter == null || Platform.isSameItemPrecise( is, filter )) )
{
int lhow_many = how_many;
if ( lhow_many > is.stackSize )
lhow_many = is.stackSize;
if ( destination != null && !destination.canInsert( is ) )
lhow_many = 0;
if ( lhow_many > 0 )
{
if ( rv == null )
{
rv = is.copy();
rv.stackSize = lhow_many;
how_many -= lhow_many;
}
else
{
rv.stackSize += lhow_many;
how_many -= lhow_many;
}
}
}
}
return rv;
}
@Override
public ItemStack addItems(ItemStack A)
{
if ( A == null )
return null;
if ( A.stackSize == 0 )
return null;
ItemStack left = A.copy();
int stack_limit = A.getMaxStackSize();
if ( stack_limit > i.getInventoryStackLimit() )
stack_limit = i.getInventoryStackLimit();
int s = i.getSizeInventory();
for (int pass = 0; pass < 2; pass++)
{
for (int x = 0; x < s; x++)
{
if ( i.isItemValidForSlot( x, A ) )
{
ItemStack is = i.getStackInSlot( x );
if ( is == null && pass != 0 )
{
ItemStack thisSlot = left.copy();
if ( thisSlot.stackSize > stack_limit )
thisSlot.stackSize = stack_limit;
left.stackSize -= thisSlot.stackSize;
i.setInventorySlotContents( x, thisSlot );
if ( left.stackSize <= 0 )
{
i.onInventoryChanged();
return null;
}
}
else if ( is != null )
{
if ( Platform.isSameItem( is, left ) )
{
if ( is.stackSize < stack_limit )
{
int room = stack_limit - is.stackSize;
int used = left.stackSize;
if ( used > room )
used = room;
is.stackSize += used;
i.setInventorySlotContents( x, is );
left.stackSize -= used;
if ( left.stackSize <= 0 )
{
i.onInventoryChanged();
return null;
}
}
}
}
}
}
}
if ( left.stackSize != A.stackSize )
i.onInventoryChanged();
return left;
}
@Override
public ItemStack simulateAdd(ItemStack A)
{
if ( A == null )
return A;
ItemStack left = A.copy();
int stack_limit = A.getMaxStackSize();
if ( stack_limit > i.getInventoryStackLimit() )
stack_limit = i.getInventoryStackLimit();
int s = i.getSizeInventory();
for (int x = 0; x < s; x++)
{
if ( i.isItemValidForSlot( x, A ) )
{
ItemStack is = i.getStackInSlot( x );
if ( is == null )
{
ItemStack thisSlot = left.copy();
if ( thisSlot.stackSize > stack_limit )
thisSlot.stackSize = stack_limit;
left.stackSize -= thisSlot.stackSize;
if ( left.stackSize <= 0 )
{
return null;
}
}
else
{
if ( Platform.isSameItem( is, left ) )
{
if ( is.stackSize < stack_limit )
{
int room = stack_limit - is.stackSize;
int used = left.stackSize;
if ( used > room )
used = room;
left.stackSize -= used;
if ( left.stackSize <= 0 )
{
return null;
}
}
}
}
}
}
return left;
}
class InvIterator implements Iterator<ItemSlot>
{
final ItemSlot is = new ItemSlot();
int x = 0;
@Override
public boolean hasNext()
{
return x < i.getSizeInventory();
}
@Override
public ItemSlot next()
{
is.slot = x;
is.itemStack = i.getStackInSlot( x++ );
return is;
}
@Override
public void remove()
{
// nothing!
}
};
@Override
public Iterator<ItemSlot> iterator()
{
return new InvIterator();
}
}
+112
View File
@@ -0,0 +1,112 @@
package appeng.util.inv;
import java.util.Iterator;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
import appeng.api.config.FuzzyMode;
import appeng.util.InventoryAdaptor;
import buildcraft.api.inventory.ISpecialInventory;
public class AdaptorISpecialInventory extends InventoryAdaptor
{
private AdaptorIInventory remover;
private ISpecialInventory i;
private ForgeDirection d;
public AdaptorISpecialInventory(ISpecialInventory s, ForgeDirection dd) {
i = s;
d = dd;
if ( s instanceof ISidedInventory )
remover = new AdaptorIInventory( new WrapperMCISidedInventory( (ISidedInventory) s, d ) );
else
remover = new AdaptorIInventory( s );
}
@Override
public ItemStack removeSimilarItems(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination)
{
return remover.removeSimilarItems( how_many, filter, fuzzyMode, destination );
}
@Override
public ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination)
{
return remover.simulateSimilarRemove( how_many, filter, fuzzyMode, destination );
}
@Override
public ItemStack removeItems(int how_many, ItemStack filter, IInventoryDestination destination)
{
return remover.removeItems( how_many, filter, destination );
}
@Override
public ItemStack simulateRemove(int how_many, ItemStack filter, IInventoryDestination destination)
{
return remover.simulateRemove( how_many, filter, destination );
}
@Override
public ItemStack addItems(ItemStack A)
{
if ( A == null )
return null;
if ( A.stackSize == 0 )
return null;
int used = i.addItem( A, true, d );
ItemStack out = A.copy();
out.stackSize -= used;
if ( out.stackSize > 0 )
return out;
return null;
}
@Override
public ItemStack simulateAdd(ItemStack A)
{
int used = i.addItem( A, false, d );
ItemStack out = A.copy();
out.stackSize -= used;
if ( out.stackSize > 0 )
return out;
return null;
}
@Override
public boolean containsItems()
{
if ( i instanceof ISidedInventory )
{
ISidedInventory sided = (ISidedInventory) i;
int slots[] = sided.getAccessibleSlotsFromSide( d.ordinal() );
if ( slots == null )
return false;
int s = slots.length;
for (int x = 0; x < s; x++)
if ( i.getStackInSlot( slots[x] ) != null )
return true;
return false;
}
int s = i.getSizeInventory();
for (int x = 0; x < s; x++)
if ( i.getStackInSlot( x ) != null )
return true;
return false;
}
@Override
public Iterator<ItemSlot> iterator()
{
return remover.iterator();
}
}
+186
View File
@@ -0,0 +1,186 @@
package appeng.util.inv;
import java.util.Iterator;
import java.util.List;
import net.minecraft.item.ItemStack;
import appeng.api.config.FuzzyMode;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.iterators.StackToSlotIterator;
public class AdaptorList extends InventoryAdaptor
{
private List<ItemStack> i;
public AdaptorList(List<ItemStack> s) {
i = s;
}
@Override
public ItemStack removeSimilarItems(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination dest)
{
int s = i.size();
for (int x = 0; x < s; x++)
{
ItemStack is = i.get( x );
if ( is != null && (filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode )) )
{
if ( how_many > is.stackSize )
how_many = is.stackSize;
if ( dest != null && !dest.canInsert( is ) )
how_many = 0;
if ( how_many > 0 )
{
ItemStack rv = is.copy();
rv.stackSize = how_many;
is.stackSize -= how_many;
// remove it..
if ( is.stackSize <= 0 )
i.remove( x );
return rv;
}
}
}
return null;
}
@Override
public ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination dest)
{
int s = i.size();
for (int x = 0; x < s; x++)
{
ItemStack is = i.get( x );
if ( is != null && (filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode )) )
{
if ( how_many > is.stackSize )
how_many = is.stackSize;
if ( dest != null && !dest.canInsert( is ) )
how_many = 0;
if ( how_many > 0 )
{
ItemStack rv = is.copy();
rv.stackSize = how_many;
return rv;
}
}
}
return null;
}
@Override
public ItemStack removeItems(int how_many, ItemStack filter, IInventoryDestination dest)
{
int s = i.size();
for (int x = 0; x < s; x++)
{
ItemStack is = i.get( x );
if ( is != null && (filter == null || Platform.isSameItemPrecise( is, filter )) )
{
if ( how_many > is.stackSize )
how_many = is.stackSize;
if ( dest != null && !dest.canInsert( is ) )
how_many = 0;
if ( how_many > 0 )
{
ItemStack rv = is.copy();
rv.stackSize = how_many;
is.stackSize -= how_many;
// remove it..
if ( is.stackSize <= 0 )
i.remove( x );
return rv;
}
}
}
return null;
}
@Override
public ItemStack simulateRemove(int how_many, ItemStack filter, IInventoryDestination dest)
{
int s = i.size();
for (int x = 0; x < s; x++)
{
ItemStack is = i.get( x );
if ( is != null && (filter == null || Platform.isSameItemPrecise( is, filter )) )
{
if ( how_many > is.stackSize )
how_many = is.stackSize;
if ( dest != null && !dest.canInsert( is ) )
how_many = 0;
if ( how_many > 0 )
{
ItemStack rv = is.copy();
rv.stackSize = how_many;
return rv;
}
}
}
return null;
}
@Override
public ItemStack addItems(ItemStack A)
{
if ( A == null )
return null;
if ( A.stackSize == 0 )
return null;
ItemStack left = A.copy();
int s = i.size();
for (int x = 0; x < s; x++)
{
ItemStack is = i.get( x );
if ( Platform.isSameItem( is, left ) )
{
is.stackSize += left.stackSize;
return null;
}
}
i.add( left );
return null;
}
@Override
public ItemStack simulateAdd(ItemStack A)
{
return null;
}
@Override
public boolean containsItems()
{
int s = i.size();
for (int x = 0; x < s; x++)
{
ItemStack is = i.get( x );
if ( is != null )
return true;
}
return false;
}
@Override
public Iterator<ItemSlot> iterator()
{
return new StackToSlotIterator( i.iterator() );
}
}
+185
View File
@@ -0,0 +1,185 @@
package appeng.util.inv;
import java.util.Iterator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import appeng.api.config.FuzzyMode;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.iterators.NullIterator;
/*
* Lets you do simply tests with the players cursor, without messing with the specifics.
*/
public class AdaptorPlayerHand extends InventoryAdaptor
{
private EntityPlayer p;
public AdaptorPlayerHand(EntityPlayer _p) {
p = _p;
}
@Override
public ItemStack removeSimilarItems(int how_many, ItemStack Filter, FuzzyMode fuzzyMode, IInventoryDestination dest)
{
ItemStack hand = p.inventory.getItemStack();
if ( hand == null )
return null;
if ( Filter == null || Platform.isSameItemFuzzy( Filter, hand, fuzzyMode ) )
{
ItemStack result = hand.copy();
result.stackSize = hand.stackSize > how_many ? how_many : hand.stackSize;
hand.stackSize -= how_many;
if ( hand.stackSize <= 0 )
p.inventory.setItemStack( null );
return result;
}
return null;
}
@Override
public ItemStack simulateSimilarRemove(int how_many, ItemStack Filter, FuzzyMode fuzzyMode, IInventoryDestination dest)
{
ItemStack hand = p.inventory.getItemStack();
if ( hand == null )
return null;
if ( Filter == null || Platform.isSameItemFuzzy( Filter, hand, fuzzyMode ) )
{
ItemStack result = hand.copy();
result.stackSize = hand.stackSize > how_many ? how_many : hand.stackSize;
return result;
}
return null;
}
@Override
public ItemStack removeItems(int how_many, ItemStack Filter, IInventoryDestination dest)
{
ItemStack hand = p.inventory.getItemStack();
if ( hand == null )
return null;
if ( Filter == null || Platform.isSameItemPrecise( Filter, hand ) )
{
ItemStack result = hand.copy();
result.stackSize = hand.stackSize > how_many ? how_many : hand.stackSize;
hand.stackSize -= how_many;
if ( hand.stackSize <= 0 )
p.inventory.setItemStack( null );
return result;
}
return null;
}
@Override
public ItemStack simulateRemove(int how_many, ItemStack Filter, IInventoryDestination dest)
{
ItemStack hand = p.inventory.getItemStack();
if ( hand == null )
return null;
if ( Filter == null || Platform.isSameItemPrecise( Filter, hand ) )
{
ItemStack result = hand.copy();
result.stackSize = hand.stackSize > how_many ? how_many : hand.stackSize;
return result;
}
return null;
}
@Override
public ItemStack addItems(ItemStack A)
{
if ( A == null )
return null;
if ( A.stackSize == 0 )
return null;
if ( p == null )
return A;
if ( p.inventory == null )
return A;
ItemStack hand = p.inventory.getItemStack();
if ( hand != null && !Platform.isSameItem( A, hand ) )
return A;
int original = 0;
ItemStack newHand = null;
if ( hand == null )
newHand = A.copy();
else
{
newHand = hand;
original = hand.stackSize;
newHand.stackSize += A.stackSize;
}
if ( newHand.stackSize > newHand.getMaxStackSize() )
{
newHand.stackSize = newHand.getMaxStackSize();
ItemStack B = A.copy();
B.stackSize -= newHand.stackSize - original;
p.inventory.setItemStack( newHand );
return B;
}
p.inventory.setItemStack( newHand );
return null;
}
@Override
public ItemStack simulateAdd(ItemStack A)
{
ItemStack hand = p.inventory.getItemStack();
if ( A == null )
return null;
if ( hand != null && !Platform.isSameItem( A, hand ) )
return A;
int original = 0;
ItemStack newHand = null;
if ( hand == null )
newHand = A.copy();
else
{
newHand = hand.copy();
original = hand.stackSize;
newHand.stackSize += A.stackSize;
}
if ( newHand.stackSize > newHand.getMaxStackSize() )
{
newHand.stackSize = newHand.getMaxStackSize();
ItemStack B = A.copy();
B.stackSize -= newHand.stackSize - original;
return B;
}
return null;
}
@Override
public boolean containsItems()
{
return p.inventory.getItemStack() != null;
}
@Override
public Iterator<ItemSlot> iterator()
{
return new NullIterator();
}
}
+99
View File
@@ -0,0 +1,99 @@
package appeng.util.inv;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
public class AdaptorPlayerInventory implements IInventory
{
private InventoryPlayer src;
private int min;
private int size;
public AdaptorPlayerInventory(InventoryPlayer a) {
src = a;
min = 0; // a.getStartInventorySide( d );
size = 36; // a.getSizeInventorySide( d );
}
@Override
public int getSizeInventory()
{
return size;
}
@Override
public ItemStack getStackInSlot(int var1)
{
return src.getStackInSlot( var1 + min );
}
@Override
public ItemStack decrStackSize(int var1, int var2)
{
return src.decrStackSize( min + var1, var2 );
}
@Override
public ItemStack getStackInSlotOnClosing(int var1)
{
return src.getStackInSlotOnClosing( min + var1 );
}
@Override
public void setInventorySlotContents(int var1, ItemStack var2)
{
src.setInventorySlotContents( var1 + min, var2 );
}
@Override
public String getInvName()
{
return src.getInvName();
}
@Override
public int getInventoryStackLimit()
{
return src.getInventoryStackLimit();
}
@Override
public void onInventoryChanged()
{
src.onInventoryChanged();
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1)
{
return src.isUseableByPlayer( var1 );
}
@Override
public void openChest()
{
src.openChest();
}
@Override
public void closeChest()
{
src.closeChest();
}
@Override
public boolean isInvNameLocalized()
{
return false;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return true;
}
}
+10
View File
@@ -0,0 +1,10 @@
package appeng.util.inv;
import net.minecraft.item.ItemStack;
public interface IInventoryDestination
{
public boolean canInsert(ItemStack stack);
}
+10
View File
@@ -0,0 +1,10 @@
package appeng.util.inv;
import net.minecraft.item.ItemStack;
public interface IInventoryWrapper
{
boolean canRemoveItemFromSlot(int x, ItemStack is);
}
+32
View File
@@ -0,0 +1,32 @@
package appeng.util.inv;
import net.minecraft.item.ItemStack;
import appeng.api.config.Actionable;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AEItemStack;
public class IMEInventoryDestination implements IInventoryDestination
{
IMEInventory<IAEItemStack> me;
public IMEInventoryDestination(IMEInventory<IAEItemStack> o) {
me = o;
}
@Override
public boolean canInsert(ItemStack stack)
{
if ( stack == null )
return false;
IAEItemStack failed = me.injectItems( AEItemStack.create( stack ), Actionable.SIMULATE );
if ( failed == null )
return true;
return failed.getStackSize() != stack.stackSize;
}
}
+11
View File
@@ -0,0 +1,11 @@
package appeng.util.inv;
import net.minecraft.item.ItemStack;
public class ItemSlot
{
public int slot;
public ItemStack itemStack;
}
+198
View File
@@ -0,0 +1,198 @@
package appeng.util.inv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
public class WrapperChainedInventory implements IInventory
{
class InvOffset
{
int offset;
int size;
IInventory i;
};
int fullSize = 0;
private List<IInventory> l;
private HashMap<Integer, InvOffset> offsets;
public WrapperChainedInventory(IInventory ilist) {
setInventory( ilist );
}
public WrapperChainedInventory(List<IInventory> ilist) {
setInventory( ilist );
}
public void calculateSizes()
{
offsets = new HashMap<Integer, WrapperChainedInventory.InvOffset>();
int offset = 0;
for (IInventory in : l)
{
InvOffset io = new InvOffset();
io.offset = offset;
io.size = in.getSizeInventory();
io.i = in;
for (int y = 0; y < io.size; y++)
{
offsets.put( y + io.offset, io );
}
offset += io.size;
}
fullSize = offset;
}
public void setInventory(IInventory a)
{
l = new ArrayList();
l.add( a );
calculateSizes();
}
public void setInventory(List<IInventory> a)
{
l = a;
calculateSizes();
}
public IInventory getInv(int idx)
{
InvOffset io = offsets.get( idx );
if ( io != null )
{
return io.i;
}
return null;
}
public int getInvSlot(int idx)
{
InvOffset io = offsets.get( idx );
if ( io != null )
{
return idx - io.offset;
}
return 0;
}
@Override
public int getSizeInventory()
{
return fullSize;
}
@Override
public ItemStack getStackInSlot(int idx)
{
InvOffset io = offsets.get( idx );
if ( io != null )
{
return io.i.getStackInSlot( idx - io.offset );
}
return null;
}
@Override
public ItemStack decrStackSize(int idx, int var2)
{
InvOffset io = offsets.get( idx );
if ( io != null )
{
return io.i.decrStackSize( idx - io.offset, var2 );
}
return null;
}
@Override
public ItemStack getStackInSlotOnClosing(int idx)
{
InvOffset io = offsets.get( idx );
if ( io != null )
{
return io.i.getStackInSlotOnClosing( idx - io.offset );
}
return null;
}
@Override
public void setInventorySlotContents(int idx, ItemStack var2)
{
InvOffset io = offsets.get( idx );
if ( io != null )
{
io.i.setInventorySlotContents( idx - io.offset, var2 );
}
}
@Override
public String getInvName()
{
return "ChainedInv";
}
@Override
public int getInventoryStackLimit()
{
int smallest = 64;
for (IInventory i : l)
smallest = Math.min( smallest, i.getInventoryStackLimit() );
return smallest;
}
@Override
public void onInventoryChanged()
{
for (IInventory i : l)
{
i.onInventoryChanged();
}
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1)
{
return false;
}
@Override
public void openChest()
{
}
@Override
public void closeChest()
{
}
@Override
public boolean isInvNameLocalized()
{
return false;
}
@Override
public boolean isItemValidForSlot(int idx, ItemStack itemstack)
{
InvOffset io = offsets.get( idx );
if ( io != null )
{
return io.i.isItemValidForSlot( idx - io.offset, itemstack );
}
return false;
}
}
+128
View File
@@ -0,0 +1,128 @@
package appeng.util.inv;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
public class WrapperInventoryRange implements IInventory
{
private IInventory src;
int[] slots;
protected boolean ignoreValidItems = false;
public static String concateLines(int[] s, String separator)
{
if ( s.length > 0 )
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length; i++)
{
if ( sb.length() > 0 )
sb.append( separator );
sb.append( s[i] );
}
return sb.toString();
}
return "";
}
public WrapperInventoryRange(IInventory a, int[] s, boolean ignoreValid) {
src = a;
slots = s;
if ( slots == null )
slots = new int[0];
ignoreValidItems = ignoreValid;
}
public WrapperInventoryRange(IInventory a, int _min, int _size, boolean ignoreValid) {
src = a;
slots = new int[_size];
for (int x = 0; x < _size; x++)
slots[x] = _min + x;
ignoreValidItems = ignoreValid;
}
@Override
public int getSizeInventory()
{
return slots.length;
}
@Override
public ItemStack getStackInSlot(int var1)
{
return src.getStackInSlot( slots[var1] );
}
@Override
public ItemStack decrStackSize(int var1, int var2)
{
return src.decrStackSize( slots[var1], var2 );
}
@Override
public ItemStack getStackInSlotOnClosing(int var1)
{
return src.getStackInSlotOnClosing( slots[var1] );
}
@Override
public void setInventorySlotContents(int var1, ItemStack var2)
{
src.setInventorySlotContents( slots[var1], var2 );
}
@Override
public String getInvName()
{
return src.getInvName();
}
@Override
public int getInventoryStackLimit()
{
return src.getInventoryStackLimit();
}
@Override
public void onInventoryChanged()
{
src.onInventoryChanged();
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1)
{
return src.isUseableByPlayer( var1 );
}
@Override
public void openChest()
{
src.openChest();
}
@Override
public void closeChest()
{
src.closeChest();
}
@Override
public boolean isInvNameLocalized()
{
return false;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
if ( ignoreValidItems )
return true;
return src.isItemValidForSlot( slots[i], itemstack );
}
}
+50
View File
@@ -0,0 +1,50 @@
package appeng.util.inv;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
public class WrapperMCISidedInventory extends WrapperInventoryRange implements IInventory, IInventoryWrapper
{
private ForgeDirection dir;
ISidedInventory side;
public WrapperMCISidedInventory(ISidedInventory a, ForgeDirection d) {
super( a, a.getAccessibleSlotsFromSide( d.ordinal() ), false );
side = a;
dir = d;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
if ( ignoreValidItems )
return true;
if ( side.isItemValidForSlot( slots[i], itemstack ) )
return side.canInsertItem( slots[i], itemstack, dir.ordinal() );
return false;
}
@Override
public boolean canRemoveItemFromSlot(int i, ItemStack is)
{
if ( is == null )
return false;
return side.canExtractItem( slots[i], is, dir.ordinal() );
}
@Override
public ItemStack decrStackSize(int var1, int var2)
{
if ( canRemoveItemFromSlot( var1, getStackInSlot( var1 ) ) )
return super.decrStackSize( var1, var2 );
return null;
}
}
+99
View File
@@ -0,0 +1,99 @@
package appeng.util.inv;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import appeng.integration.modules.BC;
public class WrapperPipe implements IInventory
{
TileEntity ad;
ForgeDirection dir;
public WrapperPipe(TileEntity te, ForgeDirection d) {
ad = te;
dir = d;
}
@Override
public int getSizeInventory()
{
return 1;
}
@Override
public ItemStack getStackInSlot(int i)
{
return null;
}
@Override
public ItemStack decrStackSize(int i, int j)
{
return null;
}
@Override
public ItemStack getStackInSlotOnClosing(int i)
{
return null;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
BC.instance.addItemsToPipe( ad, itemstack, dir );
}
@Override
public String getInvName()
{
return null;
}
@Override
public boolean isInvNameLocalized()
{
return false;
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public void onInventoryChanged()
{
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return false;
}
@Override
public void openChest()
{
}
@Override
public void closeChest()
{
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return BC.instance.canAddItemsToPipe( ad, itemstack, dir );
}
}
+319
View File
@@ -0,0 +1,319 @@
package appeng.util.item;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
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.NBTBase;
import net.minecraft.nbt.NBTTagByte;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagLong;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAETagCompound;
import appeng.util.Platform;
import com.google.common.io.ByteStreams;
public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFluidStack, Comparable<AEFluidStack>
{
public int myHash;
Fluid fluid;
protected IAETagCompound tagCompound;
@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) {
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();
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)
{
/*
* Ugly Yes, but it saves a lot in the memory department.
*/
NBTBase FluidName = i.getTag( "FluidName" );
NBTBase Count = i.getTag( "Count" );
// NBTBase Priority = i.getTag( "Priority" );
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 ( Priority != null && Priority instanceof NBTTagInt )
// ((NBTTagInt) Priority).data = this.priority;
// else
// i.setInteger( "Priority", this.priority );
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(DataOutputStream i) throws IOException
{
byte[] name = fluid.getName().getBytes( "UTF-8" );
i.writeByte( (byte) name.length );
i.write( name );
}
@Override
void readNBT(DataOutputStream 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.write( tagBytes );
}
}
public static IAEFluidStack loadItemStackFromPacket(DataInputStream 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.read( 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.readFully( bd );
DataInput di = ByteStreams.newDataInput( bd );
d.setCompoundTag( "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;
}
}
+41
View File
@@ -0,0 +1,41 @@
package appeng.util.item;
import java.util.List;
import net.minecraft.item.Item;
import appeng.api.storage.data.IAETagCompound;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class AEItemDef
{
public int myHash;
public int def;
public Item item;
public int damageValue;
public int dspDamage;
public int maxDamage;
public IAETagCompound tagCompound;
@SideOnly(Side.CLIENT)
public String displayName;
@SideOnly(Side.CLIENT)
public List tooltip;
public AEItemDef copy()
{
AEItemDef t = new AEItemDef();
t.def = def;
t.item = item;
t.damageValue = damageValue;
t.dspDamage = dspDamage;
t.maxDamage = maxDamage;
return t;
}
}
+499
View File
@@ -0,0 +1,499 @@
package appeng.util.item;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
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.NBTBase;
import net.minecraft.nbt.NBTTagByte;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagLong;
import net.minecraft.nbt.NBTTagShort;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAETagCompound;
import appeng.util.Platform;
import com.google.common.io.ByteStreams;
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 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();
def.item = is.getItem();
/*
* 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 = Item.blazeRod.getDamage( is );
def.def = is.itemID << Platform.DEF_OFFSET | def.damageValue;
def.dspDamage = is.getItemDamageForDisplay();
def.maxDamage = is.getMaxDamage();
def.tagCompound = (IAETagCompound) AESharedNBT.getSharedTagCompound( is );
stackSize = is.stackSize;
setCraftable( false );
setCountRequestable( 0 );
def.myHash = def.def ^ (def.tagCompound == null ? 0 : System.identityHashCode( def.tagCompound ));
}
public static AEItemStack create(Object a)
{
if ( a == null )
return null;
if ( a instanceof IAEItemStack )
((IAEItemStack) a).copy();
if ( a instanceof ItemStack )
return new AEItemStack( (ItemStack) a );
return null;
}
@Override
public Item getItem()
{
return def.item;
}
@Override
public boolean equals(Object ia)
{
if ( ia instanceof AEItemStack )
{
return ((AEItemStack) ia).def == 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)
{
/*
* Ugly Yes, but it saves a lot in the memory department.
*/
NBTBase id = i.getTag( "id" );
NBTBase Count = i.getTag( "Count" );
// NBTBase Priority = i.getTag( "Priority" );
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) this.def.item.itemID );
// if ( Priority != null && Priority instanceof NBTTagInt )
// ((NBTTagInt) Priority).data = this.priority;
// else
// i.setInteger( "Priority", this.priority );
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)
{
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 = def.item.itemID - b.def.item.itemID;
int dv = def.damageValue - b.def.damageValue;
int dspv = def.dspDamage - b.def.dspDamage;
return id == 0 ? (dv == 0 ? (dspv == 0 ? ((def.tagCompound == null ? 0 : def.tagCompound.hashCode()) - (b.def.tagCompound == null ? 0
: b.def.tagCompound.hashCode())) : dspv) : dv) : id;
}
@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() );
}
@Override
public IAEItemStack empty()
{
IAEItemStack dup = copy();
dup.reset();
return dup;
}
@Override
public int getItemDamage()
{
return def.damageValue;
}
@Override
void writeIdentity(DataOutputStream i) throws IOException
{
i.writeShort( def.item.itemID );
i.writeShort( getItemDamage() );
}
@Override
void readNBT(DataOutputStream 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.write( tagBytes );
}
}
public static IAEItemStack loadItemStackFromPacket(DataInputStream 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.readFully( bd );
DataInput di = ByteStreams.newDataInput( bd );
d.setCompoundTag( "tag", CompressedStreamTools.read( 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)
{
AEItemStack bottom = new AEItemStack( this );
bottom.def = bottom.def.copy();
int low = fuzzy.calculateBreakPoint( def.maxDamage );
bottom.def.dspDamage = low < def.dspDamage ? low : 0;
return bottom;
}
public IAEItemStack getHigh(FuzzyMode fuzzy)
{
AEItemStack top = new AEItemStack( this );
top.def = top.def.copy();
int high = fuzzy.calculateBreakPoint( def.maxDamage );
top.def.dspDamage = high > def.dspDamage ? high : def.maxDamage;
return top;
}
@Override
public IAETagCompound getTagCompound()
{
return def.tagCompound;
}
}
+172
View File
@@ -0,0 +1,172 @@
package appeng.util.item;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.WeakHashMap;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
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 int itemid, meta, hash;
public SharedSearchObject sso;
private IItemComparison comp;
@Override
public IItemComparison getSpecialComparison()
{
return comp;
}
private AESharedNBT(int itemID, int damageValue, String name) {
super( name );
itemid = itemID;
meta = damageValue;
}
@Override
public NBTTagCompound getNBTTagCompoundCopy()
{
return (NBTTagCompound) copy();
}
public static AESharedNBT createFromCompound(int itemID, int damageValue, NBTTagCompound c)
{
AESharedNBT x = new AESharedNBT( itemID, damageValue, c.getName() );
Iterator var2 = c.getTags().iterator();
while (var2.hasNext())
{
NBTBase tag = (NBTBase) var2.next();
x.setTag( tag.getName(), (NBTBase) tag.copy() );
}
x.hash = Platform.NBTOrderlessHash( c );
ItemStack isc = new ItemStack( itemID, 1, damageValue );
isc.setTagCompound( c );
x.comp = AEApi.instance().registries().specialComparson().getSpecialComparion( isc );
return x;
}
@Override
public boolean equals(Object par1Obj)
{
if ( par1Obj instanceof AESharedNBT )
return this == par1Obj;
return super.equals( par1Obj );
}
public boolean matches(int itemid2, int meta2, int orderlessHash)
{
return itemid2 == itemid && 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>> sharedTagCompounts = new WeakHashMap();
/*
* Debug purposes.
*/
public static int sharedTagLoad()
{
return sharedTagCompounts.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(ItemStack s)
{
NBTTagCompound tagCompound = s.getTagCompound();
if ( tagCompound == null )
return null;
if ( tagCompound.hasNoTags() )
return null;
int itemid = s.itemID;
int meta = -1;
if ( s.itemID != 0 && s.isItemStackDamageable() && s.getHasSubtypes() )
meta = s.getItemDamage();
if ( isShared( tagCompound ) )
return tagCompound;
SharedSearchObject sso = new SharedSearchObject( itemid, meta, tagCompound );
WeakReference<SharedSearchObject> c = sharedTagCompounts.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;
sharedTagCompounts.put( sso, new WeakReference<SharedSearchObject>( sso ) );
return clone;
}
}
+165
View File
@@ -0,0 +1,165 @@
package appeng.util.item;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import appeng.api.storage.data.IAEStack;
public abstract class AEStack<StackType extends IAEStack> implements IAEStack<StackType>
{
protected boolean isCraftable;
protected long stackSize;
protected long countRequestable;
@Override
public boolean isMeaninful()
{
return stackSize != 0 || getCountRequestable() > 0 || isCraftable();
}
@Override
public StackType reset()
{
stackSize = 0;
// priority = Integer.MIN_VALUE;
setCountRequestable( 0 );
setCraftable( false );
return (StackType) this;
}
@Override
public long getStackSize()
{
return stackSize;
}
@Override
public StackType setStackSize(long ss)
{
stackSize = ss;
return (StackType) this;
}
@Override
public long getCountRequestable()
{
return countRequestable;
}
@Override
public StackType setCountRequestable(long countRequestable)
{
this.countRequestable = countRequestable;
return (StackType) this;
}
@Override
public boolean isCraftable()
{
return isCraftable;
}
@Override
public StackType setCraftable(boolean isCraftable)
{
this.isCraftable = isCraftable;
return (StackType) this;
}
@Override
public void decStackSize(long i)
{
stackSize -= i;
}
@Override
public void incStackSize(long i)
{
stackSize += i;
}
@Override
public void decCountRequestable(long i)
{
countRequestable -= i;
}
@Override
public void incCountRequestable(long i)
{
countRequestable += i;
}
void putPacketValue(DataOutputStream tag, long num) throws IOException
{
if ( num <= 255 )
tag.writeByte( (byte) (num + (long) Byte.MIN_VALUE) );
else if ( num <= 65535 )
tag.writeShort( (short) (num + (long) Short.MIN_VALUE) );
else if ( num <= 4294967295L )
tag.writeInt( (int) (num + (long) Integer.MIN_VALUE) );
else
tag.writeLong( num );
}
static long getPacketValue(byte type, DataInputStream tag) throws IOException
{
if ( type == 0 )
{
long l = tag.readByte();
l -= (long) Byte.MIN_VALUE;
return l;
}
else if ( type == 1 )
{
long l = tag.readShort();
l -= (long) Short.MIN_VALUE;
return l;
}
else if ( type == 2 )
{
long l = tag.readInt();
l -= (long) Integer.MIN_VALUE;
return l;
}
return tag.readLong();
}
byte getType(long num)
{
if ( num <= 255 )
return 0;
else if ( num <= 65535 )
return 1;
else if ( num <= 4294967295L )
return 2;
else
return 3;
}
abstract void writeIdentity(DataOutputStream i) throws IOException;
abstract void readNBT(DataOutputStream i) throws IOException;
abstract boolean hasTagCompound();
@Override
public void writeToPacket(DataOutputStream i) throws IOException
{
byte mask = (byte) (getType( 0 ) | (getType( stackSize ) << 2) | (getType( getCountRequestable() ) << 4) | ((byte) (isCraftable ? 1 : 0) << 6) | (hasTagCompound() ? 1
: 0) << 7);
i.writeByte( mask );
writeIdentity( i );
readNBT( i );
// putPacketValue( i, priority );
putPacketValue( i, stackSize );
putPacketValue( i, getCountRequestable() );
}
}
+187
View File
@@ -0,0 +1,187 @@
package appeng.util.item;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
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 int currentPriority = Integer.MIN_VALUE;
int iteration = Integer.MIN_VALUE;
public Throwable stacktrace;
@Override
synchronized public void add(StackType option)
{
if ( option == null )
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 ( option == null )
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 ( option == null )
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 ( option == null )
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.isMeaninful() ) i.remove(); } }
*/
@Override
synchronized public Iterator iterator()
{
return new MeanfulIterator( records.values().iterator() );
}
@Override
synchronized public StackType findPrecise(StackType i)
{
if ( i == null )
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 records.isEmpty();
}
@Override
public Collection<StackType> findFuzzy(StackType filter, FuzzyMode fuzzy)
{
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 ( OreHelper.instance.isOre( ais ) )
{
}
StackType low = (StackType) ais.getLow( fuzzy );
StackType high = (StackType) ais.getHigh( fuzzy );
return records.subMap( low, high ).values();
}
}
+44
View File
@@ -0,0 +1,44 @@
package appeng.util.item;
import java.util.Iterator;
import appeng.api.storage.data.IAEStack;
public class MeanfulIterator<StackType extends IAEStack> implements Iterator
{
final Iterator<StackType> parent;
private StackType next;
public MeanfulIterator(Iterator<StackType> iterator) {
parent = iterator;
}
@Override
public boolean hasNext()
{
while (parent.hasNext())
{
next = parent.next();
if ( next.isMeaninful() )
return true;
else
parent.remove(); // self cleaning :3
}
return false;
}
@Override
public Object next()
{
return next;
}
@Override
public void remove()
{
parent.remove();
}
}
+29
View File
@@ -0,0 +1,29 @@
package appeng.util.item;
import net.minecraft.item.ItemStack;
import appeng.api.storage.data.IAEItemStack;
public class OreHelper
{
public static OreHelper instance;
public boolean isOre(IAEItemStack is)
{
// TODO Auto-generated method stub
return false;
}
public boolean sameOre(AEItemStack aeItemStack, IAEItemStack is)
{
// TODO Auto-generated method stub
return false;
}
public void sameOre(AEItemStack aeItemStack, ItemStack o)
{
// TODO Auto-generated method stub
}
}
+39
View File
@@ -0,0 +1,39 @@
package appeng.util.item;
import appeng.util.Platform;
import net.minecraft.nbt.NBTTagCompound;
public class SharedSearchObject
{
int def;
int hash;
NBTTagCompound compound;
public AESharedNBT shared;
public SharedSearchObject(int itemID, int damageValue, NBTTagCompound tagCompound) {
def = (damageValue << Platform.DEF_OFFSET) | 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;
}
}
+33
View File
@@ -0,0 +1,33 @@
package appeng.util.iterators;
import java.util.Iterator;
public class ChainedIterator<T> implements Iterator<T>
{
int offset = 0;
T[] list;
public ChainedIterator(T... list) {
this.list = list;
}
@Override
public boolean hasNext()
{
return offset < list.length;
}
@Override
public T next()
{
return list[offset++];
}
@Override
public void remove()
{
throw new RuntimeException( "Not implemented." );
}
}
+26
View File
@@ -0,0 +1,26 @@
package appeng.util.iterators;
import java.util.Iterator;
public class NullIterator<T> implements Iterator<T>
{
@Override
public boolean hasNext()
{
return false;
}
@Override
public T next()
{
return null;
}
@Override
public void remove()
{
}
}
+37
View File
@@ -0,0 +1,37 @@
package appeng.util.iterators;
import java.util.Iterator;
import net.minecraftforge.common.ForgeDirection;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
public class ProxyNodeIterator implements Iterator<IGridNode>
{
Iterator<IGridHost> hosts;
public ProxyNodeIterator(Iterator<IGridHost> hosts) {
this.hosts = hosts;
}
@Override
public boolean hasNext()
{
return hosts.hasNext();
}
@Override
public IGridNode next()
{
IGridHost host = hosts.next();
return host.getGridNode( ForgeDirection.UNKNOWN );
}
@Override
public void remove()
{
throw new RuntimeException( "Not implemented." );
}
}
+39
View File
@@ -0,0 +1,39 @@
package appeng.util.iterators;
import java.util.Iterator;
import net.minecraft.item.ItemStack;
import appeng.util.inv.ItemSlot;
public class StackToSlotIterator implements Iterator<ItemSlot>
{
int x = 0;
final ItemSlot iss = new ItemSlot();
final Iterator<ItemStack> is;
public StackToSlotIterator(Iterator<ItemStack> is) {
this.is = is;
}
@Override
public boolean hasNext()
{
return is.hasNext();
}
@Override
public ItemSlot next()
{
iss.slot = x++;
iss.itemStack = is.next();
return iss;
}
@Override
public void remove()
{
// uhh no.
}
}
+30
View File
@@ -0,0 +1,30 @@
package appeng.util.math;
import org.lwjgl.util.vector.Quaternion;
import org.lwjgl.util.vector.Vector3f;
public class Helper
{
public static final Helper instance = new Helper();
public Quaternion getRotation(Vector3f vForward, Vector3f vUp)
{
Vector3f vRight = new Vector3f();
Vector3f.cross( vUp, vForward, vRight );
/*
* vRight.x vRight.y, vRight.z, vUp.x, vUp.y, vUp.z, vForward.x,
* vForward.y, vForward.z
*/
Quaternion qrot = new Quaternion();
qrot.w = (float) Math.sqrt( 1.0f + vRight.x + vUp.y + vForward.z ) / 2.0f;
double dfWScale = qrot.w * 4.0;
qrot.x = (float) ((vForward.y - vUp.z) / dfWScale);
qrot.y = (float) ((vRight.z - vForward.x) / dfWScale);
qrot.z = (float) ((vUp.x - vRight.y) / dfWScale);
return qrot;
}
}
+30
View File
@@ -0,0 +1,30 @@
package appeng.util.prioitylist;
import java.util.ArrayList;
import java.util.List;
import appeng.api.storage.data.IAEStack;
public class DefaultPriorityList<T extends IAEStack<T>> implements IPartitionList<T>
{
final static List nullList = new ArrayList();
public boolean isListed(T input)
{
return false;
}
@Override
public boolean isEmpty()
{
return true;
}
@Override
public Iterable<T> getItems()
{
return (Iterable<T>) nullList;
}
}
+38
View File
@@ -0,0 +1,38 @@
package appeng.util.prioitylist;
import java.util.Collection;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
public class FuzzyPriorityList<T extends IAEStack<T>> implements IPartitionList<T>
{
final IItemList<T> list;
final FuzzyMode mode;
public FuzzyPriorityList(IItemList<T> in, FuzzyMode mode) {
list = in;
this.mode = mode;
}
public boolean isListed(T input)
{
Collection<T> out = list.findFuzzy( input, mode );
return out != null && !out.isEmpty();
}
@Override
public boolean isEmpty()
{
return list.isEmpty();
}
@Override
public Iterable<T> getItems()
{
return list;
}
}
+14
View File
@@ -0,0 +1,14 @@
package appeng.util.prioitylist;
import appeng.api.storage.data.IAEStack;
public interface IPartitionList<T extends IAEStack<T>>
{
boolean isListed(T input);
boolean isEmpty();
Iterable<T> getItems();
}
+32
View File
@@ -0,0 +1,32 @@
package appeng.util.prioitylist;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
public class PrecisePriorityList<T extends IAEStack<T>> implements IPartitionList<T>
{
final IItemList<T> list;
public PrecisePriorityList(IItemList<T> in) {
list = in;
}
public boolean isListed(T input)
{
return list.findPrecise( input ) != null;
}
@Override
public boolean isEmpty()
{
return list.isEmpty();
}
@Override
public Iterable<T> getItems()
{
return list;
}
}