Skip to content

Commit

Permalink
Defer sponge command registration (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog authored Feb 19, 2025
1 parent 77f3414 commit 61d9627
Show file tree
Hide file tree
Showing 30 changed files with 260 additions and 175 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.api.registry;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;

/**
* An event for registering commands to the {@link CommandRegistry}.
*/
@FunctionalInterface
public interface CommandRegistrationCallback {
Event<CommandRegistrationCallback> EVENT = EventFactory.createArrayBacked(CommandRegistrationCallback.class, listeners -> registry -> {
for (CommandRegistrationCallback registrar : listeners) {
registrar.register(registry);
}
});

/**
* Register your commands here.
*
* @param registry The command registry
*/
void register(CommandRegistry registry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import net.fabricmc.api.ModInitializer;

import net.legacyfabric.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.legacyfabric.fabric.api.registry.CommandRegistrationCallback;
import net.legacyfabric.fabric.api.registry.CommandRegistry;

public class CommandInitializer implements ModInitializer {
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
CommandRegistrationCallback.EVENT.invoker().register(CommandRegistry.INSTANCE);

boolean dedicated = server.isDedicated();
CommandManager manager = (CommandManager) server.getCommandManager();
CommandRegistryImpl.getCommandMap().forEach((command, commandSide) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

import net.legacyfabric.fabric.api.command.v2.CommandRegistrar;
import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandManager;
import net.legacyfabric.fabric.api.registry.CommandRegistry;
import net.legacyfabric.fabric.api.registry.CommandRegistrationCallback;

public class ImplInit implements DedicatedServerModInitializer, ClientModInitializer, CommandRegistrar {
@Override
public void register(CommandManager manager, boolean dedicated) {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
CommandRegistry.INSTANCE.register(wrapper);
CommandRegistrationCallback.EVENT.register(registry -> {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
registry.register(wrapper);
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

import net.legacyfabric.fabric.api.command.v2.CommandRegistrar;
import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandManager;
import net.legacyfabric.fabric.api.registry.CommandRegistry;
import net.legacyfabric.fabric.api.registry.CommandRegistrationCallback;

public class ImplInit implements DedicatedServerModInitializer, ClientModInitializer, CommandRegistrar {
@Override
public void register(CommandManager manager, boolean dedicated) {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
CommandRegistry.INSTANCE.register(wrapper);
CommandRegistrationCallback.EVENT.register(registry -> {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
registry.register(wrapper);
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

import net.legacyfabric.fabric.api.command.v2.CommandRegistrar;
import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandManager;
import net.legacyfabric.fabric.api.registry.CommandRegistry;
import net.legacyfabric.fabric.api.registry.CommandRegistrationCallback;

public class ImplInit implements DedicatedServerModInitializer, ClientModInitializer, CommandRegistrar {
@Override
public void register(CommandManager manager, boolean dedicated) {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
CommandRegistry.INSTANCE.register(wrapper);
CommandRegistrationCallback.EVENT.register(registry -> {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
registry.register(wrapper);
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

import net.legacyfabric.fabric.api.command.v2.CommandRegistrar;
import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandManager;
import net.legacyfabric.fabric.api.registry.CommandRegistry;
import net.legacyfabric.fabric.api.registry.CommandRegistrationCallback;

public class ImplInit implements DedicatedServerModInitializer, ClientModInitializer, CommandRegistrar {
@Override
public void register(CommandManager manager, boolean dedicated) {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
CommandRegistry.INSTANCE.register(wrapper);
CommandRegistrationCallback.EVENT.register(registry -> {
CommandRegistrar.EVENT.invoker().register(manager, dedicated);
InternalObjects.getCommandManager().getCommands().forEach(mapping -> {
CommandWrapper wrapper = new CommandWrapper(mapping);
registry.register(wrapper);
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import net.fabricmc.api.ModInitializer;

import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.api.registry.CommandRegistry;
import net.legacyfabric.fabric.api.registry.CommandRegistrationCallback;

public class CommandV1Test implements ModInitializer {
public static final Logger LOGGER = Logger.get("LegacyFabricAPI", "Test", "CommandV1Test");

@Override
public void onInitialize() {
CommandRegistry.INSTANCE.register(new ModMetadataCommandV1());
CommandRegistrationCallback.EVENT.register(registry -> {
registry.register(new ModMetadataCommandV1());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@

package net.legacyfabric.fabric.test.command;

import java.util.Objects;

import net.minecraft.text.ClickEvent;
import net.minecraft.text.LiteralText;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ContactInformation;

Expand All @@ -45,10 +42,8 @@ public static void register(CommandManager manager) {
);
}

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(new LiteralText("mod not found")));
ModContainer container = ctx.<ModContainer>getOne("modid").orElseThrow(() -> new CommandException(new LiteralText("Couldn't find Mod container")));
LiteralText builder = new LiteralText("");
builder.append("Mod Name: ".concat(container.getMetadata().getName()).concat("\n"));
builder.append("Description: ".concat(container.getMetadata().getDescription()).concat("\n"));
Expand All @@ -58,7 +53,7 @@ private static CommandResult execute(PermissibleCommandSource source, CommandCon
LiteralText issueText = new LiteralText("");
issueText.append("Issues: ");
LiteralText issueUrl = new LiteralText(contact.get("issues").get());
if (useStyle) issueUrl.setStyle(issueText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, issueUrl.computeValue())));
issueUrl.setStyle(issueText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, issueUrl.computeValue())));
issueText.append(issueUrl);
issueText.append("\n");
builder.append(issueText);
Expand All @@ -68,7 +63,7 @@ private static CommandResult execute(PermissibleCommandSource source, CommandCon
LiteralText sourcesText = new LiteralText("");
sourcesText.append("Sources: ");
LiteralText sourcesUrl = new LiteralText(contact.get("sources").get());
if (useStyle) sourcesUrl.setStyle(sourcesText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, sourcesUrl.computeValue())));
sourcesUrl.setStyle(sourcesText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, sourcesUrl.computeValue())));
sourcesText.append(sourcesUrl);
sourcesText.append("\n");
builder.append(sourcesText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import java.util.Optional;

import net.minecraft.command.AbstractCommand;
import net.minecraft.command.CommandException;
import net.minecraft.command.CommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
import net.minecraft.util.Formatting;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
Expand All @@ -40,40 +43,44 @@ public String getUsageTranslationKey(CommandSource source) {
}

@Override
public void method_3279(MinecraftServer minecraftServer, CommandSource commandSource, String[] args) throws CommandException {
public void method_3279(MinecraftServer minecraftServer, CommandSource commandSource, String[] args) {
if (args.length > 0) {
Optional<ModContainer> optionalModContainer = FabricLoader.getInstance().getModContainer(args[0]);

if (optionalModContainer.isPresent()) {
ModContainer container = optionalModContainer.get();

StringBuilder builder = new StringBuilder();
LiteralText builder = new LiteralText("");
builder.append("Mod Name: ".concat(container.getMetadata().getName()).concat("\n"));
builder.append("Description: ".concat(container.getMetadata().getDescription()).concat("\n"));
ContactInformation contact = container.getMetadata().getContact();

if (contact.get("issues").isPresent()) {
StringBuilder issueText = new StringBuilder("");
LiteralText issueText = new LiteralText("");
issueText.append("Issues: ");
StringBuilder issueUrl = new StringBuilder(contact.get("issues").get());
LiteralText issueUrl = new LiteralText(contact.get("issues").get());
issueUrl.setStyle(issueText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, issueUrl.computeValue())));
issueText.append(issueUrl);
issueText.append("\n");
builder.append(issueText);
}

if (contact.get("sources").isPresent()) {
StringBuilder sourcesText = new StringBuilder("");
LiteralText sourcesText = new LiteralText("");
sourcesText.append("Sources: ");
StringBuilder sourcesUrl = new StringBuilder(contact.get("sources").get());
LiteralText sourcesUrl = new LiteralText(contact.get("sources").get());
sourcesUrl.setStyle(sourcesText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, sourcesUrl.computeValue())));
sourcesText.append(sourcesUrl);
sourcesText.append("\n");
builder.append(sourcesText);
}

builder.append("Metadata Type: ".concat(container.getMetadata().getType()).concat("\n"));
CommandV1Test.LOGGER.info(builder.toString());
commandSource.sendMessage(builder);
} else {
CommandV1Test.LOGGER.error("Couldn't find Mod container for mod id '" + args[0] + "'");
LiteralText builder = new LiteralText("Couldn't find Mod container for mod id '" + args[0] + "'");
builder.setStyle(new Style().setFormatting(Formatting.RED));
commandSource.sendMessage(builder);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import net.fabricmc.api.ModInitializer;

import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.api.registry.CommandRegistry;
import net.legacyfabric.fabric.api.registry.CommandRegistrationCallback;

public class CommandV1Test implements ModInitializer {
public static final Logger LOGGER = Logger.get("LegacyFabricAPI", "Test", "CommandV1Test");

@Override
public void onInitialize() {
CommandRegistry.INSTANCE.register(new ModMetadataCommandV1());
CommandRegistrationCallback.EVENT.register(registry -> {
registry.register(new ModMetadataCommandV1());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@

package net.legacyfabric.fabric.test.command;

import java.util.Objects;

import net.minecraft.text.ClickEvent;
import net.minecraft.text.LiteralText;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ContactInformation;

Expand All @@ -45,10 +42,8 @@ public static void register(CommandManager manager) {
);
}

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(new LiteralText("mod not found")));
ModContainer container = ctx.<ModContainer>getOne("modid").orElseThrow(() -> new CommandException(new LiteralText("Couldn't find Mod container")));
LiteralText builder = new LiteralText("");
builder.append("Mod Name: ".concat(container.getMetadata().getName()).concat("\n"));
builder.append("Description: ".concat(container.getMetadata().getDescription()).concat("\n"));
Expand All @@ -58,7 +53,7 @@ private static CommandResult execute(PermissibleCommandSource source, CommandCon
LiteralText issueText = new LiteralText("");
issueText.append("Issues: ");
LiteralText issueUrl = new LiteralText(contact.get("issues").get());
if (useStyle) issueUrl.setStyle(issueText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, issueUrl.computeValue())));
issueUrl.setStyle(issueText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, issueUrl.computeValue())));
issueText.append(issueUrl);
issueText.append("\n");
builder.append(issueText);
Expand All @@ -68,7 +63,7 @@ private static CommandResult execute(PermissibleCommandSource source, CommandCon
LiteralText sourcesText = new LiteralText("");
sourcesText.append("Sources: ");
LiteralText sourcesUrl = new LiteralText(contact.get("sources").get());
if (useStyle) sourcesUrl.setStyle(sourcesText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, sourcesUrl.computeValue())));
sourcesUrl.setStyle(sourcesText.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, sourcesUrl.computeValue())));
sourcesText.append(sourcesUrl);
sourcesText.append("\n");
builder.append(sourcesText);
Expand Down
Loading

0 comments on commit 61d9627

Please sign in to comment.