Runing comand from AsyncPlayerChatEvent

Discussion in 'Plugin Development' started by paradocs1982, Jun 25, 2021.

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

    paradocs1982

    Hello, every one aim no to expiriance developer and need some help.

    Code:
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            String chatcheck = e.getMessage();
            if (e.getMessage().contains("silver")) {
                e.setCancelled(true);
                Bukkit.dispatchCommand(player, "clan create " + chatcheck);
            }   
        }
    and i recive a null exception pointing to a command.
    i try to make menu for my clan plugin that after pushin button in menu system will wait for the clan name that player have to input on chat
    maybe I started on the wrong side but that was my first idea
     
  2. Offline

    Kars

    Check for nulls on e.getPlayer and e.getMessage before referencing them.
    PHP:
    Player player e.getPlayer();
    if (
    player == null) return;

    String msg e.getMessage();
    if (
    msg == null) return;
     
  3. Offline

    paradocs1982

    nope, null check didn't help
    i have try also to make a method instead of dispatchCommand direcly but also did not help.
     
  4. Offline

    c7dev

    In your code, the command is running, it's just that it's an asynchronous task (meaning it's on a different thread than the rest of the server). This shown by the "Async" in "AsyncPlayerChatEvent." To ensure thread safety, and also get rid of the error in the console, you can schedule it to be executed synchronously:
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getPlugin(Main.class), new Runnable() {
                    public void run() {
                        Bukkit.dispatchCommand(player, "clan create " + chatcheck);
                    }
                });
     
    KarimAKL likes this.
  5. Offline

    KarimAKL

    Shqep likes this.
  6. Offline

    paradocs1982

    thx i didnt think of it :p
     
  7. Offline

    paradocs1982

    ok works for me thx
     
    Last edited: Jul 1, 2021
Thread Status:
Not open for further replies.

Share This Page