Spawning skeleton with item in hand

Due some problems ( Mob Spawning in Nether issue ) with wither skeleton spawning in the nether fortress, Im writing some fix plugin.

Spawning entity:

    public void spawnEntity(Location spawnLocation) {
        World world = spawnLocation.getExtent();
        Entity witherskeleton = world
            .createEntity(EntityTypes.WITHER_SKELETON, spawnLocation.getPosition());
        SpawnCause spawnCause = SpawnCause.builder().type(SpawnTypes.PLUGIN).build();
        
        world.spawnEntity(witherskeleton, Cause.source(spawnCause).build());
    }

Howto add item to skeleton hand?

When using forge, its simple:

skeletonentity.setHeldItem(EnumHand.MAIN_HAND, new ItemStack(Items.BOW));

Howto with sponge?

typecast to ArmorEquipable then you will be able to access methods such as setItemInHand, setBoots …

Im tried this:

    ArmorEquipable ae = (ArmorEquipable) witherskeleton;
    ae.setItemInHand(HandTypes.MAIN_HAND, ItemStack.of(ItemTypes.STONE_SWORD, 1));

but does not work.

final Location<World> loc; //get a location from somewhere.
final WitherSkeleton ent = (WitherSkeleton) loc.createEntity(EntityTypes.WITHER_SKELETON);
final ItemStack weapon = ItemStack.builder().itemType(ItemTypes.BOW).build();
ent.setItemInHand(HandTypes.MAIN_HAND, weapon);
loc.spawnEntities(ent, Cause.source(spawnCause)); // Your cause.

This will do what you want. if it doesn’t file a bug report.

Hmm still not work…

[14:57:57 ERROR] [Sponge]: Could not pass SpawnEntityEvent$ChunkLoad$Impl to Plugin{id=fixspawning, name=Fix Spawning Issues, version=1.0, description=fixes, source=mods/fixspawning-0.1.jar}
java.lang.AbstractMethodError: Method net/minecraft/entity/monster/EntityWitherSkeleton.setItemInHand(Lorg/spongepowered/api/data/type/HandType;Lorg/spongepowered/api/item/inventory/ItemStack;)V is abstract
at net.minecraft.entity.monster.EntityWitherSkeleton.setItemInHand(SourceFile) ~[aan.class:?]

Report that on https://github.com/SpongePowered/SpongeCommon/issues and I’ll look into that later today. I suspect this will be a quick fix, but I’m not at my computer right now to work on it.

Thanks a lot.

It works now, thanks for fix!