Jump to content

Null

Veteran
  • Content Count

    2
  • Joined

  • Last visited

  • Days Won

    2
  • Time Online

    32m 19s

Null last won the day on June 24

Null had the most liked content!

Community Reputation

2 Neutral

About Null

  • Rank
    Conversation Starter

Recent Profile Visitors

80 profile views
  1. Yes it was made for ruse
  2. Example on what you're adding: @CommandInfo( command = {"test", "tester", "testing"}, description = "Test command.", rights = {SUPPORT, MODERATOR, GLOBAL_MODERATOR, ADMINISTRATOR, OWNER, DEVELOPER, COMMUNITYMANAGER} ) public class Test extends Command { @Override public void execute(Player player, String command) { player.sendMessage("Lol working"); } } CommandInfo.java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) //on class level public @interface CommandInfo { String[] command() default ""; String description() default ""; int donationAmountRequired() default 0; PlayerRights[] rights() default PlayerRights.PLAYER; } Command.java public class Command { protected Logger logger = Logger.getLogger(super.getClass()); public void execute(Player player, String command) {}; } CommandHandler.java @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."); } } } PacketConstants.java PACKETS[103] = new CommandPacketListener(); How to load commands: 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();
×
×
  • Create New...