1.12.2 How Do I Do Gun Reload?

Discussion in 'Spigot Plugin Development' started by ytredstone, Nov 12, 2020.

  1. I'm writing gun plugin for my BattleRoyale Server.
    So, I don't know how to do reload in code. My current algorythm:
    1. Check If we are already reloading (HashMap reload<Playername, Is On Reload>)
    2. If we aren't reloading:
    3. runtasktimer()
    4. set item durability to 0 (Max Durablilty)

    But it is not working properly.

    Here is the code:
    Code (Text):
    public static void reload(Player player) {
            if(countAmmo(player, getGun(player.getItemInHand().getType())) >= getGun(player.getItemInHand().getType()).getMagazine()) {
                if(TLFortGuns.onReload.containsKey(player.getName())) {
                    player.sendTitle("",ChatColor.RED + "Уже перезаряжаемся!");
                } else {
                    TLFortGuns.onReload.put(player.getName(), true);
                    Bukkit.getScheduler().runTaskTimer(TLFortGuns.getMainPlugin(), new Runnable() {
                        int time = getGun(player.getItemInHand().getType()).getReloadTime();
                        public void run() {
                            if (this.time == 0) {
                                if(player.getItemInHand().getDurability() == player.getItemInHand().getType().getMaxDurability() - 1) {
                                    player.getItemInHand().setDurability((short) 0);
                            player.sendTitle("",ChatColor.RED + "Ждите");
                    }, 0L, 1L);
            } else {  
                player.sendTitle("",ChatColor.RED + "Закончились патроны!");
    #1 ytredstone, Nov 12, 2020
    Last edited: Nov 12, 2020
  2. Why is there an if at the middle of the code?
  3. Why is that a static method? Sounds like static abuse. Prove me wrong.
    • Agree Agree x 1
  4. In the map store the UUID of the Player That is reloading (or not) and a timestamp (System.currentTimeMilis()). If the player shot -> put him into the map. On reload -> check the map and if the current time minus the stored time (or 0 per default) is greater than your threshold, allow reload, else deny.
  5. Uh oh, just a little bug. It was fixed, but only once it reloads gun with delay, as it should, but then - immediate reload
  6. joehot200


    Are you cancelling your repeating task?
    • Informative Informative x 1
  7. Umm, no. I think that is a problem.
    How do I do this in code? I need an example
  8. joehot200


    You can use
    Code (Java):

    BukkitTask task = Bukkit.getScheduler().runTaskTimer(TLFortGuns.getMa.........

    There is also
    Code (Java):

    int taskid = task.getTaskId();
    to cancel the task by it's integer ID.