Initial commit

This commit is contained in:
Paldiu
2022-01-30 10:12:31 -06:00
commit 83bd9dfced
56 changed files with 1087 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package io.github.simplex;
import io.github.simplex.api.ICommand;
import io.github.simplex.api.annotations.Permission;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.util.RGBLike;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
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(Plugin plugin, String permission) {
this(plugin, permission, "You do not have permission to use this command.");
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String lbl, String[] args) {
if (!hasPermission(sender)) {
sender.sendMessage(Component
.empty()
.content(getPermissionMessage())
.color(TextColor.color(255, 3, 62)));
return true;
}
execute();
return true;
}
}

View File

@ -0,0 +1,74 @@
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 {
private final Plugin plugin;
public CommandLoader(Plugin plugin) {
this.plugin = plugin;
}
public Plugin getPlugin() {
return plugin;
}
public void registerCommands(Class<CommandBase> 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);
} catch (NoSuchMethodException
| InvocationTargetException
| IllegalAccessException
| InstantiationException
| NoSuchFieldException e) {
plugin.getLogger().severe(e.getMessage() + "\n\n\n" + e.getCause());
}
});
} else {
throw new RuntimeException("Missing a required annotation! Unable to load the command.");
}
}
}

View File

@ -0,0 +1,27 @@
package io.github.simplex;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class DummyCommand extends BukkitCommand {
private final CommandBase base;
protected DummyCommand(@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);
this.setUsage(usageMessage);
this.setAliases(aliases);
this.setPermission(base.getPermission());
this.base = base;
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
base.onCommand(sender, this, commandLabel, args);
return true;
}
}

View File

@ -0,0 +1,29 @@
package io.github.simplex;
import org.bukkit.command.CommandSender;
public abstract class Permissible {
private final String permission;
private final String permissionMessage;
public Permissible(String permission, String permissionMessage) {
this.permission = permission;
this.permissionMessage = permissionMessage;
}
public Permissible(String permission) {
this(permission, "You do not have permission to use this command.");
}
public String getPermission() {
return permission;
}
public String getPermissionMessage() {
return permissionMessage;
}
public boolean hasPermission(CommandSender sender) {
return sender.hasPermission(getPermission());
}
}

View File

@ -0,0 +1,8 @@
package io.github.simplex.api;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.TabCompleter;
public interface ICommand extends CommandExecutor, TabCompleter {
boolean execute();
}

View File

@ -0,0 +1,12 @@
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

@ -0,0 +1,12 @@
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

@ -0,0 +1,17 @@
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 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

@ -0,0 +1,12 @@
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

@ -0,0 +1,13 @@
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.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Permission {
String permission();
String permissionMessage() default "You do not have permission to use this command!";
}

View File

@ -0,0 +1,11 @@
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.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SubCommand {
}

View File

@ -0,0 +1,12 @@
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();
}