Solved problem with Cooldown

Discussion in 'Plugin Development' started by MrJenteZ, Jan 24, 2016.

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

    MrJenteZ

    Hi there.

    I tried adding a CoolDown to my 'Commands'. Actually they should get a cooldown when they click a sign, that gives them a kit. But i get a error.

    My events are registered and worked all before i added the cooldown.

    Here's the error:

    Code:
    [18:58:09] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to WorldOfMinecraft v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:211) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:462) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:728) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_66]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
        at me.jente.wom.worldofminecraft.worldofminecraft.onInteract(worldofminecraft.java:244) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_66]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[CraftBukkit188.jar:git-Bukkit-18fbb24]
        ... 17 more
    [18:58:10] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to WorldOfMinecraft v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:211) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:462) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:728) [CraftBukkit188.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [CraftBukk
    And here's my HashMap:

    Code:
    HashMap<String, Integer> cd = new HashMap<String, Integer>();
    And my Cooldown on the signclick:

    Code:
    @EventHandler
        public void onInteract(PlayerInteractEvent e)
        {
            Player p = e.getPlayer();
           
            if (e.getAction()!=Action.RIGHT_CLICK_BLOCK) return;
            {
                if (System.currentTimeMillis() - cd.get(p.getName()) > 300000)
                {
                    if (e.getClickedBlock().getState() instanceof Sign)
                    {
                        Sign s = (Sign)e.getClickedBlock().getState();
                        if(s.getLine(0).equalsIgnoreCase(ChatColor.RED.toString() + ChatColor.BOLD + "[World Of MC]"))
                        {
                            if(s.getLine(1).equalsIgnoreCase(ChatColor.GREEN.toString() + ChatColor.BOLD + "Class"))
                            {
                                if(s.getLine(2).equalsIgnoreCase(ChatColor.AQUA.toString() + ChatColor.BOLD + "Warrior"))
                                    {
                                    //Do Something
                                }
                               
                            }
                        }
                    }
                }
            }
        }
     
  2. Offline

    87pen

    What's line 244?
     
  3. Offline

    MrJenteZ

    This is.
     
  4. Offline

    Gorbit99

    What's cd? If I'm right, it returns null, do a null check before that line.
    Check first:
    Did you instantiated it?
    Should it return something when typing in the playername?
     
  5. Offline

    87pen

    @MrJenteZ Does cd even contain the players name. //off topic you should use UUID's =
    edit: ninja'd by Gorbit, but as he said I believe if cd is empty it may return null.
     
  6. Offline

    MrJenteZ

    I understand now. They don't get put in the Cooldown so they can't get the kit, and i'm testing if they have a cd wich returns null. Any way to fix this?
     
  7. Offline

    87pen

    @MrJenteZ Check if cd is null or just loop through the Keys and check if any of them equal your players name.
     
  8. Offline

    MrJenteZ

    How to check for null? English is not my language
     
  9. Offline

    87pen

    @MrJenteZ Is cd == null, and check if your keys contains p.getName()
     
  10. Offline

    MrJenteZ

    I did that and it worked. Now i'm trying to have an else statement to tell them they are on cooldown, and i get an error in eclipse.

    This is the else line:

    Code:
    else (cd.get(p.getName()) - System.currentTimeMillis() > 300000){
                        p.sendMessage("You're still on Cooldown. Please wait before choosing anotehr kit.");
                    }
    This is the error:

    Code:
    The left-hand side of an assignment must be a variable
     
  11. Offline

    87pen

    @MrJenteZ Else doesn't take a Boolean. Else if can, but not else. On line 25 else{ //do Stuff}
     
  12. Offline

    MrJenteZ

    Okay that worked. And now my final question, how would i removed them from the hashmap after 5 minutes?
     
  13. Offline

    87pen

    @MrJenteZ Hm. I'm drawing a blank on this one. I don't want to give you false info as I haven't played around with cooldowns yet.
     
  14. Offline

    MrJenteZ

    Also no rough idea? If not can you help
    @Gorbit99
     
  15. Offline

    87pen

  16. Offline

    MrJenteZ

  17. Offline

    87pen

    @MrJenteZ Scroll down to delayed task. When they use the kit create a delayed task that lasts 20 * seconds, and when it runs remove them from the map. Ex.
    Code:
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncDelayedTask(plugin instance, new Runnable() {
                @Override
                public void run() {
                    // Do something
                }
            }, 20 * #ofSeconds);
     
    Last edited: Jan 24, 2016
  18. Offline

    MrJenteZ

    So i give the kit where "//do something" stands? And what's with the '20'

    Also, i don't have a single clue how i would implement that in my code.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  19. Offline

    87pen

    @MrJenteZ Where //do something is remove them from the hashmap. Since scheduler programming is done in ticks, there are 20 ticks in a second so 20 * 1 would be one second. You would do what you normally do to give them the kit but after you give them the kit you would start a delayed task that after however long removes them from the hashmap.
     
  20. Offline

    MrJenteZ

    Can you show me in my code, cuz i know i will f up and wreck everything.
     
  21. Offline

    87pen

    @MrJenteZ No. I want you to wreck everything. Trial and error. You won't learn if I give you the code. After you give them the kit add them to the hashmap then start a delayed task, in the //do something remove them from the hashmap. If you still don't know what to do READ the link I sent you and don't just copy and past the code.
     
  22. Offline

    MrJenteZ

    I see, i also found a video about it and he explained it in very nice detail, and with your info combined i think i can make it. I'll post the working code if you want?
     
  23. Offline

    87pen

    @MrJenteZ Sure might help some others in the future.
     
  24. Offline

    MrJenteZ

    Great. I'll post the video also.
     
  25. Offline

    87pen

  26. Offline

    MrJenteZ

    Thanks!

    This code fixed it:

    Code:
    @EventHandler
        public void onInteract(PlayerInteractEvent e)
        {
            final Player p = e.getPlayer();
          
            if (e.getAction()!=Action.RIGHT_CLICK_BLOCK) return;
            {
                if (!cD.contains(p.getName()))
                {
                    if (e.getClickedBlock().getState() instanceof Sign)
                    {
                        cD.add(p.getName());
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            public void run()
                            {
                                cD.remove(p.getName());
                            }
                        }, 300 * 20);
                        Sign s = (Sign)e.getClickedBlock().getState();
                        if(s.getLine(0).equalsIgnoreCase(ChatColor.RED.toString() + ChatColor.BOLD + "[World Of MC]"))
                        {
                            if(s.getLine(1).equalsIgnoreCase(ChatColor.GREEN.toString() + ChatColor.BOLD + "Class"))
                            {
                                if(s.getLine(2).equalsIgnoreCase(ChatColor.AQUA.toString() + ChatColor.BOLD + "Warrior"))
                                {
                                    //do something
                                }
                            }
                        }
                    }
                }
                else {
                    p.sendMessage("You're not able to use that sign yet. Please try another time.");
                }
            }
        }

    And this is a nice and well explained bid about it:



    Thanks @87pen For your help!

    Edit: I used an ArrayList for this. no HashMap.
     
Thread Status:
Not open for further replies.

Share This Page