Search in sources :

Example 1 with EnderPearl

use of org.spongepowered.api.entity.projectile.EnderPearl in project SpongeCommon by SpongePowered.

the class LevelMixin method bridge$createEntity.

@Override
@SuppressWarnings("unchecked")
public <E extends org.spongepowered.api.entity.Entity> E bridge$createEntity(final EntityType<E> type, final Vector3d position, final boolean naturally) throws IllegalArgumentException, IllegalStateException {
    if (type == net.minecraft.world.entity.EntityType.PLAYER) {
        // Unable to construct these
        throw new IllegalArgumentException("A Player cannot be created by the API!");
    }
    net.minecraft.world.entity.Entity entity = null;
    final double x = position.x();
    final double y = position.y();
    final double z = position.z();
    final net.minecraft.world.level.Level thisWorld = (net.minecraft.world.level.Level) (Object) this;
    // Not all entities have a single World parameter as their constructor
    if (type == net.minecraft.world.entity.EntityType.LIGHTNING_BOLT) {
        entity = net.minecraft.world.entity.EntityType.LIGHTNING_BOLT.create(thisWorld);
        entity.moveTo(x, y, z);
        ((LightningBolt) entity).setVisualOnly(false);
    }
    // TODO - archetypes should solve the problem of calling the correct constructor
    if (type == net.minecraft.world.entity.EntityType.ENDER_PEARL) {
        final ArmorStand tempEntity = new ArmorStand(thisWorld, x, y, z);
        tempEntity.setPos(tempEntity.getX(), tempEntity.getY() - tempEntity.getEyeHeight(), tempEntity.getZ());
        entity = new ThrownEnderpearl(thisWorld, tempEntity);
        ((EnderPearl) entity).offer(Keys.SHOOTER, UnknownProjectileSource.UNKNOWN);
    }
    // set them is to use the more specialised constructor).
    if (type == net.minecraft.world.entity.EntityType.FALLING_BLOCK) {
        entity = new FallingBlockEntity(thisWorld, x, y, z, Blocks.SAND.defaultBlockState());
    }
    if (type == net.minecraft.world.entity.EntityType.ITEM) {
        entity = new ItemEntity(thisWorld, x, y, z, new ItemStack(Blocks.STONE));
    }
    if (entity == null) {
        final ResourceKey key = (ResourceKey) (Object) Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) type);
        try {
            entity = ((net.minecraft.world.entity.EntityType) type).create(thisWorld);
            entity.moveTo(x, y, z);
        } catch (final Exception e) {
            throw new RuntimeException("There was an issue attempting to construct " + key, e);
        }
    }
    if (entity instanceof HangingEntity) {
        if (!((HangingEntity) entity).survives()) {
            throw new IllegalArgumentException("Hanging entity does not survive at the given position: " + position);
        }
    }
    if (naturally && entity instanceof Mob) {
        // Adding the default equipment
        final DifficultyInstance difficulty = this.shadow$getCurrentDifficultyAt(new BlockPos(x, y, z));
        ((MobAccessor) entity).invoker$populateDefaultEquipmentSlots(difficulty);
    }
    if (entity instanceof Painting) {
        // This is default when art is null when reading from NBT, could
        // choose a random art instead?
        ((Painting) entity).motive = Motive.KEBAB;
    }
    return (E) entity;
}
Also used : DifficultyInstance(net.minecraft.world.DifficultyInstance) ThrownEnderpearl(net.minecraft.world.entity.projectile.ThrownEnderpearl) LightningBolt(net.minecraft.world.entity.LightningBolt) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) BlockPos(net.minecraft.core.BlockPos) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Mob(net.minecraft.world.entity.Mob) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EnderPearl(org.spongepowered.api.entity.projectile.EnderPearl) ResourceKey(org.spongepowered.api.ResourceKey) Painting(net.minecraft.world.entity.decoration.Painting) MobAccessor(org.spongepowered.common.accessor.world.entity.MobAccessor) Level(net.minecraft.world.level.Level) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

BlockPos (net.minecraft.core.BlockPos)1 DifficultyInstance (net.minecraft.world.DifficultyInstance)1 LightningBolt (net.minecraft.world.entity.LightningBolt)1 Mob (net.minecraft.world.entity.Mob)1 ArmorStand (net.minecraft.world.entity.decoration.ArmorStand)1 HangingEntity (net.minecraft.world.entity.decoration.HangingEntity)1 Painting (net.minecraft.world.entity.decoration.Painting)1 FallingBlockEntity (net.minecraft.world.entity.item.FallingBlockEntity)1 ItemEntity (net.minecraft.world.entity.item.ItemEntity)1 ThrownEnderpearl (net.minecraft.world.entity.projectile.ThrownEnderpearl)1 ItemStack (net.minecraft.world.item.ItemStack)1 Level (net.minecraft.world.level.Level)1 ResourceKey (org.spongepowered.api.ResourceKey)1 EnderPearl (org.spongepowered.api.entity.projectile.EnderPearl)1 MobAccessor (org.spongepowered.common.accessor.world.entity.MobAccessor)1