Changing Command Args

Discussion in 'Plugin Development' started by Corndogoz, Aug 12, 2015.

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

    Corndogoz

    hi guys, i am making a plugin which has to change text and commands, however i dont know how to change the args in a command so like i could change /me hi into /me hello please help!
     
  2. Moved to Plugin Development
     
  3. Offline

    Corndogoz

    would someone pleaseeee help! ive been trying for hours but have nothing!
     
  4. @Corndogoz Hi! you could create a hashmap with the original word and the new word like so:

    Code:
    HashMap<String, String> hmap = new HashMap<String, String>();
    and a string list:

    Code:
    ArrayList<String> alist = new ArrayList<String>();
    Just configure the list and the hashmap with the hashmap key being the same as the list string.

    Then loop through all the args and check if the alist contains one of them, if so get the respective hashmap object and replace it! ;) If you didn't understood just tell me!

    Hope it helps!
     
  5. args[0] = "hello";
     
  6. Offline

    Corndogoz

    @amatokus @FisheyLP/
    neither way works, unless i did it wrong, i have to send all the arguments through something which returns a string , and then change the arguments to what it returned
     
  7. Offline

    HenkDeKipGaming

    you can save your commands to a config file :D
    so like this, for example, you got the command: /heal :
    So in your onEnable you would do somehing like this:
    Code:
    public void onEnable() {
        getConfig().addDefaults("example", "test");
        getConfig().options().copyDefaults(true);
        saveConfig();
    }
    
    then you want to check if someone types that string.
    so:
    Code:
     public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, final String[] args) { 
                        if (cmd.getName().equalsIgnoreCase("command") {
                          if(args[0].equalsIgnoreCase(getConfig().getString("example")) {
                            sender.sendMessage("worked!");
                          }
                        }
    
    and to change it it would be:
    Code:
     public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, final String[] args) { 
                        if (cmd.getName().equalsIgnoreCase("change")) {
                        getConfig().set("example", args[0]);
                         }
    
    so the player can change the argument and use it :)
    hope i helped :D
    please don't blame me from mistakes, i did this from my head xD

    Greetings!
     
  8. Offline

    Corndogoz

    thanks for replying but that just changes something in the config, not what the player typed
     
  9. I would do it with a HashMap and a replace method:
    Somewhere in the class:
    Code:
    public HashMap<String, String> replace = new HashMap<String, String>();
    Somewhere in the initialization of the class:
    Code:
    replace.put("hi", "hello");
    replace.put("hate", "like");
    }
    Somewhere in the class:
    Code:
    public String[] replace(String[] args) {
    for (int i = 0; i < args.length; i++) {
    String arg = args.toLowercase();
    if (replace.containsKey(arg)) args = replace.get(arg);
    }
    return args;
    }
    In the onCommand method:
    Code:
    args = replace(args);
     
  10. Offline

    AdobeGFX

    Correct me if i'm wrong
    Are you looking for a solution on how to change sub-commands from in-game?
    Like, if there is a command, something like /scream hello
    Then you want a command that can the sub-command (hello) to something else?
     
  11. Offline

    Corndogoz

    well i acctually want the command the type to go through something which returns what i want the new subcommands to be
     
  12. Offline

    Corndogoz

    i know how todo that, PEOPLE PLEASE LISTEN: everyone keeps saying args[0]= or args= GUYS IT DOESNT WORK
     
  13. Offline

    MexMaster

    Maybe because nobody is understanding what you are trying to do??
    Your description makes no sense at all.
    Please give a better explanation and don't be rude.
    @Corndogoz
     
  14. Offline

    Corndogoz

    hmm well by the responses im getting, people understand, their code just doesnt work! so i was just trying to make that clear!
     
  15. Offline

    mythbusterma

    @Corndogoz

    Maybe it actually does and you're just incompetent? That's a pretty reasonable answer as well, in my opinion.

    I see nothing wrong with what @FisheyLP posted (the other answers aren't very good).

    Iterate over the array, replacing the arguments as they appear. Make sure your comparisons are case-insensitive.
     
    Creeper674 likes this.
  16. Offline

    griffjack

    @Corndogoz
    Try posting what you have, people might be able to see where you went wrong.
     
  17. Offline

    Corndogoz

    the problem is i have nothing! Ive tried todo it the ways posted, they just dont work, so i have nothing :(
     
  18. Offline

    mythbusterma

    @Corndogoz

    I can assure you that if you create a Map mapping Strings to one another and do case-insensitive searches and replacement, that will work.
     
Thread Status:
Not open for further replies.

Share This Page