Tasks causing laggs/freezing

Discussion in 'Plugin Development' started by PreFiXAUT, Mar 8, 2014.

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

    PreFiXAUT

    Hi Guys, I'm getting problems with the Bukkit Tasks I think.
    I want to create an TntRun-Minigame for my Plugin and now I'm at the Problem that it's taking too much resources I think. It's not much Code, but the Server still has an 11k Ping (I saw it in the Console), so what can I do to improve this?

    SourceCode:
    Show Spoiler
    GameWorker:
    Show Spoiler

    Code:java
    1. package at.prefixaut.runner;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.scheduler.BukkitTask;
    7.  
    8. import at.prefixaut.lobbys.LobbyData;
    9. import at.prefixaut.lobbys.Players;
    10. import at.prefixaut.runner.tasks.DeleteBlocksTask;
    11.  
    12. public class GameWorker {
    13.  
    14. private static Main main;
    15.  
    16. static BukkitTask deleteBlocks;
    17.  
    18. public GameWorker(Main main) {
    19. GameWorker.main = main;
    20. }
    21.  
    22. public static void startGame(final LobbyData data) {
    23.  
    24. final String[] players = Players.getPlayers(data.getGroupId());
    25.  
    26. for (int i=10; i > -1; i--) {
    27. for (int o=0; o<players.length;o++) {
    28. Bukkit.getPlayer(players[o]).sendMessage("The Game starts in: " + i + " Secounds!");
    29. try {
    30. Thread.sleep(1000);
    31. } catch (InterruptedException e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. }
    36.  
    37. double x = 0;
    38. double y = 0;
    39. double z = 0;
    40. if (data.getDestX().charAt(0) == '~') {
    41. String[] temp = data.getDestX().split("~");
    42. x = data.getBlockX() + Double.parseDouble(temp[1]);
    43. } else x = Double.parseDouble(data.getDestX());
    44. if (data.getDestY().charAt(0) == '~') {
    45. String[] temp = data.getDestY().split("~");
    46. x = data.getBlockX() + Double.parseDouble(temp[1]);
    47. } else y = Double.parseDouble(data.getDestY());
    48. if (data.getDestZ().charAt(0) == '~') {
    49. String[] temp = data.getDestZ().split("~");
    50. x = data.getBlockX() + Double.parseDouble(temp[1]);
    51. } else z = Double.parseDouble(data.getDestZ());
    52.  
    53. for (String player : players) {
    54. Player p = Bukkit.getPlayer(player);
    55. p.teleport(new Location(p.getLocation().getWorld(), x, y, z));
    56. }
    57.  
    58. deleteBlocks = new DeleteBlocksTask(data).runTaskTimer(main, 0L, 2L);
    59. }
    60. }
    61.  

    DeleteBlocksTask:
    Show Spoiler

    Code:java
    1. package at.prefixaut.runner;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.scheduler.BukkitTask;
    7.  
    8. import at.prefixaut.lobbys.LobbyData;
    9. import at.prefixaut.lobbys.Players;
    10. import at.prefixaut.runner.tasks.DeleteBlocksTask;
    11.  
    12. public class GameWorker {
    13.  
    14. private static Main main;
    15.  
    16. static BukkitTask deleteBlocks;
    17.  
    18. public GameWorker(Main main) {
    19. GameWorker.main = main;
    20. }
    21.  
    22. public static void startGame(final LobbyData data) {
    23.  
    24. final String[] players = Players.getPlayers(data.getGroupId());
    25.  
    26. for (int i=10; i > -1; i--) {
    27. for (int o=0; o<players.length;o++) {
    28. Bukkit.getPlayer(players[o]).sendMessage("The Game starts in: " + i + " Secounds!");
    29. try {
    30. Thread.sleep(1000);
    31. } catch (InterruptedException e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. }
    36.  
    37. double x = 0;
    38. double y = 0;
    39. double z = 0;
    40. if (data.getDestX().charAt(0) == '~') {
    41. String[] temp = data.getDestX().split("~");
    42. x = data.getBlockX() + Double.parseDouble(temp[1]);
    43. } else x = Double.parseDouble(data.getDestX());
    44. if (data.getDestY().charAt(0) == '~') {
    45. String[] temp = data.getDestY().split("~");
    46. x = data.getBlockX() + Double.parseDouble(temp[1]);
    47. } else y = Double.parseDouble(data.getDestY());
    48. if (data.getDestZ().charAt(0) == '~') {
    49. String[] temp = data.getDestZ().split("~");
    50. x = data.getBlockX() + Double.parseDouble(temp[1]);
    51. } else z = Double.parseDouble(data.getDestZ());
    52.  
    53. for (String player : players) {
    54. Player p = Bukkit.getPlayer(player);
    55. p.teleport(new Location(p.getLocation().getWorld(), x, y, z));
    56. }
    57.  
    58. deleteBlocks = new DeleteBlocksTask(data).runTaskTimer(main, 0L, 2L);
    59. }
    60. }
    61.  

    IgniteTntTask:
    Show Spoiler

    Code:java
    1. package at.prefixaut.runner.tasks;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.entity.TNTPrimed;
    8. import org.bukkit.scheduler.BukkitRunnable;
    9.  
    10. public class IgniteTntTask extends BukkitRunnable {
    11.  
    12. private static Location loc;
    13. private static Player p;
    14. public IgniteTntTask(Location loc, Player p) {
    15. IgniteTntTask.loc = loc;
    16. IgniteTntTask.p = p;
    17. }
    18.  
    19. @SuppressWarnings("deprecation")
    20. public void run() {
    21. try {
    22. Block b = p.getWorld().getBlockAt(loc);
    23. b.setTypeId(0);
    24. loc.setY(loc.getY()-0.3);
    25. Entity tnt = p.getWorld().spawn(loc, TNTPrimed.class);
    26. ((TNTPrimed)tnt).setFireTicks(0);
    27. Thread.sleep(100);
    28. tnt.remove();
    29. } catch (InterruptedException e) {
    30. e.printStackTrace();
    31. this.cancel();
    32. }
    33.  
    34. }
    35.  
    36. }
    37.  



    PS: I never worked with Tasks allot, and yeah I know I'm using lot of Tasks, but with the Thread.sleep Method, it's going to freez the Entire Class which means I can't do anything anymore until it's "awake" again :/

    OffTopic: In DeleteBlocksTask is an Line outcommented > p.getWorld().playEffect(p.getLocation(),Effect.SMOKE, p.getLocation().getDirection());
    won't work and I have no Idea why, because I googled it and found an Post wich was posted 3Days before, and it worked for him/her :/
    Stacktrace:
    Show Spoiler
    Code:
    [17:38:15] [Server thread/WARN]: [TntRun-Minigame] Task #5 for TntRun-Minigame v0.0.1 generated an exception
    java.lang.IllegalArgumentException: Wrong kind of data for this effect!
    at org.apache.commons.lang.Validate.isTrue(Validate.java:157) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftWorld.playEffect(CraftWorld.java:774) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftWorld.playEffect(CraftWorld.java:769) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at at.prefixaut.runner.tasks.DeleteBlocksTask.run(DeleteBlocksTask.java:30) ~[?:?]
    at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:587) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]


    Info > No Errors when I comment it out
     
  2. Offline

    werter318

    You're calling Thread.sleep() in your main thread, this will freeze the whole server .
     
  3. Offline

    PreFiXAUT

    werter318
    Oh, I was thinking it will only freeze the Task :/ But I can't create an new delayedTask for every Delay, or do I need to?
     
  4. Offline

    werter318

    PreFiXAUT I was talking about your GameWorker class, the startgame method
     
  5. Offline

    PreFiXAUT

    werter318
    The startGame Method will only be called once, and it's not my Main Class. My Main Class is the "Main" Class (lol), as you can see that the GameWorker isn't extending JavaPlugin
     
  6. Offline

    werter318

    PreFiXAUT Another class != another thread
     
  7. Offline

    PreFiXAUT

    werter318 Ok, but what should I do to delay the Methods? By using wait in an static Method will cause trouble (I know how to fix it, but it will cause other problems), and to start allways an new Task isn't that good, or am I wrong?
     
  8. Offline

    MCForger

    PreFiXAUT
    Why not simply make a task that is scheduled for every second, and inside that thread have a int called countdown that is equal to 30 seconds or etc. Then every time it is called subtract from that, and inside the run method if the countdown == 30 seconds, simply announce "Game Starting in 30 seconds" or even better idea, have an array of integers and if that number is equal to the countdown announce it.
    Example Code:
    Code:java
    1. package com.mcforger.example;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.scheduler.BukkitRunnable;
    6.  
    7. public class GameCountdown extends BukkitRunnable
    8. {
    9. private Arena arena;
    10. private int countdown;
    11. private List<Integer> announce;
    12.  
    13. public GameCountdown(Arena arena, int countdown, List<Integer> announce)
    14. {
    15. this.arena = arena;
    16. this.countdown = countdown;
    17. this.announce = announce;
    18. }
    19.  
    20. @Override
    21. public void run()
    22. {
    23. if (announce.contains(countdown))
    24. {
    25. arena.broadcastMessage("Game starting in " + countdown
    26. + " seconds!");
    27. }
    28. if (countdown == 0)
    29. {
    30. cancel();
    31. }
    32. countdown -= 1;
    33. }
    34. }
    35.  
     
  9. Offline

    GameplayJDK

    You can use the System.currentTimeMillis() to manage timing. If you wish to wait for 1 second you can do something like this:
    Code:java
    1. int lasttime = System.currentTimeMillis();
    2. int nexttime = lasttime + 1000;
    3. while (true) {
    4. if (!(System.currentTimeMillis() <= nexttime)) {
    5. // Do something
    6. }
    7. }

    (The code is not that pretty, but it should do the thing :) )
     
Thread Status:
Not open for further replies.

Share This Page