|
| 1 | +package com.cleanroommc.groovyscript.compat.mods.futuremc; |
| 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 it.unimi.dsi.fastutil.objects.Object2ByteMap; |
| 12 | +import net.minecraft.init.Items; |
| 13 | +import net.minecraft.item.ItemStack; |
| 14 | +import net.minecraft.item.crafting.Ingredient; |
| 15 | +import org.apache.commons.lang3.tuple.Pair; |
| 16 | +import org.jetbrains.annotations.ApiStatus; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | +import thedarkcolour.futuremc.block.villagepillage.ComposterBlock; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +@RegistryDescription(category = RegistryDescription.Category.ENTRIES) |
| 24 | +public class Composter extends VirtualizedRegistry<Map.Entry<Ingredient, Byte>> { |
| 25 | + |
| 26 | + @Override |
| 27 | + @GroovyBlacklist |
| 28 | + @ApiStatus.Internal |
| 29 | + public void onReload() { |
| 30 | + var instance = ComposterBlock.ItemsForComposter.INSTANCE; |
| 31 | + var entries = instance.getEntries(); |
| 32 | + removeScripted().forEach(entry -> entries.removeIf(entry.getKey()::equals)); |
| 33 | + restoreFromBackup().forEach(x -> instance.add(x.getKey(), x.getValue())); |
| 34 | + } |
| 35 | + |
| 36 | + @RecipeBuilderDescription(example = { |
| 37 | + @Example(".input(item('minecraft:clay')).chance(100)"), |
| 38 | + @Example(".input(item('minecraft:gold_ingot')).chance(30)") |
| 39 | + }) |
| 40 | + public RecipeBuilder recipeBuilder() { |
| 41 | + return new RecipeBuilder(); |
| 42 | + } |
| 43 | + |
| 44 | + @MethodDescription(type = MethodDescription.Type.ADDITION) |
| 45 | + public boolean add(IIngredient input, byte chance) { |
| 46 | + if (input == null) return false; |
| 47 | + return add(input.toMcIngredient(), chance); |
| 48 | + } |
| 49 | + |
| 50 | + @MethodDescription(type = MethodDescription.Type.ADDITION) |
| 51 | + public boolean add(Ingredient input, byte chance) { |
| 52 | + if (input == null) return false; |
| 53 | + ComposterBlock.ItemsForComposter.INSTANCE.add(input, chance); |
| 54 | + return doAddScripted(Pair.of(input, chance)); |
| 55 | + } |
| 56 | + |
| 57 | + @MethodDescription |
| 58 | + public boolean remove(IIngredient input) { |
| 59 | + if (input == null) return false; |
| 60 | + return remove(input.toMcIngredient()); |
| 61 | + } |
| 62 | + |
| 63 | + @MethodDescription |
| 64 | + public boolean remove(Ingredient input) { |
| 65 | + return input != null && ComposterBlock.ItemsForComposter.INSTANCE.getEntries().removeIf(r -> r == input && doAddBackup(Pair.of(input, r.getByteValue()))); |
| 66 | + } |
| 67 | + |
| 68 | + @MethodDescription(example = @Example("item('minecraft:cactus')")) |
| 69 | + public void removeByInput(IIngredient input) { |
| 70 | + ComposterBlock.ItemsForComposter.INSTANCE.getEntries().removeIf(r -> Arrays.stream(r.getKey().getMatchingStacks()).anyMatch(input) && doAddBackup(r)); |
| 71 | + } |
| 72 | + |
| 73 | + @MethodDescription(priority = 2000, example = @Example(commented = true)) |
| 74 | + public void removeAll() { |
| 75 | + var recipes = ComposterBlock.ItemsForComposter.INSTANCE.getEntries(); |
| 76 | + recipes.forEach(this::addBackup); |
| 77 | + recipes.clear(); |
| 78 | + } |
| 79 | + |
| 80 | + @MethodDescription(type = MethodDescription.Type.QUERY) |
| 81 | + public SimpleObjectStream<Object2ByteMap.Entry<Ingredient>> streamRecipes() { |
| 82 | + return new SimpleObjectStream<>(ComposterBlock.ItemsForComposter.INSTANCE.getEntries()).setRemover(x -> remove(x.getKey())); |
| 83 | + } |
| 84 | + |
| 85 | + @Property(property = "input", comp = @Comp(eq = 1)) |
| 86 | + public static class RecipeBuilder extends AbstractRecipeBuilder<Pair<Ingredient, Byte>> { |
| 87 | + |
| 88 | + private static final ItemStack BONE_MEAL = new ItemStack(Items.DYE, 1, 15); |
| 89 | + |
| 90 | + @Property(comp = @Comp(gte = 0, lte = 100)) |
| 91 | + private int chance; |
| 92 | + |
| 93 | + @RecipeBuilderMethodDescription |
| 94 | + public RecipeBuilder chance(int chance) { |
| 95 | + this.chance = chance; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected int getMaxItemInput() { |
| 101 | + return 1; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String getErrorMsg() { |
| 106 | + return "Error adding FutureMC Composter recipe"; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void validate(GroovyLog.Msg msg) { |
| 111 | + validateItems(msg, 1, 1, 0, 0); |
| 112 | + validateFluids(msg); |
| 113 | + if (!input.isEmpty()) msg.add(input.get(0).test(BONE_MEAL), "the input item cannot match bonemeal, yet it was {}", input.get(0)); |
| 114 | + msg.add(chance < 0 || chance > 100, "chance must be greater than or equal to 0 and less than or equal to 100, yet it was {}", chance); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + @RecipeBuilderRegistrationMethod |
| 119 | + public @Nullable Pair<Ingredient, Byte> register() { |
| 120 | + if (!validate()) return null; |
| 121 | + // can be safely cast to a byte due to always being between 0-100 |
| 122 | + ComposterBlock.ItemsForComposter.INSTANCE.add(input.get(0).toMcIngredient(), (byte) chance); |
| 123 | + var entry = Pair.of(input.get(0).toMcIngredient(), (byte) chance); |
| 124 | + ModSupport.FUTURE_MC.get().composter.addBackup(entry); |
| 125 | + return entry; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments