|
1 | 1 | package com.cleanroommc.groovyscript.helper.ingredient; |
2 | 2 |
|
3 | 3 | import com.cleanroommc.groovyscript.api.IIngredient; |
4 | | -import com.cleanroommc.groovyscript.sandbox.ClosureHelper; |
5 | | -import groovy.lang.Closure; |
| 4 | +import com.cleanroommc.groovyscript.compat.vanilla.ItemStackMixinExpansion; |
| 5 | +import com.cleanroommc.groovyscript.compat.vanilla.ItemStackTransformer; |
6 | 6 | import net.minecraft.item.ItemStack; |
| 7 | +import net.minecraft.nbt.NBTTagCompound; |
7 | 8 | import net.minecraftforge.common.ForgeHooks; |
| 9 | +import net.minecraftforge.event.ForgeEventFactory; |
8 | 10 | import org.jetbrains.annotations.Nullable; |
9 | 11 |
|
| 12 | +import java.util.function.Predicate; |
| 13 | +import java.util.function.UnaryOperator; |
| 14 | + |
10 | 15 | public abstract class IngredientBase implements IIngredient { |
11 | 16 |
|
12 | | - protected Closure<Object> matchCondition; |
13 | | - protected Closure<Object> transformer; |
| 17 | + protected Predicate<ItemStack> matchCondition; |
| 18 | + protected ItemStackTransformer transformer; |
14 | 19 | protected String mark; |
15 | 20 |
|
16 | | - public IngredientBase when(Closure<Object> matchCondition) { |
| 21 | + public IngredientBase when(Predicate<ItemStack> matchCondition) { |
17 | 22 | IngredientBase fresh = (IngredientBase) this.exactCopy(); |
18 | 23 | fresh.matchCondition = matchCondition; |
19 | 24 | return fresh; |
20 | 25 | } |
21 | 26 |
|
22 | | - public IngredientBase transform(Closure<Object> transformer) { |
| 27 | + public IngredientBase transform(ItemStackTransformer transformer) { |
23 | 28 | IngredientBase fresh = (IngredientBase) this.exactCopy(); |
24 | 29 | fresh.transformer = transformer; |
25 | 30 | return fresh; |
26 | 31 | } |
27 | 32 |
|
| 33 | + public IngredientBase transformDamage(int amount) { |
| 34 | + // reliably set itemDamage field of item stack |
| 35 | + return transform(self -> IngredientHelper.damageItem(self, amount)); |
| 36 | + } |
| 37 | + |
| 38 | + public IngredientBase transformDamage() { |
| 39 | + return transformDamage(1); |
| 40 | + } |
| 41 | + |
| 42 | + public IngredientBase transformNbt(UnaryOperator<NBTTagCompound> transformer) { |
| 43 | + return transform(self -> { |
| 44 | + ItemStackMixinExpansion.of(self).exactCopy().grs$getItemStack().setTagCompound(transformer.apply(self.getTagCompound())); |
| 45 | + return self; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
28 | 49 | public IngredientBase reuse() { |
29 | | - return transform(IngredientHelper.REUSE); |
| 50 | + return transform(self -> self); |
30 | 51 | } |
31 | 52 |
|
32 | 53 | public IngredientBase noReturn() { |
33 | | - return transform(IngredientHelper.NO_RETURN); |
| 54 | + return transform(self -> ItemStack.EMPTY); |
| 55 | + } |
| 56 | + |
| 57 | + public IngredientBase transform(ItemStack stack) { |
| 58 | + return transform(self -> stack); |
34 | 59 | } |
35 | 60 |
|
36 | 61 | @Override |
37 | 62 | public boolean test(ItemStack itemStack) { |
38 | | - return (matchCondition == null || ClosureHelper.call(true, matchCondition, itemStack)) && matches(itemStack); |
| 63 | + return (this.matchCondition == null || this.matchCondition.test(itemStack)) && matches(itemStack); |
39 | 64 | } |
40 | 65 |
|
41 | 66 | public abstract boolean matches(ItemStack itemStack); |
42 | 67 |
|
43 | 68 | @Override |
44 | 69 | public ItemStack applyTransform(ItemStack matchedInput) { |
45 | | - if (transformer != null) { |
46 | | - return ClosureHelper.call(ItemStack.EMPTY, transformer, matchedInput); |
| 70 | + if (this.transformer != null) { |
| 71 | + ItemStack result = this.transformer.transform(matchedInput); |
| 72 | + if (result == null || result.isEmpty()) return ItemStack.EMPTY; |
| 73 | + if (result.isItemStackDamageable() && result.getMetadata() > result.getMaxDamage()) { |
| 74 | + ForgeEventFactory.onPlayerDestroyItem(ForgeHooks.getCraftingPlayer(), result, null); |
| 75 | + return ItemStack.EMPTY; |
| 76 | + } |
| 77 | + return result.copy(); |
47 | 78 | } |
48 | 79 | return ForgeHooks.getContainerItem(matchedInput); |
49 | 80 | } |
|
0 commit comments