From 3bf86d170c076a289ae2aacb5440572c082fe657 Mon Sep 17 00:00:00 2001 From: Liz Graham Date: Thu, 3 Mar 2022 03:13:12 +0000 Subject: [PATCH] Add config --- .github/workflows/build.yml | 2 +- build.gradle | 30 +++++++++ gradle.properties | 4 ++ .../example/{ExampleMod.java => Dust.java} | 64 +++++++++++++------ .../java/net/fabricmc/example/DustConfig.java | 38 +++++++++++ .../fabricmc/example/DustModMenuFactory.java | 11 ++++ .../net/fabricmc/example/ModMenuScreen.java | 41 ++++++++++++ .../net/fabricmc/example/utils/JsonFile.java | 54 ++++++++++++++++ .../resources/assets/modid/lang/en_us.json | 5 ++ src/main/resources/fabric.mod.json | 19 ++++-- 10 files changed, 245 insertions(+), 23 deletions(-) rename src/main/java/net/fabricmc/example/{ExampleMod.java => Dust.java} (57%) create mode 100644 src/main/java/net/fabricmc/example/DustConfig.java create mode 100644 src/main/java/net/fabricmc/example/DustModMenuFactory.java create mode 100644 src/main/java/net/fabricmc/example/ModMenuScreen.java create mode 100644 src/main/java/net/fabricmc/example/utils/JsonFile.java create mode 100644 src/main/resources/assets/modid/lang/en_us.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a44feb7..87b0f5c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -46,5 +46,5 @@ jobs: if: ${{ runner.os == 'Linux' && matrix.java == '17' }} # Only upload artifacts built from latest java on one OS uses: actions/upload-artifact@v2 with: - name: starminerrecrafted-fabric-${{ steps.ref.outputs.branch }} + name: dust-fabric-${{ steps.ref.outputs.branch }} path: build/libs/*[0-9].jar \ No newline at end of file diff --git a/build.gradle b/build.gradle index 27ac707..0575fec 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,27 @@ version = project.mod_version group = project.maven_group repositories { + maven { + name = "Modrinth" + url = "https://api.modrinth.com/maven" + content { + includeGroup "maven.modrinth" + } + } + maven { + url = "https://maven.shedaniel.me/" + } + maven { + url = "https://maven.terraformersmc.com" + } + maven { + name = 'minelp' + url = 'https://repo.minelittlepony-mod.com/maven/snapshot' + } + maven { + name = 'minelp-release' + url = 'https://repo.minelittlepony-mod.com/maven/release' + } // Add repositories to retrieve artifacts from in here. // You should only use this when depending on other mods because // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. @@ -26,6 +47,15 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + modApi("me.shedaniel.cloth:cloth-config-fabric:${project.clothconfig_version}") { + exclude(group: "net.fabricmc.fabric-api") + } + modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}" + modApi "com.minelittlepony:kirin:${project.kirin_version}" + include "com.minelittlepony:kirin:${project.kirin_version}" + + //modImplementation "com.minelittlepony:minelittlepony:${project.minelp_version}" + modCompileOnly("com.terraformersmc:modmenu:${project.modmenu_version}") } processResources { diff --git a/gradle.properties b/gradle.properties index b839c00..ce8e7dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,3 +14,7 @@ org.gradle.jvmargs=-Xmx1G # Dependencies fabric_version=0.46.4+1.18 + clothconfig_version = 6.1.48 + modmenu_version = 3.0.1 + minelp_version=4.4.0-beta.2 + kirin_version=1.10.0-beta.2 diff --git a/src/main/java/net/fabricmc/example/ExampleMod.java b/src/main/java/net/fabricmc/example/Dust.java similarity index 57% rename from src/main/java/net/fabricmc/example/ExampleMod.java rename to src/main/java/net/fabricmc/example/Dust.java index 4bbbe99..42f6aff 100644 --- a/src/main/java/net/fabricmc/example/ExampleMod.java +++ b/src/main/java/net/fabricmc/example/Dust.java @@ -1,22 +1,60 @@ package net.fabricmc.example; -import net.fabricmc.api.ModInitializer; +import com.minelittlepony.common.util.GamePaths; +import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.minecraft.client.MinecraftClient; import net.minecraft.particle.ParticleTypes; import net.minecraft.world.World; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.nio.file.Path; import java.util.Random; -public class ExampleMod implements ModInitializer { - public static final Logger LOGGER = LoggerFactory.getLogger("modid"); +public class Dust implements ClientModInitializer { + public static final Logger LOGGER = LoggerFactory.getLogger("dust"); + + private static Dust instance; + + public static Dust getInstance() { + return instance; + } + + public Dust() { + instance = this; + } + + private DustConfig config; + + public DustConfig getConfig() { + return config; + } @Override - public void onInitialize() { - ClientTickEvents.START_CLIENT_TICK.register((client) -> { - if(!client.isPaused()){ + public void onInitializeClient() { + Path dustFolder = GamePaths.getConfigDirectory().resolve("dust"); + config = new DustConfig(dustFolder.resolve("userconfig.json"), this); + config.load(); + + ClientTickEvents.END_CLIENT_TICK.register(this::createDust); + } + + public int getRandomNumberUsingInts(int min, int max) { + Random random = new Random(); + return random.ints(min, max) + .findFirst() + .getAsInt(); + } + + public static double lerp(double min, double max, double val) { + return 1 - ((val - min) / (max - min)); + } + + private void createDust(MinecraftClient client) { + if (getConfig().getDustEnabled()) { + if (!client.isPaused()) { if (client.world != null) { World world = client.world; if (!client.player.clientWorld.isSkyVisible(client.player.getBlockPos())) { @@ -33,16 +71,6 @@ public class ExampleMod implements ModInitializer { } } } - }); - } - - public int getRandomNumberUsingInts(int min, int max) { - Random random = new Random(); - return random.ints(min,max) - .findFirst() - .getAsInt(); - } - public static double lerp(double min, double max, double val) { - return 1 - ((val - min) / (max - min)); + } } } diff --git a/src/main/java/net/fabricmc/example/DustConfig.java b/src/main/java/net/fabricmc/example/DustConfig.java new file mode 100644 index 0000000..b02ff87 --- /dev/null +++ b/src/main/java/net/fabricmc/example/DustConfig.java @@ -0,0 +1,38 @@ +package net.fabricmc.example; + +import net.fabricmc.example.utils.JsonFile; + +import java.nio.file.Path; +import java.util.Vector; + +public class DustConfig extends JsonFile { + private transient final Dust dust; + private Vector dimensions; + private boolean dustEnabled = true; + + public DustConfig(Path file, Dust dust) { + super(file); + + this.dust = dust; + } + + public Vector setDimensions(){ + + return null; + } + + public Vector getDimensions(){ + + return null; + } + + public boolean toggleDust(){ + dustEnabled = !dustEnabled; + save(); + return dustEnabled; + } + + public boolean getDustEnabled(){ + return dustEnabled; + } +} diff --git a/src/main/java/net/fabricmc/example/DustModMenuFactory.java b/src/main/java/net/fabricmc/example/DustModMenuFactory.java new file mode 100644 index 0000000..0de1f4a --- /dev/null +++ b/src/main/java/net/fabricmc/example/DustModMenuFactory.java @@ -0,0 +1,11 @@ +package net.fabricmc.example; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; + +public class DustModMenuFactory implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + return ModMenuScreen::new; + } +} diff --git a/src/main/java/net/fabricmc/example/ModMenuScreen.java b/src/main/java/net/fabricmc/example/ModMenuScreen.java new file mode 100644 index 0000000..dde36d2 --- /dev/null +++ b/src/main/java/net/fabricmc/example/ModMenuScreen.java @@ -0,0 +1,41 @@ +package net.fabricmc.example; + +import com.minelittlepony.common.client.gui.GameGui; +import com.minelittlepony.common.client.gui.element.*; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.TranslatableText; + +import javax.annotation.Nullable; + +public class ModMenuScreen extends GameGui { + public ModMenuScreen(@Nullable Screen parent) { + super(new TranslatableText("menu.dust.title"), parent); + } + + @Override + public void init() { + int left = width / 2 - 100; + int row = height / 4 + 14; + + DustConfig config = Dust.getInstance().getConfig(); + + addButton(new Label(width / 2, 30)).setCentered().getStyle() + .setText(getTitle()); + + addButton(new Button(left, row += 24).onClick(sender -> { + sender.getStyle().setText("menu.dust.global." + config.toggleDust()); + })).getStyle() + .setText("menu.dust.global." + config.getDustEnabled()); + + addButton(new Button(left, row += 34) + .onClick(sender -> finish())).getStyle() + .setText("gui.done"); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { + renderBackground(matrices); + super.render(matrices, mouseX, mouseY, partialTicks); + } +} diff --git a/src/main/java/net/fabricmc/example/utils/JsonFile.java b/src/main/java/net/fabricmc/example/utils/JsonFile.java new file mode 100644 index 0000000..179d229 --- /dev/null +++ b/src/main/java/net/fabricmc/example/utils/JsonFile.java @@ -0,0 +1,54 @@ +package net.fabricmc.example.utils; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.InstanceCreator; +import net.fabricmc.example.Dust; + +public class JsonFile { + protected transient final Gson gson = new GsonBuilder() + .registerTypeAdapter(getClass(), (InstanceCreator)t -> this) + .setPrettyPrinting() + .create(); + + private transient Path file; + + JsonFile() { } + + public JsonFile(Path file) { + this.file = file; + } + + public final void load() { + if (Files.isReadable(file)) { + try (Reader reader = Files.newBufferedReader(file)) { + load(reader); + } catch (Exception e) { + Dust.LOGGER.error("Invalid config", e); + } + } + + save(); + } + + public final void load(Reader reader) { + gson.fromJson(reader, getClass()); + } + + public final void save() { + try { + Files.createDirectories(file.getParent()); + try (BufferedWriter writer = Files.newBufferedWriter(file)) { + gson.toJson(this, writer); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/resources/assets/modid/lang/en_us.json b/src/main/resources/assets/modid/lang/en_us.json new file mode 100644 index 0000000..a8b0c57 --- /dev/null +++ b/src/main/resources/assets/modid/lang/en_us.json @@ -0,0 +1,5 @@ +{ + "menu.dust.title": "Dust", + "menu.dust.global.false": "Dust: disabled", + "menu.dust.global.true": "Dust: enabled" +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 82475fd..d15cfae 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -9,7 +9,7 @@ "LizIsTired" ], "contact": { - "homepage": "https://github.com/LizIsTired/dust", + "issues": "https://github.com/LizIsTired/dust/issues", "sources": "https://github.com/LizIsTired/dust" }, @@ -18,8 +18,11 @@ "environment": "*", "entrypoints": { - "main": [ - "net.fabricmc.example.ExampleMod" + "client": [ + "net.fabricmc.example.Dust" + ], + "modmenu": [ + "net.fabricmc.example.DustModMenuFactory" ] }, @@ -30,6 +33,14 @@ "java": ">=17" }, "suggests": { - "another-mod": "*" + "clothconfig": ">=6.1.48", + "modmenu": ">=3.0.1" + }, + "custom": { + "modmenu": { + "links": { + "modmenu.discord": "https://discord.gg/AgBfPFMJgX" + } + } } }