Search in sources :

Example 1 with PersistentEntitySectionManager

use of net.minecraft.world.level.entity.PersistentEntitySectionManager in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createEntityFromNBT.

@Override
public LivingEntity createEntityFromNBT(StackedEntityDataEntry<?> serialized, Location location, boolean addToWorld, EntityType entityType) {
    try {
        CompoundTag nbt = (CompoundTag) serialized.get();
        ListTag positionTagList = nbt.getList("Pos", Tag.TAG_DOUBLE);
        if (positionTagList == null)
            positionTagList = new ListTag();
        this.setTag(positionTagList, 0, DoubleTag.valueOf(location.getX()));
        this.setTag(positionTagList, 1, DoubleTag.valueOf(location.getY()));
        this.setTag(positionTagList, 2, DoubleTag.valueOf(location.getZ()));
        nbt.put("Pos", positionTagList);
        ListTag rotationTagList = nbt.getList("Rotation", Tag.TAG_FLOAT);
        if (rotationTagList == null)
            rotationTagList = new ListTag();
        this.setTag(rotationTagList, 0, FloatTag.valueOf(location.getYaw()));
        this.setTag(rotationTagList, 1, FloatTag.valueOf(location.getPitch()));
        nbt.put("Rotation", rotationTagList);
        // Reset the UUID to resolve possible duplicates
        nbt.putUUID("UUID", UUID.randomUUID());
        // Causes issues if this value is parsed async
        nbt.remove("AngryAt");
        Optional<net.minecraft.world.entity.EntityType<?>> optionalEntity = net.minecraft.world.entity.EntityType.byString(entityType.getKey().getKey());
        if (optionalEntity.isPresent()) {
            ServerLevel world = ((CraftWorld) location.getWorld()).getHandle();
            Entity entity = this.createCreature(optionalEntity.get(), world, nbt, null, null, new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), MobSpawnType.COMMAND);
            if (entity == null)
                throw new NullPointerException("Unable to create entity from NBT");
            // Load NBT
            entity.load(nbt);
            if (addToWorld) {
                PersistentEntitySectionManager<Entity> entityManager = (PersistentEntitySectionManager<Entity>) field_ServerLevel_entityManager.get(world);
                entityManager.addNewEntity(entity);
                entity.invulnerableTime = 0;
            }
            return (LivingEntity) entity.getBukkitEntity();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) CraftLivingEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftLivingEntity) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) LivingEntity(org.bukkit.entity.LivingEntity) CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) PersistentEntitySectionManager(net.minecraft.world.level.entity.PersistentEntitySectionManager) ListTag(net.minecraft.nbt.ListTag) EntityType(org.bukkit.entity.EntityType) CraftLivingEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftLivingEntity) LivingEntity(org.bukkit.entity.LivingEntity) BlockPos(net.minecraft.core.BlockPos) CraftWorld(org.bukkit.craftbukkit.v1_17_R1.CraftWorld) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with PersistentEntitySectionManager

use of net.minecraft.world.level.entity.PersistentEntitySectionManager in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createEntityFromNBT.

@Override
public LivingEntity createEntityFromNBT(StackedEntityDataEntry<?> serialized, Location location, boolean addToWorld, EntityType entityType) {
    try {
        CompoundTag nbt = (CompoundTag) serialized.get();
        ListTag positionTagList = nbt.getList("Pos", Tag.TAG_DOUBLE);
        if (positionTagList == null)
            positionTagList = new ListTag();
        this.setTag(positionTagList, 0, DoubleTag.valueOf(location.getX()));
        this.setTag(positionTagList, 1, DoubleTag.valueOf(location.getY()));
        this.setTag(positionTagList, 2, DoubleTag.valueOf(location.getZ()));
        nbt.put("Pos", positionTagList);
        ListTag rotationTagList = nbt.getList("Rotation", Tag.TAG_FLOAT);
        if (rotationTagList == null)
            rotationTagList = new ListTag();
        this.setTag(rotationTagList, 0, FloatTag.valueOf(location.getYaw()));
        this.setTag(rotationTagList, 1, FloatTag.valueOf(location.getPitch()));
        nbt.put("Rotation", rotationTagList);
        // Reset the UUID to resolve possible duplicates
        nbt.putUUID("UUID", UUID.randomUUID());
        // Causes issues if this value is parsed async
        nbt.remove("AngryAt");
        Optional<net.minecraft.world.entity.EntityType<?>> optionalEntity = net.minecraft.world.entity.EntityType.byString(entityType.getKey().getKey());
        if (optionalEntity.isPresent()) {
            ServerLevel world = ((CraftWorld) location.getWorld()).getHandle();
            Entity entity = this.createCreature(optionalEntity.get(), world, nbt, null, null, new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), MobSpawnType.COMMAND);
            if (entity == null)
                throw new NullPointerException("Unable to create entity from NBT");
            // Load NBT
            entity.load(nbt);
            if (addToWorld) {
                PersistentEntitySectionManager<Entity> entityManager = (PersistentEntitySectionManager<Entity>) field_ServerLevel_entityManager.get(world);
                entityManager.addNewEntity(entity);
                entity.invulnerableTime = 0;
            }
            return (LivingEntity) entity.getBukkitEntity();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) LivingEntity(org.bukkit.entity.LivingEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity) PersistentEntitySectionManager(net.minecraft.world.level.entity.PersistentEntitySectionManager) ListTag(net.minecraft.nbt.ListTag) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) BlockPos(net.minecraft.core.BlockPos) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 3 with PersistentEntitySectionManager

use of net.minecraft.world.level.entity.PersistentEntitySectionManager in project Insights by InsightsPlugin.

the class ChunkContainer method get.

@Override
public DistributionStorage get() {
    ChunkVector min = cuboid.getMin();
    ChunkVector max = cuboid.getMax();
    int minX = min.getX();
    int maxX = max.getX();
    int minZ = min.getZ();
    int maxZ = max.getZ();
    int blockMinY = Math.max(min.getY(), 0);
    int blockMaxY = Math.abs(Math.min(min.getY(), 0)) + max.getY();
    ServerLevel serverLevel = ((CraftWorld) world).getHandle();
    if (options.materials()) {
        LevelChunkSection[] chunkSections;
        try {
            chunkSections = getChunkSections();
        } catch (IOException ex) {
            throw new ChunkIOException(ex);
        }
        int minSectionY = blockMinY >> 4;
        int maxSectionY = blockMaxY >> 4;
        for (int sectionY = minSectionY; sectionY <= maxSectionY; sectionY++) {
            int minY = sectionY == minSectionY ? blockMinY & 15 : 0;
            int maxY = sectionY == maxSectionY ? blockMaxY & 15 : 15;
            LevelChunkSection section = chunkSections[sectionY];
            if (section == null) {
                // Section is empty, count everything as air
                long count = (maxX - minX + 1L) * (maxY - minY + 1L) * (maxZ - minZ + 1L);
                materialMap.merge(Material.AIR, count, Long::sum);
            } else if (minX == 0 && maxX == 15 && minY == 0 && maxY == 15 && minZ == 0 && maxZ == 15) {
                // Section can be counted as a whole
                section.getStates().count((state, count) -> {
                    try {
                        materialMap.merge(CraftMagicNumbers.getMaterial(state.getBlock()), (long) count, Long::sum);
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                });
            } else {
                // Section must be scanned block by block
                for (int x = minX; x <= maxX; x++) {
                    for (int y = minY; y <= maxY; y++) {
                        for (int z = minZ; z <= maxZ; z++) {
                            materialMap.merge(CraftMagicNumbers.getMaterial(section.getBlockState(x, y, z).getBlock()), 1L, Long::sum);
                        }
                    }
                }
            }
        }
    }
    if (options.entities()) {
        PersistentEntitySectionManager<Entity> entityManager = serverLevel.entityManager;
        long chunkKey = getChunkKey();
        final Stream<Entity> entityStream;
        if (entityManager.areEntitiesLoaded(chunkKey)) {
            EntitySectionStorage<Entity> sectionStorage;
            try {
                sectionStorage = RPersistentEntitySectionManager.getSectionStorage(entityManager);
            } catch (Throwable th) {
                throw new ChunkReflectionException(th);
            }
            entityStream = sectionStorage.getExistingSectionsInChunk(chunkKey).flatMap(EntitySection::getEntities);
        } else {
            EntityPersistentStorage<Entity> permanentStorage;
            try {
                permanentStorage = RPersistentEntitySectionManager.getPermanentStorage(entityManager);
            } catch (Throwable th) {
                throw new ChunkReflectionException(th);
            }
            entityStream = permanentStorage.loadEntities(new ChunkPos(chunkX, chunkZ)).join().getEntities();
        }
        entityStream.filter(entity -> {
            int x = entity.getBlockX() & 15;
            int y = entity.getBlockY();
            int z = entity.getBlockZ() & 15;
            return minX <= x && x <= maxX && blockMinY <= y && y <= blockMaxY && minZ <= z && z <= maxZ;
        }).forEach(entity -> entityMap.merge(entity.getBukkitEntity().getType(), 1L, Long::sum));
    }
    return DistributionStorage.of(materialMap, entityMap);
}
Also used : ChunkVector(dev.frankheijden.insights.api.objects.chunk.ChunkVector) ChunkIOException(dev.frankheijden.insights.api.exceptions.ChunkIOException) ServerLevel(net.minecraft.server.level.ServerLevel) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld) ScanOptions(dev.frankheijden.insights.api.concurrent.ScanOptions) World(org.bukkit.World) Map(java.util.Map) EntitySection(net.minecraft.world.level.entity.EntitySection) ChunkReflectionException(dev.frankheijden.insights.api.exceptions.ChunkReflectionException) Material(org.bukkit.Material) EnumMap(java.util.EnumMap) RPersistentEntitySectionManager(dev.frankheijden.insights.api.reflection.RPersistentEntitySectionManager) ChunkCuboid(dev.frankheijden.insights.api.objects.chunk.ChunkCuboid) ChunkUtils(dev.frankheijden.insights.api.utils.ChunkUtils) IOException(java.io.IOException) EntityPersistentStorage(net.minecraft.world.level.entity.EntityPersistentStorage) EntityType(org.bukkit.entity.EntityType) ChunkPos(net.minecraft.world.level.ChunkPos) CraftMagicNumbers(org.bukkit.craftbukkit.v1_18_R2.util.CraftMagicNumbers) Entity(net.minecraft.world.entity.Entity) Stream(java.util.stream.Stream) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) PersistentEntitySectionManager(net.minecraft.world.level.entity.PersistentEntitySectionManager) EntitySectionStorage(net.minecraft.world.level.entity.EntitySectionStorage) DistributionStorage(dev.frankheijden.insights.api.concurrent.storage.DistributionStorage) ServerLevel(net.minecraft.server.level.ServerLevel) ChunkIOException(dev.frankheijden.insights.api.exceptions.ChunkIOException) Entity(net.minecraft.world.entity.Entity) ChunkVector(dev.frankheijden.insights.api.objects.chunk.ChunkVector) ChunkReflectionException(dev.frankheijden.insights.api.exceptions.ChunkReflectionException) ChunkIOException(dev.frankheijden.insights.api.exceptions.ChunkIOException) IOException(java.io.IOException) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) ChunkPos(net.minecraft.world.level.ChunkPos) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld)

Example 4 with PersistentEntitySectionManager

use of net.minecraft.world.level.entity.PersistentEntitySectionManager in project RoseStacker by Rosewood-Development.

the class NMSHandlerImpl method createEntityFromNBT.

@Override
public LivingEntity createEntityFromNBT(StackedEntityDataEntry<?> serialized, Location location, boolean addToWorld, EntityType entityType) {
    try {
        CompoundTag nbt = (CompoundTag) serialized.get();
        ListTag positionTagList = nbt.getList("Pos", Tag.TAG_DOUBLE);
        if (positionTagList == null)
            positionTagList = new ListTag();
        this.setTag(positionTagList, 0, DoubleTag.valueOf(location.getX()));
        this.setTag(positionTagList, 1, DoubleTag.valueOf(location.getY()));
        this.setTag(positionTagList, 2, DoubleTag.valueOf(location.getZ()));
        nbt.put("Pos", positionTagList);
        ListTag rotationTagList = nbt.getList("Rotation", Tag.TAG_FLOAT);
        if (rotationTagList == null)
            rotationTagList = new ListTag();
        this.setTag(rotationTagList, 0, FloatTag.valueOf(location.getYaw()));
        this.setTag(rotationTagList, 1, FloatTag.valueOf(location.getPitch()));
        nbt.put("Rotation", rotationTagList);
        // Reset the UUID to resolve possible duplicates
        nbt.putUUID("UUID", UUID.randomUUID());
        // Causes issues if this value is parsed async
        nbt.remove("AngryAt");
        Optional<net.minecraft.world.entity.EntityType<?>> optionalEntity = net.minecraft.world.entity.EntityType.byString(entityType.getKey().getKey());
        if (optionalEntity.isPresent()) {
            ServerLevel world = ((CraftWorld) location.getWorld()).getHandle();
            Entity entity = this.createCreature(optionalEntity.get(), world, nbt, null, null, new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), MobSpawnType.COMMAND);
            if (entity == null)
                throw new NullPointerException("Unable to create entity from NBT");
            // Load NBT
            entity.load(nbt);
            if (addToWorld) {
                PersistentEntitySectionManager<Entity> entityManager = (PersistentEntitySectionManager<Entity>) field_ServerLevel_entityManager.get(world);
                entityManager.addNewEntity(entity);
                entity.invulnerableTime = 0;
            }
            return (LivingEntity) entity.getBukkitEntity();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R2.entity.CraftLivingEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) LivingEntity(org.bukkit.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity) PersistentEntitySectionManager(net.minecraft.world.level.entity.PersistentEntitySectionManager) ListTag(net.minecraft.nbt.ListTag) EntityType(org.bukkit.entity.EntityType) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R2.entity.CraftLivingEntity) LivingEntity(org.bukkit.entity.LivingEntity) BlockPos(net.minecraft.core.BlockPos) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)4 Entity (net.minecraft.world.entity.Entity)4 PersistentEntitySectionManager (net.minecraft.world.level.entity.PersistentEntitySectionManager)4 EntityType (org.bukkit.entity.EntityType)4 BlockPos (net.minecraft.core.BlockPos)3 CompoundTag (net.minecraft.nbt.CompoundTag)3 ListTag (net.minecraft.nbt.ListTag)3 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)3 SpawnerBlockEntity (net.minecraft.world.level.block.entity.SpawnerBlockEntity)3 LivingEntity (org.bukkit.entity.LivingEntity)3 CraftWorld (org.bukkit.craftbukkit.v1_18_R2.CraftWorld)2 ScanOptions (dev.frankheijden.insights.api.concurrent.ScanOptions)1 DistributionStorage (dev.frankheijden.insights.api.concurrent.storage.DistributionStorage)1 ChunkIOException (dev.frankheijden.insights.api.exceptions.ChunkIOException)1 ChunkReflectionException (dev.frankheijden.insights.api.exceptions.ChunkReflectionException)1 ChunkCuboid (dev.frankheijden.insights.api.objects.chunk.ChunkCuboid)1 ChunkVector (dev.frankheijden.insights.api.objects.chunk.ChunkVector)1 RPersistentEntitySectionManager (dev.frankheijden.insights.api.reflection.RPersistentEntitySectionManager)1 ChunkUtils (dev.frankheijden.insights.api.utils.ChunkUtils)1 IOException (java.io.IOException)1