Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings subsystem #11

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ tasks.named<Test>("test") {
}

loom {
accessWidenerPath = file("../../src/main/resources/${mod.id}.accesswidener")

splitEnvironmentSourceSets()

mods {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod.id=oneforall
mod.author=Koralix Studios
mod.group=com.koralix.oneforall
mod.description=One mod to replace them all
mod.version=5.8.2
mod.version=0.1.0

fabric.loader=0.15.11
fabric.versions=1.20.1
13 changes: 12 additions & 1 deletion src/client/java/com/koralix/oneforall/ClientOneForAll.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.koralix.oneforall;

import com.koralix.oneforall.commands.ClientCommands;
import com.koralix.oneforall.input.ButtonEvent;
import com.koralix.oneforall.input.hotkey.HotKey;
import com.koralix.oneforall.input.InputManager;
import com.koralix.oneforall.input.hotkey.HotKey;
import com.koralix.oneforall.network.ClientLoginManager;
import com.koralix.oneforall.platform.Platform;
import com.koralix.oneforall.settings.ClientSettings;
import com.koralix.oneforall.settings.SettingsManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import org.lwjgl.glfw.GLFW;

public class ClientOneForAll extends OneForAll {
Expand All @@ -20,6 +25,10 @@ public static ClientOneForAll getInstance() {
@Override
public void onInitialize() {
getLogger().info("Initializing OneForAll...");

SettingsManager.register(ClientSettings.class);

ClientCommandRegistrationCallback.EVENT.register(ClientCommands::register);
}

public void onInitializeClient() {
Expand All @@ -37,6 +46,8 @@ public void onInitializeClient() {
.add(new ButtonEvent(GLFW.GLFW_KEY_A, GLFW.GLFW_PRESS, ButtonEvent.ButtonType.KEYBOARD)));

inputManager.register();

ClientLoginManager.init();
}

public InputManager getInputManager() {
Expand Down
16 changes: 16 additions & 0 deletions src/client/java/com/koralix/oneforall/commands/ClientCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.koralix.oneforall.commands;

import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.command.CommandRegistryAccess;

public final class ClientCommands {
private ClientCommands() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}


public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
ClientOfaCommand.register(dispatcher, registryAccess);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.koralix.oneforall.commands;

import com.koralix.oneforall.settings.SettingsRegistry;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.command.CommandRegistryAccess;
import org.jetbrains.annotations.NotNull;

import static com.koralix.oneforall.commands.OfaCommand.settings;

public final class ClientOfaCommand {
private ClientOfaCommand() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}


public static void register(
@NotNull CommandDispatcher<FabricClientCommandSource> dispatcher,
CommandRegistryAccess registryAccess
) {
LiteralArgumentBuilder<FabricClientCommandSource> cofa = ClientCommandManager.literal("cofa");
cofa.then(settings(ClientCommandManager::literal, ClientCommandManager::argument, SettingsRegistry.Env.CLIENT));
dispatcher.register(cofa);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.koralix.oneforall.network;

import com.koralix.oneforall.OneForAll;
import com.koralix.oneforall.serde.Serde;
import com.koralix.oneforall.settings.ClientSettings;
import com.koralix.oneforall.settings.ProtocolUsageConditions;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientLoginNetworkHandler;
import net.minecraft.network.PacketByteBuf;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class ClientLoginManager {
public static void init() {
ClientLoginConnectionEvents.QUERY_START.register((handler, client) -> {
ClientLoginNetworking.registerReceiver(ServerLoginManager.getChannel("hello"), ClientLoginManager::onHello);
});
}

private static boolean doesClientEnableProtocol(boolean enforceProtocol) {
ProtocolUsageConditions usageConditions = ClientSettings.PROTOCOL_USAGE_CONDITIONS.value();

return switch (usageConditions) {
case Always -> true;
case OnlyEnforced -> enforceProtocol;
case Never -> false;
};
}

private static CompletableFuture<PacketByteBuf> onHello(MinecraftClient client, ClientLoginNetworkHandler handler, PacketByteBuf buf, Consumer<GenericFutureListener<? extends Future<? super Void>>> listenerAdder) {
String version = Serde.deserialize(buf, String.class);
boolean enforceProtocol = Serde.deserialize(buf, Boolean.class);

boolean enablesProtocol = doesClientEnableProtocol(enforceProtocol);
if (!enablesProtocol) {
return CompletableFuture.completedFuture(null);
}

PacketByteBuf response = PacketByteBufs.create();
Serde.serialize(response, OneForAll.getInstance().getMetadata().version());
return CompletableFuture.completedFuture(response);
}

}
10 changes: 10 additions & 0 deletions src/client/java/com/koralix/oneforall/settings/ClientSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.koralix.oneforall.settings;

import java.util.Objects;

@SettingsRegistry(id = "client_settings", env = SettingsRegistry.Env.CLIENT)
public class ClientSettings {
public static final ConfigValue<ProtocolUsageConditions> PROTOCOL_USAGE_CONDITIONS = ConfigValue.of(ProtocolUsageConditions.Always)
.test(Objects::nonNull)
.build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.koralix.oneforall.settings;

import net.minecraft.util.StringIdentifiable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public enum ProtocolUsageConditions implements StringIdentifiable {
Always,
OnlyEnforced,
Never;

@Contract(pure = true)
@Override
public @NotNull String asString() {
return name();
}
}
16 changes: 15 additions & 1 deletion src/main/java/com/koralix/oneforall/OneForAll.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.koralix.oneforall;

import com.koralix.oneforall.commands.Commands;
import com.koralix.oneforall.platform.ModMetadata;
import com.koralix.oneforall.platform.Platform;
import com.koralix.oneforall.settings.SettingsManager;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -10,13 +12,21 @@ public abstract class OneForAll {

private final Logger logger = LoggerFactory.getLogger("OneForAll");
private final Platform platform;
private final ModMetadata metadata;

public static OneForAll getInstance() {
return Initializer.instance;
}

public OneForAll(Platform platform) {
this.platform = platform;
this.metadata = platform.getMetadata(MOD_ID);
}

public final void initialize() {
CommandRegistrationCallback.EVENT.register(Commands::register);

onInitialize();
}

abstract void onInitialize();
Expand All @@ -28,4 +38,8 @@ public Logger getLogger() {
public Platform getPlatform() {
return platform;
}

public ModMetadata getMetadata() {
return metadata;
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/koralix/oneforall/ServerOneForAll.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.koralix.oneforall;

import com.koralix.oneforall.network.ServerLoginManager;
import com.koralix.oneforall.platform.Platform;
import com.koralix.oneforall.settings.ServerSettings;
import com.koralix.oneforall.settings.SettingsManager;
Expand All @@ -23,5 +24,7 @@ public void onInitialize() {

public void onInitializeServer() {
getLogger().info("Initializing OneForAll server...");

ServerLoginManager.init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.koralix.oneforall.commands;

import com.koralix.oneforall.settings.ConfigValue;
import com.mojang.brigadier.arguments.*;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.command.CommandSource;
import org.jetbrains.annotations.NotNull;

import java.util.function.BiFunction;

public class ArgumentTypeHelper {
@SuppressWarnings("unchecked")
public static <T extends Enum<T>, S extends CommandSource> RequiredArgumentBuilder<S, String> createEnumArg(
@NotNull BiFunction<String, ArgumentType<?>, RequiredArgumentBuilder<S, ?>> argument,
String name,
T[] values
) {
return (RequiredArgumentBuilder<S, String>) argument.apply(name, StringArgumentType.word()).suggests((context, builder) -> {
for (T value : values) {
builder.suggest(value.name().toLowerCase());
}
return builder.buildFuture();
});
}

@SuppressWarnings("unchecked")
public static <T extends Enum<T>, S extends CommandSource> @NotNull RequiredArgumentBuilder<S, String> createEnumArg(
@NotNull BiFunction<String, ArgumentType<?>, RequiredArgumentBuilder<S, ?>> argument,
String name,
@NotNull Class<?> clazz
) {
return createEnumArg(argument, name, (T[]) clazz.getEnumConstants());
}

public static <T extends Enum<T>, S extends CommandSource> T getEnumValue(
@NotNull CommandContext<S> context,
@NotNull String name,
@NotNull Class<T> clazz
) {
return Enum.valueOf(clazz, StringArgumentType.getString(context, name).toUpperCase());
}

@SuppressWarnings("unchecked")
public static <S extends CommandSource, T> @NotNull RequiredArgumentBuilder<S, T> createSettingArg(
@NotNull BiFunction<String, ArgumentType<?>, RequiredArgumentBuilder<S, ?>> ofaArgument,
@NotNull ConfigValue<T> setting
) {
if (setting.clazz().isEnum())
return (RequiredArgumentBuilder<S, T>) createEnumArg(ofaArgument, "value", setting.clazz());

return (RequiredArgumentBuilder<S, T>) ofaArgument.apply("value", settingArgumentType(setting.clazz()));
}

public static <S extends CommandSource, T> T getSettingValue(
@NotNull CommandContext<S> context,
@NotNull String name,
@NotNull ConfigValue<T> setting
) {
if (setting.clazz().isEnum()) {
Class<? extends Enum> clazz = setting.clazz().asSubclass(Enum.class);
return (T) getEnumValue(context, name, clazz);
}

return context.getArgument(name, setting.clazz());
}

@SuppressWarnings("unchecked")
private static <T> @NotNull ArgumentType<T> settingArgumentType(@NotNull Class<T> clazz) {
if (clazz == Boolean.class)
return (ArgumentType<T>) BoolArgumentType.bool();
if (clazz == String.class)
return (ArgumentType<T>) StringArgumentType.string();
if (clazz == Byte.class)
return (ArgumentType<T>) IntegerArgumentType.integer(Byte.MIN_VALUE, Byte.MAX_VALUE);
if (clazz == Short.class)
return (ArgumentType<T>) IntegerArgumentType.integer(Short.MIN_VALUE, Short.MAX_VALUE);
if (clazz == Integer.class)
return (ArgumentType<T>) IntegerArgumentType.integer();
if (clazz == Long.class)
return (ArgumentType<T>) LongArgumentType.longArg();
if (clazz == Float.class)
return (ArgumentType<T>) FloatArgumentType.floatArg();
if (clazz == Double.class)
return (ArgumentType<T>) DoubleArgumentType.doubleArg();
throw new UnsupportedOperationException("Unsupported setting type: " + clazz);
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/koralix/oneforall/commands/Commands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.koralix.oneforall.commands;

import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;

public final class Commands {
private Commands() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}

public static void register(
CommandDispatcher<ServerCommandSource> dispatcher,
CommandRegistryAccess registryAccess,
CommandManager.RegistrationEnvironment environment
) {
common(dispatcher, registryAccess, environment);
if (environment.integrated) integrated(dispatcher, registryAccess);
else if (environment.dedicated) dedicated(dispatcher, registryAccess);
else throw new UnsupportedOperationException("Unknown environment");
}

private static void integrated(
CommandDispatcher<ServerCommandSource> dispatcher,
CommandRegistryAccess registryAccess
) {
// Register integrated commands here
}

private static void dedicated(
CommandDispatcher<ServerCommandSource> dispatcher,
CommandRegistryAccess registryAccess
) {
// Register dedicated commands here
}

private static void common(
CommandDispatcher<ServerCommandSource> dispatcher,
CommandRegistryAccess registryAccess,
CommandManager.RegistrationEnvironment environment
) {
OfaCommand.register(dispatcher, registryAccess, environment);
}
}
Loading