[Unsolved] How can i make my code happen every 5 seconds until times <= maxTimes?

Discussion in 'Plugin Development' started by wadu436, Jul 25, 2013.

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

    wadu436

    I cant use Bukkit Scheduler because i need variables from the arguments.
    My code:
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class CommandExecutorFireworkShow implements CommandExecutor {
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    15. if (cmd.getName().equalsIgnoreCase("fireworkshow")) {
    16. if (sender instanceof Player) {
    17. Location loc = sender.getServer().getPlayer(sender.getName()).getTargetBlock(null, 200).getLocation();
    18. double x = 0;
    19. double z = 0;
    20. int maxTimes = 0;
    21. int positiveOrNegativeX = 0;
    22. int positiveOrNegativeZ= 0;
    23. Random generator = new Random();
    24. Player player = sender.getServer().getPlayer(sender.getName());
    25. try {
    26. maxTimes = Integer.parseInt(args[0]);
    27. } catch (NumberFormatException e) {
    28. sender.sendMessage("Insert a Number!");
    29. return false;
    30. }
    31. for (int amountOfTimes = 0; amountOfTimes>=maxTimes; amountOfTimes++) {
    32. x = loc.getX();
    33. z = loc.getZ();
    34. positiveOrNegativeX = generator.nextInt(1) + 1;
    35. positiveOrNegativeZ = generator.nextInt(1) + 1;
    36. if (positiveOrNegativeX == 1) {
    37. x = x - generator.nextDouble()*20;
    38. if (positiveOrNegativeZ == 1) {
    39. z = z - generator.nextDouble()*20;
    40. }
    41. if (positiveOrNegativeZ == 2) {
    42. z = z + generator.nextDouble()*20;
    43. }
    44. }
    45. if (positiveOrNegativeX == 2) {
    46. x = x + generator.nextDouble()*20;
    47. if (positiveOrNegativeZ == 1) {
    48. z = z - generator.nextDouble()*20;
    49. }
    50. if (positiveOrNegativeZ == 2) {
    51. z = z + generator.nextDouble()*20;
    52. }
    53. }
    54. loc.setX(x);
    55. loc.setZ(z);
    56. SpawnRandomFirework.spawnRandomFirework(loc, player);
    57. }
    58.  
    59. }
    60. else {
    61. sender.sendMessage("You have to be a Player to execute this command!");
    62. }
    63. }
    64. return false;
    65. }
    66. }
    67.  
     
  2. Offline

    Cirno

    Bukkit.getSchedular().scheduleSyncRepeatingTask(pluginClass, new Runnable(){
    public void run(){
    //do things
    times++;
    }
    });

    Don't know if this works though, since you're adding 1 to a variable that Java would want to be declared final.
     
  3. Offline

    wadu436

    what variable? and is this in another .java file?
     
  4. Offline

    Cirno

    No, this is in the same file. Just use the times variable that you provide. If that doesn't work, just create a function named "addone()" that does "times++"
     
  5. Offline

    xTrollxDudex

    wadu436
    You can use what Cirno said but the function should be in a private class, which eliminates the need for final variable, as running a class includes evything in it, and is easy to cancel.
     
  6. Offline

    wadu436

    Cirno I will try it tomorrow. Im on mobile now.
    xTrollxDudex so in an external .java?
     
  7. Offline

    xTrollxDudex

    wadu436
    Its on you. Either way, nested class or public class, they need to implement Runnable, and have an
    PHP:
    @Override
    public void run(){
    //what you want to run here
    }
    somewhere. To run, create a new instance of the class and put the instance as the second parameter.
     
  8. Offline

    wadu436

    So I can choose to put it in the same file or create a new one. But if I create a new one, how do I pass the variables?
     
  9. Offline

    xTrollxDudex

    wadu436
    Create a new instance of the class like I said.
    Say for example you have a runnable class called Task. To make a new instance
    PHP:
    Task t = new Task();
    Task is the class name, t is the instance of it, you would use that to refer the the specific instance. It doesnt matter what it is called whether it is task, tsk, wieiebdjdn, hi, or some other random name. Just use that same name when referring to the instance. New Task() actually makes the new instance, hence new. Task() is the default constructor, this would pass parameters if you needed them.
     
  10. Offline

    wadu436

    I think I get it. So first I create a bukkitscheduler class and than paste my code. Than I create a new instance with the name t of my class with the variables. Than I refer to my max amount of times etc. as as t.maxTime and t.loc etc. Than at the end of the task I add an if statement to detect the amount of times it has run and if it isn't bigger then t.max times I add 1 to the times and schedule the task again. Is this right? xTrollxDudex
     
  11. Offline

    xTrollxDudex

    wadu436
    t.maxTime()*
    You might want to add some getters and setters. A simple google will point you in the right direction
     
  12. Offline

    wadu436

    OK thx :D i will tryit tomorrow!

    xTrollxDudex Can i get some help on scheduling the task, i get an error on line 29.
    Code for my main executor:
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.scheduler.BukkitTask;
    9.  
    10. public class CommandExecutorFireworkShow implements CommandExecutor {
    11. private Location loc;
    12. private int maxTimes;
    13. private Player player;
    14. private Location originalLocation;
    15. @Override
    16. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    17. if (cmd.getName().equalsIgnoreCase("fireworkshow")) {
    18. if (sender instanceof Player) {
    19. loc = sender.getServer().getPlayer(sender.getName()).getTargetBlock(null, 200).getLocation();
    20. maxTimes = 0;
    21. player = sender.getServer().getPlayer(sender.getName());
    22. originalLocation = loc;
    23. try {
    24. maxTimes = Integer.parseInt(args[0]);
    25. } catch (NumberFormatException e) {
    26. sender.sendMessage("Insert a Number!");
    27. return false;
    28. }
    29. BukkitTask task = new ShowScheduler(new FireworkPlus()).runTaskLater(new FireworkPlus(), 100);
    30. return true;
    31. }
    32. else {
    33. sender.sendMessage("You have to be a Player to execute this command!");
    34. return false;
    35. }
    36. }
    37. return false;
    38. }
    39.  
    40. public int getMaxTimes() {
    41. return this.maxTimes;
    42. }
    43.  
    44. public Player getPlayer() {
    45. return this.player;
    46. }
    47.  
    48. public Location getLocation() {
    49. return this.loc;
    50. }
    51.  
    52. public Location getOriginalLocation() {
    53. return this.originalLocation;
    54. }
    55.  
    56. public void setLocation(Location location) {
    57. this.loc = location;
    58. }
    59.  
    60.  
    61. }


    My scheduling task:
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.scheduler.BukkitRunnable;
    9. import org.bukkit.scheduler.BukkitTask;
    10.  
    11. public class ShowScheduler extends BukkitRunnable {
    12. private final JavaPlugin plugin;
    13.  
    14. public ShowScheduler(JavaPlugin plugin) {
    15. this.plugin = plugin;
    16. }
    17.  
    18. public void run() {
    19. CommandExecutorFireworkShow t = new CommandExecutorFireworkShow();
    20. Random generator = new Random();
    21. Location loc = t.getLocation();
    22. Location oLoc = t.getOriginalLocation();
    23. double x = loc.getX();
    24. double z = loc.getZ();
    25. double firstX = oLoc.getX();
    26. double firstZ = oLoc.getZ();
    27. int times = 0;
    28. int maxTimes = t.getMaxTimes();
    29. int positiveOrNegativeX = 0;
    30. int positiveOrNegativeZ = 0;
    31. Player player = t.getPlayer();
    32. positiveOrNegativeX = generator.nextInt(1) + 1;
    33. positiveOrNegativeZ = generator.nextInt(1) + 1;
    34. if (x - firstX >= 50) {
    35. x = x - 50;
    36. } else {
    37. if (x - firstX <= -50) {
    38. x = x + 50;
    39. } else {
    40. if (z - firstZ >= 50) {
    41. z = z - 50;
    42. } else {
    43. if (z - firstZ <= -50) {
    44. z = z + 50;
    45. } else {
    46. if (positiveOrNegativeX == 1) {
    47. x = x - generator.nextDouble()*20;
    48. if (positiveOrNegativeZ == 1) {
    49. z = z - generator.nextDouble()*20;
    50. }
    51. if (positiveOrNegativeZ == 2) {
    52. z = z + generator.nextDouble()*20;
    53. }
    54. }
    55. if (positiveOrNegativeX == 2) {
    56. x = x + generator.nextDouble()*20;
    57. if (positiveOrNegativeZ == 1) {
    58. z = z - generator.nextDouble()*20;
    59. }
    60. if (positiveOrNegativeZ == 2) {
    61. z = z + generator.nextDouble()*20;
    62. }
    63. }
    64. }
    65. }
    66. }
    67. }
    68. loc.setX(x);
    69. loc.setZ(z);
    70. t.setLocation(loc);
    71. SpawnRandomFirework.spawnRandomFirework(loc, player);
    72. if (times <= maxTimes) {
    73. times++;
    74. BukkitTask task = new ShowScheduler(plugin).runTaskLater(plugin, 100);
    75. }
    76. }
    77. }
    78.  


    and My random firework spawner class:
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Color;
    6. import org.bukkit.FireworkEffect;
    7. import org.bukkit.FireworkEffect.Builder;
    8. import org.bukkit.FireworkEffect.Type;
    9. import org.bukkit.Location;
    10. import org.bukkit.entity.EntityType;
    11. import org.bukkit.entity.Firework;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.inventory.meta.FireworkMeta;
    14.  
    15. public class SpawnRandomFirework {
    16. public static void spawnRandomFirework(Location loc, Player player) {
    17. Random generator = new Random();
    18. Firework fw = (Firework) player.getWorld().spawnEntity(loc, EntityType.FIREWORK);
    19. FireworkMeta fwm = fw.getFireworkMeta();
    20. fwm.clearEffects();
    21. int color = generator.nextInt(5) + 1;
    22. int type = generator.nextInt(4) + 1;
    23. int secondEffect = generator.nextInt(2) + 1;
    24. Builder build = FireworkEffect.builder();
    25. //Random Color
    26. if (color == 1) {
    27. build.withColor(Color.RED);
    28. }
    29. if (color == 2) {
    30. build.withColor(Color.BLUE);
    31. }
    32. if (color == 3) {
    33. build.withColor(Color.YELLOW);
    34. }
    35. if (color == 4) {
    36. build.withColor(Color.PURPLE);
    37. }
    38. if (color == 5) {
    39. build.withColor(Color.GREEN);
    40. }
    41. if (color == 6) {
    42. build.withColor(Color.ORANGE);
    43. }
    44. //Random Type
    45. if (type == 1) {
    46. build.with(Type.BALL);
    47. }
    48. if (type == 1) {
    49. build.with(Type.BALL_LARGE);
    50. }
    51. if (type == 1) {
    52. build.with(Type.BURST);
    53. }
    54. if (type == 1) {
    55. build.with(Type.CREEPER);
    56. }
    57. if (type == 1) {
    58. build.with(Type.STAR);
    59. }
    60. //Random Second Effect
    61. if (secondEffect == 1) {
    62. build.withFlicker();
    63. }
    64. if (secondEffect == 2) {
    65. build.withTrail();
    66. }
    67. if (secondEffect == 3) {
    68.  
    69. }
    70. FireworkEffect effect = build.build();
    71. fwm.addEffect(effect);
    72. fw.setFireworkMeta(fwm);
    73. }
    74. }
    75.  

    and the error message:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'fireworkshow' in plugin FireworksPlus v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
    at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:962)
    at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:880)
    at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:837)
    at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
    at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
    at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
    at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: org.bukkit.plugin.IllegalPluginAccessException: Plugin attempted to register task while disabled
    at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.validate(CraftScheduler.java:394)
    at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120)
    at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:104)
    at org.bukkit.scheduler.BukkitRunnable.runTaskLater(BukkitRunnable.java:64)
    at be.wadu.fireworkplus.CommandExecutorFireworkShow.onCommand(CommandExecutorFireworkShow.java:29)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  13. Offline

    xTrollxDudex

    wadu436
    The command executor is supposed to be registered inside a class that extends JavaPlugin.
     
  14. Offline

    wadu436

    How do i reguster it? xTrollxDudex
     
  15. Offline

    xTrollxDudex

    wadu436
    In your main class, the one that extends JavaPlugin, in the onEnable() method, put in this
    PHP:
    getCommand("fireworkshow").setExecutor(new CommandExecutorFireworkShow());
     
  16. Offline

    wadu436

    I mean the bukkitrunnable. My main looks like this:
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class FireworkPlus extends JavaPlugin{
    6. @Override
    7. public void onEnable() {
    8. getCommand("firework").setExecutor(new CommandExecutorFirework());
    9. getCommand("fireworkshow").setExecutor(new CommandExecutorFireworkShow());
    10. }
    11. }

    xTrollxDudex
     
  17. Offline

    xTrollxDudex

    wadu436
    Don't use new FireworksPlus() make a constructor like in ShowScheduler but instead of JavaPlugin, make it
    PHP:
    FireworksPlus plugin;
    public 
    CommandExecutorFireworkShow(FireworksPlus plugin){
    this.plugin plugin;
    }
    And when you register the command, you would have
    PHP:
    getCommand("fireworkshow").setExecutor(new CommandExecutorFireworkShow(this));
     
  18. Offline

    wadu436

    I got a null pointer exception problem:
    Code:
    2013-07-26 22:32:35 [WARNING] [FireworksPlus] Task #11 for FireworksPlus v1.0 generated an exception
    java.lang.NullPointerException
        at be.wadu.fireworkplus.ShowScheduler.run(ShowScheduler.java:24)
        at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftTask.run(CraftTask.java:53)
        at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:522)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    ShowScheduler:
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Location;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.scheduler.BukkitRunnable;
    10. import org.bukkit.scheduler.BukkitTask;
    11.  
    12. public class ShowScheduler extends BukkitRunnable {
    13. private final JavaPlugin plugin;
    14.  
    15. public ShowScheduler(JavaPlugin plugin) {
    16. this.plugin = plugin;
    17. }
    18.  
    19. public void run() {
    20. CommandExecutorFireworkShow executor = new CommandExecutorFireworkShow(plugin);
    21. Random generator = new Random();
    22. Location loc = executor.getLocation();
    23. Location oLoc = executor.getOriginalLocation();
    24. double x = loc.getX();
    25. double z = loc.getZ();
    26. double firstX = oLoc.getX();
    27. double firstZ = oLoc.getZ();
    28. int times = 0;
    29. int mTimes = executor.getMaxTimes();
    30. int positiveOrNegativeX = 0;
    31. int positiveOrNegativeZ = 0;
    32. Player player1 = executor.getPlayer();
    33. positiveOrNegativeX = generator.nextInt(1) + 1;
    34. positiveOrNegativeZ = generator.nextInt(1) + 1;
    35. if (x - firstX >= 50) {
    36. x = x - 50;
    37. } else {
    38. if (x - firstX <= -50) {
    39. x = x + 50;
    40. } else {
    41. if (z - firstZ >= 50) {
    42. z = z - 50;
    43. } else {
    44. if (z - firstZ <= -50) {
    45. z = z + 50;
    46. } else {
    47. if (positiveOrNegativeX == 1) {
    48. x = x - generator.nextDouble()*20;
    49. if (positiveOrNegativeZ == 1) {
    50. z = z - generator.nextDouble()*20;
    51. }
    52. if (positiveOrNegativeZ == 2) {
    53. z = z + generator.nextDouble()*20;
    54. }
    55. }
    56. if (positiveOrNegativeX == 2) {
    57. x = x + generator.nextDouble()*20;
    58. if (positiveOrNegativeZ == 1) {
    59. z = z - generator.nextDouble()*20;
    60. }
    61. if (positiveOrNegativeZ == 2) {
    62. z = z + generator.nextDouble()*20;
    63. }
    64. }
    65. }
    66. }
    67. }
    68. }
    69. loc.setX(x);
    70. loc.setZ(z);
    71. executor.setLocation(loc);
    72. SpawnRandomFirework.spawnRandomFirework(loc, player1);
    73. if (times <= mTimes) {
    74. times++;
    75. BukkitTask task = new ShowScheduler(plugin).runTaskLater(plugin, 100);
    76. }
    77. }
    78. }
    79.  

    CommandExecutorFireworkShow:
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.scheduler.BukkitTask;
    10.  
    11. public class CommandExecutorFireworkShow implements CommandExecutor {
    12. private Location loc;
    13. private int maxTimes;
    14. private Player player;
    15. private Location originalLocation;
    16. JavaPlugin plugin;
    17. public CommandExecutorFireworkShow(JavaPlugin plugin){
    18. this.plugin = plugin;
    19. }
    20. @Override
    21. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    22. if (cmd.getName().equalsIgnoreCase("fireworkshow")) {
    23. if (sender instanceof Player) {
    24. if (args.length == 1) {
    25. loc = sender.getServer().getPlayer(sender.getName()).getTargetBlock(null, 200).getLocation();
    26. maxTimes = 0;
    27. player = sender.getServer().getPlayer(sender.getName());
    28. originalLocation = loc;
    29. try {
    30. maxTimes = Integer.parseInt(args[0]);
    31. } catch (NumberFormatException e) {
    32. sender.sendMessage("Insert a Number!");
    33. return false;
    34. }
    35. BukkitTask task = new ShowScheduler(plugin).runTaskLater(plugin, 100);
    36. return true;
    37. }
    38. if (args.length == 0) {
    39. sender.sendMessage("Not Enough Arguments!");
    40. return false;
    41. }
    42. }
    43. else {
    44. sender.sendMessage("You have to be a Player to execute this command!");
    45. return false;
    46. }
    47. }
    48. return false;
    49. }
    50.  
    51. public int getMaxTimes() {
    52. return this.maxTimes;
    53. }
    54.  
    55. public Player getPlayer() {
    56. return this.player;
    57. }
    58.  
    59. public Location getLocation() {
    60. return this.loc;
    61. }
    62.  
    63. public Location getOriginalLocation() {
    64. return this.originalLocation;
    65. }
    66.  
    67. public void setLocation(Location location) {
    68. this.loc = location;
    69. }
    70.  
    71.  
    72. }

    xTrollxDudex
     
  19. Offline

    xTrollxDudex

    wadu436
    You never set location
     
  20. Offline

    wadu436

    I did:
    Code:java
    1. loc = sender.getServer().getPlayer(sender.getName()).getTargetBlock(null, 200).getLocation();

    xTrollxDudex
     
  21. Offline

    xTrollxDudex

    wadu436
    Not in the command executor
     
  22. Offline

    wadu436

    But how do i get the right values then? like the right location and player etc.
    xTrollxDudex

    BUMP plz answer.
    xTrollxDudex

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

    xTrollxDudex

    Bumping is not allowed within 24 hours.

    Back on the topic, you need to set the location you want, for example, make a command to set the location in the same class, say for player.getLocation().
     
  24. Offline

    wadu436

    But how do i get the right location in that class? because the location is set in onCommand and i want that location
    xTrollxDudex
     
  25. Offline

    xTrollxDudex

    wadu436
    I don't get what you mean.
    You have a public static getLocation() in your onCommand, that gives you the location, get another command and ser it to that executor and save a location to the location defined in the the command executor. It isn't that hard.
     
  26. Offline

    wadu436

    xTrollxDudex
    It doesnt work.
    What i want are the args from the command that was send. but that givesme a nullPointerException
     
  27. Offline

    xTrollxDudex

    wadu436
    There are numerous threads about getting locations from command args. Show code?
     
  28. Offline

    wadu436

    I get the location, i know how, but the problem is getting the location in the scheduler thread
     
  29. Offline

    xTrollxDudex

    wadu436
    So what have you done so far with the code?
     
  30. Offline

    wadu436

    xTrollxDudex
    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.scheduler.BukkitTask;
    10.  
    11. public class CommandExecutorFireworkShow implements CommandExecutor {
    12. private Location loc;
    13. private int maxTimes;
    14. private Player player;
    15. private Location originalLocation;
    16. JavaPlugin plugin;
    17. public CommandExecutorFireworkShow(JavaPlugin plugin){
    18. this.plugin = plugin;
    19. }
    20. @Override
    21. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    22. if (cmd.getName().equalsIgnoreCase("fireworkshow")) {
    23. if (sender instanceof Player) {
    24. if (args.length == 1) {
    25. String playerName = sender.getName();
    26. this.setPlayer(sender.getServer().getPlayer(playerName));
    27. this.setLocation(player.getTargetBlock(null, 200).getLocation());
    28. int mTimes;
    29. try {
    30. mTimes = Integer.parseInt(args[0]);
    31. } catch (NumberFormatException e) {
    32. sender.sendMessage("Insert a Number!");
    33. return false;
    34. }
    35. this.setMaxTimes(mTimes);
    36. this.setOriginalLocation(this.getLocation());
    37.  
    38. BukkitTask task = new ShowScheduler(plugin).runTaskLater(plugin, 100);
    39. return true;
    40. }
    41. if (args.length == 0) {
    42. sender.sendMessage("Not Enough Arguments!");
    43. return false;
    44. }
    45. }
    46. else {
    47. sender.sendMessage("You have to be a Player to execute this command!");
    48. return false;
    49. }
    50. }
    51. return false;
    52. }
    53.  
    54. public int getMaxTimes() {
    55. return this.maxTimes;
    56. }
    57.  
    58. public void setMaxTimes(int maxTimes) {
    59. this.maxTimes = maxTimes;
    60. }
    61.  
    62. public Player getPlayer() {
    63. return this.player;
    64. }
    65.  
    66. public void setPlayer(Player player) {
    67. this.player = player;
    68. }
    69.  
    70. public Location getLocation() {
    71. return this.loc;
    72. }
    73.  
    74. public Location getOriginalLocation() {
    75. return this.originalLocation;
    76. }
    77.  
    78. public void setLocation(Location location) {
    79. this.loc = location;
    80. }
    81.  
    82. public void setOriginalLocation(Location originalLocation) {
    83. this.originalLocation = originalLocation;
    84. }
    85.  
    86.  
    87. }

    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.scheduler.BukkitRunnable;
    9. import org.bukkit.scheduler.BukkitTask;
    10.  
    11. public class ShowScheduler extends BukkitRunnable {
    12. private final JavaPlugin plugin;
    13.  
    14. public ShowScheduler(JavaPlugin plugin) {
    15. this.plugin = plugin;
    16. }
    17.  
    18. public void run() {
    19. CommandExecutorFireworkShow executor = new FireworkPlus().getExecutor();
    20. Random generator = new Random();
    21. Location loc = executor.getLocation();
    22. Location oLoc = executor.getOriginalLocation();
    23. double x = loc.getX();
    24. double z = loc.getZ();
    25. double firstX = oLoc.getX();
    26. double firstZ = oLoc.getZ();
    27. int times = 0;
    28. int mTimes = executor.getMaxTimes();
    29. int positiveOrNegativeX = 0;
    30. int positiveOrNegativeZ = 0;
    31. Player player1 = executor.getPlayer();
    32. positiveOrNegativeX = generator.nextInt(1) + 1;
    33. positiveOrNegativeZ = generator.nextInt(1) + 1;
    34. if (x - firstX >= 50) {
    35. x = x - 50;
    36. } else {
    37. if (x - firstX <= -50) {
    38. x = x + 50;
    39. } else {
    40. if (z - firstZ >= 50) {
    41. z = z - 50;
    42. } else {
    43. if (z - firstZ <= -50) {
    44. z = z + 50;
    45. } else {
    46. if (positiveOrNegativeX == 1) {
    47. x = x - generator.nextDouble()*20;
    48. if (positiveOrNegativeZ == 1) {
    49. z = z - generator.nextDouble()*20;
    50. }
    51. if (positiveOrNegativeZ == 2) {
    52. z = z + generator.nextDouble()*20;
    53. }
    54. }
    55. if (positiveOrNegativeX == 2) {
    56. x = x + generator.nextDouble()*20;
    57. if (positiveOrNegativeZ == 1) {
    58. z = z - generator.nextDouble()*20;
    59. }
    60. if (positiveOrNegativeZ == 2) {
    61. z = z + generator.nextDouble()*20;
    62. }
    63. }
    64. }
    65. }
    66. }
    67. }
    68. loc.setX(x);
    69. loc.setZ(z);
    70. executor.setLocation(loc);
    71. SpawnRandomFirework.spawnRandomFirework(loc, player1);
    72. if (times <= mTimes) {
    73. times++;
    74. BukkitTask task = new ShowScheduler(plugin).runTaskLater(plugin, 100);
    75. }
    76. }
    77. }
    78.  

    Code:java
    1. package be.wadu.fireworkplus;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class FireworkPlus extends JavaPlugin{
    6. public CommandExecutorFireworkShow executor = new CommandExecutorFireworkShow(this);
    7. @Override
    8. public void onEnable() {
    9. getCommand("firework").setExecutor(new CommandExecutorFirework());
    10. getCommand("fireworkshow").setExecutor(executor);
    11. }
    12.  
    13. public CommandExecutorFireworkShow getExecutor() {
    14. return this.executor;
    15. }
    16. }
    17.  
     
Thread Status:
Not open for further replies.

Share This Page