Skip to content

Commit 792cf11

Browse files
authored
Ore Ingredient Changes (#310)
* remove oredictwildcard * fix oredictingredient * stream ore names and regex ores * make IOreDicts not extend IIngredient * make it collection instead of list * add back regex matching oredict * move wildcard handling to oredictmatcheringredient * document new methods * document query type methods * fix exactCopy not being an exact copy * add regenerate * warn ore Object Mapper with *, the humble spotless apply
1 parent 343acef commit 792cf11

File tree

13 files changed

+252
-96
lines changed

13 files changed

+252
-96
lines changed

examples/postInit/immersiveengineering.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ mods.immersiveengineering.blueprint_crafting.recipeBuilder()
7979
.register()
8080

8181

82+
mods.immersiveengineering.blueprint_crafting.streamRecipesByCategory('molds')
83+
8284
// Bottling Machine:
8385
// Converts an input itemstack and fluidstack into an output itemstack.
8486

examples/postInit/minecraft.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ mods.minecraft.ore_dict.remove('netherStar', item('minecraft:nether_star'))
195195
mods.minecraft.ore_dict.add('ingotGold', item('minecraft:nether_star'))
196196
mods.minecraft.ore_dict.add('netherStar', item('minecraft:gold_ingot'))
197197

198+
mods.minecraft.ore_dict.getOres(~/.*/)
199+
mods.minecraft.ore_dict.getOres(~/.*Gold/)
200+
mods.minecraft.ore_dict.getOres(~/.*or.*/)
201+
mods.minecraft.ore_dict.getOres('ingot*')
202+
198203
// Starting Inventory:
199204
// Sets the starting inventory of the player, including armor slots and offhand.
200205

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package com.cleanroommc.groovyscript.api;
22

3-
import java.util.List;
3+
import org.jetbrains.annotations.UnmodifiableView;
4+
5+
import java.util.Collection;
46

57
/**
6-
* Indicates that the IIngredient represents one or more oredicts.
8+
* Indicates something that represents one or more oredicts, typically an {@link IIngredient}.
79
*/
8-
public interface IOreDicts extends IIngredient {
10+
public interface IOreDicts {
911
// TODO
1012
// There are a large number of places currently in the GroovyScript codebase
1113
// that check if something is "instanceof OreDictIngredient".
1214
// these should be replaced by checks against IOreDicts,
1315
// and surrounding code changed appropriately.
1416

1517
/**
16-
* @return a list of oredict strings
18+
* @return a collection of oredict strings
1719
*/
18-
List<String> getOreDicts();
20+
@UnmodifiableView
21+
Collection<String> getOreDicts();
1922
}

src/main/java/com/cleanroommc/groovyscript/compat/vanilla/OreDict.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.cleanroommc.groovyscript.api.GroovyBlacklist;
44
import com.cleanroommc.groovyscript.api.GroovyLog;
5+
import com.cleanroommc.groovyscript.api.IIngredient;
56
import com.cleanroommc.groovyscript.api.documentation.annotations.Example;
67
import com.cleanroommc.groovyscript.api.documentation.annotations.MethodDescription;
78
import com.cleanroommc.groovyscript.api.documentation.annotations.RegistryDescription;
89
import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor;
910
import com.cleanroommc.groovyscript.helper.Alias;
11+
import com.cleanroommc.groovyscript.helper.SimpleObjectStream;
1012
import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper;
13+
import com.cleanroommc.groovyscript.helper.ingredient.OreDictMatcherIngredient;
1114
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
1215
import net.minecraft.block.Block;
1316
import net.minecraft.item.Item;
@@ -18,6 +21,7 @@
1821

1922
import java.util.ArrayList;
2023
import java.util.List;
24+
import java.util.regex.Pattern;
2125

2226
@RegistryDescription(category = RegistryDescription.Category.ENTRIES)
2327
public class OreDict extends VirtualizedRegistry<OreDictEntry> {
@@ -150,4 +154,23 @@ public void removeAll() {
150154
}
151155
}
152156
}
157+
158+
@MethodDescription(type = MethodDescription.Type.QUERY)
159+
public SimpleObjectStream<String> streamOreNames() {
160+
return new SimpleObjectStream<>(OreDictionaryAccessor.getIdToName()).setRemover(this::removeAll);
161+
}
162+
163+
@MethodDescription(type = MethodDescription.Type.QUERY, example = @Example("'ingot*'"))
164+
public IIngredient getOres(String pattern) {
165+
return new OreDictMatcherIngredient(pattern);
166+
}
167+
168+
@MethodDescription(type = MethodDescription.Type.QUERY, example = {
169+
@Example("~/.*Gold/"),
170+
@Example("~/.*or.*/"),
171+
@Example("~/.*/"),
172+
})
173+
public IIngredient getOres(Pattern pattern) {
174+
return new OreDictMatcherIngredient(pattern);
175+
}
153176
}

