From c97bbcd751c1b15315a72578584d8f5dd3911685 Mon Sep 17 00:00:00 2001 From: Cat Core Date: Sat, 7 Dec 2024 13:47:29 +0100 Subject: [PATCH] Add test mod --- .../test/command/ModMetadataCommand.java | 78 +++++++++++++++++++ .../test/command/SpongeCommandTest.java | 31 ++++++++ .../src/testmod/resources/fabric.mod.json | 1 + 3 files changed, 110 insertions(+) create mode 100644 legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/ModMetadataCommand.java create mode 100644 legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/SpongeCommandTest.java diff --git a/legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/ModMetadataCommand.java b/legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/ModMetadataCommand.java new file mode 100644 index 000000000..b2945c5f1 --- /dev/null +++ b/legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/ModMetadataCommand.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.legacyfabric.fabric.test.command; + +import java.util.Objects; + +import net.minecraft.text.ChatMessage; + +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.ModContainer; +import net.fabricmc.loader.api.metadata.ContactInformation; + +import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandException; +import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandManager; +import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandResult; +import net.legacyfabric.fabric.api.command.v2.lib.sponge.args.CommandContext; +import net.legacyfabric.fabric.api.command.v2.lib.sponge.args.GenericArguments; +import net.legacyfabric.fabric.api.command.v2.lib.sponge.spec.CommandSpec; +import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource; + +public class ModMetadataCommand { + public static void register(CommandManager manager) { + manager.register( + CommandSpec.builder() + .arguments(GenericArguments.mod(ChatMessage.createTextMessage("modid"))) + .executor(ModMetadataCommand::execute) + .build(), + "modmetadata" + ); + } + + private static final boolean useStyle = !Objects.equals(FabricLoader.getInstance().getModContainer("minecraft").get().getMetadata().getVersion().getFriendlyString(), "1.8"); + + private static CommandResult execute(PermissibleCommandSource source, CommandContext ctx) throws CommandException { + ModContainer container = ctx.getOne("modid").orElseThrow(() -> new CommandException(ChatMessage.createTextMessage("mod not found"))); + ChatMessage builder = ChatMessage.createTextMessage(""); + builder.addText("Mod Name: ".concat(container.getMetadata().getName()).concat("\n")); + builder.addText("Description: ".concat(container.getMetadata().getDescription()).concat("\n")); + ContactInformation contact = container.getMetadata().getContact(); + + if (contact.get("issues").isPresent()) { + ChatMessage issueText = ChatMessage.createTextMessage(""); + issueText.addText("Issues: "); + ChatMessage issueUrl = ChatMessage.createTextMessage(contact.get("issues").get()); + issueText.addUsing(issueUrl); + issueText.addText("\n"); + builder.addUsing(issueText); + } + + if (contact.get("sources").isPresent()) { + ChatMessage sourcesText = ChatMessage.createTextMessage(""); + sourcesText.addText("Sources: "); + ChatMessage sourcesUrl = ChatMessage.createTextMessage(contact.get("sources").get()); + sourcesText.addUsing(sourcesUrl); + sourcesText.addText("\n"); + builder.addUsing(sourcesText); + } + + builder.addText("Metadata Type: ".concat(container.getMetadata().getType()).concat("\n")); + source.method_5505(builder); + return CommandResult.success(); + } +} diff --git a/legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/SpongeCommandTest.java b/legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/SpongeCommandTest.java new file mode 100644 index 000000000..af1a6c161 --- /dev/null +++ b/legacyfabric-api/1.6.4/src/testmod/java/net/legacyfabric/fabric/test/command/SpongeCommandTest.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.legacyfabric.fabric.test.command; + +import net.fabricmc.api.ModInitializer; + +import net.legacyfabric.fabric.api.command.v2.CommandRegistrar; + +public class SpongeCommandTest implements ModInitializer { + @Override + public void onInitialize() { + CommandRegistrar.EVENT.register((manager, dedicated) -> { + ModMetadataCommand.register(manager); + }); + } +} diff --git a/legacyfabric-api/1.6.4/src/testmod/resources/fabric.mod.json b/legacyfabric-api/1.6.4/src/testmod/resources/fabric.mod.json index a51bb913b..de4aaf26a 100644 --- a/legacyfabric-api/1.6.4/src/testmod/resources/fabric.mod.json +++ b/legacyfabric-api/1.6.4/src/testmod/resources/fabric.mod.json @@ -22,6 +22,7 @@ "net.legacyfabric.fabric.test.lifecycle.ServerLifecycleEventsTest", "net.legacyfabric.fabric.test.item.group.ItemGroupTest", "net.legacyfabric.fabric.test.entity.EntityEventsTest", + "net.legacyfabric.fabric.test.command.SpongeCommandTest", "net.legacyfabric.fabric.test.command.CommandV1Test" ] },