|
| 1 | +package com.cleanroommc.groovyscript.compat.mods.bloodarsenal; |
| 2 | + |
| 3 | +import arcaratus.bloodarsenal.recipe.RecipeFilter; |
| 4 | +import arcaratus.bloodarsenal.recipe.RecipeSanguineInfusion; |
| 5 | +import arcaratus.bloodarsenal.recipe.SanguineInfusionRecipeRegistry; |
| 6 | +import com.cleanroommc.groovyscript.api.GroovyLog; |
| 7 | +import com.cleanroommc.groovyscript.api.IIngredient; |
| 8 | +import com.cleanroommc.groovyscript.api.documentation.annotations.*; |
| 9 | +import com.cleanroommc.groovyscript.compat.mods.ModSupport; |
| 10 | +import com.cleanroommc.groovyscript.helper.Alias; |
| 11 | +import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; |
| 12 | +import com.cleanroommc.groovyscript.helper.recipe.AbstractRecipeBuilder; |
| 13 | +import com.cleanroommc.groovyscript.registry.AbstractReloadableStorage; |
| 14 | +import com.cleanroommc.groovyscript.registry.StandardListRegistry; |
| 15 | +import net.minecraft.item.crafting.Ingredient; |
| 16 | +import org.apache.commons.lang3.tuple.Pair; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +@RegistryDescription(admonition = @Admonition("groovyscript.wiki.bloodarsenal.sanguine_infusion.note0")) |
| 24 | +public class SanguineInfusion extends StandardListRegistry<RecipeSanguineInfusion> { |
| 25 | + |
| 26 | + @SuppressWarnings("rawtypes") |
| 27 | + private final AbstractReloadableStorage<Class> blacklistStorage = new AbstractReloadableStorage<>(); |
| 28 | + |
| 29 | + public SanguineInfusion() { |
| 30 | + super(Alias.generateOfClassAnd(SanguineInfusion.class, "Infusion")); |
| 31 | + } |
| 32 | + |
| 33 | + private static boolean containsInput(IIngredient input, List<Pair<Ingredient, Integer>> list) { |
| 34 | + for (var pair : list) { |
| 35 | + for (var stack : pair.getKey().getMatchingStacks()) { |
| 36 | + if (input.test(stack)) return true; |
| 37 | + } |
| 38 | + } |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Collection<RecipeSanguineInfusion> getRecipes() { |
| 44 | + return SanguineInfusionRecipeRegistry.getInfusionRecipes(); |
| 45 | + } |
| 46 | + |
| 47 | + @SuppressWarnings("rawtypes") |
| 48 | + public Collection<Class> getBlacklistedClasses() { |
| 49 | + return SanguineInfusionRecipeRegistry.getBlacklistedClasses(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onReload() { |
| 54 | + super.onReload(); |
| 55 | + var list = getBlacklistedClasses(); |
| 56 | + list.removeAll(blacklistStorage.removeScripted()); |
| 57 | + list.addAll(blacklistStorage.restoreFromBackup()); |
| 58 | + } |
| 59 | + |
| 60 | + @RecipeBuilderDescription(example = { |
| 61 | + @Example(".infuse(item('minecraft:gold_ingot')).input(item('minecraft:clay')).output(item('minecraft:diamond')).cost(1000)"), |
| 62 | + @Example(".infuse(item('minecraft:emerald')).input(item('minecraft:clay') * 64, item('minecraft:clay') * 64, item('minecraft:clay') * 64, item('minecraft:clay') * 64, item('minecraft:clay') * 64, item('minecraft:clay') * 64, item('minecraft:clay') * 64, item('minecraft:clay') * 64).output(item('minecraft:diamond') * 64).cost(5000)"), |
| 63 | + @Example(".infuse(item('minecraft:gold_ingot')).input(item('minecraft:clay'), item('minecraft:diamond')).output(item('minecraft:diamond'))"), |
| 64 | + @Example(".input(item('minecraft:gold_ingot') * 2, item('minecraft:gold_ingot') * 2, item('minecraft:gold_ingot') * 2, item('minecraft:gold_ingot') * 2, item('minecraft:gold_ingot') * 2, item('minecraft:gold_ingot') * 2, item('minecraft:gold_ingot') * 2, item('minecraft:gold_ingot') * 2).modifier('xperienced').levelMultiplier(3).cost(3000)") |
| 65 | + }) |
| 66 | + public RecipeBuilder recipeBuilder() { |
| 67 | + return new RecipeBuilder(); |
| 68 | + } |
| 69 | + |
| 70 | + @MethodDescription(example = { |
| 71 | + @Example("item('minecraft:feather')"), @Example("item('bloodmagic:bound_axe')") |
| 72 | + }) |
| 73 | + public void removeByInput(IIngredient input) { |
| 74 | + getRecipes().removeIf(r -> (input.test(r.getInfuse()) || containsInput(input, r.getInputs())) && doAddBackup(r)); |
| 75 | + } |
| 76 | + |
| 77 | + @MethodDescription(example = @Example("item('bloodarsenal:stasis_pickaxe')")) |
| 78 | + public void removeByOutput(IIngredient output) { |
| 79 | + getRecipes().removeIf(r -> output.test(r.getOutput()) && doAddBackup(r)); |
| 80 | + } |
| 81 | + |
| 82 | + @MethodDescription(example = @Example(value = "'beneficial_potion'", commented = true)) |
| 83 | + public void removeByModifierKey(String key) { |
| 84 | + getRecipes().removeIf(r -> key.equals(r.getModifierKey()) && doAddBackup(r)); |
| 85 | + } |
| 86 | + |
| 87 | + @MethodDescription(type = MethodDescription.Type.ADDITION, example = @Example(value = "WayofTime.bloodmagic.iface.ISigil.class", commented = true)) |
| 88 | + public boolean addBlacklist(@SuppressWarnings("rawtypes") Class clazz) { |
| 89 | + return clazz != null && getBlacklistedClasses().add(clazz) && blacklistStorage.addScripted(clazz); |
| 90 | + } |
| 91 | + |
| 92 | + @MethodDescription(example = @Example(value = "WayofTime.bloodmagic.iface.ISigil.class", commented = true)) |
| 93 | + public boolean removeBlacklist(@SuppressWarnings("rawtypes") Class clazz) { |
| 94 | + return clazz != null && getBlacklistedClasses().removeIf(x -> x == clazz) && blacklistStorage.addBackup(clazz); |
| 95 | + } |
| 96 | + |
| 97 | + @MethodDescription(priority = 2000, example = @Example(commented = true)) |
| 98 | + public void removeAllBlacklist() { |
| 99 | + var list = getBlacklistedClasses(); |
| 100 | + list.forEach(blacklistStorage::addBackup); |
| 101 | + list.clear(); |
| 102 | + } |
| 103 | + |
| 104 | + @Property(property = "input", comp = @Comp(gte = 1, lte = 8)) |
| 105 | + @Property(property = "output", comp = @Comp(gte = 0, lte = 1)) |
| 106 | + public static class RecipeBuilder extends AbstractRecipeBuilder<RecipeSanguineInfusion> { |
| 107 | + |
| 108 | + @Property(comp = @Comp(gte = 0)) |
| 109 | + private int levelMultiplier; |
| 110 | + @Property(comp = @Comp(gte = 0)) |
| 111 | + private int cost; |
| 112 | + @Property |
| 113 | + private String modifier; |
| 114 | + @Property |
| 115 | + private IIngredient infuse; |
| 116 | + @Property |
| 117 | + private IIngredient filter; |
| 118 | + |
| 119 | + @RecipeBuilderMethodDescription |
| 120 | + public RecipeBuilder levelMultiplier(int levelMultiplier) { |
| 121 | + this.levelMultiplier = levelMultiplier; |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + @RecipeBuilderMethodDescription |
| 126 | + public RecipeBuilder cost(int cost) { |
| 127 | + this.cost = cost; |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + @RecipeBuilderMethodDescription |
| 132 | + public RecipeBuilder modifier(String modifier) { |
| 133 | + this.modifier = modifier; |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + @RecipeBuilderMethodDescription |
| 138 | + public RecipeBuilder infuse(IIngredient infuse) { |
| 139 | + this.infuse = infuse; |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + @RecipeBuilderMethodDescription |
| 144 | + public RecipeBuilder filter(IIngredient filter) { |
| 145 | + this.filter = filter; |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String getErrorMsg() { |
| 151 | + return "Error adding Blood Arsenal Sanguine Infusion recipe"; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void validate(GroovyLog.Msg msg) { |
| 156 | + validateFluids(msg); |
| 157 | + |
| 158 | + if (modifier == null) { |
| 159 | + validateItems(msg, 1, 8, 1, 1); |
| 160 | + msg.add(infuse == null, "infuse was null and modifier was null, yet one must be defined"); |
| 161 | + msg.add(levelMultiplier != 0, "levelMultiplier was set to the non-default value of {} while modifier was null, yet it has no effect without modifier being set", levelMultiplier); |
| 162 | + } else { |
| 163 | + validateItems(msg, 1, 8, 0, 0); |
| 164 | + msg.add(infuse != null, "infuse was not null and modifier was not null, yet only one must be defined"); |
| 165 | + msg.add(filter != null, "filter was not null and modifier was not null, yet filter has no effect when modifier is not null"); |
| 166 | + msg.add(levelMultiplier < 0, "levelMultiplier must be a nonnegative integer, yet it was {}", levelMultiplier); |
| 167 | + } |
| 168 | + |
| 169 | + msg.add(cost < 0, "cost must be a nonnegative integer, yet it was {}", cost); |
| 170 | + } |
| 171 | + |
| 172 | + @SuppressWarnings("unchecked") |
| 173 | + @Override |
| 174 | + @RecipeBuilderRegistrationMethod |
| 175 | + public @Nullable RecipeSanguineInfusion register() { |
| 176 | + if (!validate()) return null; |
| 177 | + List<Pair<Object, Integer>> inputs = new ArrayList<>(); |
| 178 | + for (var ingredient : input) { |
| 179 | + if (ingredient instanceof OreDictIngredient ore) { |
| 180 | + inputs.add(Pair.of(ore.getOreDict(), ingredient.getAmount())); |
| 181 | + } else { |
| 182 | + inputs.add(Pair.of(ingredient.getMatchingStacks()[0], ingredient.getAmount())); |
| 183 | + } |
| 184 | + } |
| 185 | + if (filter != null) inputs.add(Pair.of(new RecipeFilter(filter), 0)); |
| 186 | + |
| 187 | + RecipeSanguineInfusion recipe = null; |
| 188 | + if (modifier == null) { |
| 189 | + for (var stack : infuse.getMatchingStacks()) { |
| 190 | + recipe = new RecipeSanguineInfusion(output.get(0), cost, stack, inputs.toArray(new Pair[0])); |
| 191 | + ModSupport.BLOOD_ARSENAL.get().sanguineInfusion.add(recipe); |
| 192 | + } |
| 193 | + } else { |
| 194 | + recipe = new RecipeSanguineInfusion(cost, modifier, inputs.toArray(new Pair[0])); |
| 195 | + recipe.setLevelMultiplier(levelMultiplier); |
| 196 | + ModSupport.BLOOD_ARSENAL.get().sanguineInfusion.add(recipe); |
| 197 | + } |
| 198 | + return recipe; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments