Else if never executing?

Discussion in 'Plugin Development' started by mrpoopy345, Jun 22, 2015.

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

    mrpoopy345

    I am trying to make a plugin that adds "variables" into commands. You use /set <variablename> <value> to set a value and then you can use any command with var:<varname> (For example you could do /set foo bar and then do "/say var:foo" and it would say "bar" in chat) for some reason my
    Code:
    else if(Arrays.toString(args).contains("var:")) {
    
    is either never executing or always returning false. Why is this? Main plugin class:
    Code:
    
    public class main extends JavaPlugin implements Listener {
        List<String> vars = new ArrayList<String>();
         public void onEnable()
         {
           getLogger();
           getServer().getPluginManager().registerEvents(this, this);
           Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Variables Enabled!");
         }
        
         public void onDisable()
         {
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Variables Disabled!");
         }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
                if(command.getName().equalsIgnoreCase("set")) {
                    vars.add(args[0] + ":" + args[1]);
                    sender.sendMessage(ChatColor.RED + "Variable " + args[0] + " added with the value " + args[1]);
                }else if(Arrays.toString(args).contains("var:")) {
                    int size = args.length;
                       for (int i=0; i<size; i++)
                       {
                          if(args[i].contains("var:")) {
                              String[] parts = args[i].split(":");
                              for (String temp : vars) {
                                  String[] varname = temp.split(":");
                                  if(varname[1].equals(parts[1])) {
                                      args[i] = varname[2];
                                  }
                              }
                          }
                       }
                  
                }
            return super.onCommand(sender, command, label, args);
        }
    
         }
    
     
  2. Offline

    SuperSniper

    Try this

    }else {
    if(Arrays.toString(args).contains("var:")) {
     
  3. Offline

    Zombie_Striker

    @mrpoopy345
    So, what you currelty have is this..

    If a command is sent and the command == set, do stuff

    else, if its not set. Then look for some unknown variable called Arrays, and try to make it a string by trying to give it a string it does not want, and then finially check if it contains "var:"

    Actually think about what you're doing and use NamingConventions.
     
  4. Offline

    Europia79

    @mrpoopy345

    Sorry, i'm not sure what's going on with your conditional because I don't have all the code, but I do see that vars.add(args[0] + ":" + args[1]); That's means that there could potentially be duplicates. Imagine you run '/set foo bar' then '/set foo fighters'. this.vars now contains { "foo:bar", "foo:fighters" };

    It looks like you're using a Collection, List, or Set... But it would probably be better to use a Map. A Map has key-value pairs. The key would be the variable name. The value would obviously be it's value.

    Code:java
    1. Map<String, String> vars = new HashMap<String, String>();
    2. // and later....
    3. this.vars.put(args[0], args[1]);
    4. // also...
    5. String $value = this.vars.get($variableName); // $local $variables
    6. $value = ($value == null) ? "" : $value;


    Code:java
    1. } else if (args.toString().contains("var:") ) {
    2. }


    But I think this is wrong, because your first if-statement checks for the /set command... Therefore, any else-statements should be checking for other commands. And each of those commands could use a method to replace any "var:VariableName" with its value.

    You could also allow users to use "var:VariableName" in Chat... An Admin could do:
    Code:
    /set url http://ourServerWebsite.mc/
    # press t to open chat window:
    [Guest] <PlayerA> : What's the website ?
    [Admin] types:
    var:url
    # Chat looks like this:
    [Guest] <PlayerA> : What's the website ?
    [Admin] SuperStarXxX : http://ourServerWebsite.mc/
     
Thread Status:
Not open for further replies.

Share This Page