src/main/java/com/cleanroommc/groovyscript/documentation/Registry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public String exampleBlock() {
134134
}
135135
out.append(documentMethodDescriptionType(MethodDescription.Type.ADDITION));
136136
out.append(documentMethodDescriptionType(MethodDescription.Type.VALUE));
137+
out.append(documentMethodDescriptionType(MethodDescription.Type.QUERY));
137138
return out.toString();
138139
}
139140

src/main/java/com/cleanroommc/groovyscript/helper/ingredient/IngredientHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static IIngredient toIIngredient(FluidStack fluidStack) {
9898
public static @NotNull List<List<Object>> cartesianProductOres(@NotNull List<IIngredient> inputs) {
9999
List<List<?>> entries = new ArrayList<>();
100100
for (var input : inputs) {
101-
if (input instanceof IOreDicts ore) entries.add(ore.getOreDicts());
101+
if (input instanceof IOreDicts ore) entries.add(new ArrayList<>(ore.getOreDicts()));
102102
else entries.add(Arrays.asList(input.getMatchingStacks()));
103103
}
104104
return Lists.cartesianProduct(entries);

src/main/java/com/cleanroommc/groovyscript/helper/ingredient/ItemsIngredient.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.Collection;
1111
import java.util.Collections;
1212
import java.util.Iterator;
13-
import java.util.List;
1413

1514
public class ItemsIngredient extends IngredientBase implements Iterable<ItemStack> {
1615

@@ -85,11 +84,6 @@ public boolean matches(ItemStack itemStack) {
8584
return false;
8685
}
8786

88-
// protected since modifying un-copied stack directly can result in unexpected results
89-
protected List<ItemStack> getItemStacks() {
90-
return Collections.unmodifiableList(this.itemStacks);
91-
}
92-
9387
@Override
9488
public @NotNull Iterator<ItemStack> iterator() {
9589
return new AbstractIterator<>() {

src/main/java/com/cleanroommc/groovyscript/helper/ingredient/OreDictIngredient.java

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,111 @@
22

33
import com.cleanroommc.groovyscript.api.IOreDicts;
44
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
5+
import com.google.common.collect.AbstractIterator;
56
import com.google.common.collect.ImmutableList;
67
import net.minecraft.item.ItemStack;
8+
import net.minecraft.item.crafting.Ingredient;
79
import net.minecraftforge.oredict.OreDictionary;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.UnmodifiableView;
812

13+
import java.util.Collection;
14+
import java.util.Iterator;
915
import java.util.List;
1016

11-
public class OreDictIngredient extends ItemsIngredient implements Iterable<ItemStack>, IOreDicts {
17+
public class OreDictIngredient extends IngredientBase implements Iterable<ItemStack>, IOreDicts {
1218

1319
private final String oreDict;
20+
private int amount = 1;
1421

1522
public OreDictIngredient(String oreDict) {
16-
super(OreDictionary.getOres(oreDict));
1723
this.oreDict = oreDict;
1824
}
1925

20-
// fast copy
21-
private OreDictIngredient(String oreDict, List<ItemStack> itemStacks) {
22-
super(itemStacks);
23-
this.oreDict = oreDict;
26+
private static ItemStack selectItemStack(List<ItemStack> stacks, int index, int amount) {
27+
if (amount == 0 || stacks.isEmpty() || stacks.size() < index) return ItemStack.EMPTY;
28+
ItemStack stack = stacks.get(index).copy();
29+
stack.setCount(amount);
30+
return stack;
2431
}
2532

2633
public String getOreDict() {
2734
return oreDict;
2835
}
2936

3037
@Override
31-
public List<String> getOreDicts() {
38+
public @UnmodifiableView Collection<String> getOreDicts() {
3239
return ImmutableList.of(getOreDict());
3340
}
3441

3542
@Override
3643
public OreDictIngredient exactCopy() {
37-
OreDictIngredient oreDictIngredient = new OreDictIngredient(this.oreDict, getItemStacks());
38-
oreDictIngredient.setAmount(getAmount());
44+
OreDictIngredient oreDictIngredient = new OreDictIngredient(this.oreDict);
45+
oreDictIngredient.amount = amount;
3946
oreDictIngredient.transformer = transformer;
4047
oreDictIngredient.matchCondition = matchCondition;
4148
return oreDictIngredient;
4249
}
4350

51+
@Override
52+
public Ingredient toMcIngredient() {
53+
return Ingredient.fromStacks(getMatchingStacks());
54+
}
55+
56+
@Override
57+
public ItemStack[] getMatchingStacks() {
58+
var stacks = getItemStacks();
59+
ItemStack[] output = new ItemStack[stacks.size()];
60+
for (int i = 0; i < output.length; i++) {
61+
output[i] = selectItemStack(stacks, i, amount);
62+
}
63+
return output;
64+
}
65+
66+
@Override
67+
public ItemStack getAt(int index) {
68+
return selectItemStack(getItemStacks(), index, amount);
69+
}
70+
71+
@Override
72+
public int getAmount() {
73+
return getItemStacks().isEmpty() ? 0 : amount;
74+
}
75+
76+
@Override
77+
public void setAmount(int amount) {
78+
this.amount = Math.max(0, amount);
79+
}
80+
81+
@Override
82+
public boolean matches(ItemStack itemStack) {
83+
for (ItemStack itemStack1 : getItemStacks()) {
84+
if (OreDictionary.itemMatches(itemStack1, itemStack, false)) {
85+
return true;
86+
}
87+
}
88+
return false;
89+
}
90+
91+
private List<ItemStack> getItemStacks() {
92+
return OreDictionary.getOres(oreDict);
93+
}
94+
95+
@Override
96+
public @NotNull Iterator<ItemStack> iterator() {
97+
return new AbstractIterator<>() {
98+
99+
private int index = 0;
100+
101+
@Override
102+
protected ItemStack computeNext() {
103+
var stacks = getItemStacks();
104+
if (index >= stacks.size()) return endOfData();
105+
return selectItemStack(stacks, index++, amount);
106+
}
107+
};
108+
}
109+
44110
@Override
45111
public String toString() {
46112
return "OreDictIngredient{ " + oreDict + " } * " + getAmount();
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.cleanroommc.groovyscript.helper.ingredient;
2+
3+
import com.cleanroommc.groovyscript.api.IIngredient;
4+
import com.cleanroommc.groovyscript.api.IOreDicts;
5+
import com.cleanroommc.groovyscript.core.mixin.OreDictionaryAccessor;
6+
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
7+
import net.minecraft.item.ItemStack;
8+
import net.minecraft.item.crafting.Ingredient;
9+
import net.minecraftforge.oredict.OreDictionary;
10+
import org.jetbrains.annotations.UnmodifiableView;
11+
12+
import java.util.Collection;
13+
import java.util.regex.Pattern;
14+
15+
public class OreDictMatcherIngredient extends IngredientBase implements IOreDicts {
16+
17+
private static final Pattern WILDCARD = Pattern.compile("\\*");
18+
19+
private final Collection<String> oreDicts;
20+
private final ItemStackList itemStacks;
21+
private final Pattern pattern;
22+
private int amount = 1;
23+
24+
public OreDictMatcherIngredient(String pattern) {
25+
this(Pattern.compile(WILDCARD.matcher(pattern).replaceAll(".*")));
26+
}
27+
28+
public OreDictMatcherIngredient(Pattern pattern) {
29+
this.pattern = pattern;
30+
this.oreDicts = new ObjectOpenHashSet<>();
31+
this.itemStacks = new ItemStackList();
32+
generate();
33+
}
34+
35+
private OreDictMatcherIngredient(Pattern pattern, Collection<String> oreDicts, ItemStackList itemStacks) {
36+
this.pattern = pattern;
37+
this.oreDicts = new ObjectOpenHashSet<>(oreDicts);
38+
this.itemStacks = new ItemStackList(itemStacks);
39+
}
40+
41+
private void generate() {
42+
for (var ore : OreDictionaryAccessor.getIdToName()) {
43+
if (pattern.matcher(ore).matches() && oreDicts.add(ore)) {
44+
itemStacks.addAll(OreDictionary.getOres(ore));
45+
}
46+
}
47+
}
48+
49+
@Override
50+
public IIngredient exactCopy() {
51+
var ingredient = new OreDictMatcherIngredient(pattern, oreDicts, itemStacks);
52+
ingredient.amount = amount;
53+
ingredient.transformer = transformer;
54+
ingredient.matchCondition = matchCondition;
55+
return ingredient;
56+
}
57+
58+
@Override
59+
public Ingredient toMcIngredient() {
60+
return Ingredient.fromStacks(getMatchingStacks());
61+
}
62+
63+
@Override
64+
public ItemStack[] getMatchingStacks() {
65+
ItemStack[] stacks = new ItemStack[itemStacks.size()];
66+
for (int i = 0; i < stacks.length; i++) {
67+
stacks[i] = getAt(i);
68+
}
69+
return stacks;
70+
}
71+
72+
@Override
73+
public ItemStack getAt(int index) {
74+
ItemStack stack = this.itemStacks.get(index).copy();
75+
stack.setCount(getAmount());
76+
return stack;
77+
}
78+
79+
@Override
80+
public int getAmount() {
81+
return itemStacks.isEmpty() ? 0 : amount;
82+
}
83+
84+
@Override
85+
public void setAmount(int amount) {
86+
this.amount = amount;
87+
}
88+
89+
@Override
90+
public boolean matches(ItemStack itemStack) {
91+
for (ItemStack itemStack1 : itemStacks) {
92+
if (OreDictionary.itemMatches(itemStack1, itemStack, false)) {
93+
return true;
94+
}
95+
}
96+
return false;
97+
}
98+
99+
@Override
100+
public @UnmodifiableView Collection<String> getOreDicts() {
101+
return oreDicts;
102+
}
103+
104+
public OreDictMatcherIngredient regenerate() {
105+
oreDicts.clear();
106+
itemStacks.clear();
107+
generate();
108+
return this;
109+
}
110+
}

0 commit comments

Comments
 (0)