mirror of
https://github.com/Lucaslah/WeatherChanger.git
synced 2026-01-02 19:47:50 +00:00
feat: add keybindings
This commit is contained in:
@@ -5,5 +5,5 @@ Supports fabric 1.19+, forge support is coming soon!\
|
||||
The fabric api is required.
|
||||
|
||||
## Links
|
||||
Curseforge: TBA<br>
|
||||
Curseforge: https://www.curseforge.com/minecraft/mc-mods/weather-changer <br>
|
||||
Modrinth: TBA
|
||||
|
||||
19
src/main/java/me/lucaslah/weatherchanger/Config.java
Normal file
19
src/main/java/me/lucaslah/weatherchanger/Config.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 class WeatherChanger implements ModInitializer {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
31
src/main/java/me/lucaslah/weatherchanger/keybind/Key.java
Normal file
31
src/main/java/me/lucaslah/weatherchanger/keybind/Key.java
Normal file
@@ -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();
|
||||
}
|
||||
@@ -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<Identifier, Key> 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<Key> getEntries() {
|
||||
if (entries.size() > 0) {
|
||||
return new ArrayList<>(entries.values());
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public static KeybindManager getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user