Functionality Update

Numerous changes;
Features that may be removed:
SubCommand system
This commit is contained in:
Paldiu
2022-02-05 22:05:33 -06:00
parent 83bd9dfced
commit 8569cf1510
46 changed files with 568 additions and 138 deletions

View File

@ -1,26 +1,29 @@
package io.github.simplex;
import io.github.simplex.api.ICommand;
import io.github.simplex.api.annotations.Permission;
import io.github.simplex.api.annotations.SubCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.util.RGBLike;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.plugin.Plugin;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class CommandBase extends Permissible implements ICommand {
private final Plugin plugin;
public CommandBase(Plugin plugin, String permission, String permissionMessage) {
super(permission, permissionMessage);
this.plugin = plugin;
public CommandBase(String permission, String permissionMessage, boolean allowConsole) {
super(permission, permissionMessage, allowConsole);
}
public CommandBase(Plugin plugin, String permission) {
this(plugin, permission, "You do not have permission to use this command.");
public CommandBase(String permission, String permissionMessage) {
this(permission, permissionMessage, true);
}
@Override
@ -32,7 +35,21 @@ public abstract class CommandBase extends Permissible implements ICommand {
.color(TextColor.color(255, 3, 62)));
return true;
}
execute();
if (!(sender instanceof Player) && !allowConsole()) {
sender.sendMessage(Component.empty().content("This command may only be run in game."));
}
execute(sender, args, allowConsole());
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return new ArrayList<>();
}
public TextComponent msg(String text) {
return Component.empty().content(text);
}
}

View File

