From 1df5dd54589147aa3900fa81fea9b2c4576053b0 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:18:34 +0500 Subject: [PATCH 1/9] Up protocol version --- coreSubProjects | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coreSubProjects b/coreSubProjects index 59767c807..d12e79773 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 59767c807b1193c184aed7b82baf55337e6ad786 +Subproject commit d12e79773223a42655d8e94fc71ef4146a261b73 From 69ffd795c961a2b579e87d6f7d564a6f7b518987 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sun, 20 Oct 2024 00:26:24 +0500 Subject: [PATCH 2/9] Split commands into classes --- .../common/AbstractModInitializer.java | 164 +----------------- .../common/commands/AbstractCommand.java | 79 +++++++++ .../common/commands/CommandInitializer.java | 40 +++++ .../common/commands/DhConfigCommand.java | 142 +++++++++++++++ .../common/commands/DhCrashCommand.java | 45 +++++ 5 files changed, 310 insertions(+), 160 deletions(-) create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/commands/DhConfigCommand.java create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/commands/DhCrashCommand.java diff --git a/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java b/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java index 4457d3105..79009d6a2 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java @@ -1,29 +1,20 @@ package com.seibel.distanthorizons.common; -import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.*; -import com.mojang.brigadier.builder.LiteralArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; import com.seibel.distanthorizons.api.methods.events.abstractEvents.DhApiAfterDhInitEvent; import com.seibel.distanthorizons.api.methods.events.abstractEvents.DhApiBeforeDhInitEvent; +import com.seibel.distanthorizons.common.commands.CommandInitializer; import com.seibel.distanthorizons.common.wrappers.DependencySetup; import com.seibel.distanthorizons.common.wrappers.minecraft.MinecraftServerWrapper; -import com.seibel.distanthorizons.common.wrappers.misc.ServerPlayerWrapper; import com.seibel.distanthorizons.core.api.internal.ClientApi; import com.seibel.distanthorizons.core.api.internal.SharedApi; import com.seibel.distanthorizons.core.config.Config; import com.seibel.distanthorizons.core.config.ConfigBase; import com.seibel.distanthorizons.core.config.eventHandlers.presets.ThreadPresetConfigEventHandler; -import com.seibel.distanthorizons.core.config.types.AbstractConfigType; -import com.seibel.distanthorizons.core.config.types.ConfigEntry; import com.seibel.distanthorizons.core.dependencyInjection.ModAccessorInjector; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.jar.ModJarInfo; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; -import com.seibel.distanthorizons.core.network.messages.base.CodecCrashMessage; -import com.seibel.distanthorizons.core.util.objects.Pair; -import com.seibel.distanthorizons.core.world.DhServerWorld; import com.seibel.distanthorizons.core.wrapperInterfaces.modAccessor.IModAccessor; import com.seibel.distanthorizons.core.wrapperInterfaces.modAccessor.IModChecker; import com.seibel.distanthorizons.coreapi.DependencyInjection.ApiEventInjector; @@ -34,25 +25,9 @@ import net.minecraft.server.dedicated.DedicatedServer; import org.apache.logging.log4j.Logger; import java.lang.invoke.MethodHandles; -import java.util.HashMap; -import java.util.Objects; -import java.util.function.BiFunction; import java.util.function.Consumer; -import java.util.function.Function; import java.util.function.Supplier; -import static com.mojang.brigadier.arguments.DoubleArgumentType.doubleArg; -import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; -import static com.seibel.distanthorizons.core.network.messages.MessageRegistry.DEBUG_CODEC_CRASH_MESSAGE; -import static net.minecraft.commands.Commands.argument; -import static net.minecraft.commands.Commands.literal; - -#if MC_VER >= MC_1_19_2 -import net.minecraft.network.chat.Component; -#else // < 1.19.2 -import net.minecraft.network.chat.TranslatableComponent; -#endif - /** * Base for all mod loader initializers * and handles most setup. @@ -61,7 +36,7 @@ public abstract class AbstractModInitializer { protected static final Logger LOGGER = DhLoggerBuilder.getLogger(MethodHandles.lookup().lookupClass().getSimpleName()); - private CommandDispatcher commandDispatcher; + private CommandInitializer commandInitializer; @@ -132,7 +107,7 @@ public abstract class AbstractModInitializer LOGGER.info(ModInfo.READABLE_NAME + " server Initialized."); ApiEventInjector.INSTANCE.fireAllEvents(DhApiAfterDhInitEvent.class, null); - this.subscribeRegisterCommandsEvent(dispatcher -> { this.commandDispatcher = dispatcher; }); + this.subscribeRegisterCommandsEvent(dispatcher -> { this.commandInitializer = new CommandInitializer(dispatcher); }); this.subscribeServerStartingEvent(server -> { @@ -140,7 +115,7 @@ public abstract class AbstractModInitializer this.initConfig(); this.postInit(); - this.initCommands(); + this.commandInitializer.initCommands(); LOGGER.info("Dedicated server initialized at " + server.getServerDirectory()); }); @@ -196,137 +171,6 @@ public abstract class AbstractModInitializer LOGGER.info("Mod Post-Initialized"); } - @SuppressWarnings({"rawtypes", "unchecked"}) - private void initCommands() - { - LiteralArgumentBuilder builder = literal("dhconfig") - .requires(source -> source.hasPermission(4)); - - for (AbstractConfigType type : ConfigBase.INSTANCE.entries) - { - if (!(type instanceof ConfigEntry)) - { - continue; - } - //noinspection PatternVariableCanBeUsed - ConfigEntry configEntry = (ConfigEntry) type; - if (configEntry.getServersideShortName() == null) - { - continue; - } - - Function< - Function, Object>, - Command - > makeConfigUpdater = (getter) -> (commandContext) -> { - Object value = getter.apply(commandContext); - - commandContext.getSource().sendSuccess( - #if MC_VER >= MC_1_20_1 - () -> Component.literal("Changed the value of "+configEntry.getServersideShortName()+" to "+value), - #elif MC_VER >= MC_1_19_2 - Component.literal("Changed the value of "+configEntry.getServersideShortName()+" to "+value), - #else - new TranslatableComponent("Changed the value of "+configEntry.getServersideShortName()+" to "+value), - #endif - true); - configEntry.set(value); - return 1; - }; - - LiteralArgumentBuilder subcommand = literal(configEntry.getServersideShortName()) - .executes((commandContext) -> { - #if MC_VER >= MC_1_20_1 - commandContext.getSource().sendSuccess(() -> Component.literal("Current value of "+configEntry.getServersideShortName()+" is "+configEntry.get()), true); - #elif MC_VER >= MC_1_19_2 - commandContext.getSource().sendSuccess(Component.literal("Current value of "+configEntry.getServersideShortName()+" is "+configEntry.get()), true); - #else // < 1.19.2 - commandContext.getSource().sendSuccess(new TranslatableComponent("Current value of "+configEntry.getServersideShortName()+" is "+configEntry.get()), true); - #endif - return 1; - }); - - if (Enum.class.isAssignableFrom(configEntry.getType())) - { - for (Object choice : configEntry.getType().getEnumConstants()) - { - subcommand.then( - literal(choice.toString()) - .executes(makeConfigUpdater.apply(c -> choice)) - ); - } - } - else - { - boolean setterAdded = false; - - for (java.util.Map.Entry, Pair>, BiFunction, String, ?>>> pair : new HashMap< - Class, - Pair< - Supplier>, - BiFunction, String, ?>> - >() {{ - this.put(Integer.class, new Pair<>(() -> integer((int) configEntry.getMin(), (int) configEntry.getMax()), IntegerArgumentType::getInteger)); - this.put(Double.class, new Pair<>(() -> doubleArg((double) configEntry.getMin(), (double) configEntry.getMax()), DoubleArgumentType::getDouble)); - this.put(Boolean.class, new Pair<>(BoolArgumentType::bool, BoolArgumentType::getBool)); - this.put(String.class, new Pair<>(StringArgumentType::string, StringArgumentType::getString)); - }}.entrySet()) - { - if (!pair.getKey().isAssignableFrom(configEntry.getType())) - { - continue; - } - - subcommand.then(argument("value", pair.getValue().first.get()) - .executes(makeConfigUpdater.apply(c -> pair.getValue().second.apply(c, "value")))); - - setterAdded = true; - break; - } - - if (!setterAdded) - { - throw new RuntimeException("Config type of "+type.getName()+" is not supported: "+configEntry.getType().getSimpleName()); - } - } - - builder.then(subcommand); - } - - this.commandDispatcher.register(builder); - - if (DEBUG_CODEC_CRASH_MESSAGE) - { - LiteralArgumentBuilder dhcrash = literal("dhcrash") - .requires(source -> source.hasPermission(4)) - .then(literal("encode") - .executes(c -> { - assert SharedApi.getIDhServerWorld() != null; - ((DhServerWorld) SharedApi.getIDhServerWorld()).getServerPlayerStateManager() - #if MC_VER >= MC_1_19_2 - .getConnectedPlayer(ServerPlayerWrapper.getWrapper(Objects.requireNonNull(c.getSource().getPlayer()))) - #else - .getConnectedPlayer(ServerPlayerWrapper.getWrapper(Objects.requireNonNull(c.getSource().getPlayerOrException()))) - #endif - .networkSession.sendMessage(new CodecCrashMessage(CodecCrashMessage.ECrashPhase.ENCODE)); - return 1; - })) - .then(literal("decode") - .executes(c -> { - assert SharedApi.getIDhServerWorld() != null; - ((DhServerWorld) SharedApi.getIDhServerWorld()).getServerPlayerStateManager() - #if MC_VER >= MC_1_19_2 - .getConnectedPlayer(ServerPlayerWrapper.getWrapper(Objects.requireNonNull(c.getSource().getPlayer()))) - #else - .getConnectedPlayer(ServerPlayerWrapper.getWrapper(Objects.requireNonNull(c.getSource().getPlayerOrException()))) - #endif - .networkSession.sendMessage(new CodecCrashMessage(CodecCrashMessage.ECrashPhase.DECODE)); - return 1; - })); - this.commandDispatcher.register(dhcrash); - } - } - //==================================// diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java new file mode 100644 index 000000000..b407aa95a --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java @@ -0,0 +1,79 @@ +package com.seibel.distanthorizons.common.commands; + +import com.mojang.brigadier.context.CommandContext; +import com.seibel.distanthorizons.common.wrappers.misc.ServerPlayerWrapper; +import com.seibel.distanthorizons.core.wrapperInterfaces.misc.IServerPlayerWrapper; +import net.minecraft.commands.CommandSourceStack; + +#if MC_VER >= MC_1_19_2 +import net.minecraft.network.chat.Component; + +import java.util.Objects; +#else // < 1.19.2 +import net.minecraft.network.chat.TranslatableComponent; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +#endif + +/** + * Abstract class providing common functionality for DH's commands. + */ +public abstract class AbstractCommand +{ + /** + * Sends a success response to the player with the given text. + * + * @param commandContext The command context to send the response to. + * @param text The text to display in the success message. + * @return 1, indicating that the command was successful. + */ + protected int sendSuccessResponse(CommandContext commandContext, String text) + { + #if MC_VER >= MC_1_20_1 + commandContext.getSource().sendSuccess(() -> Component.literal(text), true); + #elif MC_VER >= MC_1_19_2 + commandContext.getSource().sendSuccess(Component.literal(text), true); + #else + commandContext.getSource().sendSuccess(new TranslatableComponent(text), true); + #endif + return 1; + } + + /** + * Gets the server player from a command context. + * + * @param commandContext The command context to get the server player from. + * @return The server player wrapper for the player who sent the command. + */ + protected IServerPlayerWrapper getSourcePlayer(CommandContext commandContext) #if MC_VER < MC_1_19_2 throws CommandSyntaxException #endif + { + #if MC_VER >= MC_1_19_2 + return ServerPlayerWrapper.getWrapper(Objects.requireNonNull(commandContext.getSource().getPlayer())); + #else + return ServerPlayerWrapper.getWrapper(commandContext.getSource().getPlayerOrException()); + #endif + } + + /** + * Checks if the source of a command is a player. + * + * @param source The source of the command to check. + * @return True if the source is a player, false otherwise. + */ + protected boolean isPlayerSource(CommandSourceStack source) + { + #if MC_VER >= MC_1_19_2 + return source.isPlayer(); + #else + try + { + source.getPlayerOrException(); + return true; + } + catch (CommandSyntaxException e) + { + return false; + } + #endif + } + +} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java new file mode 100644 index 000000000..393052c9c --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java @@ -0,0 +1,40 @@ +package com.seibel.distanthorizons.common.commands; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.commands.CommandSourceStack; + +import static com.seibel.distanthorizons.core.network.messages.MessageRegistry.DEBUG_CODEC_CRASH_MESSAGE; + +/** + * Initializes commands of the mod. + */ +public class CommandInitializer +{ + private final CommandDispatcher commandDispatcher; + + /** + * Constructs a new instance of this class. + * + * @param commandDispatcher The dispatcher to use for registering commands. + */ + public CommandInitializer(CommandDispatcher commandDispatcher) + { + this.commandDispatcher = commandDispatcher; + } + + + + /** + * Initializes all available commands. + */ + public void initCommands() + { + new DhConfigCommand().register(this.commandDispatcher); + + if (DEBUG_CODEC_CRASH_MESSAGE) + { + new DhCrashCommand().register(this.commandDispatcher); + } + } + +} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/DhConfigCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/DhConfigCommand.java new file mode 100644 index 000000000..d1673e41a --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/DhConfigCommand.java @@ -0,0 +1,142 @@ +package com.seibel.distanthorizons.common.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.*; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.seibel.distanthorizons.core.config.ConfigBase; +import com.seibel.distanthorizons.core.config.types.AbstractConfigType; +import com.seibel.distanthorizons.core.config.types.ConfigEntry; +import net.minecraft.commands.CommandSourceStack; + +import java.util.Arrays; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.ToIntBiFunction; + +import static com.mojang.brigadier.arguments.DoubleArgumentType.doubleArg; +import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; +import static net.minecraft.commands.Commands.argument; +import static net.minecraft.commands.Commands.literal; + +/** + * Command for managing config. + */ +public class DhConfigCommand extends AbstractCommand +{ + private static final List> commandArguments = Arrays.asList( + new CommandArgumentData<>(Integer.class, configEntry -> integer(configEntry.getMin(), configEntry.getMax()), IntegerArgumentType::getInteger), + new CommandArgumentData<>(Double.class, configEntry -> doubleArg(configEntry.getMin(), configEntry.getMax()), DoubleArgumentType::getDouble), + new CommandArgumentData<>(Boolean.class, BoolArgumentType::bool, BoolArgumentType::getBool), + new CommandArgumentData<>(String.class, StringArgumentType::string, StringArgumentType::getString) + ); + + /** + * Registers the command with the given dispatcher. + * + * @param commandDispatcher the dispatcher to register the command with. + */ + @SuppressWarnings({"rawtypes", "unchecked"}) + public void register(CommandDispatcher commandDispatcher) + { + LiteralArgumentBuilder builder = literal("dhconfig") + .requires(source -> source.hasPermission(4)); + + for (AbstractConfigType type : ConfigBase.INSTANCE.entries) + { + // Skip non-config entries + if (!(type instanceof ConfigEntry)) + { + continue; + } + + //noinspection PatternVariableCanBeUsed + ConfigEntry configEntry = (ConfigEntry) type; + if (configEntry.getServersideShortName() == null) + { + continue; + } + + LiteralArgumentBuilder subcommand = literal(configEntry.getServersideShortName()) + .executes(commandContext -> this.sendSuccessResponse(commandContext, "Current value of ${configEntry.getServersideShortName()} is ${configEntry.get()}")); + + ToIntBiFunction, Object> updateConfigValue = (commandContext, value) -> { + configEntry.set(value); + return this.sendSuccessResponse(commandContext, "Changed the value of ${configEntry.getServersideShortName()} to $value"); + }; + + // Enum type needs a special case since enums aren't represented by existing argument type + // and need literals for each individual value + if (Enum.class.isAssignableFrom(configEntry.getType())) + { + for (Object choice : configEntry.getType().getEnumConstants()) + { + subcommand.then( + literal(choice.toString()) + .executes(c -> updateConfigValue.applyAsInt(c, choice)) + ); + } + } + else + { + boolean setterAdded = false; + for (CommandArgumentData commandArgumentData : commandArguments) + { + if (!commandArgumentData.argumentClass.isAssignableFrom(configEntry.getType())) + { + continue; + } + + subcommand.then(argument("value", commandArgumentData.getArgumentType(configEntry)) + .executes(c -> updateConfigValue.applyAsInt(c, commandArgumentData.getValue(c, "value")))); + + setterAdded = true; + break; + } + + if (!setterAdded) + { + throw new RuntimeException("Config type of " + type.getName() + " is not supported: " + configEntry.getType().getSimpleName()); + } + } + + builder.then(subcommand); + } + + commandDispatcher.register(builder); + } + + + + private static class CommandArgumentData + { + public final Class argumentClass; + public final Function, ArgumentType> argumentTypeFunction; + private final BiFunction, String, T> valueGetter; + + public CommandArgumentData(Class argumentClass, Supplier> argumentTypeSupplier, BiFunction, String, T> valueGetter) + { + this(argumentClass, configEntry -> argumentTypeSupplier.get(), valueGetter); + } + public CommandArgumentData(Class argumentClass, Function, ArgumentType> argumentTypeFunction, BiFunction, String, T> valueGetter) + { + this.argumentClass = argumentClass; + this.argumentTypeFunction = argumentTypeFunction; + this.valueGetter = valueGetter; + } + + public ArgumentType getArgumentType(ConfigEntry configEntry) + { + return this.argumentTypeFunction.apply(configEntry); + } + + public T getValue(CommandContext commandContext, String argumentName) + { + return this.valueGetter.apply(commandContext, argumentName); + } + + } + +} \ No newline at end of file diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/DhCrashCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/DhCrashCommand.java new file mode 100644 index 000000000..354d0cbc4 --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/DhCrashCommand.java @@ -0,0 +1,45 @@ +package com.seibel.distanthorizons.common.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.seibel.distanthorizons.core.api.internal.SharedApi; +import com.seibel.distanthorizons.core.multiplayer.server.ServerPlayerState; +import com.seibel.distanthorizons.core.network.messages.base.CodecCrashMessage; +import net.minecraft.commands.CommandSourceStack; + +import static net.minecraft.commands.Commands.literal; + +public class DhCrashCommand extends AbstractCommand +{ + public void register(CommandDispatcher commandDispatcher) + { + LiteralArgumentBuilder dhcrash = literal("dhcrash") + .requires(source -> this.isPlayerSource(source) && source.hasPermission(4)) + .then(literal("encode") + .executes(c -> { + assert SharedApi.getIDhServerWorld() != null; + + ServerPlayerState serverPlayerState = SharedApi.getIDhServerWorld().getServerPlayerStateManager() + .getConnectedPlayer(this.getSourcePlayer(c)); + if (serverPlayerState != null) + { + serverPlayerState.networkSession.sendMessage(new CodecCrashMessage(CodecCrashMessage.ECrashPhase.ENCODE)); + } + return 1; + })) + .then(literal("decode") + .executes(c -> { + assert SharedApi.getIDhServerWorld() != null; + + ServerPlayerState serverPlayerState = SharedApi.getIDhServerWorld().getServerPlayerStateManager() + .getConnectedPlayer(this.getSourcePlayer(c)); + if (serverPlayerState != null) + { + serverPlayerState.networkSession.sendMessage(new CodecCrashMessage(CodecCrashMessage.ECrashPhase.DECODE)); + } + return 1; + })); + commandDispatcher.register(dhcrash); + } + +} From 48120a4a38805ceef1498f4c4e8d6b24278a6b18 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:14:03 +0500 Subject: [PATCH 3/9] Move commands under `/dh`, add `/dh debug` command --- .../common/commands/AbstractCommand.java | 4 +++ .../common/commands/CommandInitializer.java | 12 +++++++-- ...hConfigCommand.java => ConfigCommand.java} | 15 +++++------ ...{DhCrashCommand.java => CrashCommand.java} | 11 ++++---- .../common/commands/DebugCommand.java | 25 +++++++++++++++++++ 5 files changed, 50 insertions(+), 17 deletions(-) rename common/src/main/java/com/seibel/distanthorizons/common/commands/{DhConfigCommand.java => ConfigCommand.java} (92%) rename common/src/main/java/com/seibel/distanthorizons/common/commands/{DhCrashCommand.java => CrashCommand.java} (78%) create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/commands/DebugCommand.java diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java index b407aa95a..febb0c7f4 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java @@ -1,5 +1,6 @@ package com.seibel.distanthorizons.common.commands; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.seibel.distanthorizons.common.wrappers.misc.ServerPlayerWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.misc.IServerPlayerWrapper; @@ -19,6 +20,9 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; */ public abstract class AbstractCommand { + public abstract LiteralArgumentBuilder buildCommand(); + + /** * Sends a success response to the player with the given text. * diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java index 393052c9c..d03274498 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java @@ -1,9 +1,11 @@ package com.seibel.distanthorizons.common.commands; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.minecraft.commands.CommandSourceStack; import static com.seibel.distanthorizons.core.network.messages.MessageRegistry.DEBUG_CODEC_CRASH_MESSAGE; +import static net.minecraft.commands.Commands.literal; /** * Initializes commands of the mod. @@ -29,12 +31,18 @@ public class CommandInitializer */ public void initCommands() { - new DhConfigCommand().register(this.commandDispatcher); + LiteralArgumentBuilder builder = literal("dh") + .requires(source -> source.hasPermission(4)); + + builder.then(new ConfigCommand().buildCommand()); + builder.then(new DebugCommand().buildCommand()); if (DEBUG_CODEC_CRASH_MESSAGE) { - new DhCrashCommand().register(this.commandDispatcher); + builder.then(new CrashCommand().buildCommand()); } + + this.commandDispatcher.register(builder); } } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/DhConfigCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/ConfigCommand.java similarity index 92% rename from common/src/main/java/com/seibel/distanthorizons/common/commands/DhConfigCommand.java rename to common/src/main/java/com/seibel/distanthorizons/common/commands/ConfigCommand.java index d1673e41a..ff4cbaf64 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/commands/DhConfigCommand.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/ConfigCommand.java @@ -1,6 +1,5 @@ package com.seibel.distanthorizons.common.commands; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.*; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; @@ -24,7 +23,7 @@ import static net.minecraft.commands.Commands.literal; /** * Command for managing config. */ -public class DhConfigCommand extends AbstractCommand +public class ConfigCommand extends AbstractCommand { private static final List> commandArguments = Arrays.asList( new CommandArgumentData<>(Integer.class, configEntry -> integer(configEntry.getMin(), configEntry.getMax()), IntegerArgumentType::getInteger), @@ -34,15 +33,13 @@ public class DhConfigCommand extends AbstractCommand ); /** - * Registers the command with the given dispatcher. - * - * @param commandDispatcher the dispatcher to register the command with. + * Builds a command tree. */ + @Override @SuppressWarnings({"rawtypes", "unchecked"}) - public void register(CommandDispatcher commandDispatcher) + public LiteralArgumentBuilder buildCommand() { - LiteralArgumentBuilder builder = literal("dhconfig") - .requires(source -> source.hasPermission(4)); + LiteralArgumentBuilder builder = literal("config"); for (AbstractConfigType type : ConfigBase.INSTANCE.entries) { @@ -105,7 +102,7 @@ public class DhConfigCommand extends AbstractCommand builder.then(subcommand); } - commandDispatcher.register(builder); + return builder; } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/DhCrashCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/CrashCommand.java similarity index 78% rename from common/src/main/java/com/seibel/distanthorizons/common/commands/DhCrashCommand.java rename to common/src/main/java/com/seibel/distanthorizons/common/commands/CrashCommand.java index 354d0cbc4..c44700a7c 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/commands/DhCrashCommand.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/CrashCommand.java @@ -1,6 +1,5 @@ package com.seibel.distanthorizons.common.commands; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.seibel.distanthorizons.core.api.internal.SharedApi; import com.seibel.distanthorizons.core.multiplayer.server.ServerPlayerState; @@ -9,12 +8,13 @@ import net.minecraft.commands.CommandSourceStack; import static net.minecraft.commands.Commands.literal; -public class DhCrashCommand extends AbstractCommand +public class CrashCommand extends AbstractCommand { - public void register(CommandDispatcher commandDispatcher) + @Override + public LiteralArgumentBuilder buildCommand() { - LiteralArgumentBuilder dhcrash = literal("dhcrash") - .requires(source -> this.isPlayerSource(source) && source.hasPermission(4)) + return literal("crash") + .requires(this::isPlayerSource) .then(literal("encode") .executes(c -> { assert SharedApi.getIDhServerWorld() != null; @@ -39,7 +39,6 @@ public class DhCrashCommand extends AbstractCommand } return 1; })); - commandDispatcher.register(dhcrash); } } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/DebugCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/DebugCommand.java new file mode 100644 index 000000000..fd67ec8f8 --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/DebugCommand.java @@ -0,0 +1,25 @@ +package com.seibel.distanthorizons.common.commands; + +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.seibel.distanthorizons.core.logging.f3.F3Screen; +import net.minecraft.commands.CommandSourceStack; + +import java.util.ArrayList; +import java.util.List; + +import static net.minecraft.commands.Commands.literal; + +public class DebugCommand extends AbstractCommand +{ + @Override + public LiteralArgumentBuilder buildCommand() + { + return literal("debug") + .executes(c -> { + List lines = new ArrayList<>(); + F3Screen.addStringToDisplay(lines); + return this.sendSuccessResponse(c, String.join("\n", lines)); + }); + } + +} From 17713a9f931c1414a831df44683638d481464996 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:15:15 +0500 Subject: [PATCH 4/9] Make sure data source received from file handler is fully generated before sending to client --- coreSubProjects | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coreSubProjects b/coreSubProjects index d12e79773..271f128de 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit d12e79773223a42655d8e94fc71ef4146a261b73 +Subproject commit 271f128de63daeef55b84621147f11a074608daf From 2babae40de6f9d70162ce290af791acf4ebe7dd8 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sun, 20 Oct 2024 20:04:35 +0500 Subject: [PATCH 5/9] Multiply update queue size by player count --- .../common/wrappers/minecraft/MinecraftClientWrapper.java | 7 +++++++ .../common/wrappers/minecraft/MinecraftServerWrapper.java | 6 ++++++ coreSubProjects | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftClientWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftClientWrapper.java index d66d891f1..7b8b27df0 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftClientWrapper.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftClientWrapper.java @@ -22,6 +22,7 @@ package com.seibel.distanthorizons.common.wrappers.minecraft; import java.io.File; import java.lang.invoke.MethodHandles; import java.util.ArrayList; +import java.util.Objects; import java.util.UUID; import com.mojang.blaze3d.platform.NativeImage; @@ -321,4 +322,10 @@ public class MinecraftClientWrapper implements IMinecraftClientWrapper, IMinecra @Override public boolean isWorldNew() { throw new UnsupportedOperationException("Not Implemented"); } + @Override + public int getPlayerCount() + { + return Objects.requireNonNull(MINECRAFT.getSingleplayerServer()).getPlayerCount(); + } + } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftServerWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftServerWrapper.java index 1410e785a..3eb8409fa 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftServerWrapper.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/minecraft/MinecraftServerWrapper.java @@ -47,4 +47,10 @@ public class MinecraftServerWrapper implements IMinecraftSharedWrapper public boolean isWorldNew() { return this.dedicatedServer.getWorldData().overworldData().isInitialized(); } + @Override + public int getPlayerCount() + { + return this.dedicatedServer.getPlayerCount(); + } + } diff --git a/coreSubProjects b/coreSubProjects index 271f128de..b8a862ddd 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 271f128de63daeef55b84621147f11a074608daf +Subproject commit b8a862ddd822be2367fdf1bba6b8e1ee7241dbc2 From ad0b78936ad6274d170ecb5291a88a5ae3161d27 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:43:39 +0500 Subject: [PATCH 6/9] Enable multiplayer in 1.16.5 --- fabric/build.gradle | 10 +++++++++- forge/build.gradle | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/fabric/build.gradle b/fabric/build.gradle index 479b57014..c57429c51 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -12,7 +12,15 @@ loom { setConfigName("Fabric Client") ideConfigGenerated(true) // When true a run configuration file will be generated for IDE's. By default only set to true for the root project. runDir("../run/client") - vmArgs("-Dio.netty.leakDetection.level=advanced") // https://netty.io/wiki/reference-counted-objects.html#leak-detection-levels + vmArgs( + // https://github.com/FabricMC/fabric-loom/issues/915#issuecomment-1609154390 + "-Dminecraft.api.auth.host=https://nope.invalid", + "-Dminecraft.api.account.host=https://nope.invalid", + "-Dminecraft.api.session.host=https://nope.invalid", + "-Dminecraft.api.services.host=https://nope.invalid", + // https://netty.io/wiki/reference-counted-objects.html#leak-detection-levels + "-Dio.netty.leakDetection.level=advanced" + ) programArgs("--username", "Dev") } server { diff --git a/forge/build.gradle b/forge/build.gradle index 3676f2537..e9fc94579 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -39,7 +39,15 @@ loom { setConfigName("Forge Client") ideConfigGenerated(false) // When true a run configuration file will be generated for IDE's. By default only set to true for the root project. runDir("../run/client") - vmArgs("-Dio.netty.leakDetection.level=advanced") // https://netty.io/wiki/reference-counted-objects.html#leak-detection-levels + vmArgs( + // https://github.com/FabricMC/fabric-loom/issues/915#issuecomment-1609154390 + "-Dminecraft.api.auth.host=https://nope.invalid", + "-Dminecraft.api.account.host=https://nope.invalid", + "-Dminecraft.api.session.host=https://nope.invalid", + "-Dminecraft.api.services.host=https://nope.invalid", + // https://netty.io/wiki/reference-counted-objects.html#leak-detection-levels + "-Dio.netty.leakDetection.level=advanced" + ) programArgs("--username", "Dev") } server { From 45a07206c9b9d41cef584408cc23f8ea47afdca1 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:43:46 +0500 Subject: [PATCH 7/9] Update core --- coreSubProjects | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coreSubProjects b/coreSubProjects index b8a862ddd..f94b55ff0 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit b8a862ddd822be2367fdf1bba6b8e1ee7241dbc2 +Subproject commit f94b55ff00b0703eb77bffd04cad2591ff881b45 From 9c3c37bc3e69ddc4bc40d9277fc7327bf5b8360a Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Tue, 22 Oct 2024 23:55:37 +0500 Subject: [PATCH 8/9] Use earlier event for storing server instance --- .../com/seibel/distanthorizons/neoforge/NeoforgeMain.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeMain.java b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeMain.java index f303d8c34..797004167 100644 --- a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeMain.java +++ b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeMain.java @@ -42,7 +42,7 @@ import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.fml.event.lifecycle.FMLDedicatedServerSetupEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.RegisterCommandsEvent; -import net.neoforged.neoforge.event.server.ServerStartingEvent; +import net.neoforged.neoforge.event.server.ServerAboutToStartEvent; import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; import java.util.function.Consumer; @@ -130,7 +130,7 @@ public class NeoforgeMain extends AbstractModInitializer @Override protected void subscribeServerStartingEvent(Consumer eventHandler) { - NeoForge.EVENT_BUS.addListener(EventPriority.HIGH, (ServerStartingEvent e) -> { eventHandler.accept(e.getServer()); }); + NeoForge.EVENT_BUS.addListener(EventPriority.HIGH, (ServerAboutToStartEvent e) -> { eventHandler.accept(e.getServer()); }); } @Override From 92ed9b8070290906f5315e02e85813b1678d10a6 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Tue, 22 Oct 2024 23:55:50 +0500 Subject: [PATCH 9/9] Update core --- coreSubProjects | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coreSubProjects b/coreSubProjects index f94b55ff0..45f4bd694 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit f94b55ff00b0703eb77bffd04cad2591ff881b45 +Subproject commit 45f4bd6949b5c4f97fb8836ef819232a33ba3da0