Dirkx Senne
Dirkx Senne

Reputation: 93

How do setMaxHealth to custom EntitySkeleton?

I want to create a custom Skeleton that has a name, more health and holds other custom items. I can add a name and setHealth(), but I can't setMaxHealth() and also setting items and armor just won't work.

Thanks for helping, here is my code:

Player p = (Player) sender;
WorldServer world = ((CraftWorld)p.getWorld()).getHandle();
Location loc = p.getLocation();

if (args.length > 0) {
            if (args[0].equalsIgnoreCase("define")) {
                //get worldedit selection
                if (getWorldEdit().getSelection(p) == null) {
                    p.sendMessage(title + "Please select a region with WorldEdit");
                    return false;
                }
                s = getWorldEdit().getSelection(p);
                Location min = s.getMinimumPoint();
                Location max = s.getMaximumPoint();

                //boss mob creation
                EntitySkeleton boss = new EntitySkeleton(world);
                boss.setHealth(400);
                boss.setCustomName("§4§lDAFT BOSS");
                boss.setCustomNameVisible(true);

                ItemStack weapon = new ItemStack(Material.DIAMOND_SWORD);
                weapon.setDurability((short) 0);
                weapon.addEnchantment(Enchantment.DAMAGE_ALL, 5);
                weapon.addUnsafeEnchantment(Enchantment.KNOCKBACK, 2);



                boss.setLocation(max);
                world.addEntity(boss);


            }

Upvotes: 1

Views: 3054

Answers (1)

LeoColman
LeoColman

Reputation: 7143

That can be obtained using the Attributable interface, as said in this thread from Spigot.


Example:

For 1.9 and over:

Entity boss;
Attributable bossAttributable = (Attributable) boss;
AttributeInstance ai = bossAttributable.getAttribute(Attribute.GENERIC_MAX_HEALTH);
ai.setValue(400.0);

For 1.8.8 and lower, this must be done another way:

Entity boss;
Damageable bossDamageable = (Damageable) boss;
bossDamageable.setMaxHealth(400.0);

Upvotes: 2

Related Questions