Search code examples
javacountdownbukkit

Bukkit Item LIVE Countdown


I am making a plugin that you could use an item with a spell on it but I have a problem I don't know how to make a scheduler and how to get the player in that method . Also I this is going to be for more then 1 player.

I also want to make it that I can call the method like:

 public void playertimer (Player player, Integer time) {
     // Do things
 }

 public void stoptimer(Player player) {
     // Do things
 }

I also want to be able to see the countdown in the item bar.


Solution

  • I'v got it here is my code that I use: MainClass mainclass;

    public commands(MainClass plugin) {
        mainclass = plugin;
    }
    int cooldown;
    int i = 10;
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        final Player player = (Player) sender;
    
        if(cmd.getName().equalsIgnoreCase("timer")) {
            cooldown = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(mainclass, new Runnable(){
    
                public void run(){
                    if(i != -1){
                        if(i != 0){
                            Bukkit.broadcastMessage(ChatColor.AQUA + "" + i);
                            ItemStack test = Manager.getManager().setNameAndLore(Material.APPLE, i, "lolz", "lolzz");
                            ItemStack testremove = Manager.getManager().setNameAndLore(Material.APPLE, 64, "lolz", "lolzz");
                            player.sendMessage(mainclass.getConfig().getBoolean("wom.players." + player + ".rogue") + "");
                            player.getInventory().removeItem(testremove);
                            player.getInventory().addItem(test);
                            i--;
                        }else{
                            Bukkit.broadcastMessage(ChatColor.GREEN + "begin");
                            i--;
                            Bukkit.getServer().getScheduler().cancelTask(cooldown);
    
                        }
                    }
                }
    
            },0L ,20L );
            i = 10;
        }