Skip to content

Commit

Permalink
Command registration improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Nov 2, 2024
1 parent 5fb7332 commit 482d7d4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.codersky.mcutils.spigot;

import net.codersky.mcutils.MCPlatform;
import net.codersky.mcutils.cmd.GlobalCommand;
import net.codersky.mcutils.cmd.MCCommand;
import net.codersky.mcutils.cmd.MCCommandSender;
import net.codersky.mcutils.crossplatform.player.MCPlayer;
import net.codersky.mcutils.crossplatform.player.PlayerProvider;
import net.codersky.mcutils.crossplatform.server.ServerUtils;
import net.codersky.mcutils.java.MCCollections;
import net.codersky.mcutils.java.strings.MCStrings;
import net.codersky.mcutils.spigot.cmd.AdaptedSpigotCommand;
import net.codersky.mcutils.spigot.cmd.SpigotCommand;
Expand All @@ -29,6 +31,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -201,6 +204,18 @@ protected SimpleCommandMap getCommandMap() {
return null;
}

@Override
@SuppressWarnings("unchecked")
public void registerCommands(@NotNull GlobalCommand<P>... commands) {
if (commands == null || commands.length == 0)
return;
registerCommands(MCCollections.map(
commands,
new AdaptedSpigotCommand[commands.length],
cmd -> new AdaptedSpigotCommand<>(this, cmd))
);
}

/**
* Registers the specified {@code commands}, allowing them to be executed.
* <p>
Expand All @@ -224,22 +239,17 @@ protected SimpleCommandMap getCommandMap() {
*
* @since MCUtils 1.0.0
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public void registerCommands(MCCommand<P, MCCommandSender>... commands) {
@SuppressWarnings({"unchecked"})
public void registerCommands(SpigotCommand<P>... commands) {
if (commands == null || commands.length == 0)
return;
final List<Command> remaining = new ArrayList<>();
for (MCCommand<P, MCCommandSender> command : commands) {
final SpigotCommand<P> spigotCommand;
if (command instanceof SpigotCommand)
spigotCommand = (SpigotCommand) command;
else
spigotCommand = new AdaptedSpigotCommand<>(this, command);
for (SpigotCommand<P> command : commands) {
final PluginCommand plCommand = getPlugin().getCommand(command.getName());
if (plCommand != null)
plCommand.setExecutor(spigotCommand);
plCommand.setExecutor(command);
else
remaining.add(spigotCommand);
remaining.add(command);
}
if (remaining.isEmpty())
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.codersky.mcutils.spigot.cmd;

import net.codersky.mcutils.cmd.GlobalCommand;
import net.codersky.mcutils.cmd.MCCommand;
import net.codersky.mcutils.cmd.MCCommandSender;
import net.codersky.mcutils.spigot.SpigotUtils;
Expand All @@ -10,9 +11,9 @@

public class AdaptedSpigotCommand<P extends JavaPlugin> extends SpigotCommand<P> {

private final MCCommand<?, MCCommandSender> command;
private final GlobalCommand<P> command;

public AdaptedSpigotCommand(@NotNull SpigotUtils<P> utils, @NotNull MCCommand<?, MCCommandSender> command) {
public AdaptedSpigotCommand(@NotNull SpigotUtils<P> utils, @NotNull GlobalCommand<P> command) {
super(utils, command.getName(), command.getAliases().toArray(new String[0]));
this.command = command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import net.codersky.mcutils.MCPlatform;
import net.codersky.mcutils.cmd.GlobalCommand;
import net.codersky.mcutils.cmd.MCCommand;
import net.codersky.mcutils.cmd.MCCommandSender;
import net.codersky.mcutils.crossplatform.player.MCPlayer;
import net.codersky.mcutils.crossplatform.player.PlayerProvider;
import net.codersky.mcutils.crossplatform.proxy.ProxyUtils;
import net.codersky.mcutils.java.MCCollections;
import net.codersky.mcutils.velocity.cmd.AdaptedVelocityCommand;
import net.codersky.mcutils.velocity.cmd.VelocityCommand;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -72,20 +74,26 @@ public MCPlayer getPlayer(@NotNull UUID uuid) {
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void registerCommands(MCCommand<P, MCCommandSender>... commands) {
@SuppressWarnings("unchecked")
public void registerCommands(GlobalCommand<P>... commands) {
if (commands == null || commands.length == 0)
return;
registerCommands(MCCollections.map(
commands,
new AdaptedVelocityCommand[commands.length],
cmd -> new AdaptedVelocityCommand<>(this, cmd))
);
}

@SuppressWarnings("unchecked")
public void registerCommands(VelocityCommand<P>... commands) {
final CommandManager manager = getProxy().getCommandManager();
for (MCCommand<P, MCCommandSender> mcCommand : commands) {
final VelocityCommand<P> velocityCommand;
if (mcCommand instanceof VelocityCommand)
velocityCommand = (VelocityCommand) mcCommand;
else
velocityCommand = new AdaptedVelocityCommand<>(this, mcCommand);
final CommandMeta meta = manager.metaBuilder(velocityCommand.getName())
for (VelocityCommand<P> command : commands) {
final CommandMeta meta = manager.metaBuilder(command.getName())
.plugin(getPlugin())
.aliases(velocityCommand.getAliasesArray())
.aliases(command.getAliasesArray())
.build();
manager.register(meta, velocityCommand);
manager.register(meta, command);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.codersky.mcutils.velocity.cmd;

import net.codersky.mcutils.cmd.GlobalCommand;
import net.codersky.mcutils.cmd.MCCommand;
import net.codersky.mcutils.cmd.MCCommandSender;
import net.codersky.mcutils.velocity.VelocityUtils;
Expand All @@ -9,9 +10,9 @@

public class AdaptedVelocityCommand<P> extends VelocityCommand<P> {

private final MCCommand<P, MCCommandSender> command;
private final GlobalCommand<P> command;

public AdaptedVelocityCommand(@NotNull VelocityUtils<P> utils, @NotNull MCCommand<P, MCCommandSender> command) {
public AdaptedVelocityCommand(@NotNull VelocityUtils<P> utils, @NotNull GlobalCommand<P> command) {
super(utils, command.getName(), command.getAliases().toArray(new String[0]));
this.command = command;
}
Expand Down
3 changes: 2 additions & 1 deletion shared/src/main/java/net/codersky/mcutils/MCUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.codersky.mcutils;

import net.codersky.mcutils.cmd.GlobalCommand;
import net.codersky.mcutils.cmd.MCCommand;
import net.codersky.mcutils.cmd.MCCommandSender;
import net.codersky.mcutils.crossplatform.MCConsole;
Expand Down Expand Up @@ -92,7 +93,7 @@ public final String getMCUtilsVersion() {
* Commands
*/

public abstract void registerCommands(MCCommand<P, MCCommandSender>... commands);
public abstract void registerCommands(GlobalCommand<P>... commands);

/*
* Reloadables
Expand Down

0 comments on commit 482d7d4

Please sign in to comment.