@ -2,68 +2,44 @@ package io.github.simplex;
import io.github.simplex.api.annotations.*;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.Plugin;
import org.reflections.Reflections;
import java.lang.annotation.AnnotationTypeMismatchException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Set;
public class CommandLoader {
public class CommandLoader<T extends CommandBase> {
private final Plugin plugin;
public CommandLoader(Plugin plugin) {
this.plugin = plugin;
}
public Plugin getPlugin() {
return plugin;
}
public void registerCommands(Class<CommandBase> target) {
public void registerCommands(Class<T> target) {
Reflections reflections = new Reflections(target);
if (target.getDeclaredAnnotation(Info.class) != null) {
Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(Info.class);
classSet.forEach(cmd -> {
Info info = cmd.getDeclaredAnnotation(Info.class);
try {
CommandBase base = (CommandBase) cmd.getConstructor(Plugin.class, String.class, String.class).newInstance(plugin, info.permission(), info.permissionMessage());
DummyCommand dummy = new DummyCommand(base, info.name(), info.description(), info.usage(), Arrays.asList(info.aliases().split(",")));
Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
f.setAccessible(true);
CommandMap commandMap = (CommandMap) f.get(Bukkit.getServer());
commandMap.register(info.name(), dummy);
} catch (NoSuchMethodException
| InvocationTargetException
| IllegalAccessException
| InstantiationException
| NoSuchFieldException e) {
plugin.getLogger().severe(e.getMessage() + "\n\n\n" + e.getCause());
}
});
} else if (target.getDeclaredAnnotation(Name.class) != null) {
Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(Name.class);
classSet.forEach(cmd -> {
Name name = cmd.getDeclaredAnnotation(Name.class);
Description description = cmd.getDeclaredAnnotation(Description.class);
Usage usage = cmd.getDeclaredAnnotation(Usage.class);
Aliases aliases = cmd.getDeclaredAnnotation(Aliases.class);
Permission permission = cmd.getDeclaredAnnotation(Permission.class);
try {
CommandBase base = (CommandBase) cmd.getConstructor(Plugin.class, String.class, String.class).newInstance(plugin, permission.permission(), permission.permissionMessage());
DummyCommand dummy = new DummyCommand(base, name.name(), description.description(), usage.usage(), Arrays.asList(aliases.aliases().split(",")));
Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
f.setAccessible(true);
CommandMap commandMap = (CommandMap) f.get(Bukkit.getServer());
commandMap.register(name.name(), dummy);
CommandBase base = (CommandBase) cmd.getConstructor(String.class,
String.class,
Boolean.class)
.newInstance(permission.permission(),
permission.permissionMessage(),
permission.allowConsole());
DummyCommand dummy = new DummyCommand(plugin,
base,
info.name(),
info.description(),
info.usage(),
Arrays.asList(info.aliases().split(",")));
Bukkit.getCommandMap().register(info.name(), "SimplexCL", dummy);
} catch (NoSuchMethodException
| InvocationTargetException
| IllegalAccessException
| InstantiationException
| NoSuchFieldException e) {
| InstantiationException e) {
plugin.getLogger().severe(e.getMessage() + "\n\n\n" + e.getCause());
}
});

View File

@ -1,15 +1,18 @@
package io.github.simplex;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class DummyCommand extends BukkitCommand {
public final class DummyCommand extends Command implements PluginIdentifiableCommand {
private final CommandBase base;
private final Plugin plugin;
protected DummyCommand(@NotNull CommandBase base, @NotNull String name, @NotNull String description, @NotNull String usageMessage, @NotNull List<String> aliases) {
DummyCommand(@NotNull Plugin plugin, @NotNull CommandBase base, @NotNull String name, @NotNull String description, @NotNull String usageMessage, @NotNull List<String> aliases) {
super(name, description, usageMessage, aliases);
this.setName(name);
this.setDescription(description);
@ -17,6 +20,7 @@ public class DummyCommand extends BukkitCommand {
this.setAliases(aliases);
this.setPermission(base.getPermission());
this.base = base;
this.plugin = plugin;
}
@Override
@ -24,4 +28,9 @@ public class DummyCommand extends BukkitCommand {
base.onCommand(sender, this, commandLabel, args);
return true;
}
@Override
public @NotNull Plugin getPlugin() {
return plugin;
}
}

View File

@ -5,14 +5,12 @@ import org.bukkit.command.CommandSender;
public abstract class Permissible {
private final String permission;
private final String permissionMessage;
private final boolean allowConsole;
public Permissible(String permission, String permissionMessage) {
public Permissible(String permission, String permissionMessage, boolean allowConsole) {
this.permission = permission;
this.permissionMessage = permissionMessage;
}
public Permissible(String permission) {
this(permission, "You do not have permission to use this command.");
this.allowConsole = allowConsole;
}
public String getPermission() {
@ -26,4 +24,8 @@ public abstract class Permissible {
public boolean hasPermission(CommandSender sender) {
return sender.hasPermission(getPermission());
}
public boolean allowConsole() {
return allowConsole;
}
}

View File

@ -0,0 +1,15 @@
package io.github.simplex;
/*
* This class is intended to be used as a nested class inside your main command class.
* For example:
*/
public abstract class SubCommandBase extends CommandBase {
public SubCommandBase(String permission, String permissionMessage, boolean allowConsole) {
super(permission, permissionMessage, allowConsole);
}
public SubCommandBase(String permission, String permissionMessage) {
this(permission, permissionMessage, true);
}
}

View File

@ -1,8 +1,9 @@
package io.github.simplex.api;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
public interface ICommand extends CommandExecutor, TabCompleter {
boolean execute();
void execute(CommandSender sender, String[] args, boolean allowConsole);
}

View File

@ -1,12 +0,0 @@
package io.github.simplex.api.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Aliases {
String aliases();
}

View File

@ -1,12 +0,0 @@
package io.github.simplex.api.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Description {
String description();
}

View File

@ -11,7 +11,5 @@ public @interface Info {
String name();
String description();
String usage();
String permission();
String aliases() default "";
String permissionMessage() default "You do not have permission to use this command.";
}

View File

@ -1,12 +0,0 @@
package io.github.simplex.api.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Name {
String name();
}

View File

@ -10,4 +10,5 @@ import java.lang.annotation.Target;
public @interface Permission {
String permission();
String permissionMessage() default "You do not have permission to use this command!";
boolean allowConsole() default true;
}

View File

@ -5,7 +5,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SubCommand {
String name();
}

View File

@ -1,12 +0,0 @@
package io.github.simplex.api.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Usage {
String usage();
}

View File

@ -0,0 +1,39 @@
package io.github.simplex.impl;
import io.github.simplex.CommandBase;
import io.github.simplex.SubCommandBase;
import io.github.simplex.api.annotations.Info;
import io.github.simplex.api.annotations.Permission;
import io.github.simplex.api.annotations.SubCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.command.CommandSender;
@Info(name = "example", description = "An example command implementation to see how this works.", usage = "/example [info]", aliases = "ex, impl")
@Permission(permission = "simplexcl.example", permissionMessage = "You cannot use this command!")
public final class ExampleCommand extends CommandBase {
public ExampleCommand(String permission, String permissionMessage, boolean allowConsole) {
super(permission, permissionMessage, allowConsole);
}
@Override
public void execute(CommandSender sender, String[] args, boolean allowConsole) {
}
@SubCommand(name = "info")
@Permission(permission = "simplexcl.example.info")
public class InfoSubCommand extends SubCommandBase {
public InfoSubCommand(String permission, String permissionMessage, boolean allowConsole) {
super(permission, permissionMessage, allowConsole);
}
@Override
public void execute(CommandSender sender, String[] args, boolean allowConsole) {
TextComponent message = Component.empty();
message = message.append(msg("SimplexCL was created by SimplexDevelopment!"))
.append(msg("https://github.com/SimplexDevelopment/"));
sender.sendMessage(message);
}
}
}