|
| 1 | +package com.cleanroommc.groovyscript.compat.mods.horsepower; |
| 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.recipe.AbstractRecipeBuilder; |
| 9 | +import com.cleanroommc.groovyscript.registry.StandardListRegistry; |
| 10 | +import net.minecraft.item.ItemStack; |
| 11 | +import org.jetbrains.annotations.ApiStatus; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | +import se.gory_moon.horsepower.recipes.ChoppingBlockRecipe; |
| 14 | +import se.gory_moon.horsepower.recipes.HPRecipes; |
| 15 | + |
| 16 | +import java.util.Collection; |
| 17 | + |
| 18 | +@RegistryDescription |
| 19 | +public class ChoppingBlock extends StandardListRegistry<ChoppingBlockRecipe> { |
| 20 | + |
| 21 | + @RecipeBuilderDescription(example = { |
| 22 | + @Example(".input(item('minecraft:clay')).output(item('minecraft:diamond') * 5).time(5)"), |
| 23 | + @Example(".input(item('minecraft:diamond')).output(item('minecraft:gold_ingot')).time(1)"), |
| 24 | + @Example(".input(item('minecraft:gold_ingot')).output(item('minecraft:clay'), item('minecraft:diamond')).chance(50).time(2)") |
| 25 | + }) |
| 26 | + public static RecipeBuilder recipeBuilder() { |
| 27 | + return new RecipeBuilder(); |
| 28 | + } |
| 29 | + |
| 30 | + // have to override onReload and add to add recipes to the registry due to the actual setup being a map |
| 31 | + @Override |
| 32 | + public Collection<ChoppingBlockRecipe> getRecipes() { |
| 33 | + return HPRecipes.instance().getChoppingRecipes(); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + @GroovyBlacklist |
| 38 | + @ApiStatus.Internal |
| 39 | + public void onReload() { |
| 40 | + var recipes = getRecipes(); |
| 41 | + recipes.removeAll(removeScripted()); |
| 42 | + for (var recipe : restoreFromBackup()) { |
| 43 | + HPRecipes.instance().addChoppingRecipe(recipe, false); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + @MethodDescription(type = MethodDescription.Type.ADDITION, description = "groovyscript.wiki.add_to_list", priority = 500) |
| 49 | + public boolean add(ChoppingBlockRecipe recipe) { |
| 50 | + HPRecipes.instance().addChoppingRecipe(recipe, false); |
| 51 | + return recipe != null && doAddScripted(recipe); |
| 52 | + } |
| 53 | + |
| 54 | + @MethodDescription(description = "groovyscript.wiki.horsepower.chopping_block.add0", type = MethodDescription.Type.ADDITION) |
| 55 | + public ChoppingBlockRecipe add(IIngredient input, ItemStack output, int time) { |
| 56 | + return recipeBuilder() |
| 57 | + .time(time) |
| 58 | + .output(output) |
| 59 | + .input(input) |
| 60 | + .register(); |
| 61 | + } |
| 62 | + |
| 63 | + @MethodDescription(description = "groovyscript.wiki.horsepower.chopping_block.add1", type = MethodDescription.Type.ADDITION) |
| 64 | + public ChoppingBlockRecipe add(IIngredient input, ItemStack output, ItemStack secondary, int chance, int time) { |
| 65 | + return recipeBuilder() |
| 66 | + .time(time) |
| 67 | + .chance(chance) |
| 68 | + .output(output, secondary) |
| 69 | + .input(input) |
| 70 | + .register(); |
| 71 | + } |
| 72 | + |
| 73 | + @MethodDescription(example = @Example("item('minecraft:log:3')")) |
| 74 | + public boolean removeByInput(IIngredient input) { |
| 75 | + return getRecipes().removeIf(entry -> input.test(entry.getInput()) && doAddBackup(entry)); |
| 76 | + } |
| 77 | + |
| 78 | + @MethodDescription(example = @Example("item('minecraft:planks:4')")) |
| 79 | + public boolean removeByOutput(IIngredient output) { |
| 80 | + return getRecipes().removeIf(entry -> (output.test(entry.getOutput()) || output.test(entry.getSecondary())) && doAddBackup(entry)); |
| 81 | + } |
| 82 | + |
| 83 | + @Property(property = "input", comp = @Comp(eq = 1)) |
| 84 | + @Property(property = "output", comp = @Comp(gte = 1, lte = 2)) |
| 85 | + public static class RecipeBuilder extends AbstractRecipeBuilder<ChoppingBlockRecipe> { |
| 86 | + |
| 87 | + @Property(comp = @Comp(gte = 0, lte = 100)) |
| 88 | + private int chance; |
| 89 | + @Property(comp = @Comp(gt = 0)) |
| 90 | + private int time; |
| 91 | + |
| 92 | + @RecipeBuilderMethodDescription |
| 93 | + public RecipeBuilder chance(int chance) { |
| 94 | + this.chance = chance; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + @RecipeBuilderMethodDescription |
| 99 | + public RecipeBuilder time(int time) { |
| 100 | + this.time = time; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + // todo this can only handle an input with a size of 1, should validate that here |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getErrorMsg() { |
| 108 | + return "Error adding Horse Power Chopping Block recipe"; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void validate(GroovyLog.Msg msg) { |
| 113 | + validateItems(msg, 1, 1, 1, 2); |
| 114 | + validateFluids(msg); |
| 115 | + msg.add(chance < 0 || chance > 100, "chance must be a non negative integer less than 100, yet it was {}", chance); |
| 116 | + msg.add(time <= 0, "time must be greater than 0, yet it was {}", time); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + @RecipeBuilderRegistrationMethod |
| 121 | + public @Nullable ChoppingBlockRecipe register() { |
| 122 | + if (!validate()) return null; |
| 123 | + ChoppingBlockRecipe recipe = null; |
| 124 | + for (var stack : input.get(0).getMatchingStacks()) { |
| 125 | + recipe = new ChoppingBlockRecipe(stack, output.get(0), output.getOrEmpty(1), chance, time); |
| 126 | + ModSupport.HORSE_POWER.get().choppingBlock.add(recipe); |
| 127 | + } |
| 128 | + return recipe; |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments