Solved Need help with making a sign do something upon clicking

Discussion in 'Plugin Development' started by HYPExMon5ter, Dec 30, 2015.

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

    HYPExMon5ter

    Okay so I am working on a plugin and I need to make these signs do things upon clicking if you have a [Gapple] at the top or a [Str2] at the top, and basically I have everything working EXCEPT I am checking to see if a player has a x amount of diamonds in they're inventory and if they have less than they are supposed to than return and send them a message but upon clicking one of the signs i get both messages and im only supposed to get the one for that sign so if anyone could please help ASAP it would be amazing, thanks. Here is my code if you need it.

    Code:
        @EventHandler
        public void onSignChange(SignChangeEvent e) {
                if (e.getLine(0).equalsIgnoreCase("[Gapple]")) {
                    e.setLine(0, "§4[Gapple]");
                    e.setLine(1, "§4Click to get §c1");
                    e.setLine(2, "§6Gapple §4for §c8");
                    e.setLine(3, "§bDiamonds§4!");
                }
        }
      
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (e.getClickedBlock().getState() instanceof Sign) {
                Sign s = (Sign) e.getClickedBlock().getState();
                if (!(p.getInventory().containsAtLeast(new ItemStack(Material.DIAMOND), 8))) {
                    p.sendMessage("§4You need §c8 §bDiamonds §4to buy a §6Gapple§4!");
                    return;
                }
                if (s.getLine(0).equalsIgnoreCase("§4[Gapple]")) {
                    Bukkit.getServer().dispatchCommand(console, "clear " + p.getName() + " diamond 8");
                    Bukkit.getServer().dispatchCommand(console, "give " + p.getName() + " 322:1 1");
                    return;
                }
            }
        }
        @EventHandler
        public void onSignChange2(SignChangeEvent s) {
                if (s.getLine(0).equalsIgnoreCase("[Str2]")) {
                    s.setLine(0, "§4[Stength 2]");
                    s.setLine(1, "§4Click to get §c1");
                    s.setLine(2, "§6Str2 pot §4for");
                    s.setLine(3, "§c2 §bDiamonds§4!");
                }
        }
      
        @EventHandler
        public void onPlayerInteract2(PlayerInteractEvent i) {
            Player p2 = i.getPlayer();
            if (!(i.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                return;
            }
            if (i.getClickedBlock().getState() instanceof Sign) {
                Sign s2 = (Sign) i.getClickedBlock().getState();
              
                if (!(p2.getInventory().containsAtLeast(new ItemStack(Material.DIAMOND), 2))) {
                    p2.sendMessage("§4You need §c2 §bDiamonds §4to buy a §6Stength 2 Potion§4!");
                    return;
                }
              
                if (s2.getLine(0).equalsIgnoreCase("§4[Stength 2]")) {
                    Bukkit.getServer().dispatchCommand(console, "clear " + p2.getName() + " diamond 2");
                    Bukkit.getServer().dispatchCommand(console, "give " + p2.getName() + " 373:8233 1");
                    return;
                }
            }
        }
    }
     
  2. Offline

    KingFaris11

    I don't see why you can't just merge both your Interact methods. Also, you have to send the message within the if statement block for checking if the sign is the correct sign.

    I.e. Currently, you're sending the message regardless of the sign's text.

    Move your containsAtLEast block inside the getLine(0).equalsIgnoreCase() block for both methods.

    Just a heads up, you spelt 'Strength' wrong in your code.
     
  3. Offline

    HYPExMon5ter

    Lol thanks for letting me know about the strength error and for fixing my code :p
     
Thread Status:
Not open for further replies.

Share This Page