Skip to content

Commit

Permalink
Add test mod
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Dec 7, 2024
1 parent 752f1af commit c97bbcd
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.<ModContainer>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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down

0 comments on commit c97bbcd

Please sign in to comment.