From 7a223312814b87795e3c473ccc6a0d737b507346 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Mon, 21 Apr 2025 17:04:42 -0700 Subject: [PATCH 01/12] remove oredictwildcard --- .../ingredient/OreDictWildcardIngredient.java | 70 ------------------- .../mapper/ObjectMapperManager.java | 3 +- 2 files changed, 1 insertion(+), 72 deletions(-) delete mode 100644 src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictWildcardIngredient.java diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictWildcardIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictWildcardIngredient.java deleted file mode 100644 index d77882a9d..000000000 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictWildcardIngredient.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.cleanroommc.groovyscript.helper.ingredient; - -import com.cleanroommc.groovyscript.api.IIngredient; -import com.cleanroommc.groovyscript.api.IOreDicts; -import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor; -import net.minecraft.item.ItemStack; -import net.minecraftforge.oredict.OreDictionary; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -public class OreDictWildcardIngredient extends ItemsIngredient implements IOreDicts { - - private final String oreDict; - private final List matchingOreDictionaries = new ArrayList<>(); - public final List ores = Collections.unmodifiableList(this.matchingOreDictionaries); - - private OreDictWildcardIngredient(String oreDict, List matchingOreDictionaries, List itemStacks) { - super(itemStacks); - this.oreDict = oreDict; - this.matchingOreDictionaries.addAll(matchingOreDictionaries); - } - - public OreDictWildcardIngredient(String oreDict) { - this.oreDict = oreDict; - List matchingOreDictionaries = new ArrayList<>(); - List stacks = new ArrayList<>(); - Pattern pattern = Pattern.compile(oreDict.replace("*", ".*")); - - for (String ore : OreDictionaryAccessor.getIdToName()) { - if (pattern.matcher(ore).matches()) { - matchingOreDictionaries.add(ore); - for (ItemStack stack : OreDictionary.getOres(ore)) { - stacks.add(stack.copy()); - } - } - } - this.matchingOreDictionaries.addAll(matchingOreDictionaries); - setItemStacks(stacks); - } - - public String getOreDict() { - return oreDict; - } - - @Override - public List getOreDicts() { - return getMatchingOreDictionaries(); - } - - public List getMatchingOreDictionaries() { - return ores; - } - - public List getOres() { - return this.ores.stream().map(OreDictIngredient::new).collect(Collectors.toList()); - } - - @Override - public IIngredient exactCopy() { - OreDictWildcardIngredient odwi = new OreDictWildcardIngredient(this.oreDict, this.matchingOreDictionaries, getItemStacks()); - odwi.setAmount(getAmount()); - odwi.transformer = transformer; - odwi.matchCondition = matchCondition; - return odwi; - } -} diff --git a/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java b/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java index c5d71cd2e..475b4d7f2 100644 --- a/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java +++ b/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java @@ -9,7 +9,6 @@ import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor; import com.cleanroommc.groovyscript.core.mixin.VillagerProfessionAccessor; import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; -import com.cleanroommc.groovyscript.helper.ingredient.OreDictWildcardIngredient; import com.cleanroommc.groovyscript.sandbox.expand.ExpansionHelper; import com.cleanroommc.groovyscript.server.CompletionParams; import com.cleanroommc.groovyscript.server.Completions; @@ -85,7 +84,7 @@ public static void init() { .docOfType("resource location") .register(); ObjectMapper.builder("ore", IIngredient.class) - .parser((s, args) -> s.contains(WILDCARD) ? Result.some(new OreDictWildcardIngredient(s)) : Result.some(new OreDictIngredient(s))) + .parser((s, args) -> Result.some(new OreDictIngredient(s))) .completerOfNames(OreDictionaryAccessor::getIdToName) .docOfType("ore dict entry") .textureBinder(TextureBinder.ofArray(IIngredient::getMatchingStacks, TextureBinder.ofItem())) From 151f74a671f76b4feef33df07ce0fac8b5218d1c Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Mon, 21 Apr 2025 17:17:02 -0700 Subject: [PATCH 02/12] fix oredictingredient --- .../helper/ingredient/ItemsIngredient.java | 5 -- .../helper/ingredient/OreDictIngredient.java | 80 +++++++++++++++++-- 2 files changed, 72 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java index 25180b59e..f8c08fb48 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java @@ -85,11 +85,6 @@ public boolean matches(ItemStack itemStack) { return false; } - // protected since modifying un-copied stack directly can result in unexpected results - protected List getItemStacks() { - return Collections.unmodifiableList(this.itemStacks); - } - @Override public @NotNull Iterator iterator() { return new AbstractIterator<>() { diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java index d1bcc72af..a4d8d421b 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java @@ -2,25 +2,30 @@ import com.cleanroommc.groovyscript.api.IOreDicts; import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule; +import com.google.common.collect.AbstractIterator; import com.google.common.collect.ImmutableList; import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.Ingredient; import net.minecraftforge.oredict.OreDictionary; +import org.jetbrains.annotations.NotNull; +import java.util.Iterator; import java.util.List; -public class OreDictIngredient extends ItemsIngredient implements Iterable, IOreDicts { +public class OreDictIngredient extends IngredientBase implements Iterable, IOreDicts { private final String oreDict; + private int amount = 1; public OreDictIngredient(String oreDict) { - super(OreDictionary.getOres(oreDict)); this.oreDict = oreDict; } - // fast copy - private OreDictIngredient(String oreDict, List itemStacks) { - super(itemStacks); - this.oreDict = oreDict; + private static ItemStack selectItemStack(List stacks, int index, int amount) { + if (amount == 0 || stacks.isEmpty() || stacks.size() < index) return ItemStack.EMPTY; + ItemStack stack = stacks.get(index).copy(); + stack.setCount(amount); + return stack; } public String getOreDict() { @@ -34,13 +39,72 @@ public List getOreDicts() { @Override public OreDictIngredient exactCopy() { - OreDictIngredient oreDictIngredient = new OreDictIngredient(this.oreDict, getItemStacks()); - oreDictIngredient.setAmount(getAmount()); + OreDictIngredient oreDictIngredient = new OreDictIngredient(this.oreDict); + oreDictIngredient.amount = amount; oreDictIngredient.transformer = transformer; oreDictIngredient.matchCondition = matchCondition; return oreDictIngredient; } + @Override + public Ingredient toMcIngredient() { + return Ingredient.fromStacks(getMatchingStacks()); + } + + @Override + public ItemStack[] getMatchingStacks() { + var stacks = getItemStacks(); + ItemStack[] output = new ItemStack[stacks.size()]; + for (int i = 0; i < output.length; i++) { + output[i] = selectItemStack(stacks, i, amount); + } + return output; + } + + @Override + public ItemStack getAt(int index) { + return selectItemStack(getItemStacks(), index, amount); + } + + @Override + public int getAmount() { + return getItemStacks().isEmpty() ? 0 : amount; + } + + @Override + public void setAmount(int amount) { + this.amount = Math.max(0, amount); + } + + @Override + public boolean matches(ItemStack itemStack) { + for (ItemStack itemStack1 : getItemStacks()) { + if (OreDictionary.itemMatches(itemStack1, itemStack, false)) { + return true; + } + } + return false; + } + + private List getItemStacks() { + return OreDictionary.getOres(oreDict); + } + + @Override + public @NotNull Iterator iterator() { + return new AbstractIterator<>() { + + private int index = 0; + + @Override + protected ItemStack computeNext() { + var stacks = getItemStacks(); + if (index >= stacks.size()) return endOfData(); + return selectItemStack(stacks, index++, amount); + } + }; + } + @Override public String toString() { return "OreDictIngredient{ " + oreDict + " } * " + getAmount(); From 31e240da14c885d40c3d5049f2130d8beb570778 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Mon, 21 Apr 2025 17:23:33 -0700 Subject: [PATCH 03/12] stream ore names and regex ores --- .../groovyscript/compat/vanilla/OreDict.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java b/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java index 189f5cbea..c3a390039 100644 --- a/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java +++ b/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java @@ -2,12 +2,16 @@ import com.cleanroommc.groovyscript.api.GroovyBlacklist; import com.cleanroommc.groovyscript.api.GroovyLog; +import com.cleanroommc.groovyscript.api.IIngredient; import com.cleanroommc.groovyscript.api.documentation.annotations.Example; import com.cleanroommc.groovyscript.api.documentation.annotations.MethodDescription; import com.cleanroommc.groovyscript.api.documentation.annotations.RegistryDescription; import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor; import com.cleanroommc.groovyscript.helper.Alias; +import com.cleanroommc.groovyscript.helper.SimpleObjectStream; import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper; +import com.cleanroommc.groovyscript.helper.ingredient.OrIngredient; +import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; import com.cleanroommc.groovyscript.registry.VirtualizedRegistry; import net.minecraft.block.Block; import net.minecraft.item.Item; @@ -18,6 +22,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; @RegistryDescription(category = RegistryDescription.Category.ENTRIES) public class OreDict extends VirtualizedRegistry { @@ -150,4 +155,29 @@ public void removeAll() { } } } + + @MethodDescription(type = MethodDescription.Type.QUERY) + public SimpleObjectStream streamOreNames() { + return new SimpleObjectStream<>(OreDictionaryAccessor.getIdToName()).setRemover(this::removeAll); + } + + @MethodDescription(type = MethodDescription.Type.QUERY, example = @Example("'ingot.*'")) + public IIngredient getOres(String pattern) { + return getOres(Pattern.compile(pattern)); + } + + @MethodDescription(type = MethodDescription.Type.QUERY, example = { + @Example("~/.*Gold/"), + @Example("~/.*or.*/"), + @Example("~/.*/"), + }) + public IIngredient getOres(Pattern pattern) { + var ingredient = new OrIngredient(); + for (var ore : OreDictionaryAccessor.getIdToName()) { + if (pattern.matcher(ore).matches()) { + ingredient.or(new OreDictIngredient(ore)); + } + } + return ingredient; + } } From fa6a560ef6c0efc4173e1c249e01d6be6f587317 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Thu, 15 May 2025 01:26:04 -0700 Subject: [PATCH 04/12] make IOreDicts not extend IIngredient --- src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java b/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java index 2ff243962..018950bb3 100644 --- a/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java +++ b/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java @@ -3,9 +3,9 @@ import java.util.List; /** - * Indicates that the IIngredient represents one or more oredicts. + * Indicates something that represents one or more oredicts, typically an {@link IIngredient}. */ -public interface IOreDicts extends IIngredient { +public interface IOreDicts { // TODO // There are a large number of places currently in the GroovyScript codebase // that check if something is "instanceof OreDictIngredient". From 95ad896a5d95725dfecf28c01b70b6d4f09a8fd6 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Thu, 15 May 2025 01:30:40 -0700 Subject: [PATCH 05/12] make it collection instead of list --- .../java/com/cleanroommc/groovyscript/api/IOreDicts.java | 9 ++++++--- .../groovyscript/helper/ingredient/IngredientHelper.java | 2 +- .../helper/ingredient/OreDictIngredient.java | 4 +++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java b/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java index 018950bb3..eaf8f262c 100644 --- a/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java +++ b/src/main/java/com/cleanroommc/groovyscript/api/IOreDicts.java @@ -1,6 +1,8 @@ package com.cleanroommc.groovyscript.api; -import java.util.List; +import org.jetbrains.annotations.UnmodifiableView; + +import java.util.Collection; /** * Indicates something that represents one or more oredicts, typically an {@link IIngredient}. @@ -13,7 +15,8 @@ public interface IOreDicts { // and surrounding code changed appropriately. /** - * @return a list of oredict strings + * @return a collection of oredict strings */ - List getOreDicts(); + @UnmodifiableView + Collection getOreDicts(); } diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/IngredientHelper.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/IngredientHelper.java index 20984463b..980b992b8 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/IngredientHelper.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/IngredientHelper.java @@ -98,7 +98,7 @@ public static IIngredient toIIngredient(FluidStack fluidStack) { public static @NotNull List> cartesianProductOres(@NotNull List inputs) { List> entries = new ArrayList<>(); for (var input : inputs) { - if (input instanceof IOreDicts ore) entries.add(ore.getOreDicts()); + if (input instanceof IOreDicts ore) entries.add(new ArrayList<>(ore.getOreDicts())); else entries.add(Arrays.asList(input.getMatchingStacks())); } return Lists.cartesianProduct(entries); diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java index a4d8d421b..11ef055d6 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java @@ -8,7 +8,9 @@ import net.minecraft.item.crafting.Ingredient; import net.minecraftforge.oredict.OreDictionary; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.UnmodifiableView; +import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -33,7 +35,7 @@ public String getOreDict() { } @Override - public List getOreDicts() { + public @UnmodifiableView Collection getOreDicts() { return ImmutableList.of(getOreDict()); } From dec3077d7d1148a89efc936ee72def2beec1877e Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Thu, 15 May 2025 01:31:19 -0700 Subject: [PATCH 06/12] add back regex matching oredict --- .../groovyscript/compat/vanilla/OreDict.java | 17 ++-- .../ingredient/OreDictMatcherIngredient.java | 92 +++++++++++++++++++ 2 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java diff --git a/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java b/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java index c3a390039..140233a49 100644 --- a/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java +++ b/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java @@ -10,8 +10,7 @@ import com.cleanroommc.groovyscript.helper.Alias; import com.cleanroommc.groovyscript.helper.SimpleObjectStream; import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper; -import com.cleanroommc.groovyscript.helper.ingredient.OrIngredient; -import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; +import com.cleanroommc.groovyscript.helper.ingredient.OreDictMatcherIngredient; import com.cleanroommc.groovyscript.registry.VirtualizedRegistry; import net.minecraft.block.Block; import net.minecraft.item.Item; @@ -27,6 +26,8 @@ @RegistryDescription(category = RegistryDescription.Category.ENTRIES) public class OreDict extends VirtualizedRegistry { + private static final Pattern WILDCARD = Pattern.compile("\\*"); + public OreDict() { super(Alias.generateOfClass(OreDict.class).andGenerate("OreDictionary")); } @@ -161,9 +162,9 @@ public SimpleObjectStream streamOreNames() { return new SimpleObjectStream<>(OreDictionaryAccessor.getIdToName()).setRemover(this::removeAll); } - @MethodDescription(type = MethodDescription.Type.QUERY, example = @Example("'ingot.*'")) + @MethodDescription(type = MethodDescription.Type.QUERY, example = @Example("'ingot*'")) public IIngredient getOres(String pattern) { - return getOres(Pattern.compile(pattern)); + return getOres(Pattern.compile(WILDCARD.matcher(pattern).replaceAll(".*"))); } @MethodDescription(type = MethodDescription.Type.QUERY, example = { @@ -172,12 +173,6 @@ public IIngredient getOres(String pattern) { @Example("~/.*/"), }) public IIngredient getOres(Pattern pattern) { - var ingredient = new OrIngredient(); - for (var ore : OreDictionaryAccessor.getIdToName()) { - if (pattern.matcher(ore).matches()) { - ingredient.or(new OreDictIngredient(ore)); - } - } - return ingredient; + return new OreDictMatcherIngredient(pattern); } } diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java new file mode 100644 index 000000000..3e9fb0f63 --- /dev/null +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java @@ -0,0 +1,92 @@ +package com.cleanroommc.groovyscript.helper.ingredient; + +import com.cleanroommc.groovyscript.api.IIngredient; +import com.cleanroommc.groovyscript.api.IOreDicts; +import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.Ingredient; +import net.minecraftforge.oredict.OreDictionary; +import org.jetbrains.annotations.UnmodifiableView; + +import java.util.Collection; +import java.util.regex.Pattern; + +public class OreDictMatcherIngredient extends IngredientBase implements IOreDicts { + + private final Collection oreDicts; + private final ItemStackList itemStacks; + private final Pattern pattern; + private int amount = 1; + + public OreDictMatcherIngredient(Pattern pattern) { + this.pattern = pattern; + var set = new ObjectOpenHashSet(); + var stacks = new ItemStackList(); + for (var ore : OreDictionaryAccessor.getIdToName()) { + if (pattern.matcher(ore).matches() && set.add(ore)) { + stacks.addAll(OreDictionary.getOres(ore)); + } + } + oreDicts = ImmutableSet.copyOf(set); + itemStacks = stacks; + } + + @Override + public IIngredient exactCopy() { + var ingredient = new OreDictMatcherIngredient(pattern); + ingredient.amount = amount; + ingredient.transformer = transformer; + ingredient.matchCondition = matchCondition; + return ingredient; + } + + @Override + public Ingredient toMcIngredient() { + return Ingredient.fromStacks(getMatchingStacks()); + } + + @Override + public ItemStack[] getMatchingStacks() { + ItemStack[] stacks = new ItemStack[itemStacks.size()]; + for (int i = 0; i < stacks.length; i++) { + stacks[i] = getAt(i); + } + return stacks; + } + + @Override + public ItemStack getAt(int index) { + ItemStack stack = this.itemStacks.get(index).copy(); + stack.setCount(getAmount()); + return stack; + } + + @Override + public int getAmount() { + return itemStacks.isEmpty() ? 0 : amount; + } + + @Override + public void setAmount(int amount) { + this.amount = amount; + } + + @Override + public boolean matches(ItemStack itemStack) { + for (ItemStack itemStack1 : itemStacks) { + if (OreDictionary.itemMatches(itemStack1, itemStack, false)) { + return true; + } + } + return false; + } + + @Override + public @UnmodifiableView Collection getOreDicts() { + return oreDicts; + } + +} From 9000873f301fc1a7ea2b9e20f28568d631054544 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Thu, 15 May 2025 01:42:11 -0700 Subject: [PATCH 07/12] move wildcard handling to oredictmatcheringredient --- .../cleanroommc/groovyscript/compat/vanilla/OreDict.java | 4 +--- .../helper/ingredient/OreDictMatcherIngredient.java | 7 ++++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java b/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java index 140233a49..ab3aceb0e 100644 --- a/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java +++ b/src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java @@ -26,8 +26,6 @@ @RegistryDescription(category = RegistryDescription.Category.ENTRIES) public class OreDict extends VirtualizedRegistry { - private static final Pattern WILDCARD = Pattern.compile("\\*"); - public OreDict() { super(Alias.generateOfClass(OreDict.class).andGenerate("OreDictionary")); } @@ -164,7 +162,7 @@ public SimpleObjectStream streamOreNames() { @MethodDescription(type = MethodDescription.Type.QUERY, example = @Example("'ingot*'")) public IIngredient getOres(String pattern) { - return getOres(Pattern.compile(WILDCARD.matcher(pattern).replaceAll(".*"))); + return new OreDictMatcherIngredient(pattern); } @MethodDescription(type = MethodDescription.Type.QUERY, example = { diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java index 3e9fb0f63..da88c08ba 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java @@ -3,7 +3,6 @@ import com.cleanroommc.groovyscript.api.IIngredient; import com.cleanroommc.groovyscript.api.IOreDicts; import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.minecraft.item.ItemStack; @@ -16,11 +15,17 @@ public class OreDictMatcherIngredient extends IngredientBase implements IOreDicts { + private static final Pattern WILDCARD = Pattern.compile("\\*"); + private final Collection oreDicts; private final ItemStackList itemStacks; private final Pattern pattern; private int amount = 1; + public OreDictMatcherIngredient(String pattern) { + this(Pattern.compile(WILDCARD.matcher(pattern).replaceAll(".*"))); + } + public OreDictMatcherIngredient(Pattern pattern) { this.pattern = pattern; var set = new ObjectOpenHashSet(); From 343b414d8825ef15afda9d53fdce471c06b36959 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Thu, 15 May 2025 07:25:51 -0700 Subject: [PATCH 08/12] document new methods --- src/main/resources/assets/groovyscript/lang/en_us.lang | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/resources/assets/groovyscript/lang/en_us.lang b/src/main/resources/assets/groovyscript/lang/en_us.lang index a26101856..7cfad3f12 100644 --- a/src/main/resources/assets/groovyscript/lang/en_us.lang +++ b/src/main/resources/assets/groovyscript/lang/en_us.lang @@ -160,6 +160,8 @@ groovyscript.wiki.minecraft.ore_dict.remove=Removes the given itemstack from the groovyscript.wiki.minecraft.ore_dict.clear=Removes all itemstacks from the given oredict groovyscript.wiki.minecraft.ore_dict.exists=Returns true if the given oredict exists, although this does not check if the oredict contains entries groovyscript.wiki.minecraft.ore_dict.getItems=Returns a list of all itemstacks in the given oredict +groovyscript.wiki.minecraft.ore_dict.getOres=Returns an IIngredient containing all items for all matching oredicts at the time it is called +groovyscript.wiki.minecraft.ore_dict.streamOreNames=Iterates through every registered oredict, with the ability to clear all items from each one groovyscript.wiki.minecraft.ore_dict.removeAll=Removes all itemstacks from all oredicts groovyscript.wiki.minecraft.player.title=Starting Inventory From fdc22ce2f19b5210f458a06f337ecfc4c8897762 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Thu, 15 May 2025 07:28:42 -0700 Subject: [PATCH 09/12] document query type methods --- examples/postInit/immersiveengineering.groovy | 2 ++ examples/postInit/minecraft.groovy | 5 +++++ .../com/cleanroommc/groovyscript/documentation/Registry.java | 1 + 3 files changed, 8 insertions(+) diff --git a/examples/postInit/immersiveengineering.groovy b/examples/postInit/immersiveengineering.groovy index 18ede4d13..11e67ff60 100644 --- a/examples/postInit/immersiveengineering.groovy +++ b/examples/postInit/immersiveengineering.groovy @@ -79,6 +79,8 @@ mods.immersiveengineering.blueprint_crafting.recipeBuilder() .register() +mods.immersiveengineering.blueprint_crafting.streamRecipesByCategory('molds') + // Bottling Machine: // Converts an input itemstack and fluidstack into an output itemstack. diff --git a/examples/postInit/minecraft.groovy b/examples/postInit/minecraft.groovy index b61c1e19d..8cc3b8495 100644 --- a/examples/postInit/minecraft.groovy +++ b/examples/postInit/minecraft.groovy @@ -195,6 +195,11 @@ mods.minecraft.ore_dict.remove('netherStar', item('minecraft:nether_star')) mods.minecraft.ore_dict.add('ingotGold', item('minecraft:nether_star')) mods.minecraft.ore_dict.add('netherStar', item('minecraft:gold_ingot')) +mods.minecraft.ore_dict.getOres(~/.*/) +mods.minecraft.ore_dict.getOres(~/.*Gold/) +mods.minecraft.ore_dict.getOres(~/.*or.*/) +mods.minecraft.ore_dict.getOres('ingot*') + // Starting Inventory: // Sets the starting inventory of the player, including armor slots and offhand. diff --git a/src/main/java/com/cleanroommc/groovyscript/documentation/Registry.java b/src/main/java/com/cleanroommc/groovyscript/documentation/Registry.java index a5c96587b..51955ab55 100644 --- a/src/main/java/com/cleanroommc/groovyscript/documentation/Registry.java +++ b/src/main/java/com/cleanroommc/groovyscript/documentation/Registry.java @@ -134,6 +134,7 @@ public String exampleBlock() { } out.append(documentMethodDescriptionType(MethodDescription.Type.ADDITION)); out.append(documentMethodDescriptionType(MethodDescription.Type.VALUE)); + out.append(documentMethodDescriptionType(MethodDescription.Type.QUERY)); return out.toString(); } From bfa04b32f70f4dac1c2f4706882573ddff68834a Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Sat, 24 May 2025 11:44:23 -0700 Subject: [PATCH 10/12] fix exactCopy not being an exact copy --- .../ingredient/OreDictMatcherIngredient.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java index da88c08ba..0fd653d0e 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java @@ -3,7 +3,6 @@ import com.cleanroommc.groovyscript.api.IIngredient; import com.cleanroommc.groovyscript.api.IOreDicts; import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor; -import com.google.common.collect.ImmutableSet; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.Ingredient; @@ -28,20 +27,28 @@ public OreDictMatcherIngredient(String pattern) { public OreDictMatcherIngredient(Pattern pattern) { this.pattern = pattern; - var set = new ObjectOpenHashSet(); - var stacks = new ItemStackList(); + this.oreDicts = new ObjectOpenHashSet<>(); + this.itemStacks = new ItemStackList(); + generate(); + } + + private OreDictMatcherIngredient(Pattern pattern, Collection oreDicts, ItemStackList itemStacks) { + this.pattern = pattern; + this.oreDicts = new ObjectOpenHashSet<>(oreDicts); + this.itemStacks = new ItemStackList(itemStacks); + } + + private void generate() { for (var ore : OreDictionaryAccessor.getIdToName()) { - if (pattern.matcher(ore).matches() && set.add(ore)) { - stacks.addAll(OreDictionary.getOres(ore)); + if (pattern.matcher(ore).matches() && oreDicts.add(ore)) { + itemStacks.addAll(OreDictionary.getOres(ore)); } } - oreDicts = ImmutableSet.copyOf(set); - itemStacks = stacks; } @Override public IIngredient exactCopy() { - var ingredient = new OreDictMatcherIngredient(pattern); + var ingredient = new OreDictMatcherIngredient(pattern, oreDicts, itemStacks); ingredient.amount = amount; ingredient.transformer = transformer; ingredient.matchCondition = matchCondition; From d310124fd6074661090156edaea60191b4cc3124 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Sat, 24 May 2025 11:44:31 -0700 Subject: [PATCH 11/12] add regenerate --- .../helper/ingredient/OreDictMatcherIngredient.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java index 0fd653d0e..f33888e22 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java @@ -101,4 +101,11 @@ public boolean matches(ItemStack itemStack) { return oreDicts; } + public OreDictMatcherIngredient regenerate() { + oreDicts.clear(); + itemStacks.clear(); + generate(); + return this; + } + } From 98f026ca8026db4ba6f22eb166fc43c306d3783c Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Sat, 24 May 2025 12:13:53 -0700 Subject: [PATCH 12/12] warn ore Object Mapper with *, the humble spotless apply --- .../helper/ingredient/ItemsIngredient.java | 1 - .../ingredient/OreDictMatcherIngredient.java | 1 - .../mapper/ObjectMapperManager.java | 4 +-- .../groovyscript/mapper/ObjectMappers.java | 25 ++++++++++++++++++- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java index f8c08fb48..e79b63676 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java @@ -10,7 +10,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; -import java.util.List; public class ItemsIngredient extends IngredientBase implements Iterable { diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java index f33888e22..504553304 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictMatcherIngredient.java @@ -107,5 +107,4 @@ public OreDictMatcherIngredient regenerate() { generate(); return this; } - } diff --git a/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java b/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java index 475b4d7f2..1d69f5aec 100644 --- a/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java +++ b/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMapperManager.java @@ -2,13 +2,11 @@ import com.cleanroommc.groovyscript.api.IIngredient; import com.cleanroommc.groovyscript.api.IObjectParser; -import com.cleanroommc.groovyscript.api.Result; import com.cleanroommc.groovyscript.compat.mods.GroovyContainer; import com.cleanroommc.groovyscript.compat.mods.GroovyPropertyContainer; import com.cleanroommc.groovyscript.core.mixin.CreativeTabsAccessor; import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor; import com.cleanroommc.groovyscript.core.mixin.VillagerProfessionAccessor; -import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; import com.cleanroommc.groovyscript.sandbox.expand.ExpansionHelper; import com.cleanroommc.groovyscript.server.CompletionParams; import com.cleanroommc.groovyscript.server.Completions; @@ -84,7 +82,7 @@ public static void init() { .docOfType("resource location") .register(); ObjectMapper.builder("ore", IIngredient.class) - .parser((s, args) -> Result.some(new OreDictIngredient(s))) + .parser(ObjectMappers::parseOreDict) .completerOfNames(OreDictionaryAccessor::getIdToName) .docOfType("ore dict entry") .textureBinder(TextureBinder.ofArray(IIngredient::getMatchingStacks, TextureBinder.ofItem())) diff --git a/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMappers.java b/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMappers.java index 6869cc3b1..c64205624 100644 --- a/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMappers.java +++ b/src/main/java/com/cleanroommc/groovyscript/mapper/ObjectMappers.java @@ -1,9 +1,12 @@ package com.cleanroommc.groovyscript.mapper; import com.cleanroommc.groovyscript.GroovyScript; +import com.cleanroommc.groovyscript.api.GroovyLog; +import com.cleanroommc.groovyscript.api.IIngredient; import com.cleanroommc.groovyscript.api.Result; import com.cleanroommc.groovyscript.core.mixin.CreativeTabsAccessor; import com.cleanroommc.groovyscript.core.mixin.VillagerProfessionAccessor; +import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; import com.google.common.base.Optional; import com.google.common.collect.Iterators; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; @@ -28,7 +31,10 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.util.*; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; import static com.cleanroommc.groovyscript.mapper.ObjectMapperManager.SPLITTER; import static com.cleanroommc.groovyscript.mapper.ObjectMapperManager.WILDCARD; @@ -59,6 +65,23 @@ public class ObjectMappers { return Result.some(new ResourceLocation(GroovyScript.getRunConfig().getPackId(), mainArg)); } + public static @NotNull Result parseOreDict(String mainArg, Object... args) { + if (args.length > 0) { + return Result.error("Arguments not valid for object mapper. Use 'ore(String)'"); + } + if ("Unknown".equals(mainArg)) { + return Result.error("Unknown cannot be an OreDict"); + } + // TODO: remove this warning in a later update + if (mainArg.contains("*")) { + GroovyLog.msg("ore Object Mapper '{}' contained '*'", mainArg) + .add("if this is supposed to be an OreDictWildcardIngredient, use 'oredict.getOres(name)' instead") + .warn() + .post(); + } + return Result.some(new OreDictIngredient(mainArg)); + } + public static @NotNull Result parseItemStack(String mainArg, Object... args) { if (args.length > 1 || (args.length == 1 && !(args[0] instanceof Integer))) { return Result.error("Arguments not valid for bracket handler. Use 'item(String)' or 'item(String, int meta)'");