Incorrect Event Activation

Discussion in 'Plugin Development' started by cococow123, Dec 2, 2015.

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

    cococow123

    Hello!

    I am currently making a voting plugin.
    When u click a sign with
    Code:
    [mVote]
    ID;
    it is supposed to add a file with your name as the filename.
    It will add what you voted (4th line on sign)
    And the player's UUID.

    Problem I'm Having:

    My Code:
    Code:
            @EventHandler
        public void SignClick(PlayerInteractEvent e) throws IOException {
             
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) //Checks if person is right clicking on a block
                if (e.getClickedBlock().getState() instanceof Sign) { //This checks if the player is right clicking on the sign and it'll do all the code below if it is a sign.
                 
                    Sign sign = (Sign) e.getClickedBlock().getState();
                    if(sign.getLine(0).equalsIgnoreCase("[mVote]") && (sign.getLine(1).contains("ID:")));
                     
                         
                            String[] ids = sign.getLine(1).split(": ");
                            String id = ids[1];
                     
                            final String pre = ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "mVote" + ChatColor.DARK_GRAY + "] ";
             
    // CLICKED CREATE SIGN             
                 
                         
                            File idss = new File("plugins" + File.separator + "mVote" + File.separator + "votes" + File.separator + id + File.separator +  id + ".yml");
                            FileConfiguration idd = YamlConfiguration.loadConfiguration(idss);
    
                            e.getPlayer().sendMessage(pre + ChatColor.YELLOW + "Hosted by: " + ChatColor.AQUA + idd.getString("host") );
                            e.getPlayer().sendMessage(pre + ChatColor.YELLOW + "Vote Poll Ends: " + ChatColor.AQUA + idd.getString("end-date") );
                            
    When I click the OPTION sign, it gives a stacktrace:

    Code:
    [18:05:54] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to mVote v4.3
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-6d16e64-87b9f46]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-6d16e64-87b9f46]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:227) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:736) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-6d16e64-87b9f46]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.cococow123.vote.SignListener.SignClick(SignListener.java:143) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-6d16e64-87b9f46]
        ... 17 more
    [18:11:57] [Server thread/INFO]: Stopping the server
    
    For INFO signs it has
    Code:
    [mVote]
    ID:
    (notice the ':' when the option signs have ';')

    For some reason the code gets activated EVEN though it checks if line 2 contains
    "ID:" (INFO SIGN, not OPTION SIGN)

    I don't know how to fix this.. please help!

    Thanks in advance,
    Coco Cow
     
  2. Offline

    Mrs. bwfctower

    What could be the cause of that?
     
  3. Offline

    cococow123

    @Mrs. bwfctower Was that sarcasm? And for some reason I think it like skips the "contains(":");"
    and goes straight to splitting the string. I think it gives the error because it can't split the string because it doesn't have ":"
     
  4. Offline

    Mrs. bwfctower

    @cococow123 Look at the stack trace. What line is it on? What could be causing an ArrayOutOfBoundsException on that line?
     
  5. Offline

    cococow123

    @Mrs. bwfctower Line 143.. Like I said, I think it is skipping the contains check or something.
     
  6. Offline

    Mrs. bwfctower

     
  7. Offline

    cococow123

  8. Offline

    Mrs. bwfctower

  9. Offline

    cococow123

  10. Offline

    mythbusterma

    @cococow123

    He (she?) showed you exactly what line is causing your error (or tried to, anyway).

    I'm willing to bet this is line with the error:

    As I'm pretty sure if the line doesn't exist, it returns an empty String.
     
  11. Offline

    Mrs. bwfctower

    Look at the javadoc link I posted.
    But yeah, the line I quoted was the wrong line.

    Also
     
  12. Offline

    cococow123

    @mythbusterma ohh.. i see. In mc for me though, it says "ID; 1"
     
  13. Offline

    mythbusterma

    @Mrs. bwfctower

    I did, and it's vague. I'm assuming "line does not exist" implies index > 4, as I seem to recall getting empty lines in my experience.

    @cococow123

    So you're relying on the user to make sure they format the sign correctly, then dumping a non-descript error to the console if they don't?

    You should provide the user immediate feedback in chat if they create the sign wrong. Besides, you separate based on colons but you say your sign has semicolons, those are not the same thing.
     
    Mrs. bwfctower likes this.
  14. Offline

    cococow123

    @mythbusterma @Mrs. bwfctower Also.. why is it activating the info sign code if it has ';' when I click it?

    @mythbusterma Im not relying on the player to type it accurately.. I have another event that sets it to have a new id and text on 2nd line.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 2, 2015
  15. Offline

    Mrs. bwfctower

    @cococow123 Are you sure it's a semicolon? They look awfully similar.
     
  16. Offline

    cococow123

    @Mrs. bwfctower pretty sure.. thats why I use them to distinguish info and option signs (sneaky trick of mine)
     
  17. Offline

    Scimiguy

    But you're not even using a contains check?
     
  18. Offline

    cococow123

    @Scimiguy I am using a contains check. It's on the same line as checking for [mVote] on line 1
     
  19. Offline

    Mrs. bwfctower

     
  20. Offline

    Zombie_Striker

    Why would you do this? Just add a try/catch around the area that throws it, not the whole entire method.
    What if the block is air (null)? This would through a NPE. Check to make sure the clicked block is not null.

    I don't think you fully understand what this line is actually doing. That semicolon means this line does nothing. Also, if the first line has ANY difference (a space at the end), it would not work.

    You should make sure there are two sections to this. What if the player did not add add an ID? Also, what if the player did not add the space? Without a space, there would not be a second arg; the first arg would contain both the "ID:" and the actual ID.

    There is no reason for this to be created more than once. Move this out of the method.

    This also has no need to be created each and every time. Move this out of the method as well.
     
    cococow123 likes this.
  21. Offline

    cococow123

    @Zombie_Striker thanks for replying!

    1) I'll add try and catch tomorrow.

    2) I'll add null check also

    3) this only a snippet if my code, it should automatically generate the sign with the right ID when the sign says
    Code:
    [mVote]
    create
    Also, should I have brackets instead of a semi-colon in ALL if statements? Or only that one?
     
  22. Offline

    Scimiguy

    If you don't have brackets, how will java know what you mean?

    The colon just tells java you want it to finish doing whatever its currently doing on that line

    Hence the reason i said you're not using an if statement.

    So all java sees is:

    If (true) {}
     
  23. Offline

    cococow123

    @Scimiguy
    Here is my problem in detail:

    OK. There are 2 signs: an info sign and an option sign

    I click the info sign: everything works fine (code above is info sign code)

    I click the option sign:
    It activates the info sign code or something and gives a stack trace.

    The problem: When clicking an option sign it gives a stack trace AND the stack trace error points to a line of code that ISN'T supposed to be run because it is for when someone clicks an info sign.. Not option sign.
     
  24. Offline

    teej107

    I don't see any thing that would throw an IOException. He could just remove that part entirely.


    @cococow123 you should actually just store the configuration Object in a field rather than load it everytime. (which could be inaccurate if you don't save the changes).
     
  25. Offline

    cococow123

    @Scimiguy @teej107

    Sorry for nooby question ( I haven't worked with signs in bukkit before)
    Can you set up a template for me with signs so I can get a basic idea of bukkit signs?
     
  26. Offline

    Scimiguy

    Zombie_Striker and teej107 like this.
  27. Offline

    teej107

  28. Offline

    cococow123

    @teej107 @Scimiguy OK then. I figured ppl would say that. Right now I'm learning java from thenewboston's videos and on Codeacademy.
     
  29. Offline

    Scimiguy

    Good job, keep it up.
     
    cococow123 likes this.
  30. Offline

    cococow123

    @Scimiguy I think I know my sign problem...

    When I put brackets from the line check to the end of the check, it didn't give an error or do anything.

    I'm pretty sure on the sign check, it needs to match the color also, which I didn't do.

    Hopefully that is the problem.

    But anyway thanks for all your help and support.
     
Thread Status:
Not open for further replies.

Share This Page