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
else if(Arrays.toString(args).contains("var:")) {
is either never executing or always returning false. Why is this, and how can I fix it?
Main plugin class:
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:")) { //Line problem is on
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);
}
}
EDIT: The way I know it is a problem with my else if is that if I add
sender.sendMessage("test"); right under the elseif I never get the message "test" even when I have var: in my args.
EDIT 2: I've figured out one part of it. For some reason whenever I do something like /say or /broadcast the onCommand doesn't get fired...
It seems that your problem is not with the
else ifbut with the innermostif. You compare the variable name of your command (foo) with the previously stored value (bar). Try instead:Of course, it would be even better to use more appropriate collection types. For example
varsmight be aMap<String, String>.