|
| 1 | +package com.cleanroommc.groovyscript.compat.mods.immersivepetroleum; |
| 2 | + |
| 3 | +import com.cleanroommc.groovyscript.api.GroovyBlacklist; |
| 4 | +import com.cleanroommc.groovyscript.api.GroovyLog; |
| 5 | +import com.cleanroommc.groovyscript.api.IIngredient; |
| 6 | +import com.cleanroommc.groovyscript.api.documentation.annotations.*; |
| 7 | +import com.cleanroommc.groovyscript.compat.mods.ModSupport; |
| 8 | +import com.cleanroommc.groovyscript.helper.SimpleObjectStream; |
| 9 | +import com.cleanroommc.groovyscript.helper.recipe.AbstractRecipeBuilder; |
| 10 | +import com.cleanroommc.groovyscript.registry.VirtualizedRegistry; |
| 11 | +import flaxbeard.immersivepetroleum.api.crafting.DistillationRecipe; |
| 12 | +import it.unimi.dsi.fastutil.floats.FloatArrayList; |
| 13 | +import net.minecraft.item.ItemStack; |
| 14 | +import net.minecraftforge.fluids.FluidStack; |
| 15 | +import org.jetbrains.annotations.ApiStatus; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +@RegistryDescription |
| 19 | +public class Distillation extends VirtualizedRegistry<DistillationRecipe> { |
| 20 | + |
| 21 | + @RecipeBuilderDescription(example = { |
| 22 | + @Example(".fluidInput(fluid('water') * 100).fluidOutput(fluid('water') * 50, fluid('lava') * 30).output(item('minecraft:diamond'), 0.5).output(item('minecraft:clay'), 0.2).output(item('minecraft:diamond'), 0.1).output(item('minecraft:clay'), 0.5).output(item('minecraft:diamond') * 5, 0.01).time(5).energy(1000)"), |
| 23 | + @Example(".fluidInput(fluid('lava') * 5).output(item('minecraft:diamond')).time(1)") |
| 24 | + }) |
| 25 | + public RecipeBuilder recipeBuilder() { |
| 26 | + return new RecipeBuilder(); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + @GroovyBlacklist |
| 31 | + @ApiStatus.Internal |
| 32 | + public void onReload() { |
| 33 | + removeScripted().forEach(recipe -> DistillationRecipe.recipeList.removeIf(r -> r == recipe)); |
| 34 | + DistillationRecipe.recipeList.addAll(restoreFromBackup()); |
| 35 | + } |
| 36 | + |
| 37 | + public void add(DistillationRecipe recipe) { |
| 38 | + if (recipe != null) { |
| 39 | + addScripted(recipe); |
| 40 | + DistillationRecipe.recipeList.add(recipe); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public boolean remove(DistillationRecipe recipe) { |
| 45 | + if (DistillationRecipe.recipeList.removeIf(r -> r == recipe)) { |
| 46 | + addBackup(recipe); |
| 47 | + return true; |
| 48 | + } |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + @MethodDescription(example = { |
| 53 | + @Example(value = "item('immersivepetroleum:material')", commented = true), |
| 54 | + @Example(value = "fluid('lubricant')", commented = true) |
| 55 | + }) |
| 56 | + public void removeByOutput(IIngredient output) { |
| 57 | + DistillationRecipe.recipeList.removeIf(r -> { |
| 58 | + for (ItemStack itemstack : r.getItemOutputs()) { |
| 59 | + if (output.test(itemstack)) { |
| 60 | + addBackup(r); |
| 61 | + return true; |
| 62 | + } |
| 63 | + } |
| 64 | + for (FluidStack fluidStack : r.getFluidOutputs()) { |
| 65 | + if (output.test(fluidStack)) { |
| 66 | + addBackup(r); |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + return false; |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @MethodDescription(example = @Example("fluid('oil')")) |
| 75 | + public void removeByInput(IIngredient input) { |
| 76 | + DistillationRecipe.recipeList.removeIf(r -> { |
| 77 | + for (FluidStack fluidStack : r.getFluidInputs()) { |
| 78 | + if (input.test(fluidStack)) { |
| 79 | + addBackup(r); |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + return false; |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + @MethodDescription(type = MethodDescription.Type.QUERY) |
| 88 | + public SimpleObjectStream<DistillationRecipe> streamRecipes() { |
| 89 | + return new SimpleObjectStream<>(DistillationRecipe.recipeList).setRemover(this::remove); |
| 90 | + } |
| 91 | + |
| 92 | + @MethodDescription(priority = 2000, example = @Example(commented = true)) |
| 93 | + public void removeAll() { |
| 94 | + DistillationRecipe.recipeList.forEach(this::addBackup); |
| 95 | + DistillationRecipe.recipeList.clear(); |
| 96 | + } |
| 97 | + |
| 98 | + @Property(property = "fluidInput", valid = @Comp("1")) |
| 99 | + @Property(property = "output", valid = {@Comp(type = Comp.Type.GTE, value = "0"), @Comp(type = Comp.Type.LTE, value = "Integer.MAX_VALUE")}) |
| 100 | + @Property(property = "fluidOutput", valid = {@Comp(type = Comp.Type.GTE, value = "0"), @Comp(type = Comp.Type.LTE, value = "Integer.MAX_VALUE")}) |
| 101 | + public static class RecipeBuilder extends AbstractRecipeBuilder<DistillationRecipe> { |
| 102 | + |
| 103 | + @Property(valid = @Comp(value = "0", type = Comp.Type.GTE)) |
| 104 | + private final FloatArrayList chance = new FloatArrayList(); |
| 105 | + @Property(valid = @Comp(value = "0", type = Comp.Type.GTE)) |
| 106 | + private int time; |
| 107 | + @Property(valid = @Comp(value = "0", type = Comp.Type.GTE)) |
| 108 | + private int energy; |
| 109 | + |
| 110 | + @RecipeBuilderMethodDescription(field = {"output", "chance"}) |
| 111 | + public RecipeBuilder output(ItemStack output, float chance) { |
| 112 | + this.output.add(output); |
| 113 | + this.chance.add(chance); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + @RecipeBuilderMethodDescription(field = {"output", "chance"}) |
| 119 | + public RecipeBuilder output(ItemStack output) { |
| 120 | + return this.output(output, 1); |
| 121 | + } |
| 122 | + |
| 123 | + @RecipeBuilderMethodDescription |
| 124 | + public RecipeBuilder time(int time) { |
| 125 | + this.time = time; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + @RecipeBuilderMethodDescription |
| 130 | + public RecipeBuilder energy(int energy) { |
| 131 | + this.energy = energy; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public String getErrorMsg() { |
| 137 | + return "Error adding Immersive Petroleum Distillation recipe"; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void validate(GroovyLog.Msg msg) { |
| 142 | + validateItems(msg, 0, 0, 0, Integer.MAX_VALUE); |
| 143 | + validateFluids(msg, 1, 1, 0, Integer.MAX_VALUE); |
| 144 | + chance.trim(); |
| 145 | + msg.add(output.size() != chance.size(), "output and chance must be of equal length, yet output was {} and chance was {}", output.size(), chance.size()); |
| 146 | + msg.add(time <= 0, "time must be greater than or equal to 1, yet it was {}", time); |
| 147 | + msg.add(energy < 0, "energy must be a non negative integer, yet it was {}", energy); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + @RecipeBuilderRegistrationMethod |
| 152 | + public @Nullable DistillationRecipe register() { |
| 153 | + if (!validate()) return null; |
| 154 | + DistillationRecipe recipe = new DistillationRecipe(fluidOutput.toArray(new FluidStack[0]), output.toArray(new ItemStack[0]), fluidInput.get(0), energy, time, chance.elements()); |
| 155 | + ModSupport.IMMERSIVE_PETROLEUM.get().distillation.add(recipe); |
| 156 | + return recipe; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments