Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting generated MetaItem's as an IItemStack #1352

Merged
merged 7 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MaterialMetaItem extends StandardMetaItem {

protected OrePrefix[] orePrefixes;
private ArrayList<Short> generatedItems = new ArrayList<>();
private ArrayList<ItemStack> items = new ArrayList<>();

public MaterialMetaItem(OrePrefix... orePrefixes) {
super((short) (1000 * orePrefixes.length));
Expand All @@ -53,10 +54,16 @@ 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);
items.add(item);
}
}

public List<ItemStack> getEntries() {
return items;
}

@Override
@SideOnly(Side.CLIENT)
public void registerModels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,57 @@
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;
import stanhebben.zenscript.parser.Token;
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<String, ItemStack> metaItemNames = new HashMap<>();

private final IJavaMethod method;

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
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/gregtech/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,6 +174,7 @@ public static void registerRecipesLowest(RegistryEvent.Register<IRecipe> event)
DecompositionRecipeHandler.runRecipeGeneration();
RecyclingRecipes.init();
WoodMachineRecipes.init();
MetaItemBracketHandler.rebuildComponentRegistry();
LAGIdiot marked this conversation as resolved.
Show resolved Hide resolved
}

@SubscribeEvent
Expand Down