Help canceling chat event from another method

Discussion in 'Bukkit Help' started by IkeVoodoo, Aug 14, 2020.

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

    IkeVoodoo

    Hello, as the title suggest i am trying to cancel a chat event (AsyncPlayerChatEvnet) from another method

    protected void cancelEvent()
    {

    }

    now, i tried creating a variable "AsyncPlayerChatEvent ev" and assigning ev to e (where is is the AsyncPlayerChatEvent in onPlayerChat)

    and then tried:

    protected void cancelEvent()
    {
    ev.setCancelled(true);
    }

    now, when i go to chat i get:

    java.lang.NullPointerException:null

    at line 36 (the ev.setCancelled(true) line)

    Why does this happen? Any help would be apprecciated (i am relatively new to bukkit, so i don't know yet how to do this properly, technically it shoul've worked, but practically it did not)
     
  2. Online

    timtower Administrator Administrator Moderator

    @IkeVoodoo Why not pass the event to the function?
     
  3. Offline

    IkeVoodoo

    Oh, i need it because i am calling it from another class, i am making a coding language kind of like skript (personal challenge, probs going to publish tho) and i wanted to make it so that you can cancel the chat event
     
  4. Online

    timtower Administrator Administrator Moderator

    Never said that you should remove things.
    Said that you should pass the event along to the function as argument.
     
  5. Offline

    IkeVoodoo

    Like? What do you mean? Sorry i am a bit tired
     
  6. Online

    timtower Administrator Administrator Moderator

    cancelEvent(eventToBeCancelled){
    eventToBeCancelled.cancel();
    }
    And sleep, then try again
     
  7. Offline

    IkeVoodoo

    oh, wait... OH thanks!

    Edit: Wait no... i don't understand
     
  8. Online

    timtower Administrator Administrator Moderator

    Post your full classes
     
  9. Offline

    IkeVoodoo

    Code:
    import java.io.File;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class OnPlayerChatEvent implements Listener {
       
        private String playerName;
        private String playerDisplayName;
        private Player player;
        private String playerUUID;
        private String message;
        private AsyncPlayerChatEvent ev;
        private LanguageMain lm;
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            lm = LanguageMain.getInstance();
            if (!lm.scripts.isEmpty())
                for (File f : lm.scripts)
                    lm.getCodeBlock(f, lm.chat);
            playerName = e.getPlayer().getName();
            playerDisplayName = e.getPlayer().getDisplayName();
            player = e.getPlayer();
            playerUUID = e.getPlayer().getUniqueId().toString();
            message = e.getMessage();
            ev = e;
           
        }
       
       
        protected void cancelEvent(AsyncPlayerChatEvent e) {
            e.setCancelled(true);
        }
       
        protected AsyncPlayerChatEvent getEvent() {
            return ev;
        }
       
        protected String getPlayerUUID() {
            return playerUUID;
        }
       
        protected String getMessage() {
            return message;
        }
       
        protected String getPlayerName() {
            return playerName;
        }
       
        protected String getPlayerDisplayName() {
            return playerDisplayName;
        }
       
        protected Player getPlayer() {
            return player;
        }
    }
    Then the thing where it is called:

    Code:
    if(replaceSpaces(reading).equalsIgnoreCase("cancel event") && block == chat) {
        opce.cancelEvent(opce.getEvent());
    }
    opce is the OnPlayerChatEvent
     
  10. Online

    timtower Administrator Administrator Moderator

    @IkeVoodoo And why isn't the event just checking the replaceSpaces stuff?
    You are doing things the wrong way around, you should not store events, you should handle and forget.

    Can't cancel them out of the event anyways, then the messages has been send already.
     
  11. Offline

    IkeVoodoo

    Oh, then, could there be a way to call an event handler that cancells the message instead of cancelling outside? like just having an event handler with "e.setCancelled(true);" with the hiest priority?

    Also, the replace spaces is from when it reads the code blocks and finds something, i am doing that so no matter how many spaces you put its gonna work
     
  12. Online

    timtower Administrator Administrator Moderator

    @IkeVoodoo You have a "public void onPlayerChat(AsyncPlayerChatEvent e) {" there
    Do the checks in the function.
     
  13. Offline

    IkeVoodoo

    hmm
    wait i think i found way

    i made the check return a boolean, if it is true then cancel the event :D
     
Thread Status:
Not open for further replies.

Share This Page