diff --git a/README.md b/README.md index a3216f4..31423df 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,5 @@ Supports fabric 1.19+, forge support is coming soon!\ The fabric api is required. ## Links -Curseforge: TBA
+Curseforge: https://www.curseforge.com/minecraft/mc-mods/weather-changer
Modrinth: TBA diff --git a/src/main/java/me/lucaslah/weatherchanger/Config.java b/src/main/java/me/lucaslah/weatherchanger/Config.java new file mode 100644 index 0000000..2ac57c7 --- /dev/null +++ b/src/main/java/me/lucaslah/weatherchanger/Config.java @@ -0,0 +1,19 @@ +package me.lucaslah.weatherchanger; + +public class Config { + private String mode; + + public Config() {} + + public Config(String mode) { + this.mode = mode; + } + + public String getMode() { + return mode; + } + + public void setMode(String mode) { + this.mode = mode; + } +} diff --git a/src/main/java/me/lucaslah/weatherchanger/WeatherChanger.java b/src/main/java/me/lucaslah/weatherchanger/WeatherChanger.java index 286bb77..1e9de81 100644 --- a/src/main/java/me/lucaslah/weatherchanger/WeatherChanger.java +++ b/src/main/java/me/lucaslah/weatherchanger/WeatherChanger.java @@ -1,12 +1,25 @@ package me.lucaslah.weatherchanger; +import com.google.gson.Gson; +import me.lucaslah.weatherchanger.keybind.KeybindManager; +import me.lucaslah.weatherchanger.keybindings.ToggleClearKey; +import me.lucaslah.weatherchanger.keybindings.ToggleOffKey; +import me.lucaslah.weatherchanger.keybindings.ToggleRainKey; +import me.lucaslah.weatherchanger.keybindings.ToggleThunderKey; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.text.Text; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; + public class WeatherChanger implements ModInitializer { public static WeatherChanger instance; public static final Logger LOGGER = LogManager.getLogger("weather-changer"); @@ -15,37 +28,57 @@ public class WeatherChanger implements ModInitializer { @Override public void onInitialize() { instance = this; + // Create config if it does not exist + File configFile = new File(FabricLoader.getInstance().getConfigDir().resolve("weather-changer.json").toUri()); + if (!configFile.exists()) { + try { + configFile.createNewFile(); + writeModeToConfig(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + loadModeFromFile(); + + // Keybindings + new KeybindManager() + .add(new ToggleOffKey()) + .add(new ToggleClearKey()) + .add(new ToggleRainKey()) + .add(new ToggleThunderKey()); // Command - ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { - dispatcher.register(ClientCommandManager.literal("clientweather") - .then(ClientCommandManager.literal("off") - .executes(context -> { - setMode(Mode.OFF); - context.getSource().sendFeedback(Text.literal("Set client weather to: Off")); - return 1; - }) - ).then(ClientCommandManager.literal("clear") - .executes(context -> { - setMode(Mode.CLEAR); - context.getSource().sendFeedback(Text.literal("Set client weather to: Clear")); - return 1; - }) - ).then(ClientCommandManager.literal("rain") - .executes(context -> { - setMode(Mode.RAIN); - context.getSource().sendFeedback(Text.literal("Set client weather to: Rain")); - return 1; - }) - ).then(ClientCommandManager.literal("thunder") - .executes(context -> { - setMode(Mode.THUNDER); - context.getSource().sendFeedback(Text.literal("Set client weather to: Thunder")); - return 1; - }) - ) - ); - }); + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal("clientweather") + .then(ClientCommandManager.literal("off") + .executes(context -> { + setOff(); + context.getSource().sendFeedback(Text.literal("Set client weather to: Off")); + return 1; + }) + ).then(ClientCommandManager.literal("clear") + .executes(context -> { + setClear(); + context.getSource().sendFeedback(Text.literal("Set client weather to: Clear")); + return 1; + }) + ).then(ClientCommandManager.literal("rain") + .executes(context -> { + setRain(); + context.getSource().sendFeedback(Text.literal("Set client weather to: Rain")); + return 1; + }) + ).then(ClientCommandManager.literal("thunder") + .executes(context -> { + setThunder(); + context.getSource().sendFeedback(Text.literal("Set client weather to: Thunder")); + return 1; + }) + ).executes((context -> { + context.getSource().sendFeedback(Text.literal("Client weather is set to: " + getMode().toString().toLowerCase())); + return 1; + })) + )); } public static WeatherChanger getInstance() { @@ -66,4 +99,60 @@ public void setMode(Mode mode) { public Mode getMode() { return mode; } + public void writeModeToConfig() { + Gson gson = new Gson(); + Config config = new Config(getMode().toString().toUpperCase()); + + Writer writer; + try { + writer = Files.newBufferedWriter(FabricLoader.getInstance().getConfigDir().resolve("weather-changer.json")); + } catch (IOException e) { + throw new RuntimeException(e); + } + + gson.toJson(config, writer); + try { + writer.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void loadModeFromFile() { + Gson gson = new Gson(); + + Reader reader = null; + try { + reader = Files.newBufferedReader(FabricLoader.getInstance().getConfigDir().resolve("weather-changer.json")); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Config config = gson.fromJson(reader, Config.class); + + setMode(Mode.valueOf(config.getMode().toUpperCase())); + + try { + reader.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void setOff() { + setMode(Mode.OFF); + writeModeToConfig(); + } + public void setClear() { + setMode(Mode.CLEAR); + writeModeToConfig(); + } + public void setRain() { + setMode(Mode.RAIN); + writeModeToConfig(); + } + public void setThunder() { + setMode(Mode.THUNDER); + writeModeToConfig(); + } } diff --git a/src/main/java/me/lucaslah/weatherchanger/keybind/Key.java b/src/main/java/me/lucaslah/weatherchanger/keybind/Key.java new file mode 100644 index 0000000..bc20dec --- /dev/null +++ b/src/main/java/me/lucaslah/weatherchanger/keybind/Key.java @@ -0,0 +1,31 @@ +package me.lucaslah.weatherchanger.keybind; + +import me.lucaslah.weatherchanger.WeatherChanger; +import org.jetbrains.annotations.NotNull; + +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil.Type; +import net.minecraft.util.Identifier; + +public abstract class Key { + public KeyBinding keybind; + public WeatherChanger mod = WeatherChanger.getInstance(); + public MinecraftClient mc = MinecraftClient.getInstance(); + + public Key(@NotNull String name) { + keybind = KeyBindingHelper.registerKeyBinding(new KeyBinding(this.getDisplayName(), this.getKeyType(), this.getKey(), this.getCategory())); + } + + public abstract void onPress(@NotNull MinecraftClient client); + public abstract Identifier getId(); + + public abstract KeyBinding getKeyBinding(); + + public abstract boolean isEnabled(); + public abstract String getDisplayName(); + public abstract Type getKeyType(); + public abstract String getCategory(); + public abstract int getKey(); +} diff --git a/src/main/java/me/lucaslah/weatherchanger/keybind/KeybindManager.java b/src/main/java/me/lucaslah/weatherchanger/keybind/KeybindManager.java new file mode 100644 index 0000000..fa576f0 --- /dev/null +++ b/src/main/java/me/lucaslah/weatherchanger/keybind/KeybindManager.java @@ -0,0 +1,46 @@ +package me.lucaslah.weatherchanger.keybind; + +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.minecraft.util.Identifier; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class KeybindManager { + private final HashMap entries = new HashMap<>(); + + private static KeybindManager INSTANCE; + + public KeybindManager() { + INSTANCE = this; + + ClientTickEvents.END_CLIENT_TICK.register(client -> { + for (Key key : getEntries()) { + if (key.isEnabled() && key.getKeyBinding().wasPressed()) { + key.onPress(client); + } + } + }); + } + + public KeybindManager add(Key entry) { + entries.put(entry.getId(), entry); + return this; + } + + public Key get(Identifier identifier) { + return entries.get(identifier); + } + + public List getEntries() { + if (entries.size() > 0) { + return new ArrayList<>(entries.values()); + } + return new ArrayList<>(); + } + + public static KeybindManager getInstance() { + return INSTANCE; + } +} \ No newline at end of file diff --git a/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleClearKey.java b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleClearKey.java new file mode 100644 index 0000000..0cb2393 --- /dev/null +++ b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleClearKey.java @@ -0,0 +1,57 @@ +package me.lucaslah.weatherchanger.keybindings; + +import me.lucaslah.weatherchanger.keybind.Key; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.NotNull; + +public class ToggleClearKey extends Key { + public ToggleClearKey() { + super("ToggleClearKey"); + } + + @Override + public void onPress(@NotNull MinecraftClient client) { + mod.setClear(); + assert mc.player != null; + mc.player.sendMessage(Text.of("Set client weather to: Clear"), true); + } + + @Override + public Identifier getId() { + return new Identifier("weatherchangerkeys", "toggleclearkey"); + } + + @Override + public KeyBinding getKeyBinding() { + return this.keybind; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public String getDisplayName() { + return "Toggle Weather Clear"; + } + + @Override + public InputUtil.Type getKeyType() { + return InputUtil.Type.KEYSYM; + } + + @Override + public String getCategory() { + return "Weather Changer"; + } + + @Override + public int getKey() { + return -1; + } +} diff --git a/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleOffKey.java b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleOffKey.java new file mode 100644 index 0000000..7e18383 --- /dev/null +++ b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleOffKey.java @@ -0,0 +1,57 @@ +package me.lucaslah.weatherchanger.keybindings; + +import me.lucaslah.weatherchanger.keybind.Key; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.NotNull; + +public class ToggleOffKey extends Key { + public ToggleOffKey() { + super("ToggleOffKey"); + } + + @Override + public void onPress(@NotNull MinecraftClient client) { + mod.setOff(); + assert mc.player != null; + mc.player.sendMessage(Text.of("Set client weather to: Off"), true); + } + + @Override + public Identifier getId() { + return new Identifier("weatherchangerkeys", "toggleoffkey"); + } + + @Override + public KeyBinding getKeyBinding() { + return this.keybind; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public String getDisplayName() { + return "Toggle Weather Off"; + } + + @Override + public InputUtil.Type getKeyType() { + return InputUtil.Type.KEYSYM; + } + + @Override + public String getCategory() { + return "Weather Changer"; + } + + @Override + public int getKey() { + return -1; + } +} diff --git a/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleRainKey.java b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleRainKey.java new file mode 100644 index 0000000..d4d71a7 --- /dev/null +++ b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleRainKey.java @@ -0,0 +1,57 @@ +package me.lucaslah.weatherchanger.keybindings; + +import me.lucaslah.weatherchanger.keybind.Key; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.NotNull; + +public class ToggleRainKey extends Key { + public ToggleRainKey() { + super("ToggleRainKey"); + } + + @Override + public void onPress(@NotNull MinecraftClient client) { + mod.setRain(); + assert mc.player != null; + mc.player.sendMessage(Text.of("Set client weather to: Rain"), true); + } + + @Override + public Identifier getId() { + return new Identifier("weatherchangerkeys", "togglerainkey"); + } + + @Override + public KeyBinding getKeyBinding() { + return this.keybind; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public String getDisplayName() { + return "Toggle Weather Rain"; + } + + @Override + public InputUtil.Type getKeyType() { + return InputUtil.Type.KEYSYM; + } + + @Override + public String getCategory() { + return "Weather Changer"; + } + + @Override + public int getKey() { + return -1; + } +} diff --git a/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleThunderKey.java b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleThunderKey.java new file mode 100644 index 0000000..35d7d08 --- /dev/null +++ b/src/main/java/me/lucaslah/weatherchanger/keybindings/ToggleThunderKey.java @@ -0,0 +1,57 @@ +package me.lucaslah.weatherchanger.keybindings; + +import me.lucaslah.weatherchanger.keybind.Key; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.NotNull; + +public class ToggleThunderKey extends Key { + public ToggleThunderKey() { + super("ToggleThunderKey"); + } + + @Override + public void onPress(@NotNull MinecraftClient client) { + mod.setThunder(); + assert mc.player != null; + mc.player.sendMessage(Text.of("Set client weather to: Thunder"), true); + } + + @Override + public Identifier getId() { + return new Identifier("weatherchangerkeys", "togglethunderkey"); + } + + @Override + public KeyBinding getKeyBinding() { + return this.keybind; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public String getDisplayName() { + return "Toggle Weather Thunder"; + } + + @Override + public InputUtil.Type getKeyType() { + return InputUtil.Type.KEYSYM; + } + + @Override + public String getCategory() { + return "Weather Changer"; + } + + @Override + public int getKey() { + return -1; + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 3383f6b..3d06b3c 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -9,8 +9,10 @@ "Lucaslah" ], "contact": { - "homepage": "https://cssudii.xyz/r/project/weather-changer", - "sources": "https://github.com/Lucalah/weather-changer" + "homepage": "https://cssudii.xyz/r/project/oss/weather-changer", + "sources": "https://github.com/Lucaslah/WeatherChanger", + "issues": "https://github.com/Lucaslah/WeatherChanger/issues", + "email": "ossprojects@cssudii.xyz" }, "license": "lgpl-3",