Plugin linking to external .yml

Discussion in 'Plugin Development' started by mima19, Nov 2, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    mima19

    I'm rather new to coding in Java, and I want to make a commands.yml in external file (once you run the server like config.yml) where you can put do you own command and aliases, description etc.
    How would I do that? (I know how to make a normal command...)
     
  2. Offline

    ZeusAllMighty11

    You can't... You can only make and edit commands from the plugin.yml (I mean aliases and descriptions and such)
     
  3. Offline

    mima19

    What about what the command does or things like that?
     
  4. Offline

    ZeusAllMighty11

    Commands will only be recognized in the plugin.yml file...


    Although I have wondered what would happen if I made a custom command with CommandPreprocessEvent, and checked for a string in the config, which would be the command. Then set the event to cancelled, and carry out X function
     
  5. Offline

    fireblast709

    You can filter out anything with a '/':
    Code:java
    1. @EventHandler
    2. public void onC(PlayerCommandPreprocessEvent event)
    3. {
    4. Player commandSender = event.getPlayer();
    5. String[] splitted = event.getMessage().substring(1).split(" ");
    6. String cmd = splitted[0];
    7. String[] args = Arrays.copyOfRange(splitted, 1, splitted.length);
    8. if(cmd.equals("a command in the config"))
    9. {
    10. System.out.println("Command called: "+event.getMessage());
    11. // Or whatever you want to link it to
    12. performCommand(commandSender, args);
    13. event.setCancelled(true);
    14. }
    15. }
     
  6. Offline

    mima19

    I tried this but it didn't generate a command.yml (or any other)...Do I need to create on inside my plugin so it can be taken out?

    I'm rather new to java, could you give an example?

    I just want it to work like that:
    I type /cc abc
    and server gives me abcdef....

    [or args]
    I want to be able to change abc (and add new one [command]) and change the output (abcdef...)
    Kind of like motd (/motd) where motd has a file where I can edit it. I want to change /motd to something else...Thou the plugin will do other thing

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  7. Offline

    fireblast709

    mima19 my code just filters any message that starts with a '/', and if it equals anything you like, you could perform a command with it. It is like creating a dynamic alias, or even better, it would be possible to define a command which would not be in the plugin.yml. Though you would have to write a class for it and a function it can call whenever you want to perform the command. Example:
    Code:java
    1. @EventHandler
    2. public void onC(PlayerCommandPreprocessEvent event)
    3. {
    4. Player commandSender = event.getPlayer();
    5. String[] splitted = event.getMessage().substring(1).split(" ");
    6. String cmd = splitted[0];
    7. String[] args = Arrays.copyOfRange(splitted, 1, splitted.length);
    8. // Say you have a private message command /tell
    9. if(cmd.equals("tell"))
    10. {
    11. // Either write the code you want to execute here
    12. // Note that this is not properly written for a tell :3
    13. Player other = Bukkit.getPlayer(args[0]);
    14. if(other != null)
    15. other.sendMessage(args[1]);
    16.  
    17. // Or execute a command class, usually for bigger commands
    18. // You could try finding a way to define these, though that is hard/impossible
    19. CommandTell.perform(commandSender, args);
    20. event.setCancelled(true);
    21. }
    22. }
    23. // a command class not listed in the plugin.yml
    24. class CommandTell
    25. {
    26. // Contains the command (this time, full implementation that would actually work)
    27. public static void perform(Player sender, String[] args)
    28. {
    29. if(sender.hasPermission("some.permission"))
    30. {
    31. Player target = Bukkit.getPlayer(args[0]);
    32. if(target == null)
    33. {
    34. sender.sendMessage("Player not online!");
    35. return;
    36. }
    37.  
    38. String message = "["+sender.getDisplayName()+" -> "+target.getDisplayName()+"]: ";
    39. for(String s : Arrays.copyOfRange(args, 1, args.length))
    40. {
    41. message += s;
    42. }
    43. sender.sendMessage(message);
    44. target.sendMessage(message);
    45. }
    46. }
    47. }

    Though note that apart from adding it to the plugin.yml, you probably would either predefine these commands or you would have to deal with java's reflection classes to get your stuff done (and in a hard way)
     
    ZeusAllMighty11 likes this.
Thread Status:
Not open for further replies.

Share This Page