From 311ab89f93558233a40079f7cb16605b141b5346 Mon Sep 17 00:00:00 2001 From: Johann Bernhardt Date: Sun, 12 Dec 2021 19:38:06 +0100 Subject: Move sources and resources --- .../gtPlusPlus/api/objects/GregtechException.java | 28 ++ src/main/java/gtPlusPlus/api/objects/Logger.java | 179 ++++++++ .../java/gtPlusPlus/api/objects/data/AutoMap.java | 345 ++++++++++++++++ .../api/objects/data/ConcurrentHashSet.java | 18 + .../gtPlusPlus/api/objects/data/ConcurrentSet.java | 53 +++ .../gtPlusPlus/api/objects/data/FlexiblePair.java | 39 ++ .../java/gtPlusPlus/api/objects/data/ObjMap.java | 285 +++++++++++++ .../java/gtPlusPlus/api/objects/data/Pair.java | 35 ++ .../java/gtPlusPlus/api/objects/data/Quad.java | 45 +++ .../api/objects/data/ReverseAutoMap.java | 175 ++++++++ .../java/gtPlusPlus/api/objects/data/Triplet.java | 27 ++ .../gtPlusPlus/api/objects/data/TypeCounter.java | 178 ++++++++ .../api/objects/data/WeightedCollection.java | 102 +++++ .../api/objects/data/weakref/WeakAutoMap.java | 12 + .../gtPlusPlus/api/objects/minecraft/AABB.java | 65 +++ .../api/objects/minecraft/BTF_FluidTank.java | 188 +++++++++ .../api/objects/minecraft/BTF_Inventory.java | 228 +++++++++++ .../gtPlusPlus/api/objects/minecraft/BlockPos.java | 250 ++++++++++++ .../api/objects/minecraft/CubicObject.java | 62 +++ .../api/objects/minecraft/DimChunkPos.java | 52 +++ .../api/objects/minecraft/FakeBlockPos.java | 253 ++++++++++++ .../api/objects/minecraft/FakeWorld.java | 173 ++++++++ .../gtPlusPlus/api/objects/minecraft/FluidGT6.java | 31 ++ .../objects/minecraft/FormattedTooltipString.java | 25 ++ .../api/objects/minecraft/GenericStack.java | 42 ++ .../api/objects/minecraft/ItemPackage.java | 58 +++ .../api/objects/minecraft/ItemStackData.java | 35 ++ .../objects/minecraft/NoConflictGTRecipeMap.java | 123 ++++++ .../api/objects/minecraft/SafeTexture.java | 64 +++ .../api/objects/minecraft/ShapedRecipe.java | 250 ++++++++++++ .../api/objects/minecraft/TexturePackage.java | 55 +++ .../api/objects/minecraft/ThreadPooCollector.java | 111 +++++ .../minecraft/multi/NoEUBonusMultiBehaviour.java | 27 ++ .../multi/NoOutputBonusMultiBehaviour.java | 27 ++ .../multi/NoSpeedBonusMultiBehaviour.java | 27 ++ .../minecraft/multi/SpecialMultiBehaviour.java | 44 ++ .../api/objects/random/CSPRNG_DO_NOT_USE.java | 271 +++++++++++++ .../api/objects/random/UUIDGenerator.java | 449 +++++++++++++++++++++ .../java/gtPlusPlus/api/objects/random/XSTR.java | 278 +++++++++++++ 39 files changed, 4709 insertions(+) create mode 100644 src/main/java/gtPlusPlus/api/objects/GregtechException.java create mode 100644 src/main/java/gtPlusPlus/api/objects/Logger.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/AutoMap.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/ObjMap.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/Pair.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/Quad.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/Triplet.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java create mode 100644 src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/AABB.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/BTF_FluidTank.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/BTF_Inventory.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/BlockPos.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/CubicObject.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/DimChunkPos.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/FakeBlockPos.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/FakeWorld.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/FluidGT6.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/FormattedTooltipString.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/GenericStack.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/ItemPackage.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/ItemStackData.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/NoConflictGTRecipeMap.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/SafeTexture.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/ShapedRecipe.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/TexturePackage.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/ThreadPooCollector.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/multi/NoEUBonusMultiBehaviour.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/multi/NoOutputBonusMultiBehaviour.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/multi/NoSpeedBonusMultiBehaviour.java create mode 100644 src/main/java/gtPlusPlus/api/objects/minecraft/multi/SpecialMultiBehaviour.java create mode 100644 src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java create mode 100644 src/main/java/gtPlusPlus/api/objects/random/UUIDGenerator.java create mode 100644 src/main/java/gtPlusPlus/api/objects/random/XSTR.java (limited to 'src/main/java/gtPlusPlus/api/objects') diff --git a/src/main/java/gtPlusPlus/api/objects/GregtechException.java b/src/main/java/gtPlusPlus/api/objects/GregtechException.java new file mode 100644 index 0000000000..916dbc4aaf --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/GregtechException.java @@ -0,0 +1,28 @@ +package gtPlusPlus.api.objects; + +public class GregtechException extends Throwable { + + private static final long serialVersionUID = 3601884582161841486L; + + public GregtechException(String aError) { + this(aError, true); + } + + public GregtechException(String aError, boolean aIsVerbose) { + Logger.ERROR("Throwing GT++ Exception!"); + Logger.ERROR("[EXCEPTION] "+aError); + if (aIsVerbose) { + Logger.INFO("Throwing GT++ Exception!"); + Logger.INFO("[EXCEPTION] "+aError); + printStackTrace(); + } + } + + @Override + public void printStackTrace() { + super.printStackTrace(); + } + + + +} diff --git a/src/main/java/gtPlusPlus/api/objects/Logger.java b/src/main/java/gtPlusPlus/api/objects/Logger.java new file mode 100644 index 0000000000..1a1da6868b --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/Logger.java @@ -0,0 +1,179 @@ +package gtPlusPlus.api.objects; + +import cpw.mods.fml.common.FMLLog; +import cpw.mods.fml.relauncher.FMLRelaunchLog; +import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.preloader.CORE_Preloader; +import gtPlusPlus.preloader.asm.AsmConfig; +import org.apache.logging.log4j.LogManager; + +public class Logger { + + public Logger(String string) { + + } + + // Logging Functions + public static final org.apache.logging.log4j.Logger modLogger = Logger.makeLogger(); + + // Generate GT++ Logger + public static org.apache.logging.log4j.Logger makeLogger() { + final org.apache.logging.log4j.Logger gtPlusPlusLogger = LogManager.getLogger("GT++"); + return gtPlusPlusLogger; + } + + private static final boolean enabled = !AsmConfig.disableAllLogging; + + public static final org.apache.logging.log4j.Logger getLogger() { + return modLogger; + } + + // Non-Dev Comments + public static void INFO(final String s) { + if (enabled) { + modLogger.info(s); + } + } + + // Non-Dev Comments + public static void MACHINE_INFO(String s, Object... args) { + if (enabled) { + boolean localPlayer = CORE_Preloader.DEV_ENVIRONMENT; + if (CORE.ConfigSwitches.MACHINE_INFO || localPlayer) { + final String name1 = gtPlusPlus.core.util.reflect.ReflectionUtils.getMethodName(2); + modLogger.info("Machine Info: " + s + " | " + name1, args); + } + } + } + + // Developer Comments + public static void WARNING(final String s) { + if (enabled) { + if (CORE_Preloader.DEBUG_MODE) { + modLogger.warn(s); + } + } + } + + // Errors + public static void ERROR(final String s) { + if (enabled) { + if (CORE_Preloader.DEBUG_MODE) { + modLogger.fatal(s); + } + } + } + + // Developer Logger + public static void SPECIFIC_WARNING(final String whatToLog, final String msg, final int line) { + if (enabled) { + // if (!CORE_Preloader.DEBUG_MODE){ + FMLLog.warning("GT++ |" + line + "| " + whatToLog + " | " + msg); + // } + } + } + + // ASM Comments + public static void LOG_ASM(final String s) { + if (enabled) { + FMLRelaunchLog.info("[Special ASM Logging] ", s); + } + } + + + + + + + + + + + /** + * Special Loggers + */ + + /** + * Special Logger for Bee related content + */ + public static void BEES(final String s) { + if (enabled) { + if (CORE_Preloader.DEV_ENVIRONMENT || CORE_Preloader.DEBUG_MODE) { + modLogger.info("[Bees] "+s); + } + } + } + /** + * Special Logger for Debugging Bee related content + */ + public static void DEBUG_BEES(final String s) { + if (enabled) { + if (CORE_Preloader.DEV_ENVIRONMENT || CORE_Preloader.DEBUG_MODE) { + modLogger.info("[Debug][Bees] "+s); + } + } + } + + + + /** + * Special Logger for Materials related content + */ + public static void MATERIALS(final String s) { + if (enabled) { + if (CORE_Preloader.DEV_ENVIRONMENT || CORE_Preloader.DEBUG_MODE) { + modLogger.info("[Materials] "+s); + } + } + } + /** + * Special Logger for Debugging Materials related content + */ + public static void DEBUG_MATERIALS(final String s) { + if (enabled) { + if (CORE_Preloader.DEV_ENVIRONMENT || CORE_Preloader.DEBUG_MODE) { + modLogger.info("[Debug][Materials] "+s); + } + } + } + + /** + * Special Logger for Reflection related content + */ + public static void REFLECTION(final String s) { + if (enabled) { + if (CORE_Preloader.DEV_ENVIRONMENT || CORE_Preloader.DEBUG_MODE) { + modLogger.info("[Reflection] "+s); + } + } + } + + + /** + * Special Logger for Darkworld related content + */ + public static void WORLD(final String s) { + if (enabled) { + if (CORE_Preloader.DEV_ENVIRONMENT || CORE_Preloader.DEBUG_MODE) { + modLogger.info("[WorldGen] "+s); + } + } + } + + public static void RECIPE(String string) { + if (enabled) { + if (/*CORE_Preloader.DEV_ENVIRONMENT || */CORE_Preloader.DEBUG_MODE) { + modLogger.info("[Recipe] "+string); + } + } + } + + public static void SPACE(final String s) { + if (enabled) { + modLogger.info("[Space] "+s); + } + } + + + +} diff --git a/src/main/java/gtPlusPlus/api/objects/data/AutoMap.java b/src/main/java/gtPlusPlus/api/objects/data/AutoMap.java new file mode 100644 index 0000000000..e04f1af03a --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/AutoMap.java @@ -0,0 +1,345 @@ +package gtPlusPlus.api.objects.data; + +import java.io.Serializable; +import java.util.*; + +public class AutoMap implements Iterable, Cloneable, Serializable, Collection, Queue, List { + + /** + * The Internal Map + */ + protected final Map mInternalMap; + protected final Map mInternalNameMap; + + /** + * The Internal ID + */ + private int mInternalID = 0; + private static final long serialVersionUID = 3771412318075131790L; + + + public AutoMap() { + this(new LinkedHashMap()); + } + + public Map getMap(){ + return mInternalMap; + } + + public AutoMap(Map defaultMapType) { + mInternalMap = defaultMapType; + mInternalNameMap = new LinkedHashMap(); + } + + /** + * Generates an AutoMap from the List. + * @param aList - Data to be inserted into the AutoMap. + */ + public AutoMap(List aList) { + mInternalMap = new LinkedHashMap(); + mInternalNameMap = new LinkedHashMap(); + if (aList != null && aList.size() > 0) { + for (V obj : aList) { + add(obj); + } + } + } + /** + * Generates an AutoMap from a Set. + * @param aList - Data to be inserted into the AutoMap. + */ + public AutoMap(Set aList) { + mInternalMap = new LinkedHashMap(); + mInternalNameMap = new LinkedHashMap(); + if (aList != null && aList.size() > 0) { + for (V obj : aList) { + add(obj); + } + } + } + /** + * Generates an AutoMap from a Collection. + * @param aList - Data to be inserted into the AutoMap. + */ + public AutoMap(Collection aList) { + mInternalMap = new LinkedHashMap(); + mInternalNameMap = new LinkedHashMap(); + if (aList != null && aList.size() > 0) { + for (V obj : aList) { + add(obj); + } + } + } + + /** + * Generates an AutoMap from a Array. + * @param aArray - Data to be inserted into the AutoMap. + */ + public AutoMap(V[] aArray) { + mInternalMap = new LinkedHashMap(); + mInternalNameMap = new LinkedHashMap(); + if (aArray != null && aArray.length > 0) { + for (V obj : aArray) { + add(obj); + } + } + } + + @Override + public Iterator iterator() { + return values().iterator(); + } + + public synchronized boolean setValue(V object){ + int mOriginalID = this.mInternalID; + put(object); + if (this.mInternalMap.get(mOriginalID).equals(object) || mOriginalID > this.mInternalID){ + return true; + } + else { + return false; + } + } + + public synchronized V put(V object){ + return set(object); + } + + public synchronized boolean add(V object){ + return set(object) != null; + } + + public synchronized V set(V object){ + if (object == null) { + return null; + } + mInternalNameMap.put(""+object.hashCode(), (mInternalID+1)); + return mInternalMap.put(mInternalID++, object); + } + + public synchronized V get(int id){ + return mInternalMap.get(id); + } + + public synchronized Collection values(){ + return mInternalMap.values(); + } + + public synchronized int size(){ + return mInternalMap.size(); + } + + public synchronized int hashCode(){ + return mInternalMap.hashCode(); + } + + public synchronized boolean containsKey(int key){ + return mInternalMap.containsKey(key); + } + + public synchronized boolean containsValue(V value){ + return mInternalMap.containsValue(value); + } + + public synchronized boolean isEmpty(){ + return mInternalMap.isEmpty(); + } + + public synchronized void clear(){ + this.mInternalID = 0; + this.mInternalMap.clear(); + this.mInternalNameMap.clear(); + return; + } + + @SuppressWarnings("unchecked") + public V[] toArray() { + V[] toR = (V[]) java.lang.reflect.Array.newInstance(mInternalMap.get(0).getClass(), mInternalMap.size()); + for (int i = 0; i < mInternalMap.size(); i++) { + toR[i] = mInternalMap.get(i); + } + return toR; + } + + public synchronized final int getInternalID() { + return mInternalID; + } + + public synchronized final boolean remove(Object value) { + value.getClass(); + if (this.mInternalMap.containsValue(value)) { + return this.mInternalMap.remove(mInternalNameMap.get(""+value.hashCode()), value); + } + return false; + } + + @Override + public boolean contains(Object o) { + for (V g : this.mInternalMap.values()) { + if (g.equals(o)) { + return true; + } + } + return false; + } + + @SuppressWarnings("unchecked") + @Override + public V[] toArray(V[] a) { + return (V[]) toArray(); + } + + @Override + public boolean containsAll(Collection c) { + boolean aTrue = true; + for (Object g : c) { + if (!this.contains(g)) { + aTrue = false; + } + } + return aTrue; + } + + @Override + public boolean addAll(Collection c) { + boolean aTrue = true; + for (V g : c) { + if (!this.add(g)) { + aTrue = false; + } + } + return aTrue; + } + + @Override + public boolean removeAll(Collection c) { + boolean aTrue = true; + for (Object g : c) { + if (!this.remove(g)) { + aTrue = false; + } + } + return aTrue; + } + + @Override + public boolean retainAll(Collection c) { + AutoMap aTempAllocation = new AutoMap(); + boolean aTrue = false; + aTempAllocation = this; + aTempAllocation.removeAll(c); + aTempAllocation.clear(); + aTrue = aTempAllocation.isEmpty(); + aTempAllocation.clear(); + return aTrue; + } + + @Override + public boolean offer(V e) { + return add(e); + } + + @Override + public V remove() { + V y = this.get(0); + if (remove(y)) + return y; + else + return null; + } + + @Override + public V poll() { + if (this.mInternalMap.isEmpty()) { + return null; + } + return remove(); + } + + @Override + public V element() { + if (this.mInternalMap.isEmpty()) { + return null; + } + return this.get(0); + } + + @Override + public V peek() { + return element(); + } + + @Override + public boolean addAll(int index, Collection c) { + for (V y : c) { + add(y); + } + return true; + } + + @Override + public V set(int index, V element) { + return mInternalMap.put(index, element); + } + + @Override + public void add(int index, V element) { + add(element); + } + + @Override + public V remove(int index) { + V h = mInternalMap.get(index); + set(index, null); + return h; + } + + @Override + public int indexOf(Object o) { + int aCount = 0; + for (V of : mInternalMap.values()) { + if (of != o) { + aCount++; + continue; + } + else { + return aCount; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object o) { + //TODO + return indexOf(o); + } + + @Override + public ListIterator listIterator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ListIterator listIterator(int index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List subList(int fromIndex, int toIndex) { + AutoMap aNewSubList = new AutoMap(); + for (int slot=fromIndex; slot<=toIndex; slot++) { + V obj = mInternalMap.get(slot); + if (obj == null) { + continue; + } + else { + aNewSubList.put(obj); + } + } + return aNewSubList; + } + +} diff --git a/src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java new file mode 100644 index 0000000000..991908e402 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java @@ -0,0 +1,18 @@ +package gtPlusPlus.api.objects.data; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +public class ConcurrentHashSet extends ConcurrentSet { + + private static final long serialVersionUID = -1293478938482781728L; + + public ConcurrentHashSet() { + this(new ConcurrentHashMap()); + } + + public ConcurrentHashSet(ConcurrentMap defaultMapType) { + super(defaultMapType); + } + +} diff --git a/src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java new file mode 100644 index 0000000000..1d3ffc1c01 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java @@ -0,0 +1,53 @@ +package gtPlusPlus.api.objects.data; + +import java.io.Serializable; +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.concurrent.ConcurrentMap; + +public abstract class ConcurrentSet extends AbstractSet implements Serializable { + + private static final long serialVersionUID = -6761513279741915432L; + + private final ConcurrentMap mInternalMap; + + private int mInternalID = 0; + + /** + * Creates a new instance which wraps the specified {@code map}. + */ + public ConcurrentSet(ConcurrentMap aMap) { + mInternalMap = aMap; + } + + @Override + public int size() { + return mInternalMap.size(); + } + + @Override + public boolean contains(Object o) { + return mInternalMap.containsKey(o); + } + + @Override + public boolean add(E o) { + return mInternalMap.putIfAbsent(mInternalID++, o) == null; + } + + @Override + public boolean remove(Object o) { + return mInternalMap.remove(o) != null; + } + + @Override + public void clear() { + this.mInternalID = 0; + mInternalMap.clear(); + } + + @Override + public Iterator iterator() { + return mInternalMap.values().iterator(); + } +} diff --git a/src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java b/src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java new file mode 100644 index 0000000000..64f57b4e5a --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java @@ -0,0 +1,39 @@ +package gtPlusPlus.api.objects.data; + +import java.io.Serializable; + +import com.google.common.base.Objects; + +public class FlexiblePair implements Serializable { + + /** + * SVUID + */ + private static final long serialVersionUID = 1250550491092812443L; + private final K key; + private V value; + + public FlexiblePair(final K key, final V value){ + this.key = key; + this.value = value; + } + + final public K getKey(){ + return this.key; + } + + final public V getValue(){ + return this.value; + } + + final public void setValue(V aObj) { + value = aObj; + } + + @Override + public int hashCode() { + Integer aCode = Objects.hashCode(getKey(), getValue()); + return aCode != null ? aCode : super.hashCode(); + } + +} \ No newline at end of file diff --git a/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java b/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java new file mode 100644 index 0000000000..49dd70d2b8 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java @@ -0,0 +1,285 @@ +package gtPlusPlus.api.objects.data; + +import java.util.Arrays; + +/** + * Object-2-object map based on IntIntMap4a + */ +public class ObjMap +{ + private static final Object FREE_KEY = new Object(); + private static final Object REMOVED_KEY = new Object(); + + /** Keys and values */ + private Object[] m_data; + + /** Value for the null key (if inserted into a map) */ + private Object m_nullValue; + private boolean m_hasNull; + + /** Fill factor, must be between (0 and 1) */ + private final float m_fillFactor; + /** We will resize a map once it reaches this size */ + private int m_threshold; + /** Current map size */ + private int m_size; + /** Mask to calculate the original position */ + private int m_mask; + /** Mask to wrap the actual array pointer */ + private int m_mask2; + + public ObjMap( final int size, final float fillFactor ) + { + if ( fillFactor <= 0 || fillFactor >= 1 ) + throw new IllegalArgumentException( "FillFactor must be in (0, 1)" ); + if ( size <= 0 ) + throw new IllegalArgumentException( "Size must be positive!" ); + final int capacity = arraySize(size, fillFactor); + m_mask = capacity - 1; + m_mask2 = capacity * 2 - 1; + m_fillFactor = fillFactor; + + m_data = new Object[capacity * 2]; + Arrays.fill( m_data, FREE_KEY ); + + m_threshold = (int) (capacity * fillFactor); + } + + @SuppressWarnings("unchecked") + public V get( final K key ) + { + if ( key == null ) + return (V) m_nullValue; //we null it on remove, so safe not to check a flag here + + int ptr = (key.hashCode() & m_mask) << 1; + Object k = m_data[ ptr ]; + + if ( k == FREE_KEY ) + return null; //end of chain already + if ( k.equals( key ) ) //we check FREE and REMOVED prior to this call + return (V) m_data[ ptr + 1 ]; + while ( true ) + { + ptr = (ptr + 2) & m_mask2; //that's next index + k = m_data[ ptr ]; + if ( k == FREE_KEY ) + return null; + if ( k.equals( key ) ) + return (V) m_data[ ptr + 1 ]; + } + } + + @SuppressWarnings("unchecked") + public V put( final K key, final V value ) + { + if ( key == null ) + return insertNullKey(value); + + int ptr = getStartIndex(key) << 1; + Object k = m_data[ptr]; + + if ( k == FREE_KEY ) //end of chain already + { + m_data[ ptr ] = key; + m_data[ ptr + 1 ] = value; + if ( m_size >= m_threshold ) + rehash( m_data.length * 2 ); //size is set inside + else + ++m_size; + return null; + } + else if ( k.equals( key ) ) //we check FREE and REMOVED prior to this call + { + final Object ret = m_data[ ptr + 1 ]; + m_data[ ptr + 1 ] = value; + return (V) ret; + } + + int firstRemoved = -1; + if ( k == REMOVED_KEY ) + firstRemoved = ptr; //we may find a key later + + while ( true ) + { + ptr = ( ptr + 2 ) & m_mask2; //that's next index calculation + k = m_data[ ptr ]; + if ( k == FREE_KEY ) + { + if ( firstRemoved != -1 ) + ptr = firstRemoved; + m_data[ ptr ] = key; + m_data[ ptr + 1 ] = value; + if ( m_size >= m_threshold ) + rehash( m_data.length * 2 ); //size is set inside + else + ++m_size; + return null; + } + else if ( k.equals( key ) ) + { + final Object ret = m_data[ ptr + 1 ]; + m_data[ ptr + 1 ] = value; + return (V) ret; + } + else if ( k == REMOVED_KEY ) + { + if ( firstRemoved == -1 ) + firstRemoved = ptr; + } + } + } + + @SuppressWarnings("unchecked") + public V remove( final K key ) + { + if ( key == null ) + return removeNullKey(); + + int ptr = getStartIndex(key) << 1; + Object k = m_data[ ptr ]; + if ( k == FREE_KEY ) + return null; //end of chain already + else if ( k.equals( key ) ) //we check FREE and REMOVED prior to this call + { + --m_size; + if ( m_data[ ( ptr + 2 ) & m_mask2 ] == FREE_KEY ) + m_data[ ptr ] = FREE_KEY; + else + m_data[ ptr ] = REMOVED_KEY; + final V ret = (V) m_data[ ptr + 1 ]; + m_data[ ptr + 1 ] = null; + return ret; + } + while ( true ) + { + ptr = ( ptr + 2 ) & m_mask2; //that's next index calculation + k = m_data[ ptr ]; + if ( k == FREE_KEY ) + return null; + else if ( k.equals( key ) ) + { + --m_size; + if ( m_data[ ( ptr + 2 ) & m_mask2 ] == FREE_KEY ) + m_data[ ptr ] = FREE_KEY; + else + m_data[ ptr ] = REMOVED_KEY; + final V ret = (V) m_data[ ptr + 1 ]; + m_data[ ptr + 1 ] = null; + return ret; + } + } + } + + @SuppressWarnings("unchecked") + private V insertNullKey(final V value) + { + if ( m_hasNull ) + { + final Object ret = m_nullValue; + m_nullValue = value; + return (V) ret; + } + else + { + m_nullValue = value; + ++m_size; + return null; + } + } + + @SuppressWarnings("unchecked") + private V removeNullKey() + { + if ( m_hasNull ) + { + final Object ret = m_nullValue; + m_nullValue = null; + m_hasNull = false; + --m_size; + return (V) ret; + } + else + { + return null; + } + } + + public int size() + { + return m_size; + } + + @SuppressWarnings("unchecked") + private void rehash( final int newCapacity ) + { + m_threshold = (int) (newCapacity/2 * m_fillFactor); + m_mask = newCapacity/2 - 1; + m_mask2 = newCapacity - 1; + + final int oldCapacity = m_data.length; + final Object[] oldData = m_data; + + m_data = new Object[ newCapacity ]; + Arrays.fill( m_data, FREE_KEY ); + + m_size = m_hasNull ? 1 : 0; + + for ( int i = 0; i < oldCapacity; i += 2 ) { + final Object oldKey = oldData[ i ]; + if( oldKey != FREE_KEY && oldKey != REMOVED_KEY ) + put( (K)oldKey, (V)oldData[ i + 1 ]); + } + } + + public int getStartIndex( final Object key ) + { + //key is not null here + return key.hashCode() & m_mask; + } + + public Object[] values() { + return m_data; + } + + /** Taken from FastUtil implementation */ + + /** Return the least power of two greater than or equal to the specified value. + * + *

Note that this function will return 1 when the argument is 0. + * + * @param x a long integer smaller than or equal to 262. + * @return the least power of two greater than or equal to the specified value. + */ + public static long nextPowerOfTwo( long x ) { + if ( x == 0 ) return 1; + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return ( x | x >> 32 ) + 1; + } + + /** Returns the least power of two smaller than or equal to 230 and larger than or equal to Math.ceil( expected / f ). + * + * @param expected the expected number of elements in a hash table. + * @param f the load factor. + * @return the minimum possible size for a backing array. + * @throws IllegalArgumentException if the necessary size is larger than 230. + */ + public static int arraySize( final int expected, final float f ) { + final long s = Math.max( 2, nextPowerOfTwo( (long)Math.ceil( expected / f ) ) ); + if ( s > (1 << 30) ) throw new IllegalArgumentException( "Too large (" + expected + " expected elements with load factor " + f + ")" ); + return (int)s; + } + + //taken from FastUtil + private static final int INT_PHI = 0x9E3779B9; + + public static int phiMix( final int x ) { + final int h = x * INT_PHI; + return h ^ (h >> 16); +} + +} diff --git a/src/main/java/gtPlusPlus/api/objects/data/Pair.java b/src/main/java/gtPlusPlus/api/objects/data/Pair.java new file mode 100644 index 0000000000..e1d23e6b43 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/Pair.java @@ -0,0 +1,35 @@ +package gtPlusPlus.api.objects.data; + +import java.io.Serializable; + +import com.google.common.base.Objects; + +public class Pair implements Serializable { + + /** + * SVUID + */ + private static final long serialVersionUID = 1250550491092812443L; + private final K key; + private final V value; + + public Pair(final K key, final V value){ + this.key = key; + this.value = value; + } + + final public K getKey(){ + return this.key; + } + + final public V getValue(){ + return this.value; + } + + @Override + public int hashCode() { + Integer aCode = Objects.hashCode(getKey(), getValue()); + return aCode != null ? aCode : super.hashCode(); + } + +} \ No newline at end of file diff --git a/src/main/java/gtPlusPlus/api/objects/data/Quad.java b/src/main/java/gtPlusPlus/api/objects/data/Quad.java new file mode 100644 index 0000000000..fa2e52951e --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/Quad.java @@ -0,0 +1,45 @@ +package gtPlusPlus.api.objects.data; + +import java.util.ArrayList; +import java.util.List; + +public class Quad { + + private final K key; + private final V value; + private final C value2; + private final R value3; + + public Quad(final K key, final V value, final C value2, final R value3){ + this.key = key; + this.value = value; + this.value2 = value2; + this.value3 = value3; + } + + final public K getKey(){ + return this.key; + } + + final public V getValue_1(){ + return this.value; + } + + final public C getValue_2(){ + return this.value2; + } + + final public R getValue_3(){ + return this.value3; + } + + public final List values() { + List aVals = new ArrayList(); + aVals.add(key); + aVals.add(value); + aVals.add(value2); + aVals.add(value3); + return aVals; + } + +} \ No newline at end of file diff --git a/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java b/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java new file mode 100644 index 0000000000..72ec0bc293 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java @@ -0,0 +1,175 @@ +package gtPlusPlus.api.objects.data; + +import java.util.*; + +public class ReverseAutoMap extends AutoMap { + + /** + * The Internal Map + */ + private Map mInternalMapReverseLookup = new HashMap(); + + /** + * The Internal ID + */ + private int mInternalID = 0; + private static final long serialVersionUID = 3771412318075131790L; + + @Override + public Iterator iterator() { + return values().iterator(); + } + + public synchronized boolean setValue(N object){ + int mOriginalID = this.mInternalID; + put(object); + if (this.mInternalMap.get(mOriginalID).equals(object) || mOriginalID > this.mInternalID){ + return true; + } + else { + return false; + } + } + + @Override + public synchronized N put(N object){ + return set(object); + } + + @Override + public synchronized N set(N object){ + int newID = getNextFreeMapID(); + mInternalMapReverseLookup.put(object, newID); + return mInternalMap.put(newID, object); + } + + public synchronized int putToInternalMap(N object){ + return setInternalMap(object); + } + + public synchronized int setInternalMap(N object){ + int newID = getNextFreeMapID(); + mInternalMap.put(newID, object); + mInternalMapReverseLookup.put(object, newID); + return newID; + } + + public synchronized boolean injectCleanDataToAutoMap(Integer g, N object){ + if (!mInternalMap.containsKey(g) && !mInternalMapReverseLookup.containsKey(object)) { + int a1 = 0, a2 = 0, a11 = 0, a22 = 0; + a1 = mInternalMap.size(); + a2 = mInternalMapReverseLookup.size(); + a11 = a1; + a22 = a2; + mInternalMap.put(g, object); + a1 = mInternalMap.size(); + mInternalMapReverseLookup.put(object, g); + a2 = mInternalMapReverseLookup.size(); + if (a1 > a11 && a2 > a22) + return true; + } + return false; + } + + public synchronized boolean injectDataToAutoMap(Integer g, N object){ + int a1 = 0, a2 = 0, a11 = 0, a22 = 0; + a1 = mInternalMap.size(); + a2 = mInternalMapReverseLookup.size(); + a11 = a1; + a22 = a2; + mInternalMap.put(g, object); + a1 = mInternalMap.size(); + mInternalMapReverseLookup.put(object, g); + a2 = mInternalMapReverseLookup.size(); + if (a1 > a11 && a2 > a22) + return true; + return false; + } + + private boolean raiseInternalID() { + int mOld = mInternalID; + mInternalID++; + return mInternalID > mOld; + } + + public synchronized int getNextFreeMapID() { + if (raiseInternalID()) { + return mInternalID; + } + return Short.MIN_VALUE; + } + + @Override + public synchronized N get(int id){ + return mInternalMap.get(id); + } + + public synchronized int get(N key) { + return mInternalMapReverseLookup.get(key); + } + + @Override + public synchronized Collection values(){ + return mInternalMap.values(); + } + + public synchronized Collection keys(){ + return mInternalMapReverseLookup.values(); + } + + @Override + public synchronized int size(){ + return mInternalMap.size(); + } + + @Override + public synchronized int hashCode(){ + return mInternalMap.hashCode()+mInternalMapReverseLookup.hashCode(); + } + + @Override + public synchronized boolean containsKey(int key){ + return mInternalMap.containsKey(key); + } + + @Override + public synchronized boolean containsValue(N value){ + return mInternalMap.containsValue(value); + } + + public synchronized boolean containsKey(N key){ + return mInternalMapReverseLookup.containsKey(key); + } + + public synchronized boolean containsValue(int value){ + return mInternalMapReverseLookup.containsValue(value); + } + + @Override + public synchronized boolean isEmpty(){ + return mInternalMap.isEmpty() && mInternalMapReverseLookup.isEmpty(); + } + + @Override + public synchronized void clear(){ + this.mInternalID = 0; + this.mInternalMap.clear(); + this.mInternalMapReverseLookup.clear(); + return; + } + + @Override + public synchronized N[] toArray() { + Collection col = this.mInternalMap.values(); + @SuppressWarnings("unchecked") + N[] val = (N[]) col.toArray(); + return val; + } + + public synchronized Integer[] toArrayInternalMap() { + Collection col = this.mInternalMapReverseLookup.values(); + Integer[] val = col.toArray(new Integer[col.size()]); + return val; + } + +} diff --git a/src/main/java/gtPlusPlus/api/objects/data/Triplet.java b/src/main/java/gtPlusPlus/api/objects/data/Triplet.java new file mode 100644 index 0000000000..affb03d868 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/Triplet.java @@ -0,0 +1,27 @@ +package gtPlusPlus.api.objects.data; + +public class Triplet { + + private final K key; + private final V value; + private final C count; + + public Triplet(final K key, final V value, final C value2){ + this.key = key; + this.value = value; + this.count = value2; + } + + final public K getValue_1(){ + return this.key; + } + + final public V getValue_2(){ + return this.value; + } + + final public C getValue_3(){ + return this.count; + } + +} \ No newline at end of file diff --git a/src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java b/src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java new file mode 100644 index 0000000000..601a51392f --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java @@ -0,0 +1,178 @@ +package gtPlusPlus.api.objects.data; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +import gtPlusPlus.api.objects.Logger; + + +public class TypeCounter implements Set { + + private Map> mInternalMap = new LinkedHashMap>(); + private String mHighestValueKey; + private int mHighestValue = 0; + private final Class mClass; + + public TypeCounter(Class o) { + Logger.WARNING("Created new TypeCounter for "+o.getName()); + mClass = o; + } + + public static class InternalTypeCounterObject { + private final Z mObject; + private int mCounter = 0; + + public InternalTypeCounterObject(Z o) { + mObject = o; + } + + public String hash() { + return String.valueOf(mObject.hashCode()); + } + + public Z get() { + return mObject; + } + + public void add() { + mCounter++; + } + + public int count() { + return mCounter; + } + + } + + public boolean add(V arg0) { + return add(arg0, null); + } + + public boolean add(V arg0, String aKeyName) { + String aKey = aKeyName != null ? aKeyName : arg0.toString(); + InternalTypeCounterObject aValue = mInternalMap.get(aKey); + if (aValue == null) { + aValue = new InternalTypeCounterObject((V) arg0); + Logger.WARNING("Adding new key to map: "+aKey); + } + aValue.add(); + int a = aValue.count(); + if (a > mHighestValue) { + mHighestValue = a; + mHighestValueKey = aKey; + Logger.WARNING("New Highest Count - "+aKey+":"+a); + } + mInternalMap.put(aKey, aValue); + Logger.WARNING(aKey+":"+a); + return true; + } + + @Override + public boolean addAll(Collection arg0) { + boolean aReturn = true; + for (Object o : arg0) { + if (mClass.isInstance(o)) { + V j = (V) o; + boolean b = add(j); + if (!b) { + aReturn = false; + } + } + } + return aReturn; + } + + @Override + public void clear() { + mInternalMap.clear(); + } + + @Override + public boolean contains(Object arg0) { + return mInternalMap.containsKey(arg0.toString()); + } + + @Override + public boolean containsAll(Collection arg0) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isEmpty() { + return mInternalMap.isEmpty(); + } + + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean remove(Object arg0) { + InternalTypeCounterObject aValue = mInternalMap.remove(arg0.toString()); + if (aValue != null) { + return true; + } + else { + return false; + } + } + + @Override + public boolean removeAll(Collection arg0) { + boolean aReturn = true; + for (Object o : arg0) { + boolean a = remove(o); + if (!a) { + aReturn = false; + } + } + return aReturn; + } + + @Override + public boolean retainAll(Collection arg0) { + // TODO Auto-generated method stub + return false; + } + + @Override + public int size() { + return this.mInternalMap.size(); + } + + @Override + public Object[] toArray() { + Object[] aArray = new Object[this.mInternalMap.size()]; + int aPos = 0; + for (String k : this.mInternalMap.keySet()) { + if (k != null) { + InternalTypeCounterObject aVal = this.mInternalMap.get(k); + aArray[aPos++] = new Pair>(k, aVal); + } + } + return aArray; + } + + @Override + public V[] toArray(Object[] a) { + Object[] aArray = new Object[a.length]; + int aPos = 0; + for (Object k : a) { + if (k != null) { + aArray[aPos++] = k; + } + } + return (V[]) aArray; + } + + public V getResults() { + InternalTypeCounterObject x = mInternalMap.get(mHighestValueKey); + return x.get(); + } +} diff --git a/src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java b/src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java new file mode 100644 index 0000000000..f9966474b0 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java @@ -0,0 +1,102 @@ +package gtPlusPlus.api.objects.data; + +import java.util.Collection; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Random; +import java.util.Set; +import java.util.TreeMap; + +import gtPlusPlus.api.objects.random.XSTR; + +public class WeightedCollection implements Map { + + private NavigableMap map = new TreeMap(); + private Random random; + private int total = 0; + + public WeightedCollection() { + this(new XSTR()); + } + + public WeightedCollection(Random random) { + this.random = random; + } + + public E add(int weight, E object) { + if (weight <= 0) return null; + total += weight; + return map.put(total, object); + } + + private E next() { + int value = random.nextInt(total) + 1; // Can also use floating-point weights + return map.ceilingEntry(value).getValue(); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return map.containsValue(value); + } + + public E get() { + return next(); + } + + @Override + public E get(Object key) { + return next(); + } + + @Override + public void putAll(Map m) { + map.putAll(m); + } + + @Override + public void clear() { + map.clear(); + this.total = 0; + } + + @Override + public Set keySet() { + return map.keySet(); + } + + @Override + public Collection values() { + return map.values(); + } + + @Override + public Set entrySet() { + return map.entrySet(); + } + + @Override + public E put(Integer key, E value) { + return add(key, value); + } + + @Override + public E remove(Object key) { + return map.remove(key); + } + +} \ No newline at end of file diff --git a/src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java b/src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java new file mode 100644 index 0000000000..6c55822ca9 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java @@ -0,0 +1,12 @@ +package gtPlusPlus.api.objects.data.weakref; + +import java.util.WeakHashMap; + +import gtPlusPlus.api.objects.data.AutoMap; + +public class WeakAutoMap extends AutoMap { + private static final long serialVersionUID = 8328345351801363386L; + public WeakAutoMap() { + super(new WeakHashMap()); + } +} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/AABB.java b/src/main/java/gtPlusPlus/api/objects/minecraft/AABB.java new file mode 100644 index 0000000000..722ac00b64 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/minecraft/AABB.java @@ -0,0 +1,65 @@ +package gtPlusPlus.api.objects.minecraft; + +import gtPlusPlus.core.util.minecraft.EntityUtils; +import net.minecraft.entity.Entity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.world.World; + +/** + * Generates an AABB around an entity. + * @author Alkalus + * + */ +public class AABB { + + private final AxisAlignedBB mAabb; + private final World mWorld; + + /** + * Creates a AxisAlignedBB based around an Entity. + * @param aEntity - The Entity to work with. + * @param x - Maximum X from origin. + * @param y - Maximum Y from origin. + * @param z - Maximum Z from origin. + */ + public AABB(Entity aEntity, int x, int y, int z) { + if (aEntity == null) { + mAabb = null; + mWorld = null; + } + else { + mWorld = aEntity.worldObj; + BlockPos aEntityLocation = EntityUtils.findBlockPosUnderEntity(aEntity); + int xMin, xMax, yMin, yMax, zMin, zMax; + xMin = aEntityLocation.xPos; + yMin = aEntityLocation.yPos; + zMin = aEntityLocation.zPos; + xMax = aEntityLocation.xPos + x; + yMax = aEntityLocation.yPos + y; + zMax = aEntityLocation.zPos + z; + mAabb = AxisAlignedBB.getBoundingBox(xMin, yMin, zMin, xMax, yMax, zMax); + } + + } + + /** + * Used to get the AxisAlignedBB from this class. + * @return + */ + public AxisAlignedBB get() { + return mAabb; + } + + /** + * Used to determine if this object is valid or not. + * @return + */ + public boolean valid() { + return mAabb != null && mWorld != null; + } + + public World world() { + return mWorld; + } + +} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/BTF_FluidTank.java b/src/main/java/gtPlusPlus/api/objects/minecraft/BTF_FluidTank.java new file mode 100644 index 0000000000..0b8f97b378 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/minecraft/BTF_FluidTank.java @@ -0,0 +1,188 @@ +package gtPlusPlus.api.objects.minecraft; + +import gtPlusPlus.core.util.minecraft.FluidUtils; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTank; +import net.minecraftforge.fluids.FluidTankInfo; + +public class BTF_FluidTank extends FluidTank { + + public FluidStack mFluid; + + public BTF_FluidTank(int capacity) { + super(capacity); + } + + /** + * Let's replace the Default handling with GT's own handling code, because it's probably better, right? + * @author Alkalus/GregoriusT + */ + + + public FluidStack getFluid() { + return this.getDrainableStack(); + } + + public int getFluidAmount() { + return this.getDrainableStack() != null ? this.getDrainableStack().amount : 0; + } + + public NBTTagCompound writeToNBT(NBTTagCompound aNBT) { + super.writeToNBT(aNBT); + if (this.mFluid != null) { + aNBT.setTag("mFluid", this.mFluid.writeToNBT(new NBTTagCompound())); + } + return aNBT; + } + + public FluidTank readFromNBT(NBTTagCompound aNBT) { + this.mFluid = FluidStack.loadFluidStackFromNBT(aNBT.getCompoundTag("mFluid")); + return this; + } + +/* public abstract boolean isLiquidInput(byte arg0); + + public abstract boolean isLiquidOutput(byte arg0); + + public abstract boolean doesFillContainers(); + + public abstract boolean doesEmptyContainers();*/ + + public boolean canTankBeFilled() { + return true; + } + + public boolean canTankBeEmptied() { + return true; + } + + + public boolean isFluidInputAllowed(FluidStack aFluid) { + return true; + } + + public FluidStack getFillableStack() { + return this.mFluid; + } + + public FluidStack setFillableStack(FluidStack aFluid) { + this.mFluid = aFluid; + return this.mFluid; + } + + public FluidStack getDrainableStack() { + return this.mFluid; + } + + public FluidStack setDrainableStack(FluidStack aFluid) { + this.mFluid = aFluid; + return this.mFluid; + } + + public FluidStack getDisplayedFluid() { + return this.getDrainableStack(); + } + + public boolean isFluidChangingAllowed() { + return true; + } + + public int fill(FluidStack aFluid, boolean doFill) { + if (aFluid != null && aFluid.getFluid().getID() > 0 && aFluid.amount > 0 && this.canTankBeFilled() + && this.isFluidInputAllowed(aFluid)) { + if (this.getFillableStack() != null && this.getFillableStack().getFluid().getID() > 0) { + if (!this.getFillableStack().isFluidEqual(aFluid)) { + return 0; + } else { + int space = this.getCapacity() - this.getFillableStack().amount; + if (aFluid.amount <= space) { + if (doFill) { + FluidStack arg9999 = this.getFillableStack(); + arg9999.amount += aFluid.amount; + } + + return aFluid.amount; + } else { + if (doFill) { + this.getFillableStack().amount = this.getCapacity(); + } + + return space; + } + } + } else if (aFluid.amount <= this.getCapacity()) { + if (doFill) { + this.setFillableStack(aFluid.copy()); + } + + return aFluid.amount; + } else { + if (doFill) { + this.setFillableStack(aFluid.copy()); + this.getFillableStack().amount = this.getCapacity(); + } + + return this.getCapacity(); + } + } else { + return 0; + } + } + + public FluidStack drain(int maxDrain, boolean doDrain) { + if (this.getDrainableStack() != null && this.canTankBeEmptied()) { + if (this.getDrainableStack().amount <= 0 && this.isFluidChangingAllowed()) { + this.setDrainableStack((FluidStack) null); + return null; + } else { + int used = maxDrain; + if (this.getDrainableStack().amount < maxDrain) { + used = this.getDrainableStack().amount; + } + + if (doDrain) { + FluidStack arg9999 = this.getDrainableStack(); + arg9999.amount -= used; + } + + FluidStack drained = this.getDrainableStack().copy(); + drained.amount = used; + if (this.getDrainableStack().amount <= 0 && this.isFluidChangingAllowed()) { + this.setDrainableStack((FluidStack) null); + } + + return drained; + } + } else { + return null; + } + } + + @Override + public int getCapacity() { + return super.getCapacity(); + } + + @Override + public FluidTankInfo getInfo() { + return new FluidTankInfo(this); + } + + @Override + public void setFluid(FluidStack fluid) { + setFillableStack(fluid); + } + + @Override + public void setCapacity(int capacity) { + super.setCapacity(capacity); + } + + public FluidStack drain(FluidStack aFluid, boolean doDrain) { + return drain(aFluid.amount, doDrain); + } + + + +} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/BTF_Inventory.java b/src/main/java/gtPlusPlus/api/objects/minecraft/BTF_Inventory.java new file mode 100644 index 0000000000..04ce0dff19 --- /dev/null +++ b/src/main/java/gtPlusPlus/api/objects/minecraft/BTF_Inventory.java @@ -0,0 +1,228 @@ +package gtPlusPlus.api.objects.minecraft; + +import java.util.ArrayList; + +import gregtech.api.util.GT_Utility; +import gtPlusPlus.core.tileentities.base.TileEntityBase; +import gtPlusPlus.core.util.data.ArrayUtils; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.ISidedInventory; +import net.minecraft.item.ItemStack; + +public class BTF_Inventory implements ISidedInventory{ + + public final ItemStack[] mInventory; + public final TileEntityBase mTile; + + public BTF_Inventory(int aSlots, TileEntityBase tile) { + this.mInventory = new ItemStack[aSlots]; + this.mTile = tile; + } + + public ItemStack[] getRealInventory() { + purgeNulls(); + return this.mInventory; + } + + public int getSizeInventory() { + return this.mInventory.length; + } + + public ItemStack getStackInSlot(int aIndex) { + return aIndex >= 0 && aIndex < this.mInventory.length ? this.mInventory[aIndex] : null; + } + + public void setInventorySlotContents(int aIndex, ItemStack aStack) { + if (aIndex >= 0 && aIndex < this.mInventory.length) { + this.mInventory[aIndex] = aStack; + } + } + + public boolean isAccessAllowed(EntityPlayer aPlayer) { + return true; + } + + public boolean isValidSlot(int aIndex) { + return true; + } + + public int getInventoryStackLimit() { + return 64; + } + + + public boolean setStackToZeroInsteadOfNull(int aIndex) { + return false; +} + + public boolean isItemValidForSlot(int aIndex, ItemStack aStack) { + return isValidSlot(aIndex); + } + + public ItemStack decrStackSize(int aIndex, int aAmount) { + ItemStack tStack = this.getStackInSlot(aIndex); + ItemStack rStack = GT_Utility.copy(new Object[]{tStack}); + if (tStack != null) { + if (tStack.stackSize <= aAmount) { + if (this.setStackToZeroInsteadOfNull(aIndex)) { + tStack.stackSize = 0; + } else { + this.setInventorySlotContents(aIndex, (ItemStack) null); + } + } else { + rStack = tStack.splitStack(aAmount); + if (tStack.stackSize == 0 && !this.setStackToZeroInsteadOfNull(aIndex)) { + this.setInventorySlotContents(aIndex, (ItemStack) null); + } + } + } + + return rStack; + } + + public int[] getAccessibleSlotsFromSide(int aSide) { + ArrayList tList = new ArrayList(); + TileEntityBase tTileEntity = this.mTile; + boolean tSkip = tTileEntity.getCoverBehaviorAtSide((byte) aSide).letsItemsIn((byte) aSide, + tTileEntity.getCoverIDAtSide((byte) aSide), tTileEntity.getCoverDataAtSide((byte) aSide), -2, + tTileEntity) + || tTileEntity.getCoverBehaviorAtSide((byte) aSide).letsItemsOut((byte) aSide, + tTileEntity.getCoverIDAtSide((byte) aSide), tTileEntity.getCoverDataAtSide((byte) aSide), -2, + tTileEntity); + + for (int rArray = 0; rArray < this.getSizeInventory(); ++rArray) { + if (this.isValidSlot(rArray) && (tSkip + || tTileEntity.getCoverBehaviorAtSide((byte) aSide).letsItemsOut((byte) aSide, + tTileEntity.getCoverIDAtSide((byte) aSide), tTileEntity.getCoverDataAtSide((byte) aSide), + rArray, tTileEntity) + || tTileEntity.getCoverBehaviorAtSide((byte) aSide).letsItemsIn((byte) aSide, + tTileEntity.getCoverIDAtSide((byte) aSide), tTileEntity.getCoverDataAtSide((byte) aSide), + rArray, tTileEntity))) { + tList.add(Integer.valueOf(rArray)); + } + } + + int[] arg6 = new int[tList.size()]; + + for (int i = 0; i < arg6.length; ++i) { + arg6[i] = ((Integer) tList.get(i)).intValue(); + } + + return arg6; + } + + public boolean canInsertItem(int aIndex, ItemStack aStack, int aSide) { + return this.isValidSlot(aIndex) && aStack != null && aIndex < this.mInventory.length + && (this.mInventory[aIndex] == null || GT_Utility.areStacksEqual(aStack, this.mInventory[aIndex])) + && this.allowPutStack(this.mTile, aIndex, (byte) aSide, aStack); + } + + public boolean canExtractItem(int aIndex, ItemStack aStack, int aSide) { + return this.isValidSlot(aIndex) && aStack != null && aIndex < this.mInventory.length + && this.allowPullStack(this.mTile, aIndex, (byte) aSide, aStack); + } + + public boolean allowPullStack(TileEntityBase mTile2, int aIndex, byte aSide, ItemStack aStack) { + return aIndex >= 0 && aIndex < this.getSizeInventory(); + } + + public boolean allowPutStack(TileEntityBase aBaseMetaTileEntity, int aIndex, byte aSide, ItemStack aStack) { + return (aIndex >= 0 && aIndex < this.getSizeInventory()) && (this.mInventory[aIndex] == null || GT_Utility.areStacksEqual(this.mInventory[aIndex], aStack)); + } + + public ItemStack getStackInSlotOnClosing(int i) { + return null; + } + + public final boolean hasCustomInventoryName() { + return mTile != null ? mTile.hasCustomInventoryName() : false; + } + + + public void markDirty() { + if (mTile != null) { + purgeNulls(); + mTile.markDirty(); + } + } + + public boolean isUseableByPlayer(EntityPlayer entityplayer) { + return true; + } + + public void openInventory() { + + } + + public void closeInventory() { + + } + + @Override + public final String getInventoryName() { + return this.mTile != null ? mTile.getInventoryName() : ""; + } + + public boolean isFull() { + for (int s=0;s getSurroundingBlocks(){ + AutoMap sides = new AutoMap(); + sides.put(getUp()); + sides.put(getDown()); + sides.put(getXPos()); + sides.put(getXNeg()); + sides.put(getZPos()); + sides.put(getZNeg()); + return sides; + } + + public Block getBlockAtPos() { + return getBlockAtPos(this); + } + + public Block getBlockAtPos(BlockPos pos) { + return getBlockAtPos(world, pos); + } + + public Block getBlockAtPos(World world, BlockPos pos) { + return world.getBlock(pos.xPos, pos.yPos, pos.zPos); + } + + public int getMetaAtPos() { + return getMetaAtPos(this); + } + + public int getMetaAtPos(BlockPos pos) { + return getMetaAtPos(world, pos); + } + + public int getMetaAtPos(World world, BlockPos pos) { + return world.getBlockMetadata(pos.xPos, pos.yPos, pos.zPos); + } + + public boolean hasSimilarNeighbour() { + return hasSimilarNeighbour(false); + } + + /** + * @param strict - Does this check Meta Data? + * @return - Does this block have a neighbour that is the same? + */ + public boolean hasSimilarNeighbour(boolean strict) { + for (BlockPos g : getSurroundingBlocks().values()) { + if (getBlockAtPos(g) == getBlockAtPos()) { + if (!strict) { + return true; + } + else { + if (getMetaAtPos() == getMetaAtPos(g)) { + return true; + } + } + } + } + return false; + } + + public AutoMap getSimilarNeighbour() { + return getSimilarNeighbour(false); + } + + /** + * @param strict - Does this check Meta Data? + * @return - Does this block have a neighbour that is the same? + */ + public AutoMap getSimilarNeighbour(boolean strict) { + AutoMap sides = new AutoMap(); + for (BlockPos g : getSurroundingBlocks().values()) { + if (getBlockAtPos(g) == getBlockAtPos()) { + if (!strict) { + sides.put(g); + } + else { + if (getMetaAtPos() == getMetaAtPos(g)) { + sides.put(g); + } + } + } + } + re