From 7f773f0d2b4ed7d520110757aca1f6e79a855b8a Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Wed, 12 Feb 2014 23:24:07 -0600 Subject: [PATCH] Fuzzy now supports the ore dictionary again. --- util/Platform.java | 8 +++ util/item/AEItemDef.java | 2 +- util/item/AEItemStack.java | 22 ++++-- util/item/ItemList.java | 30 ++++++-- util/item/OreHelper.java | 139 +++++++++++++++++++++++++++++++++++-- util/item/OreRefrence.java | 32 +++++++++ 6 files changed, 217 insertions(+), 16 deletions(-) create mode 100644 util/item/OreRefrence.java diff --git a/util/Platform.java b/util/Platform.java index 87d76b00..0c3be2ee 100644 --- a/util/Platform.java +++ b/util/Platform.java @@ -79,6 +79,8 @@ import appeng.server.AccessType; import appeng.util.item.AEItemStack; import appeng.util.item.AESharedNBT; import appeng.util.item.ItemList; +import appeng.util.item.OreHelper; +import appeng.util.item.OreRefrence; import buildcraft.api.tools.IToolWrench; import com.mojang.authlib.GameProfile; @@ -1078,6 +1080,12 @@ public class Platform } } + OreRefrence aOR = OreHelper.instance.isOre( a ); + OreRefrence bOR = OreHelper.instance.isOre( b ); + + if ( OreHelper.instance.sameOre( aOR, bOR ) ) + return true; + /* * // test ore dictionary.. int OreID = getOreID( a ); if ( OreID != -1 ) return OreID == getOreID( b ); * diff --git a/util/item/AEItemDef.java b/util/item/AEItemDef.java index 0635a06e..328af091 100644 --- a/util/item/AEItemDef.java +++ b/util/item/AEItemDef.java @@ -32,7 +32,7 @@ public class AEItemDef @SideOnly(Side.CLIENT) public List tooltip; - public boolean isOre; + public OreRefrence isOre; public AEItemDef copy() { diff --git a/util/item/AEItemStack.java b/util/item/AEItemStack.java index 654c66cf..05109856 100644 --- a/util/item/AEItemStack.java +++ b/util/item/AEItemStack.java @@ -82,7 +82,7 @@ public final class AEItemStack extends AEStack implements IAEItemS setCountRequestable( 0 ); def.reHash(); - def.isOre = OreHelper.instance.isOre( this ); + def.isOre = OreHelper.instance.isOre( is ); } public static AEItemStack create(Object a) @@ -460,11 +460,18 @@ public final class AEItemStack extends AEStack implements IAEItemS return false; } - public IAEItemStack getLow(FuzzyMode fuzzy) + public IAEItemStack getLow(FuzzyMode fuzzy, boolean ignoreMeta) { AEItemStack bottom = new AEItemStack( this ); AEItemDef newDef = bottom.def = bottom.def.copy(); + if ( ignoreMeta ) + { + newDef.dspDamage = newDef.damageValue = 0; + newDef.reHash(); + return bottom; + } + if ( fuzzy == FuzzyMode.IGNORE_ALL ) { newDef.dspDamage = 0; @@ -482,11 +489,18 @@ public final class AEItemStack extends AEStack implements IAEItemS return bottom; } - public IAEItemStack getHigh(FuzzyMode fuzzy) + public IAEItemStack getHigh(FuzzyMode fuzzy, boolean ignoreMeta) { AEItemStack top = new AEItemStack( this ); AEItemDef newDef = top.def = top.def.copy(); + if ( ignoreMeta ) + { + newDef.dspDamage = newDef.damageValue = Integer.MAX_VALUE; + newDef.reHash(); + return top; + } + if ( fuzzy == FuzzyMode.IGNORE_ALL ) { newDef.dspDamage = def.maxDamage + 1; @@ -548,6 +562,6 @@ public final class AEItemStack extends AEStack implements IAEItemS public boolean isOre() { - return def.isOre; + return def.isOre != null; } } diff --git a/util/item/ItemList.java b/util/item/ItemList.java index 6ca1502a..85aa145e 100644 --- a/util/item/ItemList.java +++ b/util/item/ItemList.java @@ -3,9 +3,11 @@ package appeng.util.item; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.TreeMap; +import net.minecraftforge.oredict.OreDictionary; import appeng.api.config.FuzzyMode; import appeng.api.storage.data.IAEFluidStack; import appeng.api.storage.data.IAEItemStack; @@ -166,6 +168,13 @@ public final class ItemList implements IItemList findFuzzyDamage(AEItemStack filter, FuzzyMode fuzzy, boolean ignoreMeta) + { + StackType low = (StackType) filter.getLow( fuzzy, ignoreMeta ); + StackType high = (StackType) filter.getHigh( fuzzy, ignoreMeta ); + return records.subMap( low, high ).values(); + } + @Override public Collection findFuzzy(StackType filter, FuzzyMode fuzzy) { @@ -174,14 +183,25 @@ public final class ItemList implements IItemList output = new LinkedList(); + for (IAEItemStack is : or.aeotherOptions) + output.addAll( findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ) ); + + return output; + } } - StackType low = (StackType) ais.getLow( fuzzy ); - StackType high = (StackType) ais.getHigh( fuzzy ); - return records.subMap( low, high ).values(); + return findFuzzyDamage( ais, fuzzy, false ); } } diff --git a/util/item/OreHelper.java b/util/item/OreHelper.java index 2969afc1..b4a9c8e0 100644 --- a/util/item/OreHelper.java +++ b/util/item/OreHelper.java @@ -1,6 +1,11 @@ package appeng.util.item; +import java.util.Collection; +import java.util.HashMap; + +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraftforge.oredict.OreDictionary; import appeng.api.storage.data.IAEItemStack; public class OreHelper @@ -8,22 +13,144 @@ public class OreHelper public static OreHelper instance = new OreHelper(); - public boolean isOre(IAEItemStack is) + class ItemRef { - // TODO Auto-generated method stub - return false; + + ItemRef(ItemStack stack) { + ref = stack.getItem(); + + if ( stack.getItem().isDamageable() ) + damage = 0; // IGNORED + else + damage = stack.getItemDamage(); // might be important... + + hash = ref.hashCode() ^ damage; + } + + Item ref; + int damage; + int hash; + + @Override + public boolean equals(Object o) + { + ItemRef obj = (ItemRef) o; + return damage == obj.damage && ref == obj.ref; + } + + @Override + public int hashCode() + { + return hash; + } + + }; + + class OreResult + { + + public OreRefrence oreValue = null; + + }; + + HashMap refrences = new HashMap(); + + public OreRefrence isOre(ItemStack ItemStack) + { + ItemRef ir = new ItemRef( ItemStack ); + OreResult or = refrences.get( ir ); + + if ( or == null ) + { + or = new OreResult(); + refrences.put( ir, or ); + + OreRefrence ref = new OreRefrence(); + Collection ores = ref.getOres(); + Collection set = ref.getEquivilients(); + Collection aeset = ref.getAEEquivilients(); + + for (String ore : OreDictionary.getOreNames()) + { + boolean add = false; + + for (ItemStack oreItem : OreDictionary.getOres( ore )) + { + if ( OreDictionary.itemMatches( oreItem, ItemStack, false ) ) + { + add = true; + set.add( oreItem.copy() ); + } + } + + if ( add ) + ores.add( OreDictionary.getOreID( ore ) ); + } + + if ( !set.isEmpty() ) + { + or.oreValue = ref; + + // SUMMON AE STACKS! + for (ItemStack is : set) + aeset.add( AEItemStack.create( is ) ); + } + } + + return or.oreValue; } public boolean sameOre(AEItemStack aeItemStack, IAEItemStack is) { - // TODO Auto-generated method stub + OreRefrence a = aeItemStack.def.isOre; + OreRefrence b = ((AEItemStack) aeItemStack).def.isOre; + + if ( a == b ) + return true; + + if ( a == null || b == null ) + return false; + + Collection bOres = b.getOres(); + for (Integer ore : a.ores) + { + if ( bOres.contains( ore ) ) + return true; + } + return false; } - public void sameOre(AEItemStack aeItemStack, ItemStack o) + public boolean sameOre(OreRefrence a, OreRefrence b) { - // TODO Auto-generated method stub + if ( a == b ) + return true; + if ( a == null || b == null ) + return false; + + Collection bOres = b.getOres(); + for (Integer ore : a.ores) + { + if ( bOres.contains( ore ) ) + return true; + } + + return false; } + public boolean sameOre(AEItemStack aeItemStack, ItemStack o) + { + OreRefrence a = aeItemStack.def.isOre; + if ( a == null ) + return false; + + for (ItemStack oreItem : a.getEquivilients()) + { + if ( OreDictionary.itemMatches( oreItem, o, false ) ) + return true; + } + + return false; + } } \ No newline at end of file diff --git a/util/item/OreRefrence.java b/util/item/OreRefrence.java new file mode 100644 index 00000000..c0fe8b0a --- /dev/null +++ b/util/item/OreRefrence.java @@ -0,0 +1,32 @@ +package appeng.util.item; + +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; + +import net.minecraft.item.ItemStack; +import appeng.api.storage.data.IAEItemStack; + +public class OreRefrence +{ + + LinkedList otherOptions = new LinkedList(); + LinkedList aeotherOptions = new LinkedList(); + HashSet ores = new HashSet(); + + public Collection getEquivilients() + { + return otherOptions; + } + + public Collection getAEEquivilients() + { + return aeotherOptions; + } + + public Collection getOres() + { + return ores; + } + +}