Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
15 461
src/client/java/com/worldlock/client/WorldLock.java
package com.worldlock.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
import io.wispforest.owo.ui.util.UISounds;
public class WorldLock implements ClientModInitializer {
private static boolean remembered;
private static boolean startupChecked;
private static int failures;
private static long cooldownUntil;
private static KeyBinding lockKey;
private static KeyBinding settingsKey;
@Override
public void onInitializeClient() {
WorldLockConfig.load();
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("worldlock", "general"));
lockKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.worldlock.lock", GLFW.GLFW_KEY_UNKNOWN, category));
settingsKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.worldlock.settings", GLFW.GLFW_KEY_UNKNOWN, category));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (!startupChecked && client.currentScreen != null) {
startupChecked = true;
Screen parent = client.currentScreen;
if (WorldLockConfig.instance.passwordHash.isEmpty()) open(parent, () -> client.setScreen(parent), true, false);
}
while (lockKey.wasPressed()) {
Screen parent = client.currentScreen;
open(parent, () -> client.setScreen(parent), true, false);
}
while (settingsKey.wasPressed()) {
Screen parent = client.currentScreen;
client.setScreen(configGate(parent));
}
});
}
public static boolean shouldGate(boolean world, String identity) {
WorldLockConfig config = WorldLockConfig.instance;
return config.protects(world, identity) && !(config.rememberUntilQuit && remembered);
}
public static void open(Screen parent, Runnable success, boolean forced, boolean cancelAllowed) {
MinecraftClient.getInstance().setScreen(new PasswordScreen(parent, success, forced, cancelAllowed, false));
}
public static Screen configGate(Screen parent) {
return new PasswordScreen(parent, () -> MinecraftClient.getInstance().setScreen(WorldLockSettings.create(parent)), true, true, false);
}
public static int secondsLeft() {
return (int) Math.max(0, (cooldownUntil - System.currentTimeMillis() + 999) / 1000);
}
public static boolean verify(String password, boolean create) {
WorldLockConfig config = WorldLockConfig.instance;
if (secondsLeft() > 0 || password.isEmpty()) return false;
if (create || config.matches(password)) {
if (create) { config.passwordHash = WorldLockConfig.hash(password); config.save(); }
failures = 0;
remembered = true;
return true;
}
if (++failures >= 3) {
failures = 0;
cooldownUntil = System.currentTimeMillis() + Math.max(0, config.cooldownSeconds) * 1000L;
}
return false;
}
public static void play(SoundEvent sound) {
if (WorldLockConfig.instance.soundEffects) UISounds.play(sound);
}
}