|
| 1 | +package turing.tmb.plugin; |
| 2 | + |
| 3 | +import net.minecraft.core.lang.I18n; |
| 4 | +import net.minecraft.core.util.collection.Pair; |
| 5 | +import org.luaj.vm2.LuaTable; |
| 6 | +import org.luaj.vm2.LuaValue; |
| 7 | +import org.luaj.vm2.lib.LibFunction; |
| 8 | +import org.luaj.vm2.lib.OneArgFunction; |
| 9 | +import org.luaj.vm2.lib.ThreeArgFunction; |
| 10 | +import org.luaj.vm2.lib.TwoArgFunction; |
| 11 | +import turing.btatweaker.api.ModLibrary; |
| 12 | +import turing.docs.Argument; |
| 13 | +import turing.docs.Function; |
| 14 | +import turing.docs.FunctionExample; |
| 15 | +import turing.docs.Library; |
| 16 | +import turing.tmb.*; |
| 17 | +import turing.tmb.api.ingredient.ITypedIngredient; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +@Library(value = "mods.tmb", className = "Too Many Blocks") |
| 26 | +public class TMBLib extends ModLibrary { |
| 27 | + public static final List<Pair<LibFunction, Consumer<LibFunction>>> scriptActions = new ArrayList<>(); |
| 28 | + |
| 29 | + @Override |
| 30 | + public void setupLib(LuaTable t, LuaValue env) { |
| 31 | + t.set("hideCategory", new HideCategory()); |
| 32 | + t.set("hideIngredient", new HideIngredient()); |
| 33 | + t.set("addInfo", new AddInfo()); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public List<String> getAliases() { |
| 38 | + return Collections.singletonList(TMB.MOD_ID); |
| 39 | + } |
| 40 | + |
| 41 | + @Function(value = "hideCategory", arguments = @Argument(value = "string", name = "categoryName"), examples = @FunctionExample("guidebook.section.furnace")) |
| 42 | + protected static final class HideCategory extends OneArgFunction { |
| 43 | + @Override |
| 44 | + public LuaValue call(LuaValue arg) { |
| 45 | + String name = arg.checkjstring(); |
| 46 | + TMB.getRuntime().getRecipeIndex().hideCategory(name); |
| 47 | + scriptActions.add(Pair.of(this, f -> f.call(arg))); |
| 48 | + return NIL; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Function(value = "hideIngredient", arguments = {@Argument(value = "string", name = "namespace"), @Argument(value = "string", name = "name")}) |
| 53 | + protected static final class HideIngredient extends TwoArgFunction { |
| 54 | + @Override |
| 55 | + public LuaValue call(LuaValue arg1, LuaValue arg2) { |
| 56 | + String namespace = arg1.checkjstring(); |
| 57 | + String name = arg2.checkjstring(); |
| 58 | + TMB.getRuntime().getIngredientIndex().hideIngredient(new TypedIngredient<>(namespace, name, null, null)); |
| 59 | + if (name.contains(".")) { |
| 60 | + TMB.getRuntime().getIngredientIndex().hideIngredient(new TypedIngredient<>(namespace, I18n.getInstance().translateKey(name), null, null)); |
| 61 | + scriptActions.add(Pair.of(this, f -> f.call(arg1, arg2))); |
| 62 | + } |
| 63 | + return NIL; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Function(value = "addInfo", arguments = { |
| 68 | + @Argument(value = "string", name = "namespace"), |
| 69 | + @Argument(value = "string", name = "name"), |
| 70 | + @Argument(value = "string", name = "infoText") |
| 71 | + }, examples = @FunctionExample({"\"minecraft\"", "\"Iron Ingot\"", "This is a cool item!!!"})) |
| 72 | + protected static final class AddInfo extends ThreeArgFunction { |
| 73 | + @Override |
| 74 | + public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) { |
| 75 | + String namespace = arg1.checkjstring(); |
| 76 | + String name = arg2.checkjstring(); |
| 77 | + String text = arg3.checkjstring(); |
| 78 | + Optional<ITypedIngredient<Object>> ingredient = TMB.getRuntime().getIngredientIndex().getIngredient(namespace, name); |
| 79 | + ingredient.ifPresent(i -> { |
| 80 | + TMB.getRuntime().getRecipeIndex().registerRecipe(BaseTMBPlugin.infoCategory, new IngredientInfo(i, text, false), InfoRecipeTranslator::new); |
| 81 | + scriptActions.add(Pair.of(this, f -> f.call(arg1, arg2, arg3))); |
| 82 | + }); |
| 83 | + return NIL; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments