use of org.lanternpowered.server.entity.LanternEntity in project LanternServer by LanternPowered.
the class AnvilChunkIOService method read.
@Override
public boolean read(LanternChunk chunk) throws IOException {
final int x = chunk.getX();
final int z = chunk.getZ();
final RegionFile region = this.cache.getRegionFileByChunk(x, z);
final int regionX = x & REGION_MASK;
final int regionZ = z & REGION_MASK;
final DataInputStream is = region.getChunkDataInputStream(regionX, regionZ);
if (is == null) {
return false;
}
final DataView levelDataView;
try (NbtDataContainerInputStream nbt = new NbtDataContainerInputStream(is)) {
levelDataView = nbt.read().getView(LEVEL).get();
}
// read the vertical sections
final List<DataView> sectionList = levelDataView.getViewList(SECTIONS).get();
final ChunkSection[] sections = new ChunkSection[16];
// noinspection unchecked
final Short2ObjectOpenHashMap<LanternTileEntity>[] tileEntitySections = new Short2ObjectOpenHashMap[sections.length];
for (DataView sectionTag : sectionList) {
final int y = sectionTag.getInt(Y).get();
final byte[] rawTypes = (byte[]) sectionTag.get(BLOCKS).get();
final byte[] extTypes = sectionTag.contains(BLOCKS_EXTRA) ? (byte[]) sectionTag.get(BLOCKS_EXTRA).get() : null;
final byte[] data = (byte[]) sectionTag.get(DATA).get();
final byte[] blockLight = (byte[]) sectionTag.get(BLOCK_LIGHT).get();
final byte[] skyLight = (byte[]) sectionTag.get(SKY_LIGHT).get();
final NibbleArray dataArray = new NibbleArray(rawTypes.length, data, true);
final NibbleArray extTypesArray = extTypes == null ? null : new NibbleArray(rawTypes.length, extTypes, true);
final short[] types = new short[rawTypes.length];
for (int i = 0; i < rawTypes.length; i++) {
types[i] = (short) ((extTypesArray == null ? 0 : extTypesArray.get(i)) << 12 | ((rawTypes[i] & 0xff) << 4) | dataArray.get(i));
}
tileEntitySections[y] = new Short2ObjectOpenHashMap<>();
sections[y] = new ChunkSection(types, new NibbleArray(rawTypes.length, skyLight, true), new NibbleArray(rawTypes.length, blockLight, true), tileEntitySections[y]);
}
levelDataView.getViewList(TILE_ENTITIES).ifPresent(tileEntityViews -> {
final ObjectSerializer<LanternTileEntity> tileEntitySerializer = ObjectSerializerRegistry.get().get(LanternTileEntity.class).get();
for (DataView tileEntityView : tileEntityViews) {
final int tileY = tileEntityView.getInt(TILE_ENTITY_Y).get();
final int section = tileY >> 4;
if (tileEntitySections[section] == null) {
continue;
}
final int tileZ = tileEntityView.getInt(TILE_ENTITY_Z).get();
final int tileX = tileEntityView.getInt(TILE_ENTITY_X).get();
try {
final LanternTileEntity tileEntity = tileEntitySerializer.deserialize(tileEntityView);
tileEntity.setLocation(new Location<>(this.world, tileX, tileY, tileZ));
tileEntity.setValid(true);
tileEntitySections[section].put((short) ChunkSection.index(tileX & 0xf, tileY & 0xf, tileZ & 0xf), tileEntity);
} catch (InvalidDataException e) {
this.logger.warn("Error loading tile entity at ({};{};{}) in the chunk ({},{}) in the world {}", tileX & 0xf, tileY & 0xf, tileZ & 0xf, x, z, getWorldProperties().getWorldName(), e);
}
}
});
final DataView spongeDataView = levelDataView.getView(DataQueries.SPONGE_DATA).orElse(null);
final List<DataView> trackerDataViews = spongeDataView == null ? null : levelDataView.getViewList(TRACKER_DATA_TABLE).orElse(null);
// noinspection unchecked
final Short2ObjectMap<LanternChunk.TrackerData>[] trackerData = chunk.getTrackerData().getRawObjects();
if (trackerDataViews != null) {
for (DataView dataView : trackerDataViews) {
final Optional<Short> optIndex = dataView.getShort(TRACKER_BLOCK_POS);
if (!optIndex.isPresent()) {
continue;
}
final int creatorId = dataView.getInt(TRACKER_ENTRY_CREATOR).orElse(-1);
final int notifierId = dataView.getInt(TRACKER_ENTRY_NOTIFIER).orElse(-1);
// index = z << 12 | y << 4 | x
int index = optIndex.get() & 0xffff;
final int section = (index >> 8) & 0xf;
// Convert the index to the section based system
// index = y << 8 | z << 4 | x
index = ChunkSection.index(index & 0xf, (index >> 4) & 0xf, index >> 12);
trackerData[section].put((short) index, new LanternChunk.TrackerData(creatorId, notifierId));
}
}
// initialize the chunk
chunk.initializeSections(sections);
chunk.setPopulated(levelDataView.getInt(TERRAIN_POPULATED).orElse(0) > 0);
if (levelDataView.contains(BIOMES)) {
final byte[] biomes = (byte[]) levelDataView.get(BIOMES).get();
final byte[] biomesExtra = (byte[]) (levelDataView.contains(BIOMES_EXTRA) ? levelDataView.get(BIOMES_EXTRA).get() : null);
final short[] newBiomes = new short[biomes.length];
for (int i = 0; i < biomes.length; i++) {
newBiomes[i] = (short) ((biomesExtra == null ? 0 : biomesExtra[i]) << 8 | biomes[i]);
}
chunk.initializeBiomes(newBiomes);
}
final Object heightMap;
if (levelDataView.contains(HEIGHT_MAP) && (heightMap = levelDataView.get(HEIGHT_MAP).get()) instanceof int[]) {
chunk.initializeHeightMap((int[]) heightMap);
} else {
chunk.initializeHeightMap(null);
}
levelDataView.getLong(INHABITED_TIME).ifPresent(time -> chunk.setInhabitedTime(time.intValue()));
chunk.setLightPopulated(levelDataView.getInt(LIGHT_POPULATED).orElse(0) > 0);
chunk.initializeLight();
levelDataView.getViewList(ENTITIES).ifPresent(entityViews -> {
final ObjectSerializer<LanternEntity> entitySerializer = ObjectSerializerRegistry.get().get(LanternEntity.class).get();
for (DataView entityView : entityViews) {
try {
final LanternEntity entity = entitySerializer.deserialize(entityView);
final int ySection = fixEntityYSection(entity.getPosition().getFloorY() >> 4);
chunk.addEntity(entity, ySection);
} catch (InvalidDataException e) {
this.logger.warn("Error loading entity in the chunk ({},{}) in the world {}", x, z, getWorldProperties().getWorldName(), e);
}
}
});
return true;
}
use of org.lanternpowered.server.entity.LanternEntity in project LanternServer by LanternPowered.
the class EntitySerializer method serialize.
@Override
public DataView serialize(LanternEntity object) {
final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
dataView.set(ID, object.getType().getId());
// noinspection unchecked
final ObjectStore<LanternEntity> store = (ObjectStore) ObjectStoreRegistry.get().get(object.getClass()).get();
store.serialize(object, dataView);
if (store instanceof IdentifiableObjectStore) {
((IdentifiableObjectStore) store).serializeUniqueId(dataView, object.getUniqueId());
}
return dataView;
}
use of org.lanternpowered.server.entity.LanternEntity in project LanternServer by LanternPowered.
the class LanternWorld method createEntity.
public Entity createEntity(EntityType type, Vector3d position, Consumer<Entity> entityConsumer) {
checkNotNull(position, "position");
final LanternEntityType entityType = (LanternEntityType) checkNotNull(type, "type");
final LanternEntity entity = (LanternEntity) entityType.getEntityConstructor().apply(UUID.randomUUID());
entity.setPositionAndWorld(this, position);
entityConsumer.accept(entity);
final CauseStack causeStack = CauseStack.current();
// Only throw the post event, the pre event will
// only be called in specific cases.
final ConstructEntityEvent.Post event = SpongeEventFactory.createConstructEntityEventPost(causeStack.getCurrentCause(), entity, type, entity.getTransform());
Sponge.getEventManager().post(event);
return entity;
}
use of org.lanternpowered.server.entity.LanternEntity in project LanternServer by LanternPowered.
the class ExtentViewDownsize method getEntities.
@Override
public Collection<Entity> getEntities() {
final Collection<Entity> entities = this.extent.getEntities();
for (Iterator<Entity> iterator = entities.iterator(); iterator.hasNext(); ) {
final Entity entity = iterator.next();
final Vector3d pos = ((LanternEntity) entity).getPosition();
if (!VecHelper.inBounds(pos.getX(), pos.getY(), pos.getZ(), this.blockMin, this.blockMax)) {
iterator.remove();
}
}
return entities;
}
use of org.lanternpowered.server.entity.LanternEntity in project LanternServer by LanternPowered.
the class LanternWorld method spawnEntity0.
private boolean spawnEntity0(Entity entity) {
final LanternEntity entity1 = addEntity((LanternEntity) entity);
if (entity1 != null) {
if (entity == entity1) {
Lantern.getLogger().warn("The entity " + entity1 + " is already spawned. Skipping...");
} else {
Lantern.getLogger().warn("There is already a entity spawned with the unique id: " + entity.getUniqueId() + ". Skipping...");
}
return false;
}
final LanternEntity entity2 = (LanternEntity) entity;
final Vector3i position = entity2.getPosition().toInt();
final Vector3i chunkPos = new Vector3i(position.getX() >> 4, fixEntityYSection(position.getY() >> 4), position.getZ() >> 4);
final LanternChunk chunk = (LanternChunk) loadChunk(chunkPos.getX(), 0, chunkPos.getZ(), true).get();
chunk.addEntity(entity2, chunkPos.getY());
return true;
}
Aggregations