Skip to content

Commit e1fc05b

Browse files
authored
Improve AspectHelper (#379)
* improve AspectHelper * spooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooootless * reviews
1 parent 76c3d63 commit e1fc05b

2 files changed

Lines changed: 106 additions & 121 deletions

File tree

src/main/java/com/cleanroommc/groovyscript/compat/mods/thaumcraft/aspect/AspectHelper.java

Lines changed: 103 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
import com.cleanroommc.groovyscript.api.documentation.annotations.*;
77
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
88
import com.cleanroommc.groovyscript.compat.mods.thaumcraft.Thaumcraft;
9-
import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper;
10-
import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient;
119
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
1210
import net.minecraft.item.ItemStack;
1311
import net.minecraftforge.fml.common.registry.EntityEntry;
1412
import org.jetbrains.annotations.ApiStatus;
1513
import thaumcraft.api.ThaumcraftApi;
16-
import thaumcraft.api.ThaumcraftApiHelper;
1714
import thaumcraft.api.aspects.Aspect;
1815
import thaumcraft.api.aspects.AspectList;
1916
import thaumcraft.api.internal.CommonInternals;
@@ -30,20 +27,26 @@ public class AspectHelper extends VirtualizedRegistry<AspectListHelper> {
3027
@ApiStatus.Internal
3128
public void onReload() {
3229
removeScripted().forEach(aspectList -> {
33-
if (aspectList.item != null)
34-
for (AspectStack as : aspectList.aspects)
35-
this.remove(aspectList.item, as, false);
36-
else if (aspectList.entity != null)
37-
for (AspectStack as : aspectList.aspects)
38-
this.remove(aspectList.entity, as, false);
30+
if (aspectList.item != null) {
31+
for (AspectStack as : aspectList.aspects) {
32+
remove(aspectList.item, as, false);
33+
}
34+
} else if (aspectList.entity != null) {
35+
for (AspectStack as : aspectList.aspects) {
36+
remove(aspectList.entity, as, false);
37+
}
38+
}
3939
});
4040
restoreFromBackup().forEach(aspectList -> {
41-
if (aspectList.item != null)
42-
for (AspectStack as : aspectList.aspects)
43-
this.add(aspectList.item, as, false);
44-
else if (aspectList.entity != null)
45-
for (AspectStack as : aspectList.aspects)
46-
this.add(aspectList.entity, as, false);
41+
if (aspectList.item != null) {
42+
for (AspectStack as : aspectList.aspects) {
43+
add(aspectList.item, as, false);
44+
}
45+
} else if (aspectList.entity != null) {
46+
for (AspectStack as : aspectList.aspects) {
47+
add(aspectList.entity, as, false);
48+
}
49+
}
4750
});
4851
}
4952

@@ -63,10 +66,11 @@ public void addScripted(Object target, AspectStack aspect) {
6366
if (!found.get()) {
6467
ArrayList<AspectStack> aspectList = new ArrayList<>();
6568
aspectList.add(aspect);
66-
if (target instanceof ItemStack itemStack)
69+
if (target instanceof ItemStack itemStack) {
6770
addScripted(new AspectListHelper(itemStack, aspectList));
68-
else if (target instanceof EntityEntry entityEntry)
71+
} else if (target instanceof EntityEntry entityEntry) {
6972
addScripted(new AspectListHelper(entityEntry, aspectList));
73+
}
7074
}
7175
}
7276

@@ -95,51 +99,50 @@ else if (target instanceof EntityEntry entityEntry)
9599

96100
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.add_entity", type = MethodDescription.Type.ADDITION)
97101
public void add(EntityEntry entity, AspectStack aspect) {
98-
this.add(entity, aspect, true);
99-
}
100-
101-
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.add_ore", type = MethodDescription.Type.ADDITION)
102-
public void add(OreDictIngredient oreDict, AspectStack aspect) {
103-
this.add(oreDict, aspect, true);
102+
add(entity, aspect, true);
104103
}
105104

106105
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.add_item", type = MethodDescription.Type.ADDITION)
107-
public void add(ItemStack item, AspectStack aspect) {
108-
this.add(item, aspect, true);
106+
public void add(IIngredient ingredient, AspectStack aspect) {
107+
add(ingredient, aspect, true);
109108
}
110109

111110
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.remove_entity")
112111
public void remove(EntityEntry entity, AspectStack aspect) {
113-
this.remove(entity, aspect, true);
112+
remove(entity, aspect, true);
114113
}
115114

116-
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.remove_ore")
117-
public void remove(OreDictIngredient oreDict, AspectStack aspect) {
118-
this.remove(oreDict, aspect, true);
115+
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.remove_item")
116+
public void remove(IIngredient ingredient, AspectStack aspect) {
117+
remove(ingredient, aspect, true);
119118
}
120119

121-
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.remove_item")
122-
public void remove(ItemStack item, AspectStack aspect) {
123-
this.remove(item, aspect, true);
120+
private static AspectList getAspects(ItemStack stack, boolean add) {
121+
int id = CommonInternals.generateUniqueItemstackId(stack);
122+
AspectList aspects = CommonInternals.objectTags.get(id);
123+
if (aspects == null) {
124+
aspects = new AspectList();
125+
if (add) CommonInternals.objectTags.put(id, aspects);
126+
}
127+
return aspects;
124128
}
125129

130+
@GroovyBlacklist
126131
@SuppressWarnings("deprecation")
127132
public void add(EntityEntry entity, AspectStack aspect, boolean doBackup) {
128133
if (entity != null && aspect != null) {
129-
AtomicBoolean found = new AtomicBoolean(false);
130-
CommonInternals.scanEntities.forEach(entityTags -> {
131-
if (entityTags.entityName.equals(entity.getName())) {
132-
entityTags.aspects.remove(aspect.getAspect());
133-
if (aspect.getAmount() != 0)
134-
entityTags.aspects.add(aspect.getAspect(), aspect.getAmount());
135-
found.set(true);
134+
boolean found = false;
135+
for (ThaumcraftApi.EntityTags tags : CommonInternals.scanEntities) {
136+
if (tags.entityName.equals(entity.getName())) {
137+
tags.aspects.remove(aspect.getAspect());
138+
if (aspect.getAmount() != 0) {
139+
tags.aspects.add(aspect.getAspect(), aspect.getAmount());
140+
}
141+
found = true;
136142
}
137-
});
138-
if (!found.get()) {
139-
ThaumcraftApi.registerEntityTag(
140-
entity.getName(),
141-
new AspectList().add(aspect.getAspect(), aspect.getAmount())
142-
);
143+
}
144+
if (!found) {
145+
ThaumcraftApi.registerEntityTag(entity.getName(), new AspectList().add(aspect.getAspect(), aspect.getAmount()));
143146
}
144147

145148
if (doBackup) addScripted(entity, aspect);
@@ -151,19 +154,13 @@ public void add(EntityEntry entity, AspectStack aspect, boolean doBackup) {
151154
.post();
152155
}
153156

154-
public void add(OreDictIngredient oreDic, AspectStack aspect, boolean doBackup) {
155-
if (oreDic != null && aspect != null) {
156-
List<ItemStack> ores = ThaumcraftApiHelper.getOresWithWildCards(oreDic.getOreDict());
157-
if (ores != null && !ores.isEmpty()) {
158-
159-
for (ItemStack ore : ores) {
160-
try {
161-
ItemStack oc = ore.copy();
162-
oc.setCount(1);
163-
this.add(oc, aspect, doBackup);
164-
} catch (Exception ignored) {
165-
}
166-
}
157+
@GroovyBlacklist
158+
public void add(IIngredient ingredient, AspectStack aspect, boolean doBackup) {
159+
if (ingredient != null && aspect != null) {
160+
for (ItemStack ore : ingredient.getMatchingStacks()) {
161+
ItemStack oc = ore.copy();
162+
oc.setCount(1);
163+
add(oc, aspect, doBackup);
167164
}
168165
return;
169166
}
@@ -172,34 +169,35 @@ public void add(OreDictIngredient oreDic, AspectStack aspect, boolean doBackup)
172169
.post();
173170
}
174171

172+
@GroovyBlacklist
175173
public void add(ItemStack item, AspectStack aspect, boolean doBackup) {
176174
if (item != null && aspect != null) {
177-
CommonInternals.objectTags.get(CommonInternals.generateUniqueItemstackId(item)).remove(aspect.getAspect());
178-
179-
if (aspect.getAmount() != 0)
180-
CommonInternals.objectTags.get(CommonInternals.generateUniqueItemstackId(item)).add(aspect.getAspect(), aspect.getAmount());
181-
175+
AspectList aspects = getAspects(item, aspect.getAmount() != 0);
176+
aspects.remove(aspect.getAspect());
177+
if (aspect.getAmount() != 0) {
178+
aspects.add(aspect.getAspect(), aspect.getAmount());
179+
}
182180
if (doBackup) addScripted(item, aspect);
183-
184181
return;
185182
}
186183
GroovyLog.msg("Error adding Thaumcraft Aspects from item/entity")
187184
.error()
188185
.post();
189186
}
190187

188+
@GroovyBlacklist
191189
public void remove(EntityEntry entity, AspectStack aspect, boolean doBackup) {
192190
if (entity != null && aspect != null) {
193-
CommonInternals.scanEntities.forEach(entityTags -> {
194-
if (entityTags.entityName.equals(entity.getName())) {
195-
for (Aspect a : entityTags.aspects.getAspects()) {
191+
for (ThaumcraftApi.EntityTags tags : CommonInternals.scanEntities) {
192+
if (tags.entityName.equals(entity.getName())) {
193+
for (Aspect a : tags.aspects.getAspects()) {
196194
if (a.equals(aspect.getAspect())) {
197-
aspect.setAmount(entityTags.aspects.getAmount(a));
198-
entityTags.aspects.remove(a);
195+
aspect.setAmount(tags.aspects.getAmount(a));
196+
tags.aspects.remove(a);
199197
}
200198
}
201199
}
202-
});
200+
}
203201

204202
if (doBackup) addBackup(entity, aspect);
205203

@@ -210,19 +208,13 @@ public void remove(EntityEntry entity, AspectStack aspect, boolean doBackup) {
210208
.post();
211209
}
212210

213-
public void remove(OreDictIngredient oreDic, AspectStack aspect, boolean doBackup) {
214-
if (oreDic != null && aspect != null) {
215-
List<ItemStack> ores = ThaumcraftApiHelper.getOresWithWildCards(oreDic.getOreDict());
216-
if (ores != null && !ores.isEmpty()) {
217-
218-
for (ItemStack ore : ores) {
219-
try {
220-
ItemStack oc = ore.copy();
221-
oc.setCount(1);
222-
this.remove(oc, aspect, doBackup);
223-
} catch (Exception ignored) {
224-
}
225-
}
211+
@GroovyBlacklist
212+
public void remove(IIngredient ingredient, AspectStack aspect, boolean doBackup) {
213+
if (ingredient != null && aspect != null) {
214+
for (ItemStack ore : ingredient.getMatchingStacks()) {
215+
ItemStack oc = ore.copy();
216+
oc.setCount(1);
217+
remove(oc, aspect, doBackup);
226218
}
227219
return;
228220
}
@@ -231,6 +223,7 @@ public void remove(OreDictIngredient oreDic, AspectStack aspect, boolean doBacku
231223
.post();
232224
}
233225

226+
@GroovyBlacklist
234227
public void remove(ItemStack item, AspectStack aspect, boolean doBackup) {
235228
if (item != null && aspect != null) {
236229
if (doBackup) {
@@ -253,7 +246,7 @@ public void removeAll(EntityEntry entity) {
253246
for (ThaumcraftApi.EntityTags e : CommonInternals.scanEntities) {
254247
if (e.entityName.equals(entity.getName())) {
255248
for (Aspect a : e.aspects.getAspects()) {
256-
this.remove(entity, new AspectStack(a, e.aspects.getAmount(a)));
249+
remove(entity, new AspectStack(a, e.aspects.getAmount(a)));
257250
}
258251
return;
259252
}
@@ -264,26 +257,19 @@ public void removeAll(EntityEntry entity) {
264257
.post();
265258
}
266259

267-
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.removeAll_ore")
268-
public void removeAll(OreDictIngredient oreDic) {
269-
List<ItemStack> ores = ThaumcraftApiHelper.getOresWithWildCards(oreDic.getOreDict());
270-
if (ores != null && !ores.isEmpty()) {
271-
272-
for (ItemStack ore : ores) {
273-
try {
274-
ItemStack oc = ore.copy();
275-
oc.setCount(1);
276-
this.removeAll(oc);
277-
} catch (Exception ignored) {
278-
}
279-
}
260+
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.removeAll_item")
261+
public void removeAll(IIngredient ingredient) {
262+
for (ItemStack stack : ingredient.getMatchingStacks()) {
263+
removeAll(stack);
280264
}
281265
}
282266

283267
@MethodDescription(description = "groovyscript.wiki.thaumcraft.aspect_helper.removeAll_item")
284268
public void removeAll(ItemStack target) {
285-
for (Aspect a : CommonInternals.objectTags.get(CommonInternals.generateUniqueItemstackId(target)).getAspects())
286-
this.remove(target, new AspectStack(a, CommonInternals.objectTags.get(CommonInternals.generateUniqueItemstackId(target)).getAmount(a)));
269+
AspectList aspects = getAspects(target, false);
270+
for (Aspect a : aspects.getAspects()) {
271+
remove(target, new AspectStack(a, aspects.getAmount(a)), true);
272+
}
287273
}
288274

289275
@RecipeBuilderDescription(example = {
@@ -314,6 +300,11 @@ public AspectHelperBuilder entity(EntityEntry entity) {
314300

315301
@RecipeBuilderMethodDescription
316302
public AspectHelperBuilder object(IIngredient object) {
303+
return ingredient(object);
304+
}
305+
306+
@RecipeBuilderMethodDescription(field = "object")
307+
public AspectHelperBuilder ingredient(IIngredient object) {
317308
this.object = object;
318309
return this;
319310
}
@@ -333,36 +324,33 @@ public AspectHelperBuilder aspect(String tag, int amount) {
333324

334325
@RecipeBuilderMethodDescription
335326
public AspectHelperBuilder stripAspects() {
336-
stripAspects = !stripAspects;
327+
this.stripAspects = !this.stripAspects;
337328
return this;
338329
}
339330

340331
@RecipeBuilderRegistrationMethod
341332
public void register() {
342-
if (stripAspects) {
343-
if (entity != null) {
344-
ModSupport.THAUMCRAFT.get().aspectHelper.removeAll(entity);
345-
} else if (object != null && object instanceof OreDictIngredient oreDictIngredient) {
346-
ModSupport.THAUMCRAFT.get().aspectHelper.removeAll(oreDictIngredient);
347-
} else if (object != null && IngredientHelper.isItem(object) && !IngredientHelper.isEmpty(object)) {
348-
ModSupport.THAUMCRAFT.get().aspectHelper.removeAll(IngredientHelper.toItemStack(object));
333+
if (this.stripAspects) {
334+
if (this.entity != null) {
335+
ModSupport.THAUMCRAFT.get().aspectHelper.removeAll(this.entity);
336+
} else if (this.object != null) {
337+
ModSupport.THAUMCRAFT.get().aspectHelper.removeAll(this.object);
349338
} else {
350339
GroovyLog.msg("Error removing Thaumcraft Aspects from item/entity")
351340
.error()
352341
.post();
353342
}
354343
}
355-
aspects.forEach(aspectStack -> {
356-
if (entity != null)
357-
ModSupport.THAUMCRAFT.get().aspectHelper.add(entity, aspectStack);
358-
else if (object != null && object instanceof OreDictIngredient oreDictIngredient)
359-
ModSupport.THAUMCRAFT.get().aspectHelper.add(oreDictIngredient, aspectStack);
360-
else if (object != null && IngredientHelper.isItem(object) && !IngredientHelper.isEmpty(object))
361-
ModSupport.THAUMCRAFT.get().aspectHelper.add(IngredientHelper.toItemStack(object), aspectStack);
362-
else
344+
this.aspects.forEach(aspectStack -> {
345+
if (this.entity != null) {
346+
ModSupport.THAUMCRAFT.get().aspectHelper.add(this.entity, aspectStack);
347+
} else if (this.object != null) {
348+
ModSupport.THAUMCRAFT.get().aspectHelper.add(this.object, aspectStack);
349+
} else {
363350
GroovyLog.msg("Error adding Thaumcraft Aspects to item/entity")
364351
.error()
365352
.post();
353+
}
366354
});
367355
}
368356
}

src/main/resources/assets/groovyscript/lang/en_us.lang

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3085,14 +3085,11 @@ groovyscript.wiki.thaumcraft.aspect_helper.object.value=Sets the item being modi
30853085
groovyscript.wiki.thaumcraft.aspect_helper.aspects.value=Sets the Aspects of the entity or item
30863086
groovyscript.wiki.thaumcraft.aspect_helper.stripAspects.value=Sets if all pre-existing Aspects should be removed from the entity or item before adding Aspects, if any are added
30873087
groovyscript.wiki.thaumcraft.aspect_helper.add_entity=Adds an Aspect to the given entity in the format `entity`, `aspect`
3088-
groovyscript.wiki.thaumcraft.aspect_helper.add_ore=Adds an Aspect to the given oredict in the format `oreDict`, `aspect`
3089-
groovyscript.wiki.thaumcraft.aspect_helper.add_item=Adds an Aspect to the given itemstack in the format `item`, `aspect`
3088+
groovyscript.wiki.thaumcraft.aspect_helper.add_item=Adds an Aspect to the item stacks matching the given ingredient in the format `ingredient`, `aspect`
30903089
groovyscript.wiki.thaumcraft.aspect_helper.remove_entity=Removes an Aspect from the given entity in the format `entity`, `aspect`
3091-
groovyscript.wiki.thaumcraft.aspect_helper.remove_ore=Removes an Aspect from the given oredict in the format `oreDict`, `aspect`
3092-
groovyscript.wiki.thaumcraft.aspect_helper.remove_item=Removes an Aspect from the given itemstack in the format `item`, `aspect`
3090+
groovyscript.wiki.thaumcraft.aspect_helper.remove_item=Removes an Aspect from the item stacks matching the given ingredient in the format `item`, `aspect`
30933091
groovyscript.wiki.thaumcraft.aspect_helper.removeAll_entity=Removes all Aspects from the given entity
3094-
groovyscript.wiki.thaumcraft.aspect_helper.removeAll_ore=Removes all Aspects from the given oredict
3095-
groovyscript.wiki.thaumcraft.aspect_helper.removeAll_item=Removes all Aspects from the given itemstack
3092+
groovyscript.wiki.thaumcraft.aspect_helper.removeAll_item=Removes all Aspects from the item stacks matching the given ingredient
30963093

30973094
groovyscript.wiki.thaumcraft.crucible.title=Crucible
30983095
groovyscript.wiki.thaumcraft.crucible.description=Combines an item with any number of Aspects to drop an output itemstack, potentially requiring a specific research to be completed.

0 commit comments

Comments
 (0)