Delay

Discussion in 'Plugin Development' started by CodX, Dec 1, 2018.

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

    CodX

    Hello !
    How can i make a delay for a day after the command was executed ?

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
            {  
                // Server Description
                if (cmd.getName().equalsIgnoreCase("dice"))
                {
                    Random rand = new Random();
                    Player player = ((OfflinePlayer) sender).getPlayer();
                    int dice1 = rand.nextInt(6)+1;
                    int dice2 = rand.nextInt(6)+1;
                   
                    player.sendMessage("§8[§bDICE§8] §fDice were thrown away...");
                    // Here delay for a day without can be possible to execute again the command, and can't be possible to reconnect and then use the command again.
                }
                return false;
            }
                    
     
  2. Offline

    Zombie_Striker

  3. Offline

    CodX

    @Zombie_Striker
    I tried to use that and when I reconnect to server i can execute again the command.
     
  4. Online

    timtower Administrator Administrator Moderator

    What code did you use?
     
  5. Offline

    CodX

    This:
    Code:
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    
    public final class ExamplePlugin extends JavaPlugin {
    
    @Override
    public void onEnable() {
    new ExampleListener(this);
    }
    }
    
    class ExampleListener implements Listener {
    
    private final ExamplePlugin plugin;
    
    public ExampleListener(ExamplePlugin plugin) {
    this.plugin = plugin;
    this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
    
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
    // Create the task and schedule to run it once, after 20 ticks
    BukkitTask task = new ExampleTask(this.plugin).runTaskLater(this.plugin, 20);
    }
    
    }
     
    Last edited by a moderator: Dec 1, 2018
  6. Online

    timtower Administrator Administrator Moderator

    @CodX I see nothing related to the question in the first post though.
     
  7. Offline

    Zombie_Striker

    Well, what is ExampleTask? Does it have any code that is supposed to stop the player from sending the command?
     
  8. Offline

    CodX

    I solved the problem.
    I had used a global boolean and the classic runnable bukkit and it work :).
    Thank you for help ! :)
     
  9. Online

    timtower Administrator Administrator Moderator

    And how does that work with multiple players?
     
  10. Offline

    CodX

    I don't know if work multiplayer, now i develop this plugin.
    The server will be multiplayer next week.
     
  11. Online

    timtower Administrator Administrator Moderator

    If you have the cooldown then other players will have the same cooldown.
     
  12. Offline

    CodX

    Hmm...
    How can i solve this ?
     
  13. Online

    timtower Administrator Administrator Moderator

    You need to save the timestamp when the command is allowed to be used again by that player. That would be in a map that gets saved to the config in the onDisable and loaded on onEnable.
     
  14. Offline

    CodX

    Like a database, i'm right ?
     
  15. Online

    timtower Administrator Administrator Moderator

    Config is probably easier for this.
     
  16. Offline

    CodX

    How can i create a config.yml and get data from there ?
     
Thread Status:
Not open for further replies.

Share This Page