Why will the code not run after this?

Discussion in 'Plugin Development' started by MayoDwarf, Sep 21, 2013.

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

    MayoDwarf

    Why will the code not run after the getting the string if statement?

    Code:
        @EventHandler
        public void onPlayerChallenge(EntityDamageByEntityEvent evt) {
           
            Entity target = evt.getEntity();
            Entity selector = evt.getDamager();
            if(selector instanceof Player) {
                if(target instanceof Player) {
            Player t = (Player) target;
            Player s = (Player) selector;
            s.sendMessage(ChatColor.GREEN + "Challenged player to a match!");
            t.sendMessage(ChatColor.AQUA + "" + s.getName() + ChatColor.GREEN + " " + ChatColor.GREEN + "has challenged you to a match!");
            config.set("" + s.getName(), "" + t.getName());
            saveConfig();
            if(config.getString(t.getName()) == s.getName()) {
     
  2. Offline

    chasechocolate

    Use .equals() or .equalsIgnoreCase() to compare strings.
     
  3. Offline

    MayoDwarf

    How do I check if it is equal to the player tho?
     
  4. Offline

    LaxWasHere

    Check if it's instance of a player.
     
  5. Offline

    MayoDwarf

    Still won't work.
    LaxWasHere
    chasechocolate
    Here is what I have:
    Code:
        @EventHandler
        public void onPlayerChallenge(EntityDamageByEntityEvent evt) {
         
            Entity target = evt.getEntity();
            Entity selector = evt.getDamager();
         
            if(selector instanceof Player) {
                if(target instanceof Player) {
            Player t = (Player) target;
            Player s = (Player) selector;
            if(Spawn.contains(s)) {
             
            } else {
                evt.setCancelled(true);
            }
         
            s.sendMessage(ChatColor.GREEN + "Challenged player to a match!");
            t.sendMessage(ChatColor.AQUA + "" + s.getName() + ChatColor.GREEN + " " + ChatColor.GREEN + "has challenged you to a match!");
            config.set(s.getName(),t.getName());
            saveConfig();
            if(config.equals(t.getName() == s.getName())) {
     
  6. Offline

    FurmigaHumana

    Code:java
    1. if(config.getString(t.getName()).equals(s.getName())) {
     
  7. Offline

    MayoDwarf

    Well I fixed that part but now I made it so that it would check what the item in hand is and if it was a blaze rod it would run the code but it wont... Why not?

    Code:java
    1. @EventHandler
    2. public void onPlayerChallenge(EntityDamageByEntityEvent evt) {
    3.  
    4. Entity target = evt.getEntity();
    5. Entity selector = evt.getDamager();
    6.  
    7. if(selector instanceof Player) {
    8.  
    9. ItemStack Blaze = new ItemStack(Material.BLAZE_ROD);
    10. if(((Player) selector).getItemInHand() == Blaze) {
    11. if(target instanceof Player) {
    12. Player t = (Player) target;
    13. Player s = (Player) selector;
    14. if(Spawn.contains(s)) {
    15.  
    16. } else {
    17. evt.setCancelled(true);
    18. }
    19.  
    20. s.sendMessage(ChatColor.GREEN + "Challenged player to a match!");
    21. t.sendMessage(ChatColor.AQUA + "" + s.getName() + ChatColor.GREEN + " " + ChatColor.GREEN + "has challenged you to a match!");
    22. config.set(s.getName(),t.getName());
    23. saveConfig();

    FurmigaHumana LaxWasHere chasechocolate

    Yah can someone please tell me why it's not working? Thanks

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

    metalhedd

    The same reason as last time. you can't use == to compare two objects. it will only be true if the variables point to the exact same instance in memory. (in your case they don't, ever).
     
  9. Offline

    FurmigaHumana


    Change this:

    Code:java
    1. ItemStack Blaze = new ItemStack(Material.BLAZE_ROD);
    2. if(((Player) selector).getItemInHand() == Blaze) {


    To this:

    Code:
    if(((Player) selector).getItemInHand().getType() == Material.BLAZE_ROD) {
     
Thread Status:
Not open for further replies.

Share This Page