PlayerInteractEvent Firing Twice on 1.8

Discussion in 'Plugin Development' started by aliano99, Aug 23, 2016.

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


    Hey guys, I've been having this issue for ages and I can't seem to get rid of it. For some reason when I right click the sign the code runs twice. I'm still not sure why - any ideas? Thanks!
    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import java.util.Random;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.RegisteredServiceProvider;
        public class ShopListener2 extends JavaPlugin implements Listener {
            public static Economy econ = null;
            public static EconomyResponse r;
            public void onEnable() {
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
                if (!setupEconomy() ) {
                    getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getLogger().info("Plugin has been enabled!");
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
            private boolean setupEconomy() {
                if (getServer().getPluginManager().getPlugin("Vault") == null) {
                    return false;
                RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
                if (rsp == null) {
                    return false;
                econ = rsp.getProvider();
                return econ != null;
            public void onSignChange(SignChangeEvent e) {           
                    if (e.getLine(2).equalsIgnoreCase("Investment")) {
                            e.setLine(0, "§1[Investment]");
                            e.setLine(1, "§6$150 Return");
                            e.setLine(2, "Chance:50%");
                            e.setLine(3, "§4Invest $100");
            public void Invest(PlayerInteractEvent e) {
                Block block = e.getClickedBlock();
                    if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                       if(block.getType() == Material.SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN) {
                             Sign sign = (Sign) e.getClickedBlock().getState();
                             if(sign.getLine(2).contains("Chance:50%")) {
                                   e.getPlayer().sendMessage("$100 Has Been Withdrawn");
                                    Player p = e.getPlayer();
                                   r = econ.withdrawPlayer(p, 100.00);
                                   if (r.transactionSuccess()) {
                                   Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "eco give aliano99 100");
                                   p.sendMessage(ChatColor.GREEN + "Investment has been sent to government...");
                                   Random rand = new Random();
                                   int number = 0;
                                   number = rand.nextInt(2);
                                   if(number == 0){
                                       p.sendMessage(ChatColor.GREEN + "Your investment was a success! You have returned %150");
                                       r = econ.depositPlayer(p, 150.00);
                                       p.sendMessage(ChatColor.RED + "Your investment failed! ");
  2. Offline


    I have an idea:

    Create a cooldown using a runnable. Use a HashMap to store whether the player's cooldown is complete. Every time they click the sign, check whether their cooldown is complete. If not, do nothing. If it is, run it, set cooldown complete to false, and start a delayed task that will set cooldown complete to true after a few ticks.
    Last edited by a moderator: Aug 23, 2016
  3. Offline

    timtower Administrator Administrator Moderator

    @aliano99 Don't register the events twice if you don't want them to run twice.
Thread Status:
Not open for further replies.

Share This Page