Solved Run Interact Event only once

Discussion in 'Spigot Plugin Development' started by C0lA_K1nG, Jul 28, 2018.

  1. Hi,

    I'm working on a plugin where when a player steps on a stone pressure pad with certain MetaData they will auto issue a command. I have everything working properly, however when they step on a pressure pad they will continuously issue the command, and would be spammed. I'm wondering how would I go about to issue the interact event only once per step on the pressure pad. And to issue the event again they would have to step on the pressure pad again.

    API: Spigot 1.13
    Code (Java):
     @EventHandler
      public void onInteract(PlayerInteractEvent event)
      {
        Player player = event.getPlayer();
        if ((event.getAction() == Action.PHYSICAL) && (event.getClickedBlock() != null) && (event.getClickedBlock().getType() == Material.STONE_PRESSURE_PLATE) &&
          (this.p.getConfig().getString(this.p.locToString(event.getClickedBlock().getLocation()) + ".command") != null))
        {
          Boolean byconsole = Boolean.valueOf(Boolean.parseBoolean(this.p.getConfig().getString(this.p.locToString(event.getClickedBlock().getLocation()) + ".byconsole")));
          String command = this.p.getConfig().getString(this.p.locToString(event.getClickedBlock().getLocation()) + ".command");
          if (byconsole.booleanValue()) {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command.replace("%player%", player.getName()));
          } else {
            player.chat(command);
          }
        }
      }

    thanks :)
     
  2. Add a cooldown to the interaction using a Map<Player, Long>> (Or use UUID instance Player instance)? Or start a runnable when a player step on your defined pressure plate and if there are no more interaction for a while, remove the player from the map?
    Save the current timestamp (System#currentTimeMillis) and check every time an interaction happen if the player is inside, get the last timestamp and if the difference is lower than what you want, cancel it.

    Plus why you need to make my eye bleed?
    Code (Java):
    Boolean byconsole = Boolean.valueOf(Boolean.parseBoolean(this.p.getConfig().getString(this.p.locToString(event.getClickedBlock().getLocation()) + ".byconsole")));
    When you can just do
    Code (Java):
    boolean byconsole = Boolean.parseBoolean(this.p.getConfig().getString(this.p.locToString(event.getClickedBlock().getLocation()) + ".byconsole"));
    There is no need to wrap and unwrap that boolean so many times.
     
    • Winner Winner x 2
  3. Thanks a lot that fixed it. And thanks for the additional suggestion, I'll take a look into that as well