First Bukkit Plugin Please Help!!!!!!!!!

Discussion in 'Plugin Development' started by xGamingGeneration, May 21, 2011.

Thread Status:
Not open for further replies.
  1. Ok So I Feal REEEEEAAAALLLLYYYY Stupid Asking This But Right Now One Of The Functions Of My New Plugin Is To Say BLAHBLAH Joined The Server! I Want It To Display Custom Messages For Certain People! I Have Variable P(The Persons Display Name) and Variable MSG(The Message It Will Say) So Currently The Code Says When Someone Joins Make MSG = P And Say P Has Joined The Server (This Part Works)How Can I Make It Say... Does P = GamingGeneration (My Username)? It Does! MSG = Hello Awesome Host! I Tried This (This Part Doesn't Work)

    Code:
    if (p.getName.toLowerCase() == "gaminggeneration"){
            msg = ChatColor.GREEN + "The Amazing Server Host " + ChatColor.BLUE + "(" + ChatColor.RED + n + ChatColor.BLUE + "}" + ChatColor.GREEN + " Left The Server " + ChatColor.RED + ":( " + ChatColor.GREEN + "He Will Be Missed!";
            }else{
                msg = ChatColor.GREEN + n + ChatColor.RED + " Has Left The Server... :(";
            }
    Although When I run It It Doesn't Think My Name is GamingGeneration and always shows the default message!!!! Help!!!! (This was the onLeave code because I already changed the onJoin back to regular)
     
  2. Offline

    boriater

    Putting yourself into the thing preliminarily without using a config is probably not that great of an idea, because if someone uses your plugin and you log-on, it'll show that message for them. Also using capitals every five letters, and failing to continue it is annoying. Using them anyways is still annoying, using too many exclamation marks is annoying too. And is p.getname included in Bukkit/Craftbukkit?
     
  3. it doesn't give me any errors and when i print the line in the console using p.getName() it says GamingGeneration but when I put it in an if command it doesn't work

    You have to insert p as the player and then get his name from that variable, and I know using caps a lot is annoying it's a habit.

    Player p = event.getPlayer();

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

    garbagemule

    While we're talking about caps and upper case, take a look at how strings are represented in Java. You may have noticed that when comparing integer variables, you can simply do this:
    Code:
    i == j
    This is because integers are one of the eight primitive data types (int, float, double, short, long, char, boolean). Strings are not one of these. What gives this away is that String is with a capital first letter, indicating that it is a class, and you can't compare class objects with the == operator. Why? Because when you call Player.getName(), a new String object is created (Strings are immutable, meaning its state, and thus the characters it consists of, cannot be altered), meaning a new block of memory is allocated, and the String data is copied to that block. Basically, you now have two blocks in your memory with the same information, but the memory addresses are different. When you write "blabla == "newString", a third block of memory is allocated to store the "newString" data.

    The == operator compares pointers, i.e. memory addresses. So when you write p.getName.toLowerCase() == "gaminggeneration", what happens is that Java compares the memory addresses of two String objects, which will always be different to each other.

    So how do you compare String objects in Java? If you have a look in the Java documentation, you'll find the equals-method on the String class. This method basically compares the character values of the two String objects. Here's how your code should look:
    Code:
    p.getName().equals("gaminggeneration")
    Alternatively, instead of first calling toLowerCase, you can use the case-insensitive version of equals, equalsIgnoreCase.

    If you knew all this before reading this post, I apologize for assuming you didn't! But if not, I hope it was helpful :)
     
Thread Status:
Not open for further replies.

Share This Page