Hey Recently I am trying to make a plugin for my Servers which is enderpearl cooldwnc with Xp bar I think most of you guys have heard some Famous practice PvP Server like arcane / mineman club / velt PvP I saw they have a xp bar count down after they have thrown an enderpearl like this And I want to make the same like them What I have now is https://pastebin.com/abKnLrnF This is the link of the code Download the txt Please someone help me :*(
Code (Text): package me.samsoon; import java.util.ArrayList; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; public class cooldown implements Listener, CommandExecutor{ int time; ArrayList<Player> cooldown = new ArrayList<Player>(); @EventHandler public void EnderPearl(PlayerInteractEvent e) { final Player p = e.getPlayer(); ItemStack pearl = new ItemStack(Material.ENDER_PEARL); if(p.getGameMode() == GameMode.SURVIVAL) { if(p.getInventory().getItemInHand() == pearl) { if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) { p.performCommand("epearlcd"); } } } } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { final Player p = (Player)sender; time = 16; if(label.equalsIgnoreCase("epearlcd")) { if(cooldown.contains(p)) { p.sendMessage(ChatColor.RED + "You cannot throw an ender pearl yet"); return true; } if(p instanceof Player) { time = time -1; p.setLevel(time); p.setExp(time / (float) 16); cooldown.add(p); Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() { public void run() { cooldown.remove(p); } }, 20 * 16); return true; } } return false; } }
The method player#setExp is the one you will want to use. The given argument (of float type) is, on a scale between 0 and 1, how much you want the xp bar to be filled. 0 being empty, 1 being full. This is the same math as percentage: Lets say you want a 16 sec cooldown. 16 time 1 ? To get the filling percentage related to the time left on the cooldown, you want to simply divide your time variable by the full cooldown value. (time*1/16= time/16, admitting 16 is your cooldown time)
You probably want to make a repeated scheduler, to execute the setExp method every tick with the updated time value, so the exp bar will progressively go down. Because right now you are only calling the setExp method once: this isn't magic and won't fetch the time value every time it changes for you, you will have to do it yourself
So I need a scheduler for set exp and a scheduler for player interact and execute enderpearl cooldown ?
You actually only need one scheduler. Lemme give you an example of what I'm doing for a kit of my minigame: Code (Text): int duration = 16; // The initial cooldown value new BukkitRunnable() { int countdown = duration; // We set the countdown var to the initial duration at the beginning @Override public void run() { // This if statement is just so I'm cancelling the cooldown if the player died in the game or anything that would require it to stop. You may not need it. if (g.playing == null || !g.playing.status.equals("started") || !g.alive || g.kit == null || !(g.kit instanceof Skeleton)) { p.setExp(0F); this.cancel(); return; } // // This if statement is to check, if the countdown is at 0 or below - so, cooldown has ended, we cancel the scheduler and remove the player from its cooldown if (countdown <= 0) { p.setExp(0F); g.cooldown1 = 0; // This would be "cooldown.remove(p);" for you this.cancel(); return; } p.setExp(1F * (countdown/duration)); // Here we are setting the exp bar countdown--; // And finally, removing 1 to the countdown var } }.runTaskTimer(plugin, 0L, 1L); // 0L is the delay before starting to run, so here none. 1L is how often the scheduler will repeat himself, here every tick.
Ooooooooooooo so I do not need the code above the first return of your code and use the second part of your code Alright i understand this I think this code is put in the command of the epearl cooldown? And do you see any error of my cooldown command ? @Arektor
I'm so glad you asked this. It's tilting me from the beginning. WHY THE HELL DO YOU USE A COMMAND TO TRIGGER A COOLDOWN?!
lOL If I am right I should use a Player interact event for the cooldown while insert the scheduler of the setexp into the player interact event ? Sorry for triggering you Face palm
Like the code I first posted which make a delay so they can only use it after arraylist cooldown remove them
No you dont, you only need the repeated scheduler that will count down to 0. Once it reaches 0, cancel it and remove the player from the cooldown list.