Delayed command onEnable

Discussion in 'Plugin Development' started by LaxWasHere, Nov 18, 2012.

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

    LaxWasHere

    I'm trying to make a command only usable after a specific amount of time has passed (configurable in seconds in the config) after the server starts.

    I was told to save the time when the server started, then check if (timeNow - delay > startUpTime)
    using System.currentTimeMillis(); though I have no idea how to do all that.
     
  2. Offline

    Ranzdo

    Somthing like this would work if it is what you want.

    Code:java
    1.  
    2. private long startUpTime;
    3.  
    4. //Since you want this plugin to have it count from when the server starts using onLoad should be a nice approch
    5. public void onLoad() {
    6. startUpTime = System.currentTimeMillis();
    7. }
    8.  
    9. public void onCommand(CommandSender sender, Command command, String label, String[] args) {
    10. if(System.currentTimeMillis()-startUpTime > 300000) {
    11. //5 min has past (300000 millisec)
    12. }
    13. }
    14.  
     
  3. Offline

    LaxWasHere

    Hmm, will this work? http://lazle.us/UOFYZ2


    I think I need to put
    Code:
    getConfig.getInt(stopTime);
    
    somewhere
     
  4. Do the config lax I looked over your code and I'm 85% Sure it will work
     
  5. Offline

    LaxWasHere

    And I'm also sure that I don't know the 85% of the code needed to complete this. xD

    Anyone? Need the time to be configurable though idk where to put it
    Code:
    get.Config.getInt(stopTime)
    Updated code. http://lazle.us/UO8xjX
     
  6. Offline

    Jogy34

    in you onEnable method set a boolean to true then create a delayed task for however long you have to wait then in it set the boolean to true (you will probably have to go through a method to do this) and if someone tries to use the command while the boolean is false then don't do anything
     
  7. Offline

    leiger


    Change that to:

    Code:
    int timeToWait = plugin.getConfig().getInt(stopTime);
    
    Depending on where you are running it from, plugin can either be omitted completely, or it is a reference to the main class for your Plugin.
     
  8. Offline

    LaxWasHere

    Current progress.
    Still can't figure out how to make the delay.

    Code:java
    1. package net.lazlecraft.Stop;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Stop extends JavaPlugin implements Listener {
    13.  
    14. public String prefix = "[Stop]";
    15. public String cmds = "cmds";
    16. public int stopTime;
    17. public boolean pNotify = true;
    18. public int basePlayers;
    19. private long startUpTime;
    20.  
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile plugin = getDescription();
    24. System.out.println(plugin.getName() + " version " + plugin.getVersion() + " by LaxWasHere enabled.");
    25.  
    26. getConfig().options().copyDefaults(true);
    27. this.saveDefaultConfig();
    28. // # of seconds for the command to be enabled
    29. stopTime = getConfig().getInt("DelayStopCommand");
    30. // Should we notify players that the command is available?
    31. pNotify = getConfig().getBoolean("NotifyPlayers");
    32. // # of players that must be on to allow the command
    33. basePlayers = getConfig().getInt("BasePlayers");
    34. // get the commands from the config
    35. cmds = getConfig().getString("Commands");
    36.  
    37.  
    38.  
    39. this.getServer().getPluginManager().registerEvents(this, this);
    40.  
    41. }
    42.  
    43. @Override
    44. public void onDisable() {
    45. PluginDescriptionFile plugin = getDescription();
    46. System.out.println(plugin.getName() + " version " + plugin.getVersion() + " by LaxWasHere disabled.");
    47. }
    48. public void onLoad() {
    49. if(System.currentTimeMillis()-startUpTime > 300000) {
    50.  
    51. }
    52. }
    53.  
    54.  
    55. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    56. if (!(sender instanceof Player)) {
    57. sender.sendMessage(prefix + " Please use .hold or .stopwrapper to stop the server");
    58. return false;
    59. }
    60.  
    61.  
    62. Player p = (Player)sender;
    63. if (commandLabel.equalsIgnoreCase("stop"))
    64. {
    65. if(System.currentTimeMillis()-startUpTime > 300000)
    66. // Getting the base of the player.
    67. if(getServer().getOnlinePlayers().length > basePlayers)
    68. p.sendMessage(prefix + ChatColor.GREEN + "You are the first person to typed /stop!");
    69. Bukkit.broadcastMessage(sender.getName() + " Won the game! Server is now restarting.");
    70. // do stuff here
    71.  
    72. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), getConfig().getString(cmds).replace("%player%", sender.getName()));
    73.  
    74.  
    75. //Shuts down bukkit for RTK to restart
    76. Bukkit.shutdown();
    77. return true;
    78. }
    79. {
    80. //If the base player is < this will be sent (hopefully)
    81. p.sendMessage(prefix + ChatColor.RED + "The command is not yet available.");
    82. return false;
    83.  
    84. }
    85. }
    86. }
    87.  
    88.  
     
  9. Offline

    Tirelessly

    if(currentTime - first time the command was sent > delay)

    Hashmaps for when command was sent. I use currentTimeMillis()/1000 to get it to seconds.
     
Thread Status:
Not open for further replies.

Share This Page