Enderpearl cooldown with xp bar

Discussion in 'Programming' started by HK_Sushi, Jan 29, 2018.

  1. 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 :*(
     

    Attached Files:

    #1 HK_Sushi, Jan 29, 2018
    Last edited: Jan 29, 2018
  2. Next time you’d better put your code in the code tags or hastebin.
     
    • Useful Useful x 1
  3. @Marido Okay I will do it and do you have any idea ?
     
  4. 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;
        }
    }
     
     
  5. 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)
     
  6. @Arektor
    So should I edit the p.setexp
    Or
    The int time
    ?
     
  7. 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
     
  8. So I need a scheduler for set exp and a scheduler for player interact and execute enderpearl cooldown ?
     
  9. 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.
     
  10. 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
     
  11. 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?!
    [​IMG]
     
    • Agree Agree x 2
  12. 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
     
  13. Yup, everything you do inside the command, you could just do it inside the player interact event.
     
  14. Okay so if I don't editing that I will send a code here and hope you will check if I am doing right :)
     
  15. Wait @Arektor do I need a delayed scheduler for ender pearl cool down ?
     
  16. Like the code I first posted which make a delay so they can only use it after arraylist cooldown remove them
     
  17. 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.
     
  18. So I don't need to make the enderpearl cooldown part ?
    I'm editing it
    The code will be soon done
     
  19. Btw what should I put after the .runTaskTimer ?
    Should I make it to this or the class name?
     
  20. I have coded this
    Hope I'm doing the right thing
    @Arektor
     

    Attached Files: