Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 348
src/client/java/com/kodari/jumpscare/JumpscareClient.java
package com.kodari.jumpscare;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyMappingHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
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 JumpscareClient implements ClientModInitializer {
private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir()
.resolve("jumpscare-mode.properties");
private static String boundCommand = "";
private static KeyMapping triggerKey;
@Override
public void onInitializeClient() {
loadCommand();
KeyMapping.Category category = KeyMapping.Category.register(
Identifier.fromNamespaceAndPath("jumpscare", "controls"));
triggerKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.jumpscare.trigger_command",
InputConstants.Type.KEYSYM,
InputConstants.KEY_G,
category));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (triggerKey.consumeClick()) {
if (client.player == null || boundCommand.isBlank()) {
continue;
}
client.player.connection.sendCommand(boundCommand);
}
});
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) ->
dispatcher.register(Commands.literal("jumpscare")
.then(Commands.literal("mode").executes(context -> {
Minecraft client = Minecraft.getInstance();
client.setScreen(new JumpscareModeScreen(client.screen));
return 1;
}))
.then(Commands.literal("bind").then(Commands.argument("command", StringArgumentType.greedyString())
.executes(context -> {
saveCommand(StringArgumentType.getString(context, "command"));
context.getSource().sendFeedback(Component.literal("Shortcut command saved."));
return 1;
})))));
}
static String getBoundCommand() {
return boundCommand;
}
static void saveCommand(String command) {
String normalized = command.trim();
if (normalized.startsWith("/")) {
normalized = normalized.substring(1).trim();
}
boundCommand = normalized;
Properties properties = new Properties();
properties.setProperty("command", boundCommand);
try {
Files.createDirectories(CONFIG_PATH.getParent());
try (OutputStream output = Files.newOutputStream(CONFIG_PATH)) {
properties.store(output, "Jumpscare keybind command");
}
} catch (IOException exception) {
JumpscareMod.LOGGER.error("Could not save the jumpscare shortcut command", exception);
}
}
private static void loadCommand() {
if (!Files.exists(CONFIG_PATH)) {
return;
}
Properties properties = new Properties();
try (InputStream input = Files.newInputStream(CONFIG_PATH)) {
properties.load(input);
boundCommand = properties.getProperty("command", "").trim();
if (boundCommand.startsWith("/")) {
boundCommand = boundCommand.substring(1).trim();
}
} catch (IOException exception) {
JumpscareMod.LOGGER.error("Could not load the jumpscare shortcut command", exception);
}
}
}