Was practising with annotations so no h8 pls
What your adding
Code:
@CommandInfo(
command = {"test", "tester", "testing"},
description = "Test command.",
rights = {SUPPORT, MODERATOR, GLOBAL_MODERATOR, ADMINISTRATOR, OWNER, DEVELOPER, COMMUNITYMANAGER},
donationAmountRequired = 1337
)
public class Test extends Command {
@Override
public void execute(Player player, String command) {
player.sendMessage("Lol working");
}
}
CommandInfo.java
Code:
@Retention(RetentionPolicy.RUNTIME)
@target(ElementType.TYPE)
public @interface CommandInfo {
String[] command() default "";
String description() default "";
int donationAmountRequired() default 0;
PlayerRights[] rights() default PlayerRights.PLAYER;
}
Command.java
Code:
public class Command {
protected Logger logger = Logger.getLogger(super.getClass());
public void execute(Player player, String command) {};
}
CommandHandler.java
Code:
@log4j
public class CommandHandler implements PacketListener {
public static List<Class> commands = new ArrayList<>();
@SneakyThrows
@Override
public void handleMessage(Player player, Packet packet) {
String command = Misc.readString(packet.getBuffer());
if (command.contains("\r") || command.contains("\n")) {
return;
}
Command c;
try {
for(Class clazz : commands) {
if(clazz.isAnnotationPresent(CommandInfo.class)) {
Annotation annotation = clazz.getAnnotation(CommandInfo.class);
CommandInfo commandInfo = (CommandInfo) annotation;
String[] realCommand;
if(command.contains(" ") && command.contains("-")) {
int spaceIndex = command.indexOf(" ");
int dashIndex = command.indexOf("-");
if(spaceIndex < dashIndex) {
realCommand = command.split(" ");
} else {
realCommand = command.split("-");
}
} else if(command.contains(" ")) {
realCommand = command.split(" ");
} else if(command.contains("-")) {
realCommand = command.split("-");
} else {
realCommand = new String[]{command};
}
List<String> possibleCommands = Arrays.asList(commandInfo.command());
if(possibleCommands.contains(realCommand[0])) {
c = (Command) clazz.newInstance();
List<PlayerRights> rights = Arrays.asList(commandInfo.rights());
if(player.getRights().shouldDebug() || rights.contains(player.getRights())) {
if (player.getAmountDonated() >= ((CommandInfo) annotation).donationAmountRequired()) {
c.execute(player, command);
return;
} else {
player.sendMessage("You need a total donated value of $" + commandInfo.donationAmountRequired() + " to use this command.");
}
}
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
player.getPacketSender().sendMessage("Error executing that command.");
}
}
}
How I loaded the commands..
Code:
new FastClasspathScanner().matchSubclassesOf(Command.class, clazz -> {
try {
if (!Modifier.isAbstract(clazz.getModifiers())) {
synchronized (GameServer.class) {
CommandHandler.commands.add(clazz);
}
}
}catch(Exception e) {
logger.error("Error adding command " + clazz.getPackage() + "." + clazz.getName() + "\n " + e);
}
}).scan();
After JayArrowz explained a few things to me can defo say don't use this unless you load the commands+annotations into a map on startup instead and access annotations that way