Skip to content

Commit

Permalink
Added GlobalCommand for cross-platform commands
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Sep 14, 2024
1 parent 4856a1e commit b2bd052
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public boolean onCommand(@NotNull SpigotCommandSender sender, @NotNull String[]
return command.onCommand(sender, args);
}

@NotNull
@Override
public @NotNull List<String> onTab(@NotNull SpigotCommandSender sender, @NotNull String[] args) {
public List<String> onTab(@NotNull SpigotCommandSender sender, @NotNull String[] args) {
return command.onTab(sender, args);
}

Expand All @@ -32,6 +33,7 @@ public boolean hasAccess(@NotNull SpigotCommandSender sender, boolean message) {
return command.hasAccess(sender, message);
}

@NotNull
@Override
public MCCommand<P, SpigotCommandSender> inject(@NotNull MCCommand<P, SpigotCommandSender>... commands) {
throw new UnsupportedOperationException("Adapted commands cannot inject new commands.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public boolean hasAccess(@NotNull VelocityCommandSender sender, boolean message)
return command.hasAccess(sender, message);
}

@NotNull
@Override
public MCCommand<P, VelocityCommandSender> inject(@NotNull MCCommand<P, VelocityCommandSender>... commands) {
throw new UnsupportedOperationException("Adapted commands cannot inject new commands.");
Expand Down
53 changes: 53 additions & 0 deletions shared/src/main/java/net/codersky/mcutils/cmd/GlobalCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.codersky.mcutils.cmd;

import net.codersky.mcutils.MCUtils;
import net.codersky.mcutils.crossplatform.MCConsole;
import net.codersky.mcutils.crossplatform.player.MCPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public abstract class GlobalCommand<P> implements MCCommand<P, MCCommandSender<MCPlayer<?>, MCConsole<?>>> {

private final MCUtils<P> utils;
private final String name;
private final List<String> aliases;
private final SubCommandHandler<P, MCCommandSender<MCPlayer<?>, MCConsole<?>>> subCmdHandler = new SubCommandHandler<>();

public GlobalCommand(MCUtils<P> utils, @NotNull String name, List<String> aliases) {
this.utils = utils;
this.name = name;
this.aliases = aliases;
}

public GlobalCommand(MCUtils<P> utils, @NotNull String name) {
this(utils, name, List.of());
}

public GlobalCommand(MCUtils<P> utils, @NotNull String name, String... aliases) {
this(utils, name, List.of(aliases));
}

@NotNull
@Override
public String getName() {
return name;
}

@Override
public @NotNull List<String> getAliases() {
return aliases;
}

@NotNull
@Override
public MCUtils<P> getUtils() {
return utils;
}

@Override
public @NotNull MCCommand<P, MCCommandSender<MCPlayer<?>, MCConsole<?>>> inject(@NotNull MCCommand<P, MCCommandSender<MCPlayer<?>, MCConsole<?>>>... commands) {
subCmdHandler.inject(commands);
return this;
}
}
61 changes: 42 additions & 19 deletions shared/src/main/java/net/codersky/mcutils/cmd/MCCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,35 @@
import java.util.Objects;
import java.util.function.Function;

/**
* Interface used to represent a cross-platform command.
* Commands can directly be made and implemented with this interface,
* though the {@link GlobalCommand} class is easier to use if you
* intend to make cross-platform commands.
*
* @since MCUtils 1.0.0
*
* @author xDec0de_
*
* @param <P> The plugin class that owns this {@link MCCommand}.
* @param <S> The {@link MCCommandSender} type of this {@link MCCommand}.
*/
public interface MCCommand<P, S extends MCCommandSender<?, ?>> {

/**
* Gets the name of this {@link MCCommand}, which is used in order
* to execute it as "/name".
*
* @return The name of this {@link MCCommand}.
*
* @since MCUtils 1.0.0
*/
@NotNull
String getName();

@NotNull
List<String> getAliases();

@NotNull
MCUtils<P> getUtils();

Expand All @@ -24,16 +48,14 @@ default P getPlugin() {
return getUtils().getPlugin();
}

@NotNull
List<String> getAliases();

boolean onCommand(@NotNull S sender, @NotNull String[] args);

@NotNull
List<String> onTab(@NotNull S sender, @NotNull String[] args);

boolean hasAccess(@NotNull S sender, boolean message);

@NotNull
MCCommand<P, S> inject(@NotNull MCCommand<P, S>... commands);

/**
Expand Down Expand Up @@ -64,50 +86,51 @@ default boolean removesEventPatterns() {
*/

/**
* Converts the specified {@code arg} of the {@code args} array to a any object by using the
* Converts the specified {@code arg} of the {@code args} array to any object by using the
* {@code converter} {@link Function}. Returning {@code def} if no argument is found at the
* {@code arg} position or if {@code converter} returns {@code null}.
*
* @param <T> the type of the result of the {@code converter} {@link Function}.
* @param <T> The return type of the {@code converter} {@link Function}.
* @param converter the {@link Function} that will convert the {@link String}
* found at the specified {@code arg} position. The {@link String} passed
* to the {@link Function} will <b>never</b> be {@code null}.
* @param arg the array position of the argument to get, can be out of bounds.
* @param args the array of arguments to use.
* @param def the default value to return if {@code arg} is out of bounds or
* @param arg The array position of the argument to get, can be out of bounds.
* @param args The array of arguments to use.
* @param def The default value to return if {@code arg} is out of bounds or
* {@code converter} returns {@code null}.
*
* @return The argument as converted by {@code converter} if found on the {@code args} array
* @return The argument converted by {@code converter} if found on the {@code args} array
* and {@code converter} doesn't return {@code null}. {@code def} otherwise.
*
* @throws NullPointerException if {@code args} or {@code converter} are {@code null}.
* @throws NullPointerException if {@code args}, {@code converter} or {@code def} are {@code null}.
*
* @since MCUtils 1.0.0
*/
@Nullable
default <T> T asGeneric(@NotNull Function<String, T> converter, int arg, @NotNull String[] args, @Nullable T def) {
@NotNull
default <T> T asGeneric(@NotNull Function<String, T> converter, int arg, @NotNull String[] args, @NotNull T def) {
Objects.requireNonNull(def, "def cannot be null. Remove the parameter instead.");
if (args.length <= arg)
return def;
final T converted = converter.apply(args[arg]);
return converted == null ? def : converted;
}

/**
* Converts the specified {@code arg} of the {@code args} array to a any object by using the
* Converts the specified {@code arg} of the {@code args} array to any object by using the
* {@code converter} {@link Function}. Returning {@code null} if no argument is found at the
* {@code arg} position or if {@code converter} returns {@code null}.
*
* @param <T> the type of the result of the {@code converter} {@link Function}.
* @param converter the {@link Function} that will convert the {@link String}
* @param <T> The return type of the {@code converter} {@link Function}.
* @param converter The {@link Function} that will convert the {@link String}
* found at the specified {@code arg} position. The {@link String} passed
* to the {@link Function} will <b>never</b> be {@code null}.
* @param arg the array position of the argument to get, can be out of bounds.
* @param args the array of arguments to use.
* @param arg The array position of the argument to get, can be out of bounds.
* @param args The array of arguments to use.
*
* @return The argument as converted by {@code converter} if found
* on the {@code args} array, {@code null} otherwise.
*
* @throws NullPointerException if {@code args} or {@code converter} are {@code null}.
* @throws NullPointerException If {@code args} or {@code converter} are {@code null}.
*
* @since MCUtils 1.0.0
*/
Expand Down Expand Up @@ -174,7 +197,7 @@ default String asString(int arg, @NotNull String[] args) {
* @param args the array of arguments to use.
* @param def the default value to return if <b>arg</b> is out of bounds.
*
* @return The argument as a {@link String} if found on the <b>args</b> array, null otherwise.
* @return The argument as a {@link String} if found on the <b>args</b> array, {@code def} otherwise.
*
* @throws NullPointerException if <b>modifier</b> is {@code null}.
*
Expand Down

0 comments on commit b2bd052

Please sign in to comment.