Durability Bar

Discussion in 'Plugin Development' started by Ghost105, Dec 2, 2017.

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

    Ghost105

    Hi,
    I'm trying to make a zombie survival plugin, and wanted to make a reload bar for a grenade launcher. I'm trying to make it so the durability bar goes down with each shot, and when you reload it slowly goes back up. The only trouble I've had is with the durability going back up. Currently, the durability bar pauses for 3 seconds and then instantly sets to max. Anyone know the code for how i would make the bar go back up slowly?

    code:
    Code:
    short minus = 1560; // the durability taken away per shot
    short increm = -10; //the durability added back per second
    if(!(e.getItem().getType() == Material.DIAMOND_SPADE)) return; // if the item isnt a dia shovel end code
    
    if((e.getAction()==Action.RIGHT_CLICK_AIR || e.getAction()==Action.RIGHT_CLICK_BLOCK)) { //if right click with a diamond spade
        if(e.getItem().getDurability() >= 1560) { // if out of ammo
            e.getItem().setDurability((short)1560);
            player.sendMessage("Left-Click to reload!"); //sends a message to player to left click to reload
        }
       
        else {
        e.getItem().setDurability((short) (e.getItem().getDurability()+minus)); //take away durablility
        Snowball s = e.getPlayer().launchProjectile(Snowball.class); //launch snowball
        s.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10); //play a smoke effect at the shooter's location
    }
    }
    
    if((e.getAction()==Action.LEFT_CLICK_AIR || e.getAction()==Action.LEFT_CLICK_BLOCK) && e.getItem().getType()==Material.DIAMOND_SPADE) {
            player.sendMessage("Reloading");
    
            if(e.getItem().getDurability() > 0) {
                try       
                {
                    Thread.sleep(1000);
                }
                catch(InterruptedException ex)
                {
                    Thread.currentThread().interrupt();
                }
                e.getItem().setDurability(increm);
            }
            else {
                player.sendMessage("Loaded");
            }
    
    }
     
  2. Offline

    Caderape2

    @Ghost105
    You are stopping the whole server with his. Use a repeating task instead.
     
  3. Offline

    MightyOne

    Well yeah you definitely shoud not use Thread.sleep(). Bukkit provides Bukkit.getScheduler() or even better a BukkitRunnable
     
  4. Offline

    Ghost105

    Thanks! Can someone show me an example of how i would use that?
     
  5. Your goal is to slowly make the durability go up. Real actions in minecraft can happen once every tick, which normally is 20 times a second when running at optimum speed (plugin data and code can run at any time). So to do this, you'd want to increase the durability a bit every tick and then stop once it's reloaded. Or you could have the durability go from empty... then after 3 seconds... set it instantly to full (which may end up with a chunkier reloading feel, which you may or may not like).

    To have a BukkitRunnable that will do this upon running, you'll need to either extend a BukkitRunnable or create an anonymous class of it, and of course override the run task. Then just schedule it, either from within the runnable or in the code above. For a better tutorial on runnables, look at https://bukkit.gamepedia.com/Scheduler_Programming
     
Thread Status:
Not open for further replies.

Share This Page