From 1bd0a3fa1da5e9a3c187179c0b472695393c4090 Mon Sep 17 00:00:00 2001 From: TexTrue <65154269+TexBlock@users.noreply.github.com> Date: Sat, 5 Oct 2024 14:05:42 +0800 Subject: [PATCH] rename on `platform` class name, add ClientCommandRegistryEvent --- .../event/ClientCommandRegistryEvent.java | 24 +++ .../event}/CommandRegistryEvent.java | 2 +- .../command/util/ClientCommandManager.java | 31 +++ .../util/ClientCommandSourceExtension.java | 86 ++++++++ .../api/command/util/ClientCommandUtils.java | 200 ++++++++++++++++++ .../command/ClientCommandSourceMixin.java | 45 ++++ .../mixin/command/HelpCommandAccessor.java | 14 ++ .../testmod/command/KessokuTestCommand.java | 2 +- .../command/fabric/KessokuCommandImpl.java | 9 +- .../command/neoforge/KessokuCommandImpl.java | 2 +- .../lib/api/entrypoint/KessokuEntrypoint.java | 12 +- .../lib/api/entrypoint/LanguageAdapter.java | 6 +- .../impl/entrypoint/JavaLanguageAdapter.java | 6 +- ...ependencyInfo.java => DependencyInfo.java} | 6 +- .../platform/{ModLoader.java => Loader.java} | 32 +-- .../platform/{ModData.java => Metadata.java} | 4 +- ...dLoaderService.java => LoaderService.java} | 12 +- ...yInfoImpl.java => DependencyInfoImpl.java} | 6 +- .../{ModLoaderImpl.java => LoaderImpl.java} | 18 +- .../{ModDataImpl.java => MetadataImpl.java} | 14 +- ...yInfoImpl.java => DependencyInfoImpl.java} | 6 +- .../{ModLoaderImpl.java => LoaderImpl.java} | 18 +- .../{ModDataImpl.java => MetadataImpl.java} | 14 +- 23 files changed, 488 insertions(+), 81 deletions(-) create mode 100644 command/common/src/main/java/band/kessoku/lib/api/command/event/ClientCommandRegistryEvent.java rename command/common/src/main/java/band/kessoku/lib/api/{event/command => command/event}/CommandRegistryEvent.java (97%) create mode 100644 command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandManager.java create mode 100644 command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandSourceExtension.java create mode 100644 command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandUtils.java create mode 100644 command/common/src/main/java/band/kessoku/lib/mixin/command/ClientCommandSourceMixin.java create mode 100644 command/common/src/main/java/band/kessoku/lib/mixin/command/HelpCommandAccessor.java rename platform/common/src/main/java/band/kessoku/lib/api/platform/{ModDependencyInfo.java => DependencyInfo.java} (94%) rename platform/common/src/main/java/band/kessoku/lib/api/platform/{ModLoader.java => Loader.java} (56%) rename platform/common/src/main/java/band/kessoku/lib/api/platform/{ModData.java => Metadata.java} (96%) rename platform/common/src/main/java/band/kessoku/lib/service/platform/{ModLoaderService.java => LoaderService.java} (79%) rename platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/{ModDependencyInfoImpl.java => DependencyInfoImpl.java} (87%) rename platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/{ModLoaderImpl.java => LoaderImpl.java} (81%) rename platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/{ModDataImpl.java => MetadataImpl.java} (87%) rename platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/{ModDependencyInfoImpl.java => DependencyInfoImpl.java} (87%) rename platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/{ModLoaderImpl.java => LoaderImpl.java} (78%) rename platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/{ModDataImpl.java => MetadataImpl.java} (89%) diff --git a/command/common/src/main/java/band/kessoku/lib/api/command/event/ClientCommandRegistryEvent.java b/command/common/src/main/java/band/kessoku/lib/api/command/event/ClientCommandRegistryEvent.java new file mode 100644 index 00000000..6e926fe5 --- /dev/null +++ b/command/common/src/main/java/band/kessoku/lib/api/command/event/ClientCommandRegistryEvent.java @@ -0,0 +1,24 @@ +package band.kessoku.lib.api.command.event; + +import band.kessoku.lib.api.command.util.ClientCommandSourceExtension; +import band.kessoku.lib.event.api.Event; +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.command.CommandRegistryAccess; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.NonExtendable +public interface ClientCommandRegistryEvent { + Event EVENT = Event.of(clientCommandRegistryEvents -> (dispatcher, registryAccess) -> { + for (ClientCommandRegistryEvent callback : clientCommandRegistryEvents) { + callback.register(dispatcher, registryAccess); + } + }); + + /** + * Called when registering client commands. + * + * @param dispatcher the command dispatcher to register commands to + * @param registryAccess object exposing access to the game's registries + */ + void register(CommandDispatcher dispatcher, CommandRegistryAccess registryAccess); +} diff --git a/command/common/src/main/java/band/kessoku/lib/api/event/command/CommandRegistryEvent.java b/command/common/src/main/java/band/kessoku/lib/api/command/event/CommandRegistryEvent.java similarity index 97% rename from command/common/src/main/java/band/kessoku/lib/api/event/command/CommandRegistryEvent.java rename to command/common/src/main/java/band/kessoku/lib/api/command/event/CommandRegistryEvent.java index ca36d488..fe90f650 100644 --- a/command/common/src/main/java/band/kessoku/lib/api/event/command/CommandRegistryEvent.java +++ b/command/common/src/main/java/band/kessoku/lib/api/command/event/CommandRegistryEvent.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package band.kessoku.lib.api.event.command; +package band.kessoku.lib.api.command.event; import band.kessoku.lib.event.api.Event; import com.mojang.brigadier.CommandDispatcher; diff --git a/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandManager.java b/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandManager.java new file mode 100644 index 00000000..bb558204 --- /dev/null +++ b/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandManager.java @@ -0,0 +1,31 @@ +package band.kessoku.lib.api.command.util; + +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; + +public class ClientCommandManager { + + + /** + * Creates a literal argument builder. + * + * @param name the literal name + * @return the created argument builder + */ + public static LiteralArgumentBuilder literal(String name) { + return LiteralArgumentBuilder.literal(name); + } + + /** + * Creates a required argument builder. + * + * @param name the name of the argument + * @param type the type of the argument + * @param the type of the parsed argument value + * @return the created argument builder + */ + public static RequiredArgumentBuilder argument(String name, ArgumentType type) { + return RequiredArgumentBuilder.argument(name, type); + } +} diff --git a/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandSourceExtension.java b/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandSourceExtension.java new file mode 100644 index 00000000..cdc92d14 --- /dev/null +++ b/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandSourceExtension.java @@ -0,0 +1,86 @@ +package band.kessoku.lib.api.command.util; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.command.CommandSource; +import net.minecraft.entity.Entity; +import net.minecraft.text.Text; +import net.minecraft.util.math.Vec2f; +import net.minecraft.util.math.Vec3d; + +public interface ClientCommandSourceExtension extends CommandSource { + /** + * Sends a feedback message to the player. + * + * @param message the feedback message + */ + void kessokulib$sendFeedback(Text message); + + /** + * Sends an error message to the player. + * + * @param message the error message + */ + void kessokulib$sendError(Text message); + + /** + * Gets the client instance used to run the command. + * + * @return the client + */ + MinecraftClient kessokulib$getClient(); + + /** + * Gets the player that used the command. + * + * @return the player + */ + ClientPlayerEntity kessokulib$getPlayer(); + + /** + * Gets the entity that used the command. + * + * @return the entity + */ + default Entity kessokulib$getEntity() { + return kessokulib$getPlayer(); + } + + /** + * Gets the position from where the command has been executed. + * + * @return the position + */ + default Vec3d kessokulib$getPosition() { + return kessokulib$getPlayer().getPos(); + } + + /** + * Gets the rotation of the entity that used the command. + * + * @return the rotation + */ + default Vec2f kessokulib$getRotation() { + return kessokulib$getPlayer().getRotationClient(); + } + + /** + * Gets the world where the player used the command. + * + * @return the world + */ + ClientWorld kessokulib$getWorld(); + + /** + * Gets the meta property under {@code key} that was assigned to this source. + * + *

This method should return the same result for every call with the same {@code key}. + * + * @param key the meta key + * @return the meta + */ + default Object kessokulib$getMeta(String key) { + return null; + } +} diff --git a/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandUtils.java b/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandUtils.java new file mode 100644 index 00000000..04d067cb --- /dev/null +++ b/command/common/src/main/java/band/kessoku/lib/api/command/util/ClientCommandUtils.java @@ -0,0 +1,200 @@ +package band.kessoku.lib.api.command.util; + +import band.kessoku.lib.api.KessokuLib; +import band.kessoku.lib.api.command.KessokuCommand; +import band.kessoku.lib.mixin.command.HelpCommandAccessor; +import com.google.common.collect.Iterables; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.ParseResults; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.context.ParsedCommandNode; +import com.mojang.brigadier.exceptions.BuiltInExceptionProvider; +import com.mojang.brigadier.exceptions.CommandExceptionType; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.tree.CommandNode; +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; +import net.minecraft.text.Texts; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public final class ClientCommandUtils { + private static @Nullable CommandDispatcher activeDispatcher; + + public static void setActiveDispatcher(@Nullable CommandDispatcher dispatcher) { + ClientCommandUtils.activeDispatcher = dispatcher; + } + + public static @Nullable CommandDispatcher getActiveDispatcher() { + return activeDispatcher; + } + + /** + * Executes a client-sided command. Callers should ensure that this is only called + * on slash-prefixed messages and the slash needs to be removed before calling. + * (This is the same requirement as {@code ClientPlayerEntity#sendCommand}.) + * + * @param command the command with slash removed + * @return true if the command should not be sent to the server, false otherwise + */ + public static boolean executeCommand(String command) { + MinecraftClient client = MinecraftClient.getInstance(); + + // The interface is implemented on ClientCommandSource with a mixin. + // noinspection ConstantConditions + ClientCommandSourceExtension commandSource = (ClientCommandSourceExtension) client.getNetworkHandler().getCommandSource(); + + client.getProfiler().push(command); + + try { + // TODO: Check for server commands before executing. + // This requires parsing the command, checking if they match a server command + // and then executing the command with the parse results. + activeDispatcher.execute(command, commandSource); + return true; + } catch (CommandSyntaxException e) { + boolean ignored = isIgnoredException(e.getType()); + + if (ignored) { + KessokuLib.getLogger().debug("Syntax exception for client-sided command '{}'", command, e); + return false; + } + + KessokuLib.getLogger().warn("Syntax exception for client-sided command '{}'", command, e); + commandSource.kessokulib$sendError(getErrorMessage(e)); + return true; + } catch (Exception e) { + KessokuLib.getLogger().warn("Error while executing client-sided command '{}'", command, e); + commandSource.kessokulib$sendError(Text.of(e.getMessage())); + return true; + } finally { + client.getProfiler().pop(); + } + } + + /** + * Tests whether a command syntax exception with the type + * should be ignored and the command sent to the server. + * + * @param type the exception type + * @return true if ignored, false otherwise + */ + private static boolean isIgnoredException(CommandExceptionType type) { + BuiltInExceptionProvider builtins = CommandSyntaxException.BUILT_IN_EXCEPTIONS; + + // Only ignore unknown commands and node parse exceptions. + // The argument-related dispatcher exceptions are not ignored because + // they will only happen if the user enters a correct command. + return type == builtins.dispatcherUnknownCommand() || type == builtins.dispatcherParseException(); + } + + // See ChatInputSuggestor.formatException. That cannot be used directly as it returns an OrderedText instead of a Text. + private static Text getErrorMessage(CommandSyntaxException e) { + Text message = Texts.toText(e.getRawMessage()); + String context = e.getContext(); + + return context != null ? Text.translatable("command.context.parse_error", message, e.getCursor(), context) : message; + } + + /** + * Runs final initialization tasks such as {@link CommandDispatcher#findAmbiguities(AmbiguityConsumer)} + * on the command dispatcher. Also registers a {@code /fcc help} command if there are other commands present. + */ + public static void finalizeInit() { + if (!activeDispatcher.getRoot().getChildren().isEmpty()) { + // Register an API command if there are other commands; + // these helpers are not needed if there are no client commands + LiteralArgumentBuilder help = ClientCommandManager.literal("help"); + help.executes(ClientCommandUtils::executeRootHelp); + help.then(ClientCommandManager.argument("command", StringArgumentType.greedyString()).executes(ClientCommandUtils::executeArgumentHelp)); + + CommandNode mainNode = activeDispatcher.register(ClientCommandManager.literal(KessokuCommand.MOD_ID).then(help)); + activeDispatcher.register(ClientCommandManager.literal(KessokuCommand.MOD_ID).redirect(mainNode)); + } + + // noinspection CodeBlock2Expr + activeDispatcher.findAmbiguities((parent, child, sibling, inputs) -> { + KessokuLib.getLogger().warn("Ambiguity between arguments {} and {} with inputs: {}", activeDispatcher.getPath(child), activeDispatcher.getPath(sibling), inputs); + }); + } + + private static int executeRootHelp(CommandContext context) { + return executeHelp(activeDispatcher.getRoot(), context); + } + + private static int executeArgumentHelp(CommandContext context) throws CommandSyntaxException { + ParseResults parseResults = activeDispatcher.parse(StringArgumentType.getString(context, "command"), context.getSource()); + List> nodes = parseResults.getContext().getNodes(); + + if (nodes.isEmpty()) { + throw HelpCommandAccessor.getFailedException().create(); + } + + return executeHelp(Iterables.getLast(nodes).getNode(), context); + } + + private static int executeHelp(CommandNode startNode, CommandContext context) { + Map, String> commands = activeDispatcher.getSmartUsage(startNode, context.getSource()); + + for (String command : commands.values()) { + context.getSource().kessokulib$sendFeedback(Text.literal("/" + command)); + } + + return commands.size(); + } + + public static void addCommands(CommandDispatcher target, ClientCommandSourceExtension source) { + Map, CommandNode> originalToCopy = new HashMap<>(); + originalToCopy.put(activeDispatcher.getRoot(), target.getRoot()); + copyChildren(activeDispatcher.getRoot(), target.getRoot(), source, originalToCopy); + } + + /** + * Copies the child commands from origin to target, filtered by {@code child.canUse(source)}. + * Mimics vanilla's CommandManager.makeTreeForSource. + * + * @param origin the source command node + * @param target the target command node + * @param source the command source + * @param originalToCopy a mutable map from original command nodes to their copies, used for redirects; + * should contain a mapping from origin to target + */ + private static void copyChildren( + CommandNode origin, + CommandNode target, + ClientCommandSourceExtension source, + Map, CommandNode> originalToCopy + ) { + for (CommandNode child : origin.getChildren()) { + if (!child.canUse(source)) continue; + + ArgumentBuilder builder = child.createBuilder(); + + // Reset the unnecessary non-completion stuff from the builder + builder.requires(s -> true); // This is checked with the if check above. + + if (builder.getCommand() != null) { + builder.executes(context -> 0); + } + + // Set up redirects + if (builder.getRedirect() != null) { + builder.redirect(originalToCopy.get(builder.getRedirect())); + } + + CommandNode result = builder.build(); + originalToCopy.put(child, result); + target.addChild(result); + + if (!child.getChildren().isEmpty()) { + copyChildren(child, result, source, originalToCopy); + } + } + } +} diff --git a/command/common/src/main/java/band/kessoku/lib/mixin/command/ClientCommandSourceMixin.java b/command/common/src/main/java/band/kessoku/lib/mixin/command/ClientCommandSourceMixin.java new file mode 100644 index 00000000..a5024cf1 --- /dev/null +++ b/command/common/src/main/java/band/kessoku/lib/mixin/command/ClientCommandSourceMixin.java @@ -0,0 +1,45 @@ +package band.kessoku.lib.mixin.command; + +import band.kessoku.lib.api.command.util.ClientCommandSourceExtension; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientCommandSource; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(ClientCommandSource.class) +abstract class ClientCommandSourceMixin implements ClientCommandSourceExtension { + @Shadow + @Final + private MinecraftClient client; + + @Override + public void kessokulib$sendFeedback(Text message) { + this.client.inGameHud.getChatHud().addMessage(message); + this.client.getNarratorManager().narrate(message); + } + + @Override + public void kessokulib$sendError(Text message) { + kessokulib$sendFeedback(Text.empty().append(message).formatted(Formatting.RED)); + } + + @Override + public MinecraftClient kessokulib$getClient() { + return client; + } + + @Override + public ClientPlayerEntity kessokulib$getPlayer() { + return client.player; + } + + @Override + public ClientWorld kessokulib$getWorld() { + return client.world; + } +} diff --git a/command/common/src/main/java/band/kessoku/lib/mixin/command/HelpCommandAccessor.java b/command/common/src/main/java/band/kessoku/lib/mixin/command/HelpCommandAccessor.java new file mode 100644 index 00000000..d4547ffb --- /dev/null +++ b/command/common/src/main/java/band/kessoku/lib/mixin/command/HelpCommandAccessor.java @@ -0,0 +1,14 @@ +package band.kessoku.lib.mixin.command; + +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import net.minecraft.server.command.HelpCommand; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(HelpCommand.class) +public interface HelpCommandAccessor { + @Accessor("FAILED_EXCEPTION") + static SimpleCommandExceptionType getFailedException() { + throw new AssertionError("mixin"); + } +} diff --git a/command/common/src/test/java/kessoku/testmod/command/KessokuTestCommand.java b/command/common/src/test/java/kessoku/testmod/command/KessokuTestCommand.java index 62189cce..4908435f 100644 --- a/command/common/src/test/java/kessoku/testmod/command/KessokuTestCommand.java +++ b/command/common/src/test/java/kessoku/testmod/command/KessokuTestCommand.java @@ -16,7 +16,7 @@ package kessoku.testmod.command; import band.kessoku.lib.api.entrypoint.entrypoints.KessokuModInitializer; -import band.kessoku.lib.api.event.command.CommandRegistryEvent; +import band.kessoku.lib.api.command.event.CommandRegistryEvent; import net.minecraft.server.command.CommandManager; import net.minecraft.text.Text; diff --git a/command/fabric/src/main/java/band/kessoku/lib/impl/command/fabric/KessokuCommandImpl.java b/command/fabric/src/main/java/band/kessoku/lib/impl/command/fabric/KessokuCommandImpl.java index e4509235..ac0c77c4 100644 --- a/command/fabric/src/main/java/band/kessoku/lib/impl/command/fabric/KessokuCommandImpl.java +++ b/command/fabric/src/main/java/band/kessoku/lib/impl/command/fabric/KessokuCommandImpl.java @@ -15,11 +15,18 @@ */ package band.kessoku.lib.impl.command.fabric; -import band.kessoku.lib.api.event.command.CommandRegistryEvent; +import band.kessoku.lib.api.command.event.CommandRegistryEvent; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; public final class KessokuCommandImpl { + public static void registerClientEvents() { + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + + }); + } + public static void registerCommonEvents() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { CommandRegistryEvent.EVENT.invoker().register(dispatcher, registryAccess, environment); diff --git a/command/neo/src/main/java/band/kessoku/lib/impl/command/neoforge/KessokuCommandImpl.java b/command/neo/src/main/java/band/kessoku/lib/impl/command/neoforge/KessokuCommandImpl.java index a6373e58..7d36aa47 100644 --- a/command/neo/src/main/java/band/kessoku/lib/impl/command/neoforge/KessokuCommandImpl.java +++ b/command/neo/src/main/java/band/kessoku/lib/impl/command/neoforge/KessokuCommandImpl.java @@ -16,7 +16,7 @@ package band.kessoku.lib.impl.command.neoforge; import band.kessoku.lib.api.base.neoforge.NeoEventUtils; -import band.kessoku.lib.api.event.command.CommandRegistryEvent; +import band.kessoku.lib.api.command.event.CommandRegistryEvent; import net.neoforged.bus.api.IEventBus; import net.neoforged.neoforge.event.RegisterCommandsEvent; diff --git a/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/KessokuEntrypoint.java b/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/KessokuEntrypoint.java index a1413052..d3337157 100644 --- a/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/KessokuEntrypoint.java +++ b/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/KessokuEntrypoint.java @@ -20,8 +20,8 @@ import java.util.*; import band.kessoku.lib.api.KessokuLib; -import band.kessoku.lib.api.platform.ModData; -import band.kessoku.lib.api.platform.ModLoader; +import band.kessoku.lib.api.platform.Metadata; +import band.kessoku.lib.api.platform.Loader; import band.kessoku.lib.impl.entrypoint.JavaLanguageAdapter; import band.kessoku.lib.impl.entrypoint.exceptions.LanguageAdapterException; import club.someoneice.json.JSON; @@ -45,7 +45,7 @@ private KessokuEntrypoint() { static { KessokuLib.getLogger().info(MARKER, "Start loading Kessoku Entrypoint API."); Map modInfoMap = new HashMap<>(); - for (ModData modData : ModLoader.getMods()) { + for (Metadata modData : Loader.getMods()) { final String modid = modData.getModId(); final Path kessokuJsonPath = modData.findPath("kessoku.json").orElse(null); // Not found @@ -71,7 +71,7 @@ private KessokuEntrypoint() { List entries = new ArrayList<>(); entrypointMetadataList.forEach(entrypointMetadata -> { try { - Object instance = getAdapter(entrypointMetadata.getAdapter()).parse(ModLoader.getModData(modid), entrypointMetadata.getValue()); + Object instance = getAdapter(entrypointMetadata.getAdapter()).parse(Loader.getModMetadata(modid), entrypointMetadata.getValue()); entries.add(new Entry(modid, instance)); } catch (LanguageAdapterException e) { throw new RuntimeException(e); @@ -98,11 +98,11 @@ public static Optional getKessokuMetadata(String modid) { @SuppressWarnings("unchecked") private static final class Entry { - public final ModData modData; + public final Metadata metadata; private final Object instance; public Entry(String modid, Object instance) { - this.modData = ModLoader.getModData(modid); + this.metadata = Loader.getModMetadata(modid); this.instance = instance; } diff --git a/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/LanguageAdapter.java b/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/LanguageAdapter.java index de547d90..cbd84803 100644 --- a/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/LanguageAdapter.java +++ b/entrypoint/common/src/main/java/band/kessoku/lib/api/entrypoint/LanguageAdapter.java @@ -15,7 +15,7 @@ */ package band.kessoku.lib.api.entrypoint; -import band.kessoku.lib.api.platform.ModData; +import band.kessoku.lib.api.platform.Metadata; import band.kessoku.lib.impl.entrypoint.JavaLanguageAdapter; import band.kessoku.lib.impl.entrypoint.exceptions.LanguageAdapterException; @@ -30,10 +30,10 @@ static LanguageAdapter getDefault() { /** * Creates an object of {@code type} from an arbitrary string declaration. * - * @param mod the mod which the object is from + * @param metadata the mod which the object is from * @param value the string declaration of the object * @return the created object * @throws LanguageAdapterException if a problem arises during creation, such as an invalid declaration */ - Object parse(ModData mod, String value) throws LanguageAdapterException; + Object parse(Metadata metadata, String value) throws LanguageAdapterException; } diff --git a/entrypoint/common/src/main/java/band/kessoku/lib/impl/entrypoint/JavaLanguageAdapter.java b/entrypoint/common/src/main/java/band/kessoku/lib/impl/entrypoint/JavaLanguageAdapter.java index d603650c..f28571c4 100644 --- a/entrypoint/common/src/main/java/band/kessoku/lib/impl/entrypoint/JavaLanguageAdapter.java +++ b/entrypoint/common/src/main/java/band/kessoku/lib/impl/entrypoint/JavaLanguageAdapter.java @@ -21,7 +21,7 @@ import java.lang.reflect.Modifier; import band.kessoku.lib.api.entrypoint.LanguageAdapter; -import band.kessoku.lib.api.platform.ModData; +import band.kessoku.lib.api.platform.Metadata; import band.kessoku.lib.impl.entrypoint.exceptions.LanguageAdapterException; public final class JavaLanguageAdapter implements LanguageAdapter { @@ -31,7 +31,7 @@ private JavaLanguageAdapter() { } @Override - public Object parse(final ModData mod, final String value) throws LanguageAdapterException { + public Object parse(final Metadata mod, final String value) throws LanguageAdapterException { final String[] methodSplit = value.split("::"); if (methodSplit.length >= 3) { throw new LanguageAdapterException("Invalid handle format: " + value); @@ -57,7 +57,7 @@ public Object parse(final ModData mod, final String value) throws LanguageAdapte Getter getter = null; // Try to find method try { - final Method method = c.getMethod(methodSplit[1], ModData.class); + final Method method = c.getMethod(methodSplit[1], Metadata.class); if (!Modifier.isStatic(method.getModifiers())) throw new LanguageAdapterException("Method " + value + " must be static!"); getter = () -> { method.setAccessible(true); diff --git a/platform/common/src/main/java/band/kessoku/lib/api/platform/ModDependencyInfo.java b/platform/common/src/main/java/band/kessoku/lib/api/platform/DependencyInfo.java similarity index 94% rename from platform/common/src/main/java/band/kessoku/lib/api/platform/ModDependencyInfo.java rename to platform/common/src/main/java/band/kessoku/lib/api/platform/DependencyInfo.java index e98d16e2..afb1c9d4 100644 --- a/platform/common/src/main/java/band/kessoku/lib/api/platform/ModDependencyInfo.java +++ b/platform/common/src/main/java/band/kessoku/lib/api/platform/DependencyInfo.java @@ -15,10 +15,10 @@ */ package band.kessoku.lib.api.platform; -import band.kessoku.lib.service.platform.ModLoaderService; +import band.kessoku.lib.service.platform.LoaderService; import org.jetbrains.annotations.Nullable; -public interface ModDependencyInfo { +public interface DependencyInfo { /** * Get the kind of dependency. */ @@ -90,7 +90,7 @@ public boolean isSoft() { */ @Nullable public String getKey() { - return ModLoaderService.getInstance().isFabric() ? fabricKey : neoKey; + return LoaderService.getInstance().isFabric() ? fabricKey : neoKey; } } } diff --git a/platform/common/src/main/java/band/kessoku/lib/api/platform/ModLoader.java b/platform/common/src/main/java/band/kessoku/lib/api/platform/Loader.java similarity index 56% rename from platform/common/src/main/java/band/kessoku/lib/api/platform/ModLoader.java rename to platform/common/src/main/java/band/kessoku/lib/api/platform/Loader.java index 7dec0e5c..8a43edd4 100644 --- a/platform/common/src/main/java/band/kessoku/lib/api/platform/ModLoader.java +++ b/platform/common/src/main/java/band/kessoku/lib/api/platform/Loader.java @@ -18,53 +18,53 @@ import java.nio.file.Path; import java.util.Collection; -import band.kessoku.lib.service.platform.ModLoaderService; +import band.kessoku.lib.service.platform.LoaderService; -public final class ModLoader { - private ModLoader() { +public final class Loader { + private Loader() { } - public static ModData getModData(String modid) { - return ModLoaderService.getInstance().getModData(modid); + public static Metadata getModMetadata(String modid) { + return LoaderService.getInstance().getModMetadata(modid); } public static boolean isFabric() { - return ModLoaderService.getInstance().isFabric(); + return LoaderService.getInstance().isFabric(); } public static boolean isNeoForge() { - return ModLoaderService.getInstance().isNeoForge(); + return LoaderService.getInstance().isNeoForge(); } public static Env getEnv() { - return ModLoaderService.getInstance().getEnv(); + return LoaderService.getInstance().getEnv(); } public static Path getGameFolder() { - return ModLoaderService.getInstance().getGameFolder(); + return LoaderService.getInstance().getGameFolder(); } public static Path getConfigFolder() { - return ModLoaderService.getInstance().getConfigFolder(); + return LoaderService.getInstance().getConfigFolder(); } public static Path getModsFolder() { - return ModLoaderService.getInstance().getModsFolder(); + return LoaderService.getInstance().getModsFolder(); } public static boolean isModLoaded(String id) { - return ModLoaderService.getInstance().isModLoaded(id); + return LoaderService.getInstance().isModLoaded(id); } public static Collection getModIds() { - return ModLoaderService.getInstance().getModIds(); + return LoaderService.getInstance().getModIds(); } - public static Collection getMods() { - return ModLoaderService.getInstance().getMods(); + public static Collection getMods() { + return LoaderService.getInstance().getMods(); } public static boolean isDevEnv() { - return ModLoaderService.getInstance().isDevEnv(); + return LoaderService.getInstance().isDevEnv(); } } diff --git a/platform/common/src/main/java/band/kessoku/lib/api/platform/ModData.java b/platform/common/src/main/java/band/kessoku/lib/api/platform/Metadata.java similarity index 96% rename from platform/common/src/main/java/band/kessoku/lib/api/platform/ModData.java rename to platform/common/src/main/java/band/kessoku/lib/api/platform/Metadata.java index 321f2218..26343617 100644 --- a/platform/common/src/main/java/band/kessoku/lib/api/platform/ModData.java +++ b/platform/common/src/main/java/band/kessoku/lib/api/platform/Metadata.java @@ -20,7 +20,7 @@ import java.util.List; import java.util.Optional; -public interface ModData { +public interface Metadata { /** * Returns the mod's ID. */ @@ -34,7 +34,7 @@ public interface ModData { /** * Returns all of the mod's dependencies. */ - Collection getDependencies(); + Collection getDependencies(); /** * Get the name of the mod. diff --git a/platform/common/src/main/java/band/kessoku/lib/service/platform/ModLoaderService.java b/platform/common/src/main/java/band/kessoku/lib/service/platform/LoaderService.java similarity index 79% rename from platform/common/src/main/java/band/kessoku/lib/service/platform/ModLoaderService.java rename to platform/common/src/main/java/band/kessoku/lib/service/platform/LoaderService.java index 6b71ec32..390c1a44 100644 --- a/platform/common/src/main/java/band/kessoku/lib/service/platform/ModLoaderService.java +++ b/platform/common/src/main/java/band/kessoku/lib/service/platform/LoaderService.java @@ -20,14 +20,14 @@ import band.kessoku.lib.api.KessokuLib; import band.kessoku.lib.api.platform.Env; -import band.kessoku.lib.api.platform.ModData; +import band.kessoku.lib.api.platform.Metadata; -public interface ModLoaderService { - static ModLoaderService getInstance() { - return KessokuLib.loadService(ModLoaderService.class); +public interface LoaderService { + static LoaderService getInstance() { + return KessokuLib.loadService(LoaderService.class); } - ModData getModData(String modid); + Metadata getModMetadata(String modid); boolean isFabric(); @@ -45,7 +45,7 @@ static ModLoaderService getInstance() { Collection getModIds(); - Collection getMods(); + Collection getMods(); boolean isDevEnv(); } diff --git a/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModDependencyInfoImpl.java b/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/DependencyInfoImpl.java similarity index 87% rename from platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModDependencyInfoImpl.java rename to platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/DependencyInfoImpl.java index 8590b719..763963c1 100644 --- a/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModDependencyInfoImpl.java +++ b/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/DependencyInfoImpl.java @@ -15,14 +15,14 @@ */ package band.kessoku.lib.impl.platform.fabric; -import band.kessoku.lib.api.platform.ModDependencyInfo; +import band.kessoku.lib.api.platform.DependencyInfo; import net.fabricmc.loader.api.metadata.ModDependency; -public final class ModDependencyInfoImpl implements ModDependencyInfo { +public final class DependencyInfoImpl implements DependencyInfo { private final ModDependency value; - public ModDependencyInfoImpl(ModDependency dependency) { + public DependencyInfoImpl(ModDependency dependency) { this.value = dependency; } diff --git a/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModLoaderImpl.java b/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/LoaderImpl.java similarity index 81% rename from platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModLoaderImpl.java rename to platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/LoaderImpl.java index 323f774d..50393b5e 100644 --- a/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModLoaderImpl.java +++ b/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/LoaderImpl.java @@ -21,8 +21,8 @@ import java.util.concurrent.ConcurrentHashMap; import band.kessoku.lib.api.platform.Env; -import band.kessoku.lib.api.platform.ModData; -import band.kessoku.lib.service.platform.ModLoaderService; +import band.kessoku.lib.api.platform.Metadata; +import band.kessoku.lib.service.platform.LoaderService; import com.google.auto.service.AutoService; import net.fabricmc.api.EnvType; @@ -30,13 +30,13 @@ import net.fabricmc.loader.api.ModContainer; import net.fabricmc.loader.api.metadata.ModMetadata; -@AutoService(ModLoaderService.class) -public final class ModLoaderImpl implements ModLoaderService { - private final Map modDataMap = new ConcurrentHashMap<>(); +@AutoService(LoaderService.class) +public final class LoaderImpl implements LoaderService { + private final Map modMetadataMap = new ConcurrentHashMap<>(); @Override - public ModData getModData(String modid) { - return modDataMap.computeIfAbsent(modid, ModDataImpl::new); + public Metadata getModMetadata(String modid) { + return modMetadataMap.computeIfAbsent(modid, MetadataImpl::new); } @Override @@ -80,8 +80,8 @@ public Collection getModIds() { } @Override - public Collection getMods() { - return FabricLoader.getInstance().getAllMods().parallelStream().map(ModDataImpl::new).toList(); + public Collection getMods() { + return FabricLoader.getInstance().getAllMods().parallelStream().map(MetadataImpl::new).toList(); } @Override diff --git a/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModDataImpl.java b/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/MetadataImpl.java similarity index 87% rename from platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModDataImpl.java rename to platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/MetadataImpl.java index 0d855670..105df2c1 100644 --- a/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/ModDataImpl.java +++ b/platform/fabric/src/main/java/band/kessoku/lib/impl/platform/fabric/MetadataImpl.java @@ -21,24 +21,24 @@ import java.util.Optional; import java.util.stream.Collectors; -import band.kessoku.lib.api.platform.ModData; -import band.kessoku.lib.api.platform.ModDependencyInfo; +import band.kessoku.lib.api.platform.Metadata; +import band.kessoku.lib.api.platform.DependencyInfo; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.fabricmc.loader.api.metadata.ModMetadata; import net.fabricmc.loader.api.metadata.Person; -public final class ModDataImpl implements ModData { +public final class MetadataImpl implements Metadata { private final ModContainer modContainer; private final ModMetadata modMetadata; - public ModDataImpl(String modid) { + public MetadataImpl(String modid) { this.modContainer = FabricLoader.getInstance().getModContainer(modid).orElseThrow(); this.modMetadata = modContainer.getMetadata(); } - public ModDataImpl(ModContainer container) { + public MetadataImpl(ModContainer container) { this.modContainer = container; this.modMetadata = modContainer.getMetadata(); } @@ -54,8 +54,8 @@ public String getVersion() { } @Override - public Collection getDependencies() { - return modMetadata.getDependencies().stream().map(ModDependencyInfoImpl::new).toList(); + public Collection getDependencies() { + return modMetadata.getDependencies().stream().map(DependencyInfoImpl::new).toList(); } @Override diff --git a/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModDependencyInfoImpl.java b/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/DependencyInfoImpl.java similarity index 87% rename from platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModDependencyInfoImpl.java rename to platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/DependencyInfoImpl.java index c7913199..5b112b65 100644 --- a/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModDependencyInfoImpl.java +++ b/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/DependencyInfoImpl.java @@ -15,14 +15,14 @@ */ package band.kessoku.lib.impl.platform.neoforge; -import band.kessoku.lib.api.platform.ModDependencyInfo; +import band.kessoku.lib.api.platform.DependencyInfo; import net.neoforged.neoforgespi.language.IModInfo; -public final class ModDependencyInfoImpl implements ModDependencyInfo { +public final class DependencyInfoImpl implements DependencyInfo { private final IModInfo.ModVersion value; - public ModDependencyInfoImpl(IModInfo.ModVersion modVersion) { + public DependencyInfoImpl(IModInfo.ModVersion modVersion) { this.value = modVersion; } diff --git a/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModLoaderImpl.java b/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/LoaderImpl.java similarity index 78% rename from platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModLoaderImpl.java rename to platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/LoaderImpl.java index cad8b2df..3ec8be44 100644 --- a/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModLoaderImpl.java +++ b/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/LoaderImpl.java @@ -21,8 +21,8 @@ import java.util.concurrent.ConcurrentHashMap; import band.kessoku.lib.api.platform.Env; -import band.kessoku.lib.api.platform.ModData; -import band.kessoku.lib.service.platform.ModLoaderService; +import band.kessoku.lib.api.platform.Metadata; +import band.kessoku.lib.service.platform.LoaderService; import com.google.auto.service.AutoService; import net.neoforged.fml.ModList; @@ -30,13 +30,13 @@ import net.neoforged.fml.loading.FMLPaths; import net.neoforged.neoforgespi.language.IModInfo; -@AutoService(ModLoaderService.class) -public final class ModLoaderImpl implements ModLoaderService { - private final Map modDataMap = new ConcurrentHashMap<>(); +@AutoService(LoaderService.class) +public final class LoaderImpl implements LoaderService { + private final Map modMatadataMap = new ConcurrentHashMap<>(); @Override - public ModData getModData(String modid) { - return modDataMap.computeIfAbsent(modid, ModDataImpl::new); + public Metadata getModMetadata(String modid) { + return modMatadataMap.computeIfAbsent(modid, MetadataImpl::new); } @Override @@ -80,8 +80,8 @@ public Collection getModIds() { } @Override - public Collection getMods() { - return ModList.get().getMods().parallelStream().map(ModDataImpl::new).toList(); + public Collection getMods() { + return ModList.get().getMods().parallelStream().map(MetadataImpl::new).toList(); } @Override diff --git a/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModDataImpl.java b/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/MetadataImpl.java similarity index 89% rename from platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModDataImpl.java rename to platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/MetadataImpl.java index 3b8038c8..4032ad26 100644 --- a/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/ModDataImpl.java +++ b/platform/neo/src/main/java/band/kessoku/lib/impl/platform/neoforge/MetadataImpl.java @@ -22,8 +22,8 @@ import java.util.List; import java.util.Optional; -import band.kessoku.lib.api.platform.ModData; -import band.kessoku.lib.api.platform.ModDependencyInfo; +import band.kessoku.lib.api.platform.Metadata; +import band.kessoku.lib.api.platform.DependencyInfo; import net.neoforged.fml.ModContainer; import net.neoforged.fml.ModList; @@ -31,16 +31,16 @@ import net.neoforged.neoforgespi.language.IModFileInfo; import net.neoforged.neoforgespi.language.IModInfo; -public final class ModDataImpl implements ModData { +public final class MetadataImpl implements Metadata { private final ModContainer modContainer; private final IModInfo modInfo; - public ModDataImpl(String modid) { + public MetadataImpl(String modid) { this.modContainer = ModList.get().getModContainerById(modid).orElseThrow(); this.modInfo = modContainer.getModInfo(); } - public ModDataImpl(IModInfo info) { + public MetadataImpl(IModInfo info) { this.modContainer = ModList.get().getModContainerById(info.getModId()).orElseThrow(); this.modInfo = info; } @@ -56,8 +56,8 @@ public String getVersion() { } @Override - public Collection getDependencies() { - return modInfo.getDependencies().stream().map(ModDependencyInfoImpl::new).toList(); + public Collection getDependencies() { + return modInfo.getDependencies().stream().map(DependencyInfoImpl::new).toList(); } @Override