Need Help with Plugin Problem

Discussion in 'Plugin Development' started by MnMaxon, Aug 14, 2012.

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

    MnMaxon

    Code:
        public boolean request(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (checkKing(player) != 0) {
                plugin.getServer().getPlayer(args[1]).sendMessage(":)");
            } else {
                player.sendMessage(plugin.getConfig().getString("Kingdoms.1.King")
                        + " != " + player.getName());
                player.sendMessage("NOT KING :( " + checkKing(player));
            }
            return false;
        }
     
        public int checkKing(Player PLAYER) {
            int pKing;
            if (plugin.getConfig().getString("Kingdoms.1.King") == PLAYER.getName()) {
                pKing = 1;
            }
            return pKing;
        }
    request(CommandSender sender, Command cmd, String commandLabel, String[] args)
    Is activated when I type a coomand, and after typing it, I get:
    MnMaxon != MnMaxon
    Not King : ( 0

    Can someone please explain why I get those messages instead of : ) ?
    Thanks
     
  2. Offline

    Timr

    Instead of:

    Code:
    if (plugin.getConfig().getString("Kingdoms.1.King") == PLAYER.getName()) {}
    Try this:

    Code:
    if (plugin.getConfig().getString("Kingdoms.1.King").equals(PLAYER.getName())) {}
    String.equals() sometimes acts differently than the == operator, and I prefer to use it whenever possible.
     
    MnMaxon likes this.
  3. Offline

    Jogy34

    As an addition to this. I java comparing 2 strings with the == operator will never work. You have to use .equals() or .equalsIgnoreCase();
     
    MnMaxon and Timr like this.
  4. Offline

    MnMaxon

    Thank you it works.

    Thank you, I didn't know that, I thought they did the same thing.
     
  5. Offline

    Courier

    Not NEVER... technically. Using == compares the object references.
    Code:java
    1. String str1 = "test";
    2. String str2 = str1;
    3. String str3 = "test";
    4. String str4 = new String("test");
    For example, in that code, all of them are == except str4 (which is not == to any of them, despite having the same character representation). There could be a time when you want to check the references with ==, but it is not very common. This is true for all Objects. == is generally only for primitives and Enums.

    You usually want .equals() (or .equalsIgnoreCase(), in the case of Strings).
     
    MnMaxon likes this.
  6. Offline

    MnMaxon

    Thanks
     
Thread Status:
Not open for further replies.

Share This Page