ArrayList and AsyncChatEvent Error

Discussion in 'Programming' started by roimeiqui, Jan 15, 2018.

  1. Hello, I'm making a new plugin, but I have an error.

    Console error:
    Code (Text):
    [18:44:10 ERROR]: Could not pass event AsyncPlayerChatEvent to Test v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:509) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:491) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PlayerConnection.chat(PlayerConnection.java:965) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:905) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat$1.run(PacketPlayInChat.java:59) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_151]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_151]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_151]
            at me.roimeiqui.test.Test.onChat(Test.java:152) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_151]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_151]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_151]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_151]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]

    Code:
    Code (Text):

        @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
       
            if(e.getMessage().contains(prohibedWords.get(1))){
                prohibedWords.add(1, "test");
                p.sendMessage("Dont Say " + e.getMessage());
               
               
                e.setCancelled(true);
                this.getConfig().set("Players." + e.getPlayer().getUniqueId() + "." + e.getPlayer().getName(), e.getMessage());
                saveConfig();
            }
        }
    Help me please :(
     
  2. Choco

    Moderator

    0. Array (and consequentially, Collection) indexes start at 0, not 1
    1. You're calling List#get() at an index that does not yet exist. This throws an IndexOutOfBoundsException
    2. Your List should contain those "prohibited words" indefinitely. Shouldn't be any reason to modify it in the method. You're adding values after getting them.
     
  3. Now it works well, but there are an error

    Error:
    Code (Text):
    [21:39:34 ERROR]: Could not pass event AsyncPlayerChatEvent to Test v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:509) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:491) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PlayerConnection.chat(PlayerConnection.java:965) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:905) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat$1.run(PacketPlayInChat.java:59) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_151]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]
    Caused by: java.lang.NullPointerException
            at me.roimeiqui.test.Test.onChat(Test.java:160) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_151]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_151]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_151]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_151]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]

    Code:
    Code (Text):
    @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            prohibedWords.add(0, "test");
            Player p = e.getPlayer();
     
            if(e.getMessage().contains(prohibedWords.get(0))){
         
                p.sendMessage("Dont Say " + e.getMessage());
             
             
                e.setCancelled(true);
                this.getConfig().set("Players." + e.getPlayer().getUniqueId() + "." + e.getPlayer().getName(), e.getMessage());
                saveConfig();
            }
        }
     
  4. Try this:

    Code (Text):
    @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            prohibedWords.add("test");
            Player p = e.getPlayer();
     
            if(prohibedwords.contains(e.getMessage())){
       
                p.sendMessage("Dont Say " + e.getMessage());
           
           
                e.setCancelled(true);
                this.getConfig().set("Players." + e.getPlayer().getUniqueId() + "." + e.getPlayer().getName(), e.getMessage());
                saveConfig();
            }
        }
     
  5. Mas

    Mas

    Issues with your code:
    • You are adding the word "test" to the list each time a player chats. Instead, keep a final immutable list as an instance variable.
    • You only check if the word at the first element is contained within the message. You should loop through the list and check instead.p, for when you want to add more words.
    • Your error is on line 160, my guess is that you haven't ever initialised the "prohibitedWords" list.
    • When you set the config, you include both the player's UUID and their name in the path. There isn't any point to doing this because a UUID will only ever belong to one account.

    Stop spoonfeeding. Your code is just as broken and it's clear from your other posts you can't program effectively.
     
  6. Your code literally makes it even worse. What you're doing is checking whether the list of forbidden words contains the entire sentence. That's never gonna work unless the player literally only says a forbidden word. What he did first was correct, roughly, though he should loop over all forbidden words and then check if the message contains it.

    Please show us the FULL code and paste it in http://hastebin.com.
     
  7. @Mas, can you show me the correct code?
     
  8. Mas

    Mas

    No, we aren't here to spoon-feed you. Read the above post by MrDienns that outlines the fault in the logic of your code.