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

Mod Object Mapper and Ingredient #275

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,56 @@
package com.cleanroommc.groovyscript.helper.ingredient;

import com.cleanroommc.groovyscript.api.IIngredient;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

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

public class ModItemsIngredient extends ItemsIngredient {

private static final Map<String, List<ItemStack>> CACHE = new HashMap<>();

private final String modName;

public ModItemsIngredient(String modName, List<ItemStack> itemStacks) {
super(itemStacks);
this.modName = modName;
}

public static ModItemsIngredient of(ModContainer mod) {
return of(mod.getModId());
}

public static ModItemsIngredient of(String modId) {
return new ModItemsIngredient(modId, CACHE.computeIfAbsent(modId, ModItemsIngredient::itemStacksFromLocation));
}

private static List<ItemStack> itemStacksFromLocation(String namespace) {
NonNullList<ItemStack> stacks = NonNullList.create();
ForgeRegistries.ITEMS.getEntries()
.stream()
.filter(entry -> entry.getValue() != null)
.filter(entry -> namespace.equals(entry.getKey().getNamespace()))
.forEach(entry -> entry.getValue().getSubItems(CreativeTabs.SEARCH, stacks));
return new ArrayList<>(stacks);
}

public String getModName() {
return modName;
}

@Override
public IIngredient exactCopy() {
var mii = new ModItemsIngredient(this.modName, getItemStacks());
mii.setAmount(getAmount());
mii.transformer = transformer;
mii.matchCondition = matchCondition;
return mii;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraft.world.biome.Biome;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.common.registry.VillagerRegistry;
Expand Down Expand Up @@ -199,6 +200,10 @@ public static void init() {
.defaultValue(NBTTagCompound::new)
.docOfType("nbt tag")
.register();
ObjectMapper.builder("mod", ModContainer.class)
.parser(ObjectMappers::parseMod)
.docOfType("mod")
.register();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.common.registry.VillagerRegistry;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -228,4 +230,10 @@ public static Result<Material> parseBlockMaterial(String mainArg, Object... args
return Result.error("unable to parse provided nbt string");
}
}

public static @NotNull Result<ModContainer> parseMod(String mainArg, Object... args) {
ModContainer mod = Loader.instance().getIndexedModList().get(mainArg);
if (mod == null) return Result.error("unable to identify mod with the name", mainArg);
return Result.some(mod);
}
}