Solved Help with code!

Discussion in 'Plugin Development' started by EnderPilot105, Feb 26, 2014.

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

    EnderPilot105

    Why does it crash the server?
    Code:java
    1. if (clicked.equals(item5)) {
    2. Bukkit.broadcastMessage("" + ChatColor.DARK_PURPLE
    3. + ChatColor.BOLD + "<Ghost Battle Dome>"
    4. + ChatColor.RESET + ChatColor.MAGIC
    5. + ");
    6. Bukkit.broadcastMessage(ChatColor.GREEN + "<Battle Dome>");
    7. Bukkit.broadcastMessage("" + ChatColor.AQUA + ChatColor.BOLD
    8. + ChatColor.ITALIC
    9. + "Tip: Ghost Battle Dome is 2 minutes long...");
    10. Bukkit.broadcastMessage(""
    11. + ChatColor.AQUA
    12. + ChatColor.BOLD
    13. + ChatColor.ITALIC
    14. + "Tip: When Ghost Battle Dome is over you get your items back...");
    15. for (Player bluep : Bukkit.getOnlinePlayers()) {
    16. saveInventory(bluep.getPlayer());
    17. bluep.getPlayer().getInventory().clear();
    18. ItemStack a = new ItemStack(Material.SKULL_ITEM);
    19. ItemStack b = new ItemStack(Material.LEATHER_CHESTPLATE);
    20. LeatherArmorMeta bmeta = (LeatherArmorMeta) b.getItemMeta();
    21. bmeta.setColor(Color.BLACK);
    22. b.setItemMeta(bmeta);
    23. ItemStack c = new ItemStack(Material.GHAST_TEAR);
    24. ItemStack d = new ItemStack(Material.GHAST_TEAR);
    25. PotionEffect e = new PotionEffect(
    26. PotionEffectType.INVISIBILITY, Integer.MAX_VALUE,
    27. Integer.MAX_VALUE);
    28. PotionEffect f = new PotionEffect(PotionEffectType.JUMP,
    29. Integer.MAX_VALUE, 1);
    30. bluep.getPlayer().getWorld().setTime(18000);
    31. bluep.getInventory().setHelmet(a);
    32. bluep.getInventory().setChestplate(b);
    33. bluep.getInventory().setLeggings(c);
    34. bluep.getInventory().setBoots(d);
    35. bluep.addPotionEffect(e);
    36. bluep.addPotionEffect(f);
    37. }
    38. for (final Player bluep : Bukkit.getOnlinePlayers()) {
    39.  
    40. this.getServer().getScheduler()
    41. .runTaskLater(this, new Runnable() {
    42.  
    43. public void run() {
    44. bluep.getServer()
    45. .getScheduler().cancelTask(num);
    46. bluep.getInventory().clear();
    47. restoreInventory(bluep);
    48.  
    49. }
    50.  
    51. }, 10000);
    52.  
    53. this.getServer().getScheduler()
    54. .scheduleAsyncRepeatingTask(this, new Runnable() {
    55.  
    56. public void run() {
    57. if (num != -1) {
    58. if (num != 0) {
    59. Location bl = bluep
    60. .getLocation();
    61. bluep.getPlayer()
    62. .getWorld()
    63. .playEffect(
    64. bl,
    65. Effect.MOBSPAWNER_FLAMES,
    66. 1);
    67. num--;
    68. } else {
    69. bluep.getServer().getScheduler().cancelTask(num);
    70. bluep.getWorld()
    71. .setTime(1000);
    72. num--;
    73. }
    74. }
    75.  
    76. }
    77. }, 0L, 10L);
    78. }
    79. }
     
  2. Offline

    alex123099

    EnderPilot105
    Any stack trace of an error?
    And why are you cancelling the task of id num? what is num exactly as you seem to be decreasing it all the time?
     
    EnderPilot105 likes this.
  3. Offline

    nitrousspark

    just gonna throw this out there, it looks like your canceling a task for the integer num, but then in a different scheduler your changing num around.

    Code:
     
    this.getServer().getScheduler().runTaskLater(this, new Runnable()
    {
     
    public void run()
    {
    bluep.getServer().getScheduler().cancelTask(num); <--- HERE
    bluep.getInventory().clear();
    restoreInventory(bluep);
     
    }
     
    }, 10000);
     
    this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
    {
     
    public void run()
    {
    if(num != -1)
    {
    if(num != 0)
    {
    Location bl = bluep.getLocation();
    bluep.getPlayer().getWorld().playEffect(bl, Effect.MOBSPAWNER_FLAMES, 1);
    num--; <--- HERE
    }
    else
    {
    bluep.getServer().getScheduler().cancelTask(num);
    bluep.getWorld().setTime(1000);
    num--; <--- HERE
    }
    }
     
    }
    }, 0L, 10L);
    you may also want to use BukkitRunnables. try using this for timers

    Code:
    new BukkitRunnable() {
    @Override
    public void run() {
    }
    }.runTaskTimer(this, 0L, 10L);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
    EnderPilot105 likes this.
  4. Offline

    Traks

  5. Offline

    nitrousspark

    The problem was probably that you were using an Async task instead of a Sync task
     
  6. Offline

    EnderPilot105

  7. Offline

    alex123099

    EnderPilot105
    What is the start command exactly? And what errors do you get at crash?
    Btw, I suggest you cancel the repeating task once it has finished what it should do.
     
  8. Offline

    Traks

    This. Use .scheduleSyncRepeatingTask instead of .scheduleAsyncRepeatingTask
     
  9. Offline

    EnderPilot105

    Problem: The console stops working and just freezes, the server freezes too there are no errors

    How can you stop a tasks?

    alex123099 Traks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  10. Offline

    alex123099

    EnderPilot105
    When you create the task, the function returns an object of the task. Store that in a global static(?) variable (haven't used tasks in a long time) and then when the task is done doing it's job, do a cancel function on that object.

    Since the server freezes along with the console, I believe that it's either stuck on a VERY long loop, maybe a Thread.sleep, or out of memory
     
  11. Offline

    coasterman10

    This, except the variable should not be static nor global unless it absolutely must be. If you're cancelling the task from the same class as you created it, you can just store its ID in a private variable.
     
    alex123099 likes this.
  12. Offline

    EnderPilot105

    I have made progress it crashes with error - doesnt freeze!
    I changed the runnable() to BukkitRunnable()
    and .scheduleSyncRepeatingTask to .scheduleAsyncRepeatingTask
    it says null pointer exeption coasterman10 alex123099 Traks
     
  13. Offline

    Traks

    Please post error and full code and/or on what line the null pointer occurs, also don't use async tasks when you use Bukkit methods in them
     
  14. Offline

    EnderPilot105

    Last edited by a moderator: Jun 6, 2016
  15. Offline

    Traks

    What is on line 826 of the Youtube class?
     
  16. Offline

    alex123099

  17. Offline

    EnderPilot105

    a.sendMessage("" + ChatColor.GREEN + "<Battle Dome> "
     
  18. Offline

    EnderPilot105

Thread Status:
Not open for further replies.

Share This Page