Schedular doesn't work!

Discussion in 'Plugin Development' started by Julz_Dev, Jan 12, 2014.

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

    Julz_Dev

    Hi.
    I need help with this!

    This code should do this:

    Every second there must be firework, but there is only one time firework and the next seconds there doesn't spawn any firework.
    The schedular is running without an error!

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    2.  
    3. Player p = (Player)sender;
    4.  
    5. if(cmdLabel.equals("c")){
    6. if(args.length>0){
    7. if(args[0].equals("fw")){
    8. Bukkit.getServer().getWorld("world").setTime(16000);
    9. starttimer();
    10. }else{
    11. p.sendMessage(ChatColor.RED + "Diese Aktion gibt es nicht!");
    12. }
    13. }else{
    14. p.sendMessage(ChatColor.RED + "Bitte geben Sie Ihre Wunschaktion ein!");
    15. }
    16.  
    17.  
    18. return true;
    19. }
    20.  
    21.  
    22.  
    23. return true;
    24.  
    25. }
    26.  
    27. public void starttimer(){
    28. Cooldown task = new Cooldown();
    29. task.setId(Bukkit.getScheduler().scheduleSyncRepeatingTask(this, task, 0L, 20L));
    30. }
    31.  
    32. public class Cooldown implements Runnable{
    33.  
    34. int id;
    35. int co = 10;
    36.  
    37. public void setId(int id){
    38. this.id = id;
    39. }
    40.  
    41. public void cancel(){
    42. Bukkit.getScheduler().cancelTask(this.id);
    43. }
    44.  
    45. public void run(){
    46. if(co==0){
    47. cancel();
    48. }else{
    49. Random r = new Random();
    50. for(int i = 0;i<5;i++){
    51. int z = r.nextInt(25)+16;
    52. Location tempf = firework;
    53. tempf.setX(-z);
    54. tempf.setZ(tempf.getZ()+10);
    55. launchFirework((r.nextInt(50)+50), tempf);
    56. }
    57. co-=1;
    58. }
    59.  
    60. }
    61.  
    62. }
    63.  
    64. public static void launchFirework(int speed, Location loc) {
    65. Firework fw = (Firework) loc.getWorld().spawn(loc, Firework.class);
    66. FireworkMeta meta = fw.getFireworkMeta();
    67. Random r= new Random();
    68. int fType = r.nextInt(4)+1;
    69. Type type = null;
    70. switch(fType){
    71. default:
    72. case 1:
    73. type = Type.BALL;
    74. break;
    75. case 2:
    76. type = Type.BALL_LARGE;
    77. break;
    78. case 3:
    79. type = Type.STAR;
    80. break;
    81. case 4:
    82. type = Type.BURST;
    83. break;
    84. }
    85. int c1i = r.nextInt(16)+1;
    86. int c2i = r.nextInt(16)+1;
    87. Color c1 = getColour(c1i);
    88. Color c2 = getColour(c2i);
    89. FireworkEffect effect = FireworkEffect.builder()
    90. .flicker(r.nextBoolean()).withColor(c1).withFade(c2)
    91. .with(type).trail(r.nextBoolean()).build();
    92. meta.addEffect(effect);
    93. int power = r.nextInt(1)+2;
    94. meta.setPower(power);
    95. fw.setFireworkMeta(meta);
    96. }
    97.  
    98. public static Color getColour(int c){
    99. switch (c) {
    100. default:
    101. case 1:return Color.AQUA;
    102. case 2:return Color.BLUE;
    103. case 3:return Color.FUCHSIA;
    104. case 4:return Color.GREEN;
    105. case 5:return Color.LIME;
    106. case 6:return Color.GRAY;
    107. case 7:return Color.MAROON;
    108. case 8:return Color.NAVY;
    109. case 9:return Color.OLIVE;
    110. case 10:return Color.PURPLE;
    111. case 11:return Color.RED;
    112. case 12:return Color.SILVER;
    113. case 13:return Color.TEAL;
    114. case 14:return Color.WHITE;
    115. case 15:return Color.YELLOW;
    116. case 16:return Color.AQUA;
    117. }
    118. }


    Here is my code.

    :/
     
  2. Offline

    Ironraptor3

    for this i usually just use a bukkitrunnable, I've been coding with them all day :/

    Here's an example:

    Code:java
    1. int count = 0;
    2. int length = 30;
    3. //you could use booleans that change on command, etc...
    4.  
    5. new BukkitRunnable() {
    6. @Override
    7. public void run() {
    8.  
    9. if (count =< length) {
    10. count++;
    11. //do stuff
    12. }
    13. else if (count > length) {
    14. cancel();
    15. }
    16. }
    17. }.runTaskTimer(mainclass.plugin, startdelay, 20);
    18. //20 ticks in a second
    19.  
    20.  
    21.  


    Tahg me if you need anything else :D
     
  3. Offline

    AmShaegar

    Julz_Dev
    Your code looks correct. The only thing I can imagine is that there is an error while launching the firework that causes the scheduler to stop. Do you get any error messages on the console?
     
  4. Offline

    Julz_Dev

    AmShaegar
    No that is the problem :/
    And if I write some test Bukkit.broadcastmessage()... in the code, everything works fine.
    ALSO THE SCHEDULAR!
    So the error MUST be in the firework method :/
     
  5. Offline

    AmShaegar

    Could you post your whole class? I cannnot see where the Location 'firework' is coming from. Maybe I can find something when seeing the big picture.
     
  6. Offline

    Julz_Dev

    AmShaegar
    of course :)

    The location is only a point to see where I can start to calculate the spawn point :)

    Code:java
    1. package me.jucrowd.main;
    2.  
    3.  
    4. import java.util.Random;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Color;
    9. import org.bukkit.FireworkEffect;
    10. import org.bukkit.FireworkEffect.Type;
    11. import org.bukkit.Location;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Firework;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.inventory.meta.FireworkMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19.  
    20. public class Main extends JavaPlugin{
    21.  
    22. private Location firework = new Location(Bukkit.getWorld("world"),-20,68,20);
    23.  
    24. //private PlayerMoveListener pmv;
    25. private PlayerInteractListener pil;
    26.  
    27. private int co = 10;
    28.  
    29.  
    30. public void onEnable(){
    31. //pmv = new PlayerMoveListener(this);
    32. pil = new PlayerInteractListener(this);
    33. System.out.println("[CH] Plugin aktiviert!");
    34. }
    35.  
    36. public void onDisable(){
    37. System.out.println("[CD] Plugin deaktiviert!");
    38. }
    39.  
    40. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    41.  
    42. Player p = (Player)sender;
    43.  
    44.  
    45. if(cmdLabel.equals("c")){
    46. if(args.length>0){
    47. if(args[0].equals("fw")){
    48. Bukkit.getServer().getWorld("world").setTime(16000);
    49. starttimer();
    50. }else{
    51. p.sendMessage(ChatColor.RED + "Diese Aktion gibt es nicht!");
    52. }
    53. }else{
    54. p.sendMessage(ChatColor.RED + "Bitte geben Sie Ihre Wunschaktion ein!");
    55. }
    56.  
    57.  
    58. return true;
    59. }
    60.  
    61.  
    62.  
    63. return true;
    64.  
    65. }
    66.  
    67. public void starttimer(){
    68. Cooldown task = new Cooldown();
    69. task.setId(Bukkit.getScheduler().scheduleSyncRepeatingTask(this, task, 0L, 20L));
    70. }
    71.  
    72. public class Cooldown implements Runnable{
    73.  
    74. int id;
    75. int co = 10;
    76.  
    77. public void setId(int id){
    78. this.id = id;
    79. }
    80.  
    81. public void cancel(){
    82. Bukkit.getScheduler().cancelTask(this.id);
    83. }
    84.  
    85. public void run(){
    86. if(co==0){
    87. cancel();
    88. }else{
    89. Random r = new Random();
    90. for(int i = 0;i<5;i++){
    91. int z = r.nextInt(25)+16;
    92. Location tempf = firework;
    93. tempf.setX(-z);
    94. tempf.setZ(tempf.getZ()+10);
    95. launchFirework((r.nextInt(50)+50), tempf);
    96. }
    97. co-=1;
    98. }
    99.  
    100. }
    101.  
    102. }
    103.  
    104. public static void launchFirework(int speed, Location loc) {
    105. Firework fw = (Firework) loc.getWorld().spawn(loc, Firework.class);
    106. FireworkMeta meta = fw.getFireworkMeta();
    107. Random r= new Random();
    108. int fType = r.nextInt(4)+1;
    109. Type type = null;
    110. switch(fType){
    111. default:
    112. case 1:
    113. type = Type.BALL;
    114. break;
    115. case 2:
    116. type = Type.BALL_LARGE;
    117. break;
    118. case 3:
    119. type = Type.STAR;
    120. break;
    121. case 4:
    122. type = Type.BURST;
    123. break;
    124. }
    125. int c1i = r.nextInt(16)+1;
    126. int c2i = r.nextInt(16)+1;
    127. Color c1 = getColour(c1i);
    128. Color c2 = getColour(c2i);
    129. FireworkEffect effect = FireworkEffect.builder()
    130. .flicker(r.nextBoolean()).withColor(c1).withFade(c2)
    131. .with(type).trail(r.nextBoolean()).build();
    132. meta.addEffect(effect);
    133. int power = r.nextInt(1)+2;
    134. meta.setPower(power);
    135. fw.setFireworkMeta(meta);
    136. }
    137.  
    138. public static Color getColour(int c){
    139. switch (c) {
    140. default:
    141. case 1:return Color.AQUA;
    142. case 2:return Color.BLUE;
    143. case 3:return Color.FUCHSIA;
    144. case 4:return Color.GREEN;
    145. case 5:return Color.LIME;
    146. case 6:return Color.GRAY;
    147. case 7:return Color.MAROON;
    148. case 8:return Color.NAVY;
    149. case 9:return Color.OLIVE;
    150. case 10:return Color.PURPLE;
    151. case 11:return Color.RED;
    152. case 12:return Color.SILVER;
    153. case 13:return Color.TEAL;
    154. case 14:return Color.WHITE;
    155. case 15:return Color.YELLOW;
    156. case 16:return Color.AQUA;
    157. }
    158. }
    159.  
    160. }
    161.  
     
  7. Offline

    AmShaegar

    I debugged your code and it works fine if the fw is in the right location. I am sure this is messing it up for you, because you have no idea where the fw is launched actually.

    Code:java
    1. tempf.setX(-z);
    2. tempf.setZ(tempf.getZ()+10);
     
Thread Status:
Not open for further replies.

Share This Page