Making a method get ran ONCE on player move event?

Discussion in 'Plugin Development' started by megasaad44, Jun 26, 2014.

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

    megasaad44

    I got a player move event, and an if statement, if the if statement is true, it runs a method. But of course, every time the player moves, its gonna get ran, how do i run it ONCE AND ONLY ONCE when the event is true?
     
  2. Offline

    stirante

    Add to list player UUID after running that method and check next time if list contains that UUID.
     
  3. Offline

    megasaad44

    Sound like a good idea. I'll try that.
     
  4. Offline

    Cirno

    I would suggest doing the opposite; add everyone you want to run once and remove them once they move. That way, you don't call contains() (which, even if it has O(1) for HashMaps, it would still generate some form of lag) every single micron moved.
     
  5. Offline

    megasaad44

    Cirno
    Actually your solution doesn't match my criteria. I'm using the playermoveevent to make some form of constant checking. If i made a loop in a raw method with nothing else to go on, it would be super laggy and super super fast and crazy. playermoveevent is my alternative. I repeating task would be slightly more tedious. By so far i see no lag. So that's good.... I guess..
     
  6. Offline

    Rocoty

    Cirno Well, obviously you would have to check if the player is in the list anyway, except with your solution it would take longer time after they moved because you would have to check the entire list.

    Even so, whether OP decides to do one or the other it wouldn't matter much with even 100 players online. You really shouldn't be thinking about performance that much.
     
    AdamQpzm likes this.
  7. Offline

    megasaad44

    Rocoty My thoughts exactly! Player move event wouldn't hurt the server at all unless you have a thousand of them in classes.
    And my list is limited to 2 people only.
    While you're here, The problem is solved kinda. I have a scheduled repeating task here and it wont cancel.
    Here's a bit of the code:
    Code:java
    1.  
    2. public void countdown() {
    3. for (UUID player : hpusers1) {
    4. final Player p = (Player) Bukkit.getPlayer(player);
    5. hplobby1.add(player);
    6. countdowntimer = Main
    7. .getInstance()
    8. .getServer()
    9. .getScheduler()
    10. .scheduleSyncRepeatingTask(Main.getInstance(),
    11. new BukkitRunnable() {
    12. int time = 5;
    13.  
    14. public void run() {
    15. if (time > 0) {
    16. time--;
    17.  
    18.  
    19. }
    20. if (time == 0) {
    21. Bukkit.getScheduler().cancelTask(
    22. countdowntimer);
    23. startit();
    24. p.sendMessage(message2);
    25.  
    26. }
    27. }
    28. }, 20L, 20L);
    29. }
    30. }

    When time = 0. It wont cancel. It keep on going. It's a countdown.
     
  8. Offline

    Gater12

    megasaad44

    BukkitRunnable has cancel() method.
     
  9. Offline

    megasaad44

    Gater12 Someone suggested something like that. Could you elaborate? I haven't really heard of BukkitRunnable's extentions. What can i do with the current code?
     
  10. Offline

    Gater12

  11. Offline

    megasaad44

    Gater12
    I used cancel(); It worked!
    But the server is generating exceptions pointing at the cancel(); is their something i'm missing? and the fields in the if statement with the cancel(); and not being ran. hence an error.
    Code:
    1:17:21 PM [WARNING] [TheDiverge] Task #22 for TheDiverge v1.0 generated an exception
    1:17:21 PM java.lang.IllegalStateException: Not scheduled yet
    1:17:21 PM    at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at org.bukkit.scheduler.BukkitRunnable.cancel(BukkitRunnable.java:18) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at me.Lordsaad44.diverge.hotpotato.HpArena1$1.run(HpArena1.java:78) ~[?:?]
    1:17:21 PM    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    1:17:21 PM    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
     
  12. Offline

    Gater12

    megasaad44
    Create anonymous BukkitRunnable instead.
     
  13. Offline

    Rocoty

    megasaad44 exactly what it says. You haven't scheduled it before trying to cancel it.
     
  14. Offline

    megasaad44

    Rocoty I love the word "exactly".
    Currently have this, its not working. no errors, but it's like there is no scheduler.
    Code:java
    1. new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4. //Crap
    5.  
    6. }
    7. }.runTaskTimer(Main.getInstance(), 20, 20);
    8. }
    9. }
     
Thread Status:
Not open for further replies.

Share This Page