Skip to content

Commit

Permalink
MCCommand: Added #getUtils & #getPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Sep 10, 2024
1 parent 4d810ef commit dae0abd
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,16 @@
import java.util.function.Consumer;
import java.util.function.Predicate;

public class SpigotUtils<P extends JavaPlugin> extends MCUtils {
public class SpigotUtils<P extends JavaPlugin> extends MCUtils<P> {

private final P plugin;
private final SpigotConsole console;
private PlayerProvider<Player> playerProvider;

public SpigotUtils(@NotNull P plugin) {
this.plugin = Objects.requireNonNull(plugin);
super(plugin);
this.console = new SpigotConsole(Bukkit.getConsoleSender());
}

@NotNull
public P getPlugin() {
return plugin;
}

@NotNull
public SpigotUtils<P> setPlayerProvider(@NotNull PlayerProvider<Player> playerProvider) {
this.playerProvider = Objects.requireNonNull(playerProvider, "Player provider cannot be null");
Expand Down Expand Up @@ -144,7 +138,7 @@ else if (c >= '0' && c <= '9')
*/
@NotNull
public <T extends Listener> T registerEvents(@NotNull T listener) {
Bukkit.getPluginManager().registerEvents(listener, plugin);
Bukkit.getPluginManager().registerEvents(listener, getPlugin());
return listener;
}

Expand Down Expand Up @@ -186,8 +180,8 @@ protected SimpleCommandMap getCommandMap() {
final RefObject map = new RefObject(Bukkit.getServer()).invoke("getCommandMap");
if (map != null)
return (SimpleCommandMap) map.getInstance();
logCol( "&8[&6" + plugin.getName() + "&8] &cCould get the command map, please inform about this&8.",
" &8- &7MCUtils is at fault here, do not contact &e" + plugin.getName() + "&7's author(s)&8.",
logCol( "&8[&6" + getPlugin().getName() + "&8] &cCould get the command map, please inform about this&8.",
" &8- &7MCUtils is at fault here, do not contact &e" + getPlugin().getName() + "&7's author(s)&8.",
" &8- &7Contact&8: &espigotmc.org/members/xdec0de_.178174/ &7or Discord &9@xdec0de_",
" &8- &7Server info&8: &b" + Bukkit.getServer().getName() + " " + getServerVersion(),
" &8- &7Using MCUtils version &bv" + getMCUtilsVersion());
Expand Down Expand Up @@ -215,29 +209,27 @@ protected SimpleCommandMap getCommandMap() {
*
* @throws ClassCastException if any of the passed {@code commands} isn't an instance of {@link SpigotCommand}.
*
* @return This {@link SpigotUtils}.
*
* @since MCUtils 1.0.0
*/
public void registerCommands(@Nullable MCCommand<?>... commands) {
public void registerCommands(@Nullable MCCommand<?, P>... commands) {
if (commands == null || commands.length == 0)
return;
final List<Command> remaining = new ArrayList<>();
for (MCCommand<?> command : commands) {
final SpigotCommand spigotCommand = (SpigotCommand) command;
final PluginCommand plCommand = plugin.getCommand(command.getName());
for (MCCommand<?, P> command : commands) {
final SpigotCommand<P> spigotCommand = (SpigotCommand<P>) command;
final PluginCommand plCommand = getPlugin().getCommand(command.getName());
if (plCommand != null)
plCommand.setExecutor(spigotCommand);
else
remaining.add(spigotCommand);
}
if (remaining.size() == 0)
if (remaining.isEmpty())
return;
final SimpleCommandMap commandMap = getCommandMap();
if (commandMap == null)
Bukkit.getPluginManager().disablePlugin(plugin);
Bukkit.getPluginManager().disablePlugin(getPlugin());
else
commandMap.registerAll(plugin.getName(), remaining);
commandMap.registerAll(getPlugin().getName(), remaining);
}

/**
Expand All @@ -257,7 +249,7 @@ public void registerCommands(@Nullable MCCommand<?>... commands) {
* has been unregistered successfully, {@code false} otherwise.
*/
public boolean unregisterCommand(@NotNull String name) {
final PluginCommand plCommand = plugin.getCommand(name);
final PluginCommand plCommand = getPlugin().getCommand(name);
final SimpleCommandMap commandMap = getCommandMap();
return plCommand == null || commandMap == null ? false : plCommand.unregister(commandMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Arrays;
import java.util.List;

public abstract class SpigotCommand<P extends JavaPlugin> extends Command implements MCCommand<SpigotCommandSender>, PluginIdentifiableCommand, TabExecutor {
public abstract class SpigotCommand<P extends JavaPlugin> extends Command implements MCCommand<SpigotCommandSender, P>, PluginIdentifiableCommand, TabExecutor {

private final SpigotUtils<P> utils;
private final SubCommandHandler<SpigotCommandSender> subCommandHandler = new SubCommandHandler<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@
import java.util.Objects;
import java.util.UUID;

public class VelocityUtils<P> extends MCUtils {
public class VelocityUtils<P> extends MCUtils<P> {

private final P plugin;
private final ProxyServer proxy;
private final VelocityConsole console;
private PlayerProvider<Player> playerProvider;

public VelocityUtils(@NotNull P plugin, @NotNull ProxyServer proxy) {
this.plugin = Objects.requireNonNull(plugin);
super(plugin);
this.proxy = Objects.requireNonNull(proxy);
this.console = new VelocityConsole(proxy.getConsoleCommandSource());
}

@NotNull
public final P getPlugin() {
return plugin;
}

@NotNull
public final ProxyServer getProxy() {
return this.proxy;
Expand Down Expand Up @@ -60,12 +54,12 @@ public MCPlayer<Player> getPlayer(@NotNull UUID uuid) {
}

@Override
public void registerCommands(MCCommand<?>... commands) {
public void registerCommands(MCCommand<?, P>... commands) {
final CommandManager manager = getProxy().getCommandManager();
for (MCCommand<?> mcCommand : commands) {
final VelocityCommand velocityCommand = (VelocityCommand) mcCommand;
for (MCCommand<?, P> mcCommand : commands) {
final VelocityCommand<P> velocityCommand = (VelocityCommand<P>) mcCommand;
final CommandMeta meta = manager.metaBuilder(velocityCommand.getName())
.plugin(plugin)
.plugin(getPlugin())
.aliases(velocityCommand.getAliasesArray())
.build();
manager.register(meta, velocityCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public abstract class VelocityCommand<P> implements SimpleCommand, MCCommand<VelocityCommandSender> {
public abstract class VelocityCommand<P> implements SimpleCommand, MCCommand<VelocityCommandSender, P> {

private final VelocityUtils<P> utils;
private final String name;
Expand Down Expand Up @@ -59,7 +59,7 @@ public final String[] getAliasesArray() {
// Command logic //

@SafeVarargs
public final VelocityCommand<U> inject(VelocityCommand<U>... commands) {
public final VelocityCommand<P> inject(VelocityCommand<P>... commands) {
subCmdHandler.inject(commands);
return this;
}
Expand Down
14 changes: 12 additions & 2 deletions shared/src/main/java/net/codersky/mcutils/MCUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@
import java.util.Objects;
import java.util.UUID;

public abstract class MCUtils {
public abstract class MCUtils<P> {

private final P plugin;
protected final LinkedList<FileHolder> files = new LinkedList<>();

public MCUtils(@NotNull P plugin) {
this.plugin = Objects.requireNonNull(plugin);
}

@NotNull
public final P getPlugin() {
return plugin;
}

@Nullable
public abstract MCPlayer<?> getPlayer(@NotNull UUID uuid);

Expand Down Expand Up @@ -66,7 +76,7 @@ public <T extends FileHolder> T registerFile(@NotNull T file) {
return file;
}

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

/**
* Gets the files currently being handled by this {@link MCUtils} instance. The list returned
Expand Down
11 changes: 8 additions & 3 deletions shared/src/main/java/net/codersky/mcutils/cmd/MCCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
import java.util.List;
import java.util.function.Function;

public interface MCCommand<S extends MCCommandSender<?, ?>> {
public interface MCCommand<S extends MCCommandSender<?, ?>, P> {

@NotNull
String getName();

@NotNull
MCUtils getUtils();
MCUtils<P> getUtils();

@NotNull
default P getPlugin() {
return getUtils().getPlugin();
}

@NotNull
List<String> getAliases();
Expand All @@ -28,7 +33,7 @@ public interface MCCommand<S extends MCCommandSender<?, ?>> {

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

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

/**
* Returns whether this {@link MCCommand} removes
Expand Down

0 comments on commit dae0abd

Please sign in to comment.