Solved AsyncPlayerChatEvent can someone help me please

Discussion in 'Spigot Plugin Development' started by ArKeid0s, Feb 12, 2020.

  1. So i have an error with my player.getNearbyEntities here is the code and the error

    Code (Java):
        @EventHandler
        public void playerChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();

            String rawMessage = e.getMessage();
            String message = "<" + e.getPlayer().getDisplayName() + "> " + rawMessage;
            List<Entity> nearbyEnts = player.getNearbyEntities(25,25,25);
            for(Entity ents : nearbyEnts) {
                if(ents instanceof LivingEntity) {
                    if(ents instanceof Player) {
                        Player nearbyPlayers = (Player) ents;
                        nearbyPlayers.sendMessage(message);
                    }
                    else {
                        int randomNum = (int) ((Math.random() * 100) + 1);
                        if (randomNum <= 60) {
                            player.sendMessage("§oThe wind passes by...no one hears you...");
                        }
                    }

                }
            }

            e.getRecipients().clear();


        }
    Code (Text):
    12.02 15:46:41 [Server] Async Chat Thread - #16/ERROR Could not pass event AsyncPlayerChatEvent to HeoZRadio v0.1
    12.02 15:46:41 [Server] INFO org.bukkit.event.EventException: null
    12.02 15:46:41 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:508) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at net.minecraft.server.v1_15_R1.PlayerConnection.chat(PlayerConnection.java:1579) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1517) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at net.minecraft.server.v1_15_R1.PacketPlayInChat$1.run(PacketPlayInChat.java:41) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]
    12.02 15:46:41 [Server] INFO Caused by: java.lang.IllegalStateException: Asynchronous getNearbyEntities!
    12.02 15:46:41 [Server] INFO at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity.getNearbyEntities(CraftEntity.java:495) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO at fr.arkeidos.heozradio.EventsClass.playerChat(EventsClass.java:61) ~[?:?]
    12.02 15:46:41 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_232]
    12.02 15:46:41 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 15:46:41 [Server] INFO ... 11 more
    Thanks in advance
     
  2. can't call bukkit methods from asyncplayerchatevent, the event is always called asynchronously.
    Code (Text):
    12.02 15:46:41 [Server] INFO Caused by: java.lang.IllegalStateException: Asynchronous getNearbyEntities!
    put all your event logic regarding the bukkit api into a task scheduled with the server.
     
  3. It‘s called Async for a reason. You should not use Bukkit-Methods in asynchronous threads (exceptions apply). Schedule a Task that will be executed after 1 tick or synchronize otherwise then access these Bukkit-Methods
     
  4. i will try that
     
  5. so, i tried that but i have an error
    Code (Java):
        @EventHandler
        public void playerChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            e.getRecipients().clear();
            String rawMessage = e.getMessage();
            String message = "<" + e.getPlayer().getDisplayName() + "> " + rawMessage;
            chatScheduler(player, e, message);
        }
       
        public void chatScheduler(Player player, AsyncPlayerChatEvent e, String message) {
            Bukkit.getServer().getScheduler().runTaskLater(main, new Runnable() { //scheduleSyncDelayedTask(main, new Runnable() {
                @Override
                public void run()
                {
                    List<Entity> ents = player.getNearbyEntities(25, 25, 25);
                    for(Entity nearbyPlayers : ents) {
                        if(!(nearbyPlayers instanceof Player)) {
                            ents.remove(nearbyPlayers);
                        }
                    }
                    if(ents.size() != 0) {
                        for(Entity players : ents) {
                            players.sendMessage(message);
                        }
                    }
                    else {
                        e.getPlayer().sendMessage("§oThe wind passes by...no one hears you...");
                    }
                }
            }, 1);
        }
    Code (Text):
    12.02 16:22:59 [Server] Server thread/WARN [HeoZRadio] Task #226176 for HeoZRadio v0.1 generated an exception
    12.02 16:22:59 [Server] INFO java.util.ConcurrentModificationException: null
    12.02 16:22:59 [Server] INFO at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909) ~[?:1.8.0_232]
    12.02 16:22:59 [Server] INFO at java.util.ArrayList$Itr.next(ArrayList.java:859) ~[?:1.8.0_232]
    12.02 16:22:59 [Server] INFO at fr.arkeidos.heozradio.EventsClass$1.run(EventsClass.java:70) ~[?:?]
    12.02 16:22:59 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftTask.run(CraftTask.java:81) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 16:22:59 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:394) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 16:22:59 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.b(MinecraftServer.java:1036) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 16:22:59 [Server] INFO at net.minecraft.server.v1_15_R1.DedicatedServer.b(DedicatedServer.java:399) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 16:22:59 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:984) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 16:22:59 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:824) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    12.02 16:22:59 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]
     
  6. INFO at fr.arkeidos.heozradio.EventsClass$1.run(EventsClass.java:70) ~[?:?]
    What's the line 70?
     
  7. You can't modify iterated collection. Use Iterator<Entity> or iterate it via for(int i...)
     
  8. Or use List#removeIf(Predicate)
     
    • Agree Agree x 1
  9. drives_a_ford

    Moderator

    Or you can use the World#getNearbyEntities with the predicate argument to only list entities which are players.
     
  10. first of all what is my error ? why does mc throw an error ?
    and line 70 is : for(Entity nearbyPlayers : ents) {
     
  11. You are modifying the contents of a collection while iterating over that collection.
     
  12. so, what are my solutions to fix that ?
     
  13. Read the three answers on top of your question:
    1) Use Iterators
    2) Use World-getNearbyEntities() with a predicate
    3) Use List#removeIf
     
  14. you call iterators for(int i ....) ?
    what is a predicate ?
     
  15. Strahan

    Benefactor

    I'd also add that there is no need to pass the chat event to the function with the runnable; you are already passing the Player and that's all you are using the event for in the function. Also use ChatColor.ITALIC instead of embedding the color character.
     
  16. What do you mean by that? This would be an example applicable to your problem for an iterator
    Code (Text):
    Iterator<Entity> iter = nearbyEnts.iterator();
    while(iter.hasNext()) {
        final Entity ent = iter.next();
        if (ent instanceof Player) {
           iter.remove();
        }
    }
    A predicate is a functional interface that allows you to define a boolean function that takes one argument. Here's what we mean:

    Only gets entities that are of type player:
    Code (Java):
    world.getNearbyEntities(loc, 5, 5, 5, entity -> entity instanceof Player);
    Removes if entity is not of type player:
    Code (Java):
    ents.removeIf(entity -> !(entity instanceof Player))
    Edit: Also, no need to cast your random value to an integer:
    Code (Java):
    if (Math.random() <= 0.6) {
       
    }
     
  17. why for the chatcolor ? it is so annoying to always write ChatColor etc...
    ok thx @Schottky i will try that
     
  18. ChatColor is more version-stable and can be used for console output as well (just to mention two advantages)
     
  19. Code cleanup time

    Code (Java):


        @EventHandler
        public void playerChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            e.getRecipients().clear();
            String rawMessage = e.getMessage();
            String message = "<" + e.getPlayer().getDisplayName() + "> " + rawMessage;
            chatScheduler(player, e, message);
        }

        public void chatScheduler(Player player, AsyncPlayerChatEvent e, String message) {
            Bukkit.getServer().getScheduler().runTaskLater(main, new Runnable() { //scheduleSyncDelayedTask(main, new Runnable() {
                @Override
                public void run()
                {
                    List<Entity> ents = player.getNearbyEntities(25, 25, 25);
                    if (ents.size() == 0) {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&oThe wind passes by...no one hears you..."));
                        return;
                    }
                    ents.stream().filter(e -> e instanceof Player).forEach(p -> p.sendMessage(ChatColor.translateAlternateColorCodes('&', message)));
                }
            }, 1);
        }

     
     
    • Useful Useful x 1