diff --git a/src/main/java/gregtech/api/items/materialitem/MaterialMetaItem.java b/src/main/java/gregtech/api/items/materialitem/MaterialMetaItem.java index f0244e568d..ab4ce2fac5 100644 --- a/src/main/java/gregtech/api/items/materialitem/MaterialMetaItem.java +++ b/src/main/java/gregtech/api/items/materialitem/MaterialMetaItem.java @@ -32,6 +32,7 @@ public class MaterialMetaItem extends StandardMetaItem { protected OrePrefix[] orePrefixes; private ArrayList generatedItems = new ArrayList<>(); + private ArrayList items = new ArrayList<>(); public MaterialMetaItem(OrePrefix... orePrefixes) { super((short) (1000 * orePrefixes.length)); @@ -53,13 +54,19 @@ public void registerOreDict() { for (short metaItem : generatedItems) { OrePrefix prefix = this.orePrefixes[metaItem / 1000]; Material material = Material.MATERIAL_REGISTRY.getObjectById(metaItem % 1000); - OreDictUnifier.registerOre(new ItemStack(this, 1, metaItem), prefix, material); - + ItemStack item = new ItemStack(this, 1, metaItem); + OreDictUnifier.registerOre(item, prefix, material); if(prefix.name().equals("dust")) - OreDictUnifier.registerOre(new ItemStack(this, 1, metaItem), "dustRegular", material); + OreDictUnifier.registerOre(item, "dustRegular", material); + + items.add(item); } } + public List getEntries() { + return items; + } + @Override @SideOnly(Side.CLIENT) public void registerModels() { diff --git a/src/main/java/gregtech/api/recipes/crafttweaker/MetaItemBracketHandler.java b/src/main/java/gregtech/api/recipes/crafttweaker/MetaItemBracketHandler.java index e228e770ee..9f96c833fe 100644 --- a/src/main/java/gregtech/api/recipes/crafttweaker/MetaItemBracketHandler.java +++ b/src/main/java/gregtech/api/recipes/crafttweaker/MetaItemBracketHandler.java @@ -6,8 +6,11 @@ import crafttweaker.api.item.IItemStack; import crafttweaker.mc1120.item.MCItemStack; import crafttweaker.zenscript.IBracketHandler; +import gregtech.api.items.materialitem.MaterialMetaItem; import gregtech.api.items.metaitem.MetaItem; import gregtech.api.items.metaitem.MetaItem.MetaValueItem; +import gregtech.api.unification.OreDictUnifier; +import net.minecraft.item.ItemStack; import stanhebben.zenscript.compiler.IEnvironmentGlobal; import stanhebben.zenscript.expression.ExpressionCallStatic; import stanhebben.zenscript.expression.ExpressionString; @@ -15,11 +18,14 @@ import stanhebben.zenscript.symbols.IZenSymbol; import stanhebben.zenscript.type.natives.IJavaMethod; +import java.util.HashMap; import java.util.List; +import java.util.Map; @BracketHandler @ZenRegister public class MetaItemBracketHandler implements IBracketHandler { + private static final Map metaItemNames = new HashMap<>(); private final IJavaMethod method; @@ -27,13 +33,30 @@ public MetaItemBracketHandler() { this.method = CraftTweakerAPI.getJavaMethod(MetaItemBracketHandler.class, "getMetaItem", String.class); } + @SuppressWarnings("unchecked") + public static void rebuildComponentRegistry() { + metaItemNames.clear(); + for (MetaItem item : MetaItem.getMetaItems()) { + if (item instanceof MaterialMetaItem) { + for(ItemStack entry : ((MaterialMetaItem) item).getEntries()) { + metaItemNames.put(OreDictUnifier.getPrefix(entry).name() + OreDictUnifier.getMaterial(entry).material.toCamelCaseString(), entry); + } + } + for(MetaValueItem entry : item.getAllItems()) { + if (!entry.unlocalizedName.equals("meta_item")) { + metaItemNames.put(entry.unlocalizedName, entry.getStackForm()); + } + } + } + } + public static IItemStack getMetaItem(String name) { - MetaValueItem targetItem = MetaItem.getMetaItems().stream() - .flatMap(item -> item.getAllItems().stream()) - .map(item -> (MetaValueItem) item) - .filter(item -> item.unlocalizedName.equals(name)) - .findFirst().orElse(null); - return targetItem == null ? null : new MCItemStack(targetItem.getStackForm()); + ItemStack item = metaItemNames.get(name); + if(item != null) { + return new MCItemStack(item); + } else { + return null; + } } @Override diff --git a/src/main/java/gregtech/common/CommonProxy.java b/src/main/java/gregtech/common/CommonProxy.java index 049f7f3abf..6f910f31f0 100644 --- a/src/main/java/gregtech/common/CommonProxy.java +++ b/src/main/java/gregtech/common/CommonProxy.java @@ -4,6 +4,7 @@ import gregtech.api.block.machines.MachineItemBlock; import gregtech.api.enchants.EnchantmentEnderDamage; import gregtech.api.items.metaitem.MetaItem; +import gregtech.api.recipes.crafttweaker.MetaItemBracketHandler; import gregtech.api.unification.material.type.DustMaterial; import gregtech.api.unification.material.type.Material; import gregtech.api.unification.ore.OrePrefix; @@ -172,6 +173,7 @@ public static void registerRecipesLowest(RegistryEvent.Register event) DecompositionRecipeHandler.runRecipeGeneration(); RecyclingRecipes.init(); WoodMachineRecipes.init(); + MetaItemBracketHandler.rebuildComponentRegistry(); } @SubscribeEvent