1.15.2 Im stuck trying to get an @EventHandler from another class

Discussion in 'Spigot Plugin Development' started by Xdlob9, Jan 31, 2020.

  1. Hello there.
    I've been trying to solve this problem for a month, which (probably) will be an absurd problem.
    Im trying to make an event work (an @EventHandler) from other class, but for any reason it doesn't work.
    The event (supposedly) occurs when you type a command (i.e. /command) and it activates a private chat, so the words that you type (for example, Hello There) needs to be send to the chat but only the people with an specific permission will see it.

    This is the @EventHandler code:
    Code (Java):
          @EventHandler
          public void onPlayerChat(AsyncPlayerChatEvent e)
          {
            Player p = e.getPlayer();
            if (this.privatechat.contains(p.getName()))
            {
              for (Player pl : Bukkit.getOnlinePlayers()) {
                if (pl.hasPermission("privatechat.receive")) {
                  pl.sendMessage(cc(plugin.getConfig().getString("PrivateC_ChatFormat").replace("$privatecprefix", plugin.getConfig().getString("PrivateC_Prefix")).replace("$player", p.getName()).replace("$displayname", p.getDisplayName()).replace("$privatemessage", e.getMessage())));
                }
              }
              e.setCancelled(true);
            }
          }
    Thanks in advice for the help!
     
  2. Did you register the class that this method is inside?
    If not, you will have to use
    Code (Java):
    Bukkit.getPluginManager().registerListeners(Listener, plugin instance);
     
  3. So you want to register the Event outside of your main class that extends JavaPlugin?
    Also if yo want to test whether the event triggers in general, you can just broadcast a message before all the checks.
     
  4. If you are refering if the class its registered in the Main class, yes.

    Yes, and thanks for that tip, I will probably use it.
     
  5. M
    Maybe you should check whether the instance of your main class you use to register the event is the same that was loaded by the server.
     
  6. Im using two lines of code to check the command and the event (I don't know if this is useful or not)
    Code (Java):
    getCommand("helperchat").setExecutor(new HelperChat(this));
        Bukkit.getPluginManager().registerEvents(new HelperChat(this), this);
     
  7. Oh, and also that Event will not get fired if a command is being executed, only if the player is typing a normal message.
    In order to check for Commands, you should use the PlayerCommandPreprocessEvent
     
    • Agree Agree x 1
  8. This is definitely the issue. Listening for an event after a command where both things happen at the same time won't work.
     
  9. Could you please tell me the code for do that action please? I know that the PlayerCommandPreprocessEvent can be used in the @EventHandler, the problem is that my event its an "onPlayerChat"

    If I reverse the action? I mean, first put the registerEvents and then the getCommand (but I think that this would not solve the problem)
     
  10. The order of how you register the event and the command doesn't matter. Just make sure the event can be triggered independantly of the command.
     
  11. I mean... if you want to handle a command, you should use the command interface:
    Code (Java):
    public class CustomCommandExecutor implements CommandExecutor {
        @Override
        public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
            // your code goes in here
            // you'll need to register your command both in your main class and your plugin.yml

        }
    }
    otherwise:

    Code (Java):
          @EventHandler
          public void onPlayerChat(PlayerCommandPreprocessEvent e)
          {
            Player p = e.getPlayer();
            if (this.privatechat.contains(p.getName()))
            {
              for (Player pl : Bukkit.getOnlinePlayers()) {
                if (pl.hasPermission("privatechat.receive")) {
                  // also, this chaining of methods is really prone to catch errors
                  pl.sendMessage(cc(plugin.getConfig().getString("PrivateC_ChatFormat").replace("$privatecprefix", plugin.getConfig().getString("PrivateC_Prefix")).replace("$player", p.getName()).replace("$displayname", p.getDisplayName()).replace("$privatemessage", e.getMessage().replaceFirst("/", "");)));
                }
              }
              e.setCancelled(true);
            }
          }
     
  12. Thanks for all the help guys, I will try all of your tips right now.
     
    • Friendly Friendly x 1
  13. @Schottky it keeps happening the same, even with your @EventHandler code.
    [​IMG]
     
  14. Wait so you actually have a command and you are not awaiting on a command.
    In that case, we need to see more code, your event should trigger normally. Did you give yourself the permission? Did you register the permission? Where did you register the event? Can you be sure that you store the right player in privateChats?
     
  15. Well, this is the first part of the code in my PrivateChat class:
    Code (Java):
    public class PrivateChat
        implements CommandExecutor
        {
     
        private ArrayList<String> privatechat = new ArrayList<String>();

        private final Main plugin;

        public PrivateChat(Main instance) {
            this.plugin = instance;
        }

     
    Im using the correct permissions.
    EDIT: Probably I should modify that, and, as well, in my Main Class there is a main private chat which works perfectly and its almost the same as the PrivateChat class code.
     
  16. So when do you add anything to the privateChat list?
    Also, make it re-implement Listener, not CommandExecutor...