How to create player-specific timers in listener

Discussion in 'Plugin Development' started by 1Camer0471, Dec 17, 2014.

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

    1Camer0471

    SO, I'm trying to write a plugin that gives the player a daily reward, and I need to put a timer that lets them redeem the reward only every 12 hours. Here is the code I am using
    Code (open)

    public int time = 5;

    @EventHandler
    public void onClick(PlayerInteractEvent e) {

    final Player player = e.getPlayer();

    if (e.getAction() == Action.RIGHT_CLICK_AIR && e.getPlayer().getItemInHand().getType() == Material.DIAMOND) {

    Map<String, Integer> taskID = new HashMap<String, Integer>();


    final int tid = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
    public void run() {
    if (time != -1) {
    if (time != 0) {
    Bukkit.getServer().broadcastMessage("" + time + " Until u can get reward");
    time --;
    } else {

    //TEMP
    player.sendMessage("You can redeem your daily reward.");
    player.getInventory().addItem(new ItemStack(Material.GOLD_INGOT, 1));

    }
    }

    }





    }


    , 0, 20);

    taskID.put(player.getName(), tid);

    }

    And here is the error I am getting:
    Error (open)

    [18:28:27 ERROR]: Could not pass event PlayerInteractEvent to HubThings v0.0.9
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:216) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-
    g8688bd4-b3092jnks]
    at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:186) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-
    g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :605) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.ClassCastException: me.Camer047.ListenerClass cannot be cas
    t to org.bukkit.plugin.Plugin
    at me.Camer047.ListenerClass.onClick(ListenerClass.java:41) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    ... 15 more
    >
     
  2. Offline

    teej107

    I think your problem is self explanatory.

    And using the system time rather than runnables will be much more efficient.
     
  3. Offline

    Webbeh

    This is basically how I would do it, and yeah, it uses teej107 method :

    - Create a static hashmap linking a players' UUID and a system time
    - When a player checks out (using a command, right click on a sign/block, whatever), check :
    -- If the player isn't in the hashmap, allow him to check out right away, add the time in the hashmap.
    -- if he IS in the hashmap, check if "actualsystemtime" - "lastsystemtime" is over what you want (12h in milliseconds are a lot)
    --- if the condition is met, allow him, overwrite the last systemtime with the actual
    --- if it's not met, kick the player or whatever.

    No runnable necessary at all.

    You could also save the hashmaps' content upon disabling, and load it upon enabling.
     
  4. Offline

    teej107

     
  5. Offline

    Webbeh

    Will anyone stop with that "don't use statics" idiocy ? Thanks.
     
  6. Offline

    mythbusterma

    @Webbeh

    Nope, there's very good reasons for it. And it's very important that you don't misuse it. So explain how it's idiocy.
     
    teej107 likes this.
  7. Offline

    Webbeh

    There's not a single thread on here that hasn't a comment stating "don't use statics".

    Statics exist for a reason, they are useful and are needed for a lot of uses. Yet people go crazy about it here.

    And you're one of those. If statics are well used, they can dramatically increase your coding speed whilst increasing the program's performance.

    But hey, our comments were deleted on another thread, you want us to get banned ?
     
  8. Offline

    teej107

  9. Offline

    mythbusterma

    @Webbeh

    If it means I can stop you encouraging terrible coding practises, yes. They aren't needed in this case and could easily introduce bugs into the software, and I'd like to see performance increases. Even if there are any, they come at the cost of readability and tons of bugs.
     
  10. Offline

    Webbeh

    @teej107 - That's great, dear.
    I admit the static reference to the main plugin was wrong, thanks for the lack of sleep, but I see people have their only presence on this forum be to only tell people "learn java" and "don't use statics", it makes me angry.
     
  11. Offline

    eyamaz

    @Webbeh @mythbusterma

    This will be it, you want to argue the use of static, due it in a thread that isn't trying to help someone with their plugin. Keep it as a separate discussion.
     
Thread Status:
Not open for further replies.

Share This Page