Skip to content

Commit

Permalink
Attempt some memoization, please test and report pgatt, judge
Browse files Browse the repository at this point in the history
  • Loading branch information
aidancbrady committed Feb 17, 2014
1 parent db7d4ad commit b3f9c48
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 62 deletions.
37 changes: 37 additions & 0 deletions common/mekanism/api/ItemInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mekanism.api;

import net.minecraft.item.ItemStack;

public class ItemInfo
{
public int id;
public int meta;

public ItemInfo(int i, int j)
{
id = i;
meta = j;
}

public static ItemInfo get(ItemStack stack)
{
return new ItemInfo(stack.itemID, stack.getItemDamage());
}

@Override
public boolean equals(Object obj)
{
return obj instanceof ItemInfo &&
((ItemInfo)obj).id == id &&
((ItemInfo)obj).meta == meta;
}

@Override
public int hashCode()
{
int code = 1;
code = 31 * code + id;
code = 31 * code + meta;
return code;
}
}
39 changes: 5 additions & 34 deletions common/mekanism/api/MekanismAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
public class MekanismAPI
{
//Add a BlockInfo value here if you don't want a certain block to be picked up by cardboard boxes
private static Set<BlockInfo> cardboardBoxIgnore = new HashSet<BlockInfo>();
private static Set<ItemInfo> cardboardBoxIgnore = new HashSet<ItemInfo>();

public static boolean isBlockCompatible(int id, int meta)
{
for(BlockInfo i : cardboardBoxIgnore)
for(ItemInfo i : cardboardBoxIgnore)
{
if(i.id == id && (i.meta == OreDictionary.WILDCARD_VALUE || i.meta == meta))
{
Expand All @@ -26,47 +26,18 @@ public static boolean isBlockCompatible(int id, int meta)

public static void addBoxBlacklist(int id, int meta)
{
cardboardBoxIgnore.add(new BlockInfo(id, meta));
cardboardBoxIgnore.add(new ItemInfo(id, meta));
}

public static void removeBoxBlacklist(int id, int meta)
{
cardboardBoxIgnore.remove(new BlockInfo(id, meta));
cardboardBoxIgnore.remove(new ItemInfo(id, meta));
}

public static Set<BlockInfo> getBoxIgnore()
public static Set<ItemInfo> getBoxIgnore()
{
return cardboardBoxIgnore;
}

public static class BlockInfo
{
public int id;
public int meta;

public BlockInfo(int i, int j)
{
id = i;
meta = j;
}

@Override
public boolean equals(Object obj)
{
return obj instanceof BlockInfo &&
((BlockInfo)obj).id == id &&
((BlockInfo)obj).meta == meta;
}

@Override
public int hashCode()
{
int code = 1;
code = 31 * code + id;
code = 31 * code + meta;
return code;
}
}

public static class BoxBlacklistEvent extends Event {}
}
54 changes: 54 additions & 0 deletions common/mekanism/common/OreDictCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package mekanism.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mekanism.api.ItemInfo;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.StackUtils;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

public class OreDictCache
{
public static HashMap<ItemInfo, List<String>> cachedKeys = new HashMap<ItemInfo, List<String>>();

public static List<String> getOreDictName(ItemStack check)
{
ItemInfo info = ItemInfo.get(check);

if(cachedKeys.get(info) != null)
{
return cachedKeys.get(info);
}

List<Integer> idsFound = new ArrayList<Integer>();
HashMap<Integer, ArrayList<ItemStack>> oreStacks = (HashMap<Integer, ArrayList<ItemStack>>)MekanismUtils.getPrivateValue(null, OreDictionary.class, new String[] {"oreStacks"});
oreStacks = (HashMap<Integer, ArrayList<ItemStack>>)oreStacks.clone();

for(Map.Entry<Integer, ArrayList<ItemStack>> entry : oreStacks.entrySet())
{
for(ItemStack stack : entry.getValue())
{
if(StackUtils.equalsWildcard(stack, check))
{
idsFound.add(entry.getKey());
break;
}
}
}

List<String> ret = new ArrayList<String>();

for(Integer id : idsFound)
{
ret.add(OreDictionary.getOreName(id));
}

cachedKeys.put(info, ret);

return ret;
}
}
6 changes: 2 additions & 4 deletions common/mekanism/common/network/PacketBoxBlacklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.io.DataOutputStream;

import mekanism.api.ItemInfo;
import mekanism.api.MekanismAPI;
import mekanism.api.MekanismAPI.BlockInfo;
import mekanism.common.IModule;
import mekanism.common.Mekanism;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

Expand Down Expand Up @@ -45,7 +43,7 @@ public void write(DataOutputStream dataStream) throws Exception
{
dataStream.writeInt(MekanismAPI.getBoxIgnore().size());

for(BlockInfo info : MekanismAPI.getBoxIgnore())
for(ItemInfo info : MekanismAPI.getBoxIgnore())
{
dataStream.writeInt(info.id);
dataStream.writeInt(info.meta);
Expand Down
26 changes: 2 additions & 24 deletions common/mekanism/common/util/MekanismUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import mekanism.common.IInvConfiguration;
import mekanism.common.IModule;
import mekanism.common.IRedstoneControl;
import mekanism.common.OreDictCache;
import mekanism.common.IRedstoneControl.RedstoneControl;
import mekanism.common.Mekanism;
import mekanism.common.PacketHandler;
Expand Down Expand Up @@ -485,30 +486,7 @@ public static boolean oreDictCheck(ItemStack check, String oreDict)
*/
public static List<String> getOreDictName(ItemStack check)
{
List<Integer> idsFound = new ArrayList<Integer>();
HashMap<Integer, ArrayList<ItemStack>> oreStacks = (HashMap<Integer, ArrayList<ItemStack>>)MekanismUtils.getPrivateValue(null, OreDictionary.class, new String[] {"oreStacks"});
oreStacks = (HashMap<Integer, ArrayList<ItemStack>>)oreStacks.clone();

for(Map.Entry<Integer, ArrayList<ItemStack>> entry : oreStacks.entrySet())
{
for(ItemStack stack : entry.getValue())
{
if(StackUtils.equalsWildcard(stack, check))
{
idsFound.add(entry.getKey());
break;
}
}
}

List<String> ret = new ArrayList<String>();

for(Integer id : idsFound)
{
ret.add(OreDictionary.getOreName(id));
}

return ret;
return OreDictCache.getOreDictName(check);
}

/**
Expand Down

0 comments on commit b3f9c48

Please sign in to comment.