Thread: Command System

Results 1 to 3 of 3
  1. #1 Command System 
    Banned

    Join Date
    Jun 2013
    Posts
    499
    Thanks given
    119
    Thanks received
    70
    Rep Power
    0
    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
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Feb 2010
    Posts
    3,253
    Thanks given
    1,145
    Thanks received
    909
    Rep Power
    2081
    nice idea but seems massively overcomplicated, thanks for release though
    Reply With Quote  
     

  3. #3  
    Banned

    Join Date
    Jun 2013
    Posts
    499
    Thanks given
    119
    Thanks received
    70
    Rep Power
    0
    Quote Originally Posted by Blacklist View Post
    nice idea but seems massively overcomplicated, thanks for release though

    Yeah is a mess, cleaned it up myself but not hard to clean up, mainly did this for a bit of practice on the annotations
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [PI]Using Shard Revolution's command system.
    By Tyluur in forum Snippets
    Replies: 2
    Last Post: 04-15-2011, 03:39 AM
  2. Far easier command system [Devo]
    By Tobias in forum Snippets
    Replies: 9
    Last Post: 05-27-2010, 10:11 AM
  3. System Update by command [System Update in 5:00]
    By tucybro in forum Tutorials
    Replies: 5
    Last Post: 06-27-2009, 04:26 PM
  4. Command system splitting on " "
    By tj007razor in forum Tutorials
    Replies: 8
    Last Post: 03-05-2009, 02:39 PM
  5. Half decent command system.
    By atrox_ in forum Tutorials
    Replies: 8
    Last Post: 01-02-2009, 11:03 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •