Why is it sending both messages?

Discussion in 'Spigot Plugin Development' started by glasseater84, May 14, 2016.

  1. Hey guys, recently I made a thread about something similar. And It worked what they told me to do. However, I changed up my code a bit, and now it does not work again. Basically, I want to send the player a message if they do not have enough coins to buy the rank. And a message saying they already have this rank, if they already have it. This is what my code looks like:

    Code (Text):
       private void sqlCheck(Player player, String rank) {
            if (SQL.getValue(player.getName(), "coins") >= 15000) {
                SQL.buyRank(player.getName());

                McMyths.chat().sendMessage(player, "You bought the §" + rank + " §7rank!");
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),
                        "removemoney " + player + " 15000");
            } else {
               
                player.sendMessage("§7You need 15000 coins to buy this rank!");
           
            }
        }
    }
    And the click event:
    Code (Text):
        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Farmer")) {
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                if (e.getWhoClicked().hasPermission("rank.farmer"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
               
                sqlCheck((Player) e.getWhoClicked(), "Farmer");
                return;
               
            } else

            /*
             * SECOND
             */
    I do it for all 5 ranks, thats why there is an else :)

    Thanks in advance
     
  2. Can you try to test it by having 25000 coins and then have the permission "rank.farmer" at the same time?
    Doubt it would only send the already have a rank message
     
  3. Inkzzz

    Resource Staff

    Fix your code conventions.
     
  4. It didn't send both messages, but after I bought it and clicked it again, it didn't say I already have the rank
     
  5. Did you do it while having the permission?
    Hmm, i thought it should only send this according to the code you have here
    Code (Text):
    if (e.getWhoClicked().hasPermission("rank.farmer"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
     
  6. Code (Text):
        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Farmer")) {
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                if (e.getWhoClicked().hasPermission("rank.farmer"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
                *else* sqlCheck((Player) e.getWhoClicked(), "Farmer");
                return;
           
            } else

            /*
             * SECOND
             */

    sqlCheck() was being run regardless of if the player already had the rank. i'm not sure if this is the problem, but this is a big problem. try adding in the else there.
     
    #6 akselm, May 14, 2016
    Last edited: May 14, 2016
    • Agree Agree x 2
  7. Yeah I added the permission and its still sending both messages
     
  8. I already have an else in the SQLcheck?
     
  9. Hmm in this case, try to debug, *I usually use remote debugging* add some breakpoint to see what has been executed in SQLcheck() and the EH
     
  10. well in your original post its missing it, i modified the code in the quote in my post to show what i mean. sorry for not being clear.
     
  11. I would just change this:
    Code (Text):

                if (e.getWhoClicked().hasPermission("rank.farmer"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
               
                sqlCheck((Player) e.getWhoClicked(), "Farmer");
     
    To this:
    Code (Text):

                if (!e.getWhoClicked().hasPermission("rank.farmer")) {
                    sqlCheck((Player) e.getWhoClicked(), "Farmer");
                }
                else {
                    e.getWhoClicked().sendMessage("You already have this rank!");
                }
     
    Something like that anyway. The way your code is now its always going to call the sqlCheck method regardless of if they have already purchased the rank or not.
    Another option would be to move the permission check to the sqlCheck method as well.
     
    • Agree Agree x 1
  12. Is that considered spoonfeeding?
     
  13. Most likely :oops:
     
    • Funny Funny x 1
  14. Hmm well I think that works, just its not adding the perm, if I do it through my code. If i do it by hand it works.
    This is how i am adding it through my code
    Code (Text):
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + player + " add" + " " + "rank." + rank.toLowerCase());
    Its saying it was added but its not adding
     
  15. What is "player" here? Is it a player instance? If so you need to call getName() on it.
     
  16. Very stupid mistake on my part :p
    Now, if I wanted to make each rank a separate price, would I be able to do that in the SQLcheck method? I would like to keep it as organized as possible. :p Thanks!
     
  17. Easy fix.
    Code (Text):
       
    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Farmer")) {
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                if (e.getWhoClicked().hasPermission("rank.farmer")){
                    e.getWhoClicked().sendMessage("You already have this rank!");
               
                sqlCheck((Player) e.getWhoClicked(), "Farmer");
                return;
               
            } else

            /*
             * SECOND
             */
     
  18. Im sorry, I don't understand what you mean? Is there a way to keep it in the sqlCheck method?
     
  19. are u using vault? this would be much easier tbh (for removing the coins from players balance.)
     
  20. Nope :/ custom currency