Search in sources :

Example 1 with EntityTypes

use of net.minecraft.server.v1_16_R2.EntityTypes in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createEntityFromNBT.

@Override
public LivingEntity createEntityFromNBT(StackedEntityDataEntry<?> serialized, Location location, boolean addToWorld, EntityType entityType) {
    try {
        NBTTagCompound nbt = (NBTTagCompound) serialized.get();
        NBTTagList positionTagList = nbt.getList("Pos", 6);
        if (positionTagList == null)
            positionTagList = new NBTTagList();
        this.setTag(positionTagList, 0, NBTTagDouble.a(location.getX()));
        this.setTag(positionTagList, 1, NBTTagDouble.a(location.getY()));
        this.setTag(positionTagList, 2, NBTTagDouble.a(location.getZ()));
        nbt.set("Pos", positionTagList);
        NBTTagList rotationTagList = nbt.getList("Rotation", 5);
        if (rotationTagList == null)
            rotationTagList = new NBTTagList();
        this.setTag(rotationTagList, 0, NBTTagFloat.a(location.getYaw()));
        this.setTag(rotationTagList, 1, NBTTagFloat.a(location.getPitch()));
        nbt.set("Rotation", rotationTagList);
        // Reset the UUID to resolve possible duplicates
        nbt.a("UUID", UUID.randomUUID());
        Optional<EntityTypes<?>> optionalEntity = EntityTypes.a(entityType.getKey().getKey());
        if (optionalEntity.isPresent()) {
            WorldServer world = ((CraftWorld) location.getWorld()).getHandle();
            Entity entity = this.createCreature(optionalEntity.get(), world, nbt, null, null, new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), EnumMobSpawn.COMMAND);
            if (entity == null)
                throw new NullPointerException("Unable to create entity from NBT");
            // Load NBT
            entity.load(nbt);
            if (addToWorld) {
                IChunkAccess ichunkaccess = world.getChunkAt(MathHelper.floor(entity.locX() / 16.0D), MathHelper.floor(entity.locZ() / 16.0D), ChunkStatus.FULL, true);
                if (!(ichunkaccess instanceof Chunk))
                    throw new NullPointerException("Unable to spawn entity from NBT, couldn't get chunk");
                ichunkaccess.a(entity);
                method_WorldServer_registerEntity.invoke(world, entity);
                entity.noDamageTicks = 0;
            }
            return (LivingEntity) entity.getBukkitEntity();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : EntityTypes(net.minecraft.server.v1_16_R3.EntityTypes) IChunkAccess(net.minecraft.server.v1_16_R3.IChunkAccess) Entity(net.minecraft.server.v1_16_R3.Entity) GroupDataEntity(net.minecraft.server.v1_16_R3.GroupDataEntity) CraftEntity(org.bukkit.craftbukkit.v1_16_R3.entity.CraftEntity) TileEntity(net.minecraft.server.v1_16_R3.TileEntity) LivingEntity(org.bukkit.entity.LivingEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_16_R3.entity.CraftLivingEntity) BlockPosition(net.minecraft.server.v1_16_R3.BlockPosition) NBTTagCompound(net.minecraft.server.v1_16_R3.NBTTagCompound) WorldServer(net.minecraft.server.v1_16_R3.WorldServer) Chunk(net.minecraft.server.v1_16_R3.Chunk) NBTTagList(net.minecraft.server.v1_16_R3.NBTTagList) LivingEntity(org.bukkit.entity.LivingEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_16_R3.entity.CraftLivingEntity) CraftWorld(org.bukkit.craftbukkit.v1_16_R3.CraftWorld)

Example 2 with EntityTypes

use of net.minecraft.server.v1_16_R2.EntityTypes in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createEntityFromNBT.

@Override
public LivingEntity createEntityFromNBT(StackedEntityDataEntry<?> serialized, Location location, boolean addToWorld, EntityType entityType) {
    try {
        NBTTagCompound nbt = (NBTTagCompound) serialized.get();
        NBTTagList positionTagList = nbt.getList("Pos", 6);
        if (positionTagList == null)
            positionTagList = new NBTTagList();
        this.setTag(positionTagList, 0, NBTTagDouble.a(location.getX()));
        this.setTag(positionTagList, 1, NBTTagDouble.a(location.getY()));
        this.setTag(positionTagList, 2, NBTTagDouble.a(location.getZ()));
        nbt.set("Pos", positionTagList);
        NBTTagList rotationTagList = nbt.getList("Rotation", 5);
        if (rotationTagList == null)
            rotationTagList = new NBTTagList();
        this.setTag(rotationTagList, 0, NBTTagFloat.a(location.getYaw()));
        this.setTag(rotationTagList, 1, NBTTagFloat.a(location.getPitch()));
        nbt.set("Rotation", rotationTagList);
        // Reset the UUID to resolve possible duplicates
        nbt.a("UUID", UUID.randomUUID());
        Optional<EntityTypes<?>> optionalEntity = EntityTypes.a(entityType.getKey().getKey());
        if (optionalEntity.isPresent()) {
            WorldServer world = ((CraftWorld) location.getWorld()).getHandle();
            Entity entity = this.createCreature(optionalEntity.get(), world, nbt, null, null, new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), EnumMobSpawn.COMMAND);
            if (entity == null)
                throw new NullPointerException("Unable to create entity from NBT");
            // Load NBT
            entity.load(nbt);
            if (addToWorld) {
                IChunkAccess ichunkaccess = world.getChunkAt(MathHelper.floor(entity.locX() / 16.0D), MathHelper.floor(entity.locZ() / 16.0D), ChunkStatus.FULL, true);
                if (!(ichunkaccess instanceof Chunk))
                    throw new NullPointerException("Unable to spawn entity from NBT, couldn't get chunk");
                ichunkaccess.a(entity);
                method_WorldServer_registerEntity.invoke(world, entity);
                entity.noDamageTicks = 0;
            }
            return (LivingEntity) entity.getBukkitEntity();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : EntityTypes(net.minecraft.server.v1_16_R2.EntityTypes) IChunkAccess(net.minecraft.server.v1_16_R2.IChunkAccess) Entity(net.minecraft.server.v1_16_R2.Entity) CraftLivingEntity(org.bukkit.craftbukkit.v1_16_R2.entity.CraftLivingEntity) LivingEntity(org.bukkit.entity.LivingEntity) TileEntity(net.minecraft.server.v1_16_R2.TileEntity) GroupDataEntity(net.minecraft.server.v1_16_R2.GroupDataEntity) CraftEntity(org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity) BlockPosition(net.minecraft.server.v1_16_R2.BlockPosition) NBTTagCompound(net.minecraft.server.v1_16_R2.NBTTagCompound) WorldServer(net.minecraft.server.v1_16_R2.WorldServer) Chunk(net.minecraft.server.v1_16_R2.Chunk) NBTTagList(net.minecraft.server.v1_16_R2.NBTTagList) CraftLivingEntity(org.bukkit.craftbukkit.v1_16_R2.entity.CraftLivingEntity) LivingEntity(org.bukkit.entity.LivingEntity) CraftWorld(org.bukkit.craftbukkit.v1_16_R2.CraftWorld)

Example 3 with EntityTypes

use of net.minecraft.server.v1_16_R2.EntityTypes in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createNewEntityUnspawned.

@Override
public LivingEntity createNewEntityUnspawned(EntityType entityType, Location location, SpawnReason spawnReason) {
    World world = location.getWorld();
    if (world == null)
        return null;
    Class<? extends org.bukkit.entity.Entity> entityClass = entityType.getEntityClass();
    if (entityClass == null || !LivingEntity.class.isAssignableFrom(entityClass))
        throw new IllegalArgumentException("EntityType must be of a LivingEntity");
    EntityTypes<? extends Entity> nmsEntityType = IRegistry.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(entityType.getKey()));
    Entity nmsEntity = this.createCreature(nmsEntityType, ((CraftWorld) world).getHandle(), null, null, null, new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), this.toNmsSpawnReason(spawnReason));
    return nmsEntity == null ? null : (LivingEntity) nmsEntity.getBukkitEntity();
}
Also used : Entity(net.minecraft.server.v1_16_R2.Entity) CraftLivingEntity(org.bukkit.craftbukkit.v1_16_R2.entity.CraftLivingEntity) LivingEntity(org.bukkit.entity.LivingEntity) TileEntity(net.minecraft.server.v1_16_R2.TileEntity) GroupDataEntity(net.minecraft.server.v1_16_R2.GroupDataEntity) CraftEntity(org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity) BlockPosition(net.minecraft.server.v1_16_R2.BlockPosition) World(org.bukkit.World) CraftWorld(org.bukkit.craftbukkit.v1_16_R2.CraftWorld)

Example 4 with EntityTypes

use of net.minecraft.server.v1_16_R2.EntityTypes in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createCreature.

/**
 * Duplicate of {@link EntityTypes#createCreature(WorldServer, NBTTagCompound, IChatBaseComponent, EntityHuman, BlockPosition, EnumMobSpawn, boolean, boolean)}.
 * Contains a patch to prevent chicken jockeys from spawning and to not play the mob sound upon creation.
 */
private <T extends Entity> T createCreature(EntityTypes<T> entityTypes, WorldServer worldserver, NBTTagCompound nbttagcompound, IChatBaseComponent ichatbasecomponent, EntityHuman entityhuman, BlockPosition blockposition, EnumMobSpawn enummobspawn) {
    T newEntity;
    if (entityTypes == EntityTypes.SPIDER) {
        newEntity = (T) new SoloEntitySpider((EntityTypes<? extends EntitySpider>) entityTypes, worldserver);
    } else if (entityTypes == EntityTypes.STRIDER) {
        newEntity = (T) new SoloEntityStrider((EntityTypes<? extends EntityStrider>) entityTypes, worldserver);
    } else {
        newEntity = entityTypes.a(worldserver);
    }
    if (newEntity == null) {
        return null;
    } else {
        if (field_Entity_spawnReason != null) {
            try {
                field_Entity_spawnReason.set(newEntity, this.toBukkitSpawnReason(enummobspawn));
            } catch (IllegalAccessException ignored) {
            }
        }
        newEntity.setPositionRotation(blockposition.getX() + 0.5D, blockposition.getY(), blockposition.getZ() + 0.5D, MathHelper.g(worldserver.random.nextFloat() * 360.0F), 0.0F);
        if (newEntity instanceof EntityInsentient) {
            EntityInsentient entityinsentient = (EntityInsentient) newEntity;
            entityinsentient.aC = entityinsentient.yaw;
            entityinsentient.aA = entityinsentient.yaw;
            GroupDataEntity groupDataEntity = null;
            if (entityTypes == EntityTypes.DROWNED || entityTypes == EntityTypes.HUSK || entityTypes == EntityTypes.ZOMBIE_VILLAGER || entityTypes == EntityTypes.ZOMBIFIED_PIGLIN || entityTypes == EntityTypes.ZOMBIE) {
                // Don't allow chicken jockeys to spawn
                groupDataEntity = new EntityZombie.GroupDataZombie(EntityZombie.a(worldserver.getRandom()), false);
            }
            entityinsentient.prepare(worldserver, worldserver.getDamageScaler(entityinsentient.getChunkCoordinates()), enummobspawn, groupDataEntity, nbttagcompound);
        }
        if (ichatbasecomponent != null && newEntity instanceof EntityLiving) {
            newEntity.setCustomName(ichatbasecomponent);
        }
        try {
            EntityTypes.a(worldserver, entityhuman, newEntity, nbttagcompound);
        } catch (Throwable ignored) {
        }
        return newEntity;
    }
}
Also used : EntityTypes(net.minecraft.server.v1_16_R2.EntityTypes) SoloEntitySpider(dev.rosewood.rosestacker.nms.v1_16_R2.entity.SoloEntitySpider) SoloEntityStrider(dev.rosewood.rosestacker.nms.v1_16_R2.entity.SoloEntityStrider) EntityStrider(net.minecraft.server.v1_16_R2.EntityStrider) SoloEntityStrider(dev.rosewood.rosestacker.nms.v1_16_R2.entity.SoloEntityStrider) EntityLiving(net.minecraft.server.v1_16_R2.EntityLiving) EntityZombie(net.minecraft.server.v1_16_R2.EntityZombie) GroupDataEntity(net.minecraft.server.v1_16_R2.GroupDataEntity) EntityInsentient(net.minecraft.server.v1_16_R2.EntityInsentient)

Example 5 with EntityTypes

use of net.minecraft.server.v1_16_R2.EntityTypes in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createCreature.

/**
 * Duplicate of {@link EntityTypes#createCreature(WorldServer, NBTTagCompound, IChatBaseComponent, EntityHuman, BlockPosition, EnumMobSpawn, boolean, boolean)}.
 * Contains a patch to prevent chicken jockeys from spawning and to not play the mob sound upon creation.
 */
private <T extends Entity> T createCreature(EntityTypes<T> entityTypes, WorldServer worldserver, NBTTagCompound nbttagcompound, IChatBaseComponent ichatbasecomponent, EntityHuman entityhuman, BlockPosition blockposition, EnumMobSpawn enummobspawn) {
    T newEntity;
    if (entityTypes == EntityTypes.SPIDER) {
        newEntity = (T) new SoloEntitySpider((EntityTypes<? extends EntitySpider>) entityTypes, worldserver);
    } else if (entityTypes == EntityTypes.STRIDER) {
        newEntity = (T) new SoloEntityStrider((EntityTypes<? extends EntityStrider>) entityTypes, worldserver);
    } else {
        newEntity = entityTypes.a(worldserver);
    }
    if (newEntity == null) {
        return null;
    } else {
        if (field_Entity_spawnReason != null) {
            try {
                field_Entity_spawnReason.set(newEntity, this.toBukkitSpawnReason(enummobspawn));
            } catch (IllegalAccessException ignored) {
            }
        }
        newEntity.setPositionRotation(blockposition.getX() + 0.5D, blockposition.getY(), blockposition.getZ() + 0.5D, MathHelper.g(worldserver.random.nextFloat() * 360.0F), 0.0F);
        if (newEntity instanceof EntityInsentient) {
            EntityInsentient entityinsentient = (EntityInsentient) newEntity;
            entityinsentient.aC = entityinsentient.yaw;
            entityinsentient.aA = entityinsentient.yaw;
            GroupDataEntity groupDataEntity = null;
            if (entityTypes == EntityTypes.DROWNED || entityTypes == EntityTypes.HUSK || entityTypes == EntityTypes.ZOMBIE_VILLAGER || entityTypes == EntityTypes.ZOMBIFIED_PIGLIN || entityTypes == EntityTypes.ZOMBIE) {
                // Don't allow chicken jockeys to spawn
                groupDataEntity = new EntityZombie.GroupDataZombie(EntityZombie.a(worldserver.getRandom()), false);
            }
            entityinsentient.prepare(worldserver, worldserver.getDamageScaler(entityinsentient.getChunkCoordinates()), enummobspawn, groupDataEntity, nbttagcompound);
        }
        if (ichatbasecomponent != null && newEntity instanceof EntityLiving) {
            newEntity.setCustomName(ichatbasecomponent);
        }
        try {
            EntityTypes.a(worldserver, entityhuman, newEntity, nbttagcompound);
        } catch (Throwable ignored) {
        }
        return newEntity;
    }
}
Also used : EntityTypes(net.minecraft.server.v1_16_R3.EntityTypes) SoloEntitySpider(dev.rosewood.rosestacker.nms.v1_16_R3.entity.SoloEntitySpider) SoloEntityStrider(dev.rosewood.rosestacker.nms.v1_16_R3.entity.SoloEntityStrider) EntityStrider(net.minecraft.server.v1_16_R3.EntityStrider) SoloEntityStrider(dev.rosewood.rosestacker.nms.v1_16_R3.entity.SoloEntityStrider) EntityLiving(net.minecraft.server.v1_16_R3.EntityLiving) EntityZombie(net.minecraft.server.v1_16_R3.EntityZombie) GroupDataEntity(net.minecraft.server.v1_16_R3.GroupDataEntity) EntityInsentient(net.minecraft.server.v1_16_R3.EntityInsentient)

Aggregations

GroupDataEntity (net.minecraft.server.v1_16_R2.GroupDataEntity)3 LivingEntity (org.bukkit.entity.LivingEntity)3 BlockPosition (net.minecraft.server.v1_16_R2.BlockPosition)2 Entity (net.minecraft.server.v1_16_R2.Entity)2 EntityTypes (net.minecraft.server.v1_16_R2.EntityTypes)2 TileEntity (net.minecraft.server.v1_16_R2.TileEntity)2 EntityTypes (net.minecraft.server.v1_16_R3.EntityTypes)2 GroupDataEntity (net.minecraft.server.v1_16_R3.GroupDataEntity)2 CraftWorld (org.bukkit.craftbukkit.v1_16_R2.CraftWorld)2 CraftEntity (org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity)2 CraftLivingEntity (org.bukkit.craftbukkit.v1_16_R2.entity.CraftLivingEntity)2 SoloEntitySpider (dev.rosewood.rosestacker.nms.v1_16_R2.entity.SoloEntitySpider)1 SoloEntityStrider (dev.rosewood.rosestacker.nms.v1_16_R2.entity.SoloEntityStrider)1 SoloEntitySpider (dev.rosewood.rosestacker.nms.v1_16_R3.entity.SoloEntitySpider)1 SoloEntityStrider (dev.rosewood.rosestacker.nms.v1_16_R3.entity.SoloEntityStrider)1 Chunk (net.minecraft.server.v1_16_R2.Chunk)1 EntityInsentient (net.minecraft.server.v1_16_R2.EntityInsentient)1 EntityLiving (net.minecraft.server.v1_16_R2.EntityLiving)1 EntityStrider (net.minecraft.server.v1_16_R2.EntityStrider)1 EntityZombie (net.minecraft.server.v1_16_R2.EntityZombie)1