|
| 1 | +package io.github.notenoughupdates.moulconfig.test; |
| 2 | + |
| 3 | +import io.github.notenoughupdates.moulconfig.common.IItemStack; |
| 4 | +import io.github.notenoughupdates.moulconfig.common.IMinecraft; |
| 5 | +import io.github.notenoughupdates.moulconfig.common.MyResourceLocation; |
| 6 | +import io.github.notenoughupdates.moulconfig.gui.CloseEventListener; |
| 7 | +import io.github.notenoughupdates.moulconfig.managed.ManagedConfig; |
| 8 | +import io.github.notenoughupdates.moulconfig.observer.ObservableList; |
| 9 | +import io.github.notenoughupdates.moulconfig.platform.MoulConfigPlatform; |
| 10 | +import io.github.notenoughupdates.moulconfig.xml.Bind; |
| 11 | +import io.github.notenoughupdates.moulconfig.xml.XMLUniverse; |
| 12 | +import net.fabricmc.api.ModInitializer; |
| 13 | +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; |
| 14 | +import net.minecraft.client.Minecraft; |
| 15 | +import net.minecraft.world.item.ItemStack; |
| 16 | +import net.minecraft.world.level.block.Blocks; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +import static io.github.notenoughupdates.moulconfig.test.CommandUtils.literal; |
| 24 | + |
| 25 | +public class FabricMain implements ModInitializer { |
| 26 | + @Override |
| 27 | + public void onInitialize() { |
| 28 | + if (!"true".equals(System.getProperty("moulconfig.testmod"))) return; |
| 29 | + ManagedConfig<TestConfig> config = ManagedConfig.create(new File("config/moulconfig/test.json"), TestConfig.class); |
| 30 | + ClientCommandRegistrationCallback.EVENT.register((a, b) -> { |
| 31 | + a.register(literal("moulconfig").executes(ctx -> { |
| 32 | + Minecraft.getInstance().schedule(() -> { |
| 33 | + var editor = config.getEditor(); |
| 34 | + editor.setWide(config.getInstance().getTestCategoryA().isWide()); |
| 35 | + IMinecraft.INSTANCE.openWrappedScreen(editor); |
| 36 | + }); |
| 37 | + return 0; |
| 38 | + })); |
| 39 | + a.register(literal("moulconfigxml").executes(ctx -> { |
| 40 | + Minecraft.getInstance().schedule(() -> { |
| 41 | + XMLUniverse xmlUniverse = XMLUniverse.getDefaultUniverse(); |
| 42 | + var scene = xmlUniverse.load( |
| 43 | + new ObjectBound(), |
| 44 | + IMinecraft.INSTANCE.loadResourceLocation(MyResourceLocation.Companion.parse("moulconfig:test.xml")) |
| 45 | + ); |
| 46 | + IMinecraft.getInstance() |
| 47 | + .openWrappedScreen(scene); |
| 48 | + }); |
| 49 | + return 0; |
| 50 | + })); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + public static class Element { |
| 55 | + @Bind |
| 56 | + public String text; |
| 57 | + |
| 58 | + @Bind |
| 59 | + public boolean enabled = false; |
| 60 | + |
| 61 | + public Element(String text) { |
| 62 | + this.text = text; |
| 63 | + } |
| 64 | + |
| 65 | + @Bind |
| 66 | + public void randomize() { |
| 67 | + text = "§" + "abcdef0123456789".charAt(new Random().nextInt(16)) + text.replaceAll("§.", ""); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class ObjectBound { |
| 72 | + @Bind |
| 73 | + public Runnable requestClose = null; |
| 74 | + |
| 75 | + @Bind |
| 76 | + public void afterClose() { |
| 77 | + System.out.println("After close"); |
| 78 | + } |
| 79 | + |
| 80 | + @Bind |
| 81 | + public CloseEventListener.CloseAction beforeClose() { |
| 82 | + System.out.println("Before close"); |
| 83 | + return CloseEventListener.CloseAction.NO_OBJECTIONS_TO_CLOSE; |
| 84 | + } |
| 85 | + |
| 86 | + @Bind |
| 87 | + public IItemStack itemStack = MoulConfigPlatform.wrap(new ItemStack(Blocks.SAND)); |
| 88 | + |
| 89 | + @Bind |
| 90 | + public boolean value = false; |
| 91 | + |
| 92 | + @Bind |
| 93 | + public String textField = ""; |
| 94 | + |
| 95 | + @Bind |
| 96 | + public float slider = 0f; |
| 97 | + |
| 98 | + @Bind |
| 99 | + public void addElement() { |
| 100 | + data.add(new Element(textField)); |
| 101 | + textField = ""; |
| 102 | + } |
| 103 | + |
| 104 | + @Bind |
| 105 | + public ObservableList<Element> data = |
| 106 | + new ObservableList<>(new ArrayList<>(Arrays.asList( |
| 107 | + new Element("Test 1"), new Element("Test 2"), new Element("Test 3") |
| 108 | + ))); |
| 109 | + } |
| 110 | +} |
0 commit comments