Skip to content

Commit

Permalink
feat: add keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucaslah committed Sep 29, 2022
1 parent f22b1e6 commit 8266f95
Show file tree
Hide file tree
Showing 10 changed files with 447 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 19 additions & 0 deletions src/main/java/me/lucaslah/weatherchanger/Config.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
147 changes: 118 additions & 29 deletions src/main/java/me/lucaslah/weatherchanger/WeatherChanger.java
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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() {
Expand All @@ -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();
}
}
31 changes: 31 additions & 0 deletions src/main/java/me/lucaslah/weatherchanger/keybind/Key.java
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 8266f95

Please sign in to comment.