No more event.getEntity().setHealth?

Discussion in 'Plugin Development' started by xCyclonez, Jun 28, 2017.

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

    xCyclonez

    I was wondering if there is a way to set an entity's health when a player hits it.
     
  2. Offline

    mehboss

    void setHealth(double health)
    Sets the entity's health from 0 to getMaxHealth(), where 0 is dead.
    void setMaxHealth(double health)

    Use the EntityDamageByEntity event. ;)
     
  3. Offline

    xCyclonez

    So what do you mean @mehboss? make a public void setHealth(double health)
    and setMaxHealth?
     
  4. Offline

    mehboss

    #getEntity#setMaxHealth
     
  5. Offline

    xCyclonez

    @mehboss
    Code:
    public void onMobHit(EntityDamageByEntityEvent event) {
          
            ItemStack stick = new ItemStack(Material.STICK);
            ItemMeta stickmeta = stick.getItemMeta();
            stickmeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick");
            stick.setItemMeta(stickmeta);
            stick.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
            stick.addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 4);
            if(event.getEntity() instanceof Player && event.getDamager() instanceof Player) {
                Player p = (Player) event.getDamager();
                if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                    p.sendMessage(ChatColor.RED + "You cannot hit another player with this!");
                event.setCancelled(true);
              
              
            }
                if(event.getEntity() instanceof Cow && event.getDamager() instanceof Player) {
                    if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                    ((Damageable) event.getEntity()).setHealth(0);
                }
                }
                if(event.getEntity() instanceof Chicken && event.getDamager() instanceof Player) {
                    if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                    ((Damageable) event.getEntity()).setHealth(0);
                }
                }
                if(event.getEntity() instanceof Pig && event.getDamager() instanceof Player) {
                    if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                    ((Damageable) event.getEntity()).setHealth(0);
                }
            }
                if(event.getEntity() instanceof Sheep && event.getDamager() instanceof Player) {
                    if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                    ((Damageable) event.getEntity()).setHealth(0);
                }
            }
        }
    
    }
    }
    Can you help me fix this?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @xCyclonez Your mob checks are located in the player vs player block.
    Hit ctrl+shift+f to see what I am talking about.
     
  7. Offline

    xCyclonez

    @timtower I can still hit players and it does not one shot the mobs
     
  8. Offline

    timtower Administrator Administrator Moderator

    @xCyclonez Can you repost your formatted code?
     
  9. Offline

    xCyclonez

    Code:
    public void onMobHit(EntityDamageByEntityEvent event) {
    
            ItemStack stick = new ItemStack(Material.STICK);
            ItemMeta stickmeta = stick.getItemMeta();
            stickmeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick");
            stick.setItemMeta(stickmeta);
            stick.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
            stick.addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 4);
          
            Player p = (Player) event.getDamager();
            if (event.getEntity() instanceof Player && event.getDamager() instanceof Player) {
                if (p.getItemInHand().getItemMeta().getDisplayName()
                        .equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                    p.sendMessage(ChatColor.RED + "You cannot hit another player with this!");
                    event.setCancelled(true);
                }
                }
                if (event.getEntity() instanceof Cow && event.getDamager() instanceof Player) {
                    if (p.getItemInHand().getItemMeta().getDisplayName()
                            .equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                        ((Damageable) event.getEntity()).setHealth(0);
                    }
                }
                if (event.getEntity() instanceof Chicken && event.getDamager() instanceof Player) {
                    if (p.getItemInHand().getItemMeta().getDisplayName()
                            .equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                        ((Damageable) event.getEntity()).setHealth(0);
                    }
                }
                if (event.getEntity() instanceof Pig && event.getDamager() instanceof Player) {
                    if (p.getItemInHand().getItemMeta().getDisplayName()
                            .equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                        ((Damageable) event.getEntity()).setHealth(0);
                    }
                }
                if (event.getEntity() instanceof Sheep && event.getDamager() instanceof Player) {
                    if (p.getItemInHand().getItemMeta().getDisplayName()
                            .equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
                        ((Damageable) event.getEntity()).setHealth(0);
                    }
                }
            }
    
        }
    @timtower
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    xCyclonez

    @timtower how do I do that?

    Also I would still beable to hit another player
     
  12. Offline

    timtower Administrator Administrator Moderator

  13. Offline

    xCyclonez

    So would it be like this?
    if (event.getEntity() instanceof Sheep && event.getDamager() instanceof Player) {
    if (p.getItemInHand().getItemMeta().getDisplayName()
    .equals(ChatColor.RED + "" + ChatColor.BOLD + "Grind Stick")) {
    BukkitTask task = new GrindingStick(this.plugin).runTaskLater(this.plugin, 1);
    ((Damageable) event.getEntity()).setHealth(0);
    }
    }
    }
    @timtower
     
Thread Status:
Not open for further replies.

Share This Page