Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 274
src/client/java/com/kodari/frostclient/FrostClient.java
package com.kodari.frostclient;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.InputUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
public final class FrostClient implements ClientModInitializer {
private static final Path CONFIG_FILE = Path.of("config", "frostclient.properties");
private static int openKey = InputUtil.GLFW_KEY_UP;
private static boolean wasOpenKeyDown;
@Override
public void onInitializeClient() {
loadOpenKey();
ClientTickEvents.END_CLIENT_TICK.register(FrostClient::onEndTick);
}
static int getOpenKey() {
return openKey;
}
static void setOpenKey(int key) {
openKey = key;
saveOpenKey();
}
private static void onEndTick(MinecraftClient client) {
boolean keyDown = InputUtil.isKeyPressed(client.getWindow(), openKey);
if (client.currentScreen == null && keyDown && !wasOpenKeyDown) {
client.setScreen(new FrostClientScreen());
}
wasOpenKeyDown = keyDown;
}
private static void loadOpenKey() {
if (!Files.isRegularFile(CONFIG_FILE)) {
return;
}
Properties properties = new Properties();
try (InputStream input = Files.newInputStream(CONFIG_FILE)) {
properties.load(input);
openKey = Integer.parseInt(properties.getProperty("openKey", Integer.toString(InputUtil.GLFW_KEY_UP)));
} catch (IOException | NumberFormatException exception) {
openKey = InputUtil.GLFW_KEY_UP;
}
}
private static void saveOpenKey() {
Properties properties = new Properties();
properties.setProperty("openKey", Integer.toString(openKey));
try {
Files.createDirectories(CONFIG_FILE.getParent());
try (OutputStream output = Files.newOutputStream(CONFIG_FILE)) {
properties.store(output, "Frost Client controls");
}
} catch (IOException exception) {
System.getLogger(FrostClient.class.getName()).log(System.Logger.Level.WARNING, "Could not save Frost Client controls", exception);
}
}
}