How do I make a sword that less hp you have more strength you get

Discussion in 'Plugin Development' started by wooethan, Jun 6, 2015.

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

    wooethan

    I want to make a sword that when you holding on your hand, It will have a potion effect after decreasing some HP. The less HP you have the more Strength Potion level you got. While Holding other items will automatically disable those effects.

    Please help me figure out , cause now its still not working.
    Code:
        @EventHandler
        public void PlayerInventoryEvent(PlayerItemHeldEvent evt){
            Player player = evt.getPlayer();
            if(player.getItemInHand().getType() == Material.DIAMOND_SWORD) {
            if(player.getItemInHand().getItemMeta().getDisplayName() == "§dBerserker §eSword"){
            if(player.getHealth() < 15 ){
             player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 1));
             }
             if(player.getHealth() < 8){
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 2));
                }
             if(player.getHealth() < 6){
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 3));
                }
             if(player.getHealth() < 5){
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 4));
                }
             if(player.getHealth() < 3){
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 5));
                }
             if(player.getHealth() < 2){
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 7));
                }
            }
            }
               
            }
     
  2. Offline

    kaif21

    @wooethan
    Maybe a scheduler that runs every second and loop all online players and check their health and do what u did
    ............
     
  3. Offline

    Monollyth

    I wouldn't use a HeldItem event for this. Try using a scheduler that runs every 'x' amount of ticks, and check if the player's health is a certain integer, and if so, add the strength.
     
  4. Offline

    wooethan

    Why is my code not working and , can you explain of making scheduler ?
    I saw only you fill the players name and setSwag?

    Oh Okay thanks I will try that

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jun 6, 2015
  5. Offline

    Monollyth

    @wooethan You would create a method, the name of your choice, and inside that method, you would create a BukkitRunnable, or a repeating task that would run every 'x' (your choice) amount of ticks. When it runs, you would loop through every player on the server and check their health. If their health is a certain integer, set their strength.
     
  6. Invisible

    Reynergodoy

    what are you making?
    there are none ELSEs there
     
  7. Offline

    wooethan

    Can you show an example for me please ? I'm still a beginner in coding . I only know basics
    Please guide me thanks :(
     
  8. Offline

    Monollyth

    @wooethan Here is an example.

    Code:
    public void taskTimer(final Plugin plugin)
    {
    new BukkitRunnable()
        {
           for(Player online : Bukkit.getOnlinePlayers())
           {
                  if(online.getHealth() == x)
                  {
                           //ADD STRENGTH
                  }
           }
        }.runTaskTimer (plugin, 0L, 20L);
    }
    
     
  9. Offline

    SuperOriginal

    Instead of a scheduler, why not just check EntityDamageEvent and EntityRegainHealthEvent?
     
  10. Offline

    wooethan

    Will EntityDamageEvent and EntityRegainHealthEvent work in this project?
     
  11. Offline

    SuperOriginal

    Yes, it's an alternative to schedulers as there is no point in updating their strength if their health hasn't changed.
     
  12. Offline

    wooethan

    Whats the different btw EntityDamageEvent and EntityDamagebyEntityEvent and How do I use EntityRegainHealth to check players health? Sorry I'm a novice only
     
  13. Offline

    redside100

    Just make it so whenever a player hits another player with that sword, it checks the player's health. If the player's health is lower than n, then set the final damage as something different.
     
  14. Offline

    Boomer

    entity damage event will trigger from drowning, fire, suffocation, fallling...
    entity damage by entity will trigger from strikes by mobs/players, projectiles, etc
     
Thread Status:
Not open for further replies.

Share This Page