Skip to content

Commit

Permalink
Move command dispatcher building to a seperate function
Browse files Browse the repository at this point in the history
... so that the command dispatcher can be rebuilt allowing for brigadier to not suggest and run removed commands
  • Loading branch information
InsertSoda committed Apr 30, 2024
1 parent 4585710 commit 56512f5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 34 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/insertsoda/craterchat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.insertsoda.craterchat.api.v1.CommandContainer;
import com.insertsoda.craterchat.impl.CommandSourceImpl;
import com.insertsoda.craterchat.api.v1.CommandSource;
import com.insertsoda.craterchat.mixins.UITextInputAccessor;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.Suggestions;
Expand Down Expand Up @@ -77,8 +79,16 @@ public void clearChat() {
this.messages = new LinkedList<>();
}

public CommandDispatcher<CommandSource> getCommandDispatcher(){
return this.commandDispatcher;
public void buildCommandDispatcher(){
CraterChat.LOGGER.info("Building the command dispatcher...");
this.commandDispatcher = new CommandDispatcher<>();
for(Map.Entry<String, CommandContainer> entry : CraterChat.getRegisteredCommands().entrySet()){
CommandContainer commandContainer = entry.getValue();
LiteralArgumentBuilder<CommandSource> literalArgumentBuilder = LiteralArgumentBuilder.literal(entry.getKey());
commandContainer.getCommand().register(literalArgumentBuilder);
this.commandDispatcher.register(literalArgumentBuilder);
}
CraterChat.LOGGER.info("Built the command dispatcher");
}

public void render(Viewport uiViewport, OrthographicCamera uiCamera){
Expand Down
85 changes: 53 additions & 32 deletions src/main/java/com/insertsoda/craterchat/CraterChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ public class CraterChat implements ModInitializer {

private static final LinkedHashMap<String, CommandContainer> registeredCommands = new LinkedHashMap<>();

private static boolean isCraterChatInitialized = false;

public static LinkedHashMap<String, CommandContainer> getRegisteredCommands() {
return registeredCommands;
}

public static boolean isIsCraterChatInitialized() {
return isCraterChatInitialized;
}

@Override
public void onInitialize(ModContainer mod) {
for (EntrypointContainer<CraterChatPlugin> plugin : QuiltLoader.getEntrypointContainers("craterchatplugin", CraterChatPlugin.class) ) {
for (Class<? extends Command> commandClass : plugin.getEntrypoint().register()) {
try {
Command command = commandClass.getDeclaredConstructor().newInstance();
CommandContainerImpl commandContainer = new CommandContainerImpl(command, plugin.getProvider());
if(!registeredCommands.containsKey(commandContainer.getMetadata().getName())) {
LiteralArgumentBuilder<CommandSource> literalArgumentBuilder = LiteralArgumentBuilder.literal(commandContainer.getMetadata().getName());
command.register(literalArgumentBuilder);
Chat.getCommandDispatcher().register(literalArgumentBuilder);
if(!registeredCommands.containsKey(commandContainer.getMetadata().getName()) || Objects.equals(plugin.getProvider().metadata().id(), "craterchat")) {
registeredCommands.put(commandContainer.getMetadata().getName(), commandContainer);

for (String alias : commandContainer.getMetadata().getAliases()) {
if(!registeredCommands.containsKey(alias)){
LiteralArgumentBuilder<CommandSource> aliasLiteralArgumentBuilder = LiteralArgumentBuilder.literal(alias);
command.register(aliasLiteralArgumentBuilder);
Chat.getCommandDispatcher().register(aliasLiteralArgumentBuilder);
if(!registeredCommands.containsKey(alias) || Objects.equals(plugin.getProvider().metadata().id(), "craterchat")){
registeredCommands.put(alias, new CommandContainerImpl(command, plugin.getProvider(), alias));
} else {
LOGGER.warn(commandContainer.getMetadata().getSourceModContainer().metadata().name() + " attempted to register command alias /" + commandContainer.getMetadata().getName() + ", but it was already taken by another mod!");
Expand All @@ -65,11 +65,14 @@ public void onInitialize(ModContainer mod) {

}

CraterChat.Chat.buildCommandDispatcher();
isCraterChatInitialized = true;

LOGGER.info("CraterChat initialized");
}

private static void refreshCaches(){
if(registeredCommands.containsKey("help") && registeredCommands.containsKey("plugins")) {
if(isCraterChatInitialized) {
((HelpCommand) registeredCommands.get("help").getCommand()).refreshCommandsCache();
((PluginsCommand) registeredCommands.get("plugins").getCommand()).refreshPluginsCache();
}
Expand All @@ -90,7 +93,14 @@ public static boolean isCommandRegistered(String commandName){
*/
@ApiStatus.Experimental
public static void removeCommand(String commandName){
registeredCommands.remove(commandName);
removeCommands(List.of(commandName));
}

public static void removeCommands(List<String> commandNames){
for (String commandName : commandNames) {
registeredCommands.remove(commandName);
}
CraterChat.Chat.buildCommandDispatcher();
}

/**
Expand All @@ -101,32 +111,43 @@ public static void removeCommand(String commandName){
*/
@ApiStatus.Experimental
public static boolean registerCommand(Command command, ModContainer modContainer){
CommandContainer commandContainer = new CommandContainerImpl(command, modContainer);

if(!registeredCommands.containsKey(commandContainer.getMetadata().getName())) {
LiteralArgumentBuilder<CommandSource> literalArgumentBuilder = LiteralArgumentBuilder.literal(commandContainer.getMetadata().getName());
command.register(literalArgumentBuilder);
Chat.getCommandDispatcher().register(literalArgumentBuilder);
registeredCommands.put(commandContainer.getMetadata().getName(), commandContainer);

for (String alias : commandContainer.getMetadata().getAliases()) {
if(!registeredCommands.containsKey(alias)){
LiteralArgumentBuilder<CommandSource> aliasLiteralArgumentBuilder = LiteralArgumentBuilder.literal(alias);
command.register(aliasLiteralArgumentBuilder);
Chat.getCommandDispatcher().register(aliasLiteralArgumentBuilder);
registeredCommands.put(alias, new CommandContainerImpl(command, modContainer, alias));
} else {
LOGGER.warn(commandContainer.getMetadata().getSourceModContainer().metadata().name() + " attempted to register command alias /" + commandContainer.getMetadata().getName() + ", but it was already taken by another mod!");
return registerCommands(List.of(command), modContainer);
}

/**
* Allows you to register multiple commands while the game is running. It isn't recommended to use this as your main way of registering commands.
* @param commands The list of commands to be registered.
* @param modContainer The Quilt ModContainer from which the command originates from.
* @return true or false depending on whether all the commands under their respective command name were successfully registered (results from registering aliases are ignored).
*/
@ApiStatus.Experimental
public static boolean registerCommands(List<Command> commands, ModContainer modContainer){

boolean allSuccessful = true;

for(Command command : commands){
CommandContainer commandContainer = new CommandContainerImpl(command, modContainer);

if(!registeredCommands.containsKey(commandContainer.getMetadata().getName())) {
registeredCommands.put(commandContainer.getMetadata().getName(), commandContainer);

for (String alias : commandContainer.getMetadata().getAliases()) {
if(!registeredCommands.containsKey(alias)){
registeredCommands.put(alias, new CommandContainerImpl(command, modContainer, alias));
} else {
LOGGER.warn(commandContainer.getMetadata().getSourceModContainer().metadata().name() + " attempted to register command alias /" + commandContainer.getMetadata().getName() + ", but it was already taken by another mod!");
}
}
} else {
LOGGER.warn(commandContainer.getMetadata().getSourceModContainer().metadata().name() + " attempted to register command /" + commandContainer.getMetadata().getName() + ", but it was already taken by another mod!");
allSuccessful = false;
}
}

refreshCaches();
refreshCaches();
CraterChat.Chat.buildCommandDispatcher();

return true;
} else {
LOGGER.warn(commandContainer.getMetadata().getSourceModContainer().metadata().name() + " attempted to register command /" + commandContainer.getMetadata().getName() + ", but it was already taken by another mod!");
return false;
}
return allSuccessful;
}
}

0 comments on commit 56512f5

Please sign in to comment.