|
| 1 | +package com.cleanroommc.groovyscript.compat.mods.additionalenchantedminer; |
| 2 | + |
| 3 | +import com.cleanroommc.groovyscript.api.GroovyLog; |
| 4 | +import com.cleanroommc.groovyscript.api.documentation.annotations.*; |
| 5 | +import com.cleanroommc.groovyscript.compat.mods.ModSupport; |
| 6 | +import com.cleanroommc.groovyscript.helper.recipe.AbstractRecipeBuilder; |
| 7 | +import com.cleanroommc.groovyscript.registry.VirtualizedRegistry; |
| 8 | +import com.yogpc.qp.recipe.IngredientRecipe; |
| 9 | +import com.yogpc.qp.recipe.WorkbenchRecipe; |
| 10 | +import com.yogpc.qp.tile.ItemDamage; |
| 11 | +import com.yogpc.qp.utils.IngredientWithCount; |
| 12 | +import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.util.ResourceLocation; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | +import scala.collection.JavaConversions; |
| 16 | +import scala.collection.JavaConverters; |
| 17 | +import scala.collection.Seq; |
| 18 | +import scala.collection.SeqLike; |
| 19 | +import scala.collection.convert.Decorators; |
| 20 | +import scala.collection.immutable.Map; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +@RegistryDescription |
| 27 | +public class WorkBenchPlus extends VirtualizedRegistry<IngredientRecipe> { |
| 28 | + |
| 29 | + @Override |
| 30 | + public void onReload() { |
| 31 | + removeScripted().forEach(recipe -> WorkbenchRecipe.removeRecipe(recipe.location())); |
| 32 | + restoreFromBackup().forEach(recipe -> WorkbenchRecipe.addIngredientRecipe(recipe.location(), recipe.getOutput(), recipe.energy(), recipe.inputs(), recipe.hardCode(), recipe.showInJEI())); |
| 33 | + } |
| 34 | + |
| 35 | + @MethodDescription(example = @Example("item('quarryplus:quarry')")) |
| 36 | + public void removeByOutput(ItemStack output) { |
| 37 | + ItemDamage itemDamage = ItemDamage.apply(output); |
| 38 | + Map<ResourceLocation, WorkbenchRecipe> recipeMap = WorkbenchRecipe.getRecipeMap(); |
| 39 | + Iterable<WorkbenchRecipe> iterable = JavaConversions.asJavaIterable(recipeMap.values()); |
| 40 | + iterable.forEach(recipe -> { |
| 41 | + if (recipe.key().equals(itemDamage)) { |
| 42 | + addBackup(new IngredientRecipe(recipe.location(), recipe.getOutput(), recipe.energy(), recipe.showInJEI(), recipe.inputs(), recipe.hardCode())); |
| 43 | + } |
| 44 | + }); |
| 45 | + WorkbenchRecipe.removeRecipe(ItemDamage.apply(output)); |
| 46 | + } |
| 47 | + |
| 48 | + @MethodDescription(priority = 2000, example = @Example(commented = true)) |
| 49 | + public void removeAll() { |
| 50 | + Map<ResourceLocation, WorkbenchRecipe> recipeMap = WorkbenchRecipe.getRecipeMap(); |
| 51 | + Iterable<ResourceLocation> iterableLocation = JavaConversions.asJavaIterable(recipeMap.keys()); |
| 52 | + Iterable<WorkbenchRecipe> iterableRecipe = JavaConversions.asJavaIterable(recipeMap.values()); |
| 53 | + iterableLocation.forEach( |
| 54 | + WorkbenchRecipe::removeRecipe |
| 55 | + ); |
| 56 | + iterableRecipe.forEach( |
| 57 | + recipe -> addBackup(new IngredientRecipe(recipe.location(),recipe.getOutput(),recipe.energy(),recipe.showInJEI(),recipe.inputs(),recipe.hardCode())) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + private void add(IngredientRecipe recipe) { |
| 62 | + addScripted(recipe); |
| 63 | + WorkbenchRecipe.addIngredientRecipe(recipe.location(), recipe.getOutput(), recipe.energy(), recipe.inputs(), recipe.hardCode(), recipe.showInJEI()); |
| 64 | + } |
| 65 | + |
| 66 | + @RecipeBuilderDescription(example = @Example(".output(item('minecraft:nether_star')).input(item('minecraft:diamond'),item('minecraft:gold_ingot')).energy(10000)")) |
| 67 | + public RecipeBuilder recipeBuilder() { |
| 68 | + return new RecipeBuilder(); |
| 69 | + } |
| 70 | + |
| 71 | + @Property(property = "input", comp = @Comp(gte = 1, lte = 27)) |
| 72 | + @Property(property = "output", comp = @Comp(eq = 1)) |
| 73 | + public static class RecipeBuilder extends AbstractRecipeBuilder<IngredientRecipe> { |
| 74 | + |
| 75 | + @Property(comp = @Comp(gt = 0)) |
| 76 | + private double energy; |
| 77 | + |
| 78 | + @RecipeBuilderMethodDescription |
| 79 | + public RecipeBuilder energy(double energy) { |
| 80 | + this.energy = energy; |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String getRecipeNamePrefix() { |
| 86 | + return "additionalenchantedminer_workbenchplus_"; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getErrorMsg() { |
| 91 | + return "Error adding Additional Enchanted Miner WorkbenchPlus recipe"; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void validate(GroovyLog.Msg msg) { |
| 96 | + validateName(); |
| 97 | + validateItems(msg, 1, 27, 1, 1); |
| 98 | + msg.add(energy <= 0, "energy must be an integer greater than 0, yet it was {}", energy); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + @RecipeBuilderRegistrationMethod |
| 103 | + public @Nullable IngredientRecipe register() { |
| 104 | + if (!validate()) return null; |
| 105 | + //convert Java List to Scala Seq |
| 106 | + List<List<IngredientWithCount>> inputJavaList = input.stream() |
| 107 | + .map(i -> { |
| 108 | + IngredientWithCount ingredient = new IngredientWithCount(i.toMcIngredient(), i.getAmount()); |
| 109 | + return Collections.singletonList(ingredient); |
| 110 | + }) |
| 111 | + .collect(Collectors.toList()); |
| 112 | + Seq<Seq<IngredientWithCount>> inputScalaList = JavaConverters.asScalaBufferConverter( |
| 113 | + inputJavaList.stream() |
| 114 | + .map(JavaConverters::asScalaBufferConverter) |
| 115 | + .map(Decorators.AsScala::asScala) |
| 116 | + .map(SeqLike::toSeq) |
| 117 | + .collect(Collectors.toList())).asScala().toSeq(); |
| 118 | + IngredientRecipe recipe = new IngredientRecipe(this.name, output.get(0), energy, true, inputScalaList, true); |
| 119 | + ModSupport.ADDITIONAL_ENCHANTED_MINER.get().workBenchPlus.add(recipe); |
| 120 | + return recipe; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments