use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class Level method getCollidingEntities.
public Entity[] getCollidingEntities(AxisAlignedBB bb, Entity entity) {
List<Entity> nearby = new ArrayList<>();
if (entity == null || entity.canCollide()) {
int minX = NukkitMath.floorDouble((bb.getMinX() - 2) / 16);
int maxX = NukkitMath.ceilDouble((bb.getMaxX() + 2) / 16);
int minZ = NukkitMath.floorDouble((bb.getMinZ() - 2) / 16);
int maxZ = NukkitMath.ceilDouble((bb.getMaxZ() + 2) / 16);
for (int x = minX; x <= maxX; ++x) {
for (int z = minZ; z <= maxZ; ++z) {
for (Entity ent : this.getChunkEntities(x, z).values()) {
if ((entity == null || (ent != entity && entity.canCollideWith(ent))) && ent.boundingBox.intersectsWith(bb)) {
nearby.add(ent);
}
}
}
}
}
return nearby.stream().toArray(Entity[]::new);
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class Level method getCollisionCubes.
public AxisAlignedBB[] getCollisionCubes(Entity entity, AxisAlignedBB bb, boolean entities) {
int minX = NukkitMath.floorDouble(bb.getMinX());
int minY = NukkitMath.floorDouble(bb.getMinY());
int minZ = NukkitMath.floorDouble(bb.getMinZ());
int maxX = NukkitMath.ceilDouble(bb.getMaxX());
int maxY = NukkitMath.ceilDouble(bb.getMaxY());
int maxZ = NukkitMath.ceilDouble(bb.getMaxZ());
List<AxisAlignedBB> collides = new ArrayList<>();
for (int z = minZ; z <= maxZ; ++z) {
for (int x = minX; x <= maxX; ++x) {
for (int y = minY; y <= maxY; ++y) {
Block block = this.getBlock(this.temporalVector.setComponents(x, y, z));
if (!block.canPassThrough() && block.collidesWithBB(bb)) {
collides.add(block.getBoundingBox());
}
}
}
}
if (entities) {
for (Entity ent : this.getCollidingEntities(bb.grow(0.25f, 0.25f, 0.25f), entity)) {
collides.add(ent.boundingBox.clone());
}
}
return collides.stream().toArray(AxisAlignedBB[]::new);
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class Chunk method toBinary.
@Override
public byte[] toBinary() {
CompoundTag nbt = this.getNBT().copy();
nbt.putInt("xPos", this.getX());
nbt.putInt("zPos", this.getZ());
if (this.isGenerated()) {
nbt.putByteArray("Blocks", this.getBlockIdArray());
nbt.putByteArray("Data", this.getBlockDataArray());
nbt.putByteArray("SkyLight", this.getBlockSkyLightArray());
nbt.putByteArray("BlockLight", this.getBlockLightArray());
nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
int[] heightInts = new int[256];
byte[] heightBytes = this.getHeightMapArray();
for (int i = 0; i < heightInts.length; i++) {
heightInts[i] = heightBytes[i] & 0xFF;
}
nbt.putIntArray("HeightMap", heightInts);
}
ArrayList<CompoundTag> entities = new ArrayList<>();
for (Entity entity : this.getEntities().values()) {
if (!(entity instanceof Player) && !entity.closed) {
entity.saveNBT();
entities.add(entity.namedTag);
}
}
ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
entityListTag.setAll(entities);
nbt.putList(entityListTag);
ArrayList<CompoundTag> tiles = new ArrayList<>();
for (BlockEntity blockEntity : this.getBlockEntities().values()) {
blockEntity.saveNBT();
tiles.add(blockEntity.namedTag);
}
ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
tileListTag.setAll(tiles);
nbt.putList(tileListTag);
BinaryStream extraData = new BinaryStream();
Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
extraData.putInt(extraDataArray.size());
for (Integer key : extraDataArray.keySet()) {
extraData.putInt(key);
extraData.putShort(extraDataArray.get(key));
}
nbt.putByteArray("ExtraData", extraData.getBuffer());
CompoundTag chunk = new CompoundTag("");
chunk.putCompound("Level", nbt);
try {
return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class Chunk method toFastBinary.
@Override
public byte[] toFastBinary() {
CompoundTag nbt = this.getNBT().copy();
nbt.putInt("xPos", this.getX());
nbt.putInt("zPos", this.getZ());
nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
int[] heightInts = new int[256];
byte[] heightBytes = this.getHeightMapArray();
for (int i = 0; i < heightInts.length; i++) {
heightInts[i] = heightBytes[i] & 0xFF;
}
for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
if (section instanceof EmptyChunkSection) {
continue;
}
CompoundTag s = new CompoundTag(null);
s.putByte("Y", section.getY());
s.putByteArray("Blocks", section.getIdArray());
s.putByteArray("Data", section.getDataArray());
s.putByteArray("BlockLight", section.getLightArray());
s.putByteArray("SkyLight", section.getSkyLightArray());
nbt.getList("Sections", CompoundTag.class).add(s);
}
ArrayList<CompoundTag> entities = new ArrayList<>();
for (Entity entity : this.getEntities().values()) {
if (!(entity instanceof Player) && !entity.closed) {
entity.saveNBT();
entities.add(entity.namedTag);
}
}
ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
entityListTag.setAll(entities);
nbt.putList(entityListTag);
ArrayList<CompoundTag> tiles = new ArrayList<>();
for (BlockEntity blockEntity : this.getBlockEntities().values()) {
blockEntity.saveNBT();
tiles.add(blockEntity.namedTag);
}
ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
tileListTag.setAll(tiles);
nbt.putList(tileListTag);
Set<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);
if (entries != null) {
ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
long totalTime = this.provider.getLevel().getCurrentTick();
for (BlockUpdateEntry entry : entries) {
CompoundTag entryNBT = new CompoundTag().putString("i", entry.block.getSaveId()).putInt("x", entry.pos.getFloorX()).putInt("y", entry.pos.getFloorY()).putInt("z", entry.pos.getFloorZ()).putInt("t", (int) (entry.delay - totalTime)).putInt("p", entry.priority);
tileTickTag.add(entryNBT);
}
nbt.putList(tileTickTag);
}
BinaryStream extraData = new BinaryStream();
Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
extraData.putInt(extraDataArray.size());
for (Integer key : extraDataArray.keySet()) {
extraData.putInt(key);
extraData.putShort(extraDataArray.get(key));
}
nbt.putByteArray("ExtraData", extraData.getBuffer());
CompoundTag chunk = new CompoundTag("");
chunk.putCompound("Level", nbt);
try {
return NBTIO.write(chunk, ByteOrder.BIG_ENDIAN);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of cn.nukkit.entity.Entity in project Nukkit by Nukkit.
the class BaseFullChunk method initChunk.
public void initChunk() {
if (this.getProvider() != null && !this.isInit) {
boolean changed = false;
if (this.NBTentities != null) {
this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.startTiming();
for (CompoundTag nbt : NBTentities) {
if (!nbt.contains("id")) {
this.setChanged();
continue;
}
ListTag pos = nbt.getList("Pos");
if ((((NumberTag) pos.get(0)).getData().intValue() >> 4) != this.getX() || ((((NumberTag) pos.get(2)).getData().intValue() >> 4) != this.getZ())) {
changed = true;
continue;
}
Entity entity = Entity.createEntity(nbt.getString("id"), this, nbt);
if (entity != null) {
changed = true;
continue;
}
}
this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.stopTiming();
this.NBTentities = null;
}
if (this.NBTtiles != null) {
this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.startTiming();
for (CompoundTag nbt : NBTtiles) {
if (nbt != null) {
if (!nbt.contains("id")) {
changed = true;
continue;
}
if ((nbt.getInt("x") >> 4) != this.getX() || ((nbt.getInt("z") >> 4) != this.getZ())) {
changed = true;
continue;
}
BlockEntity blockEntity = BlockEntity.createBlockEntity(nbt.getString("id"), this, nbt);
if (blockEntity == null) {
changed = true;
continue;
}
}
}
this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.stopTiming();
this.NBTtiles = null;
}
this.setChanged(changed);
this.isInit = true;
}
}
Aggregations