use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BaseFullChunk method unload.
@Override
public boolean unload(boolean save, boolean safe) {
LevelProvider level = this.getProvider();
if (level == null) {
return true;
}
if (save && this.changes != 0) {
level.saveChunk(this.getX(), this.getZ());
}
if (safe) {
for (Entity entity : this.getEntities().values()) {
if (entity instanceof Player) {
return false;
}
}
}
for (Entity entity : new ArrayList<>(this.getEntities().values())) {
if (entity instanceof Player) {
continue;
}
entity.close();
}
for (BlockEntity blockEntity : new ArrayList<>(this.getBlockEntities().values())) {
blockEntity.close();
}
this.provider = null;
return true;
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class LevelDB method requestChunkTask.
@Override
public AsyncTask requestChunkTask(int x, int z) {
Chunk chunk = this.getChunk(x, z, false);
if (chunk == null) {
throw new ChunkException("Invalid Chunk sent");
}
long timestamp = chunk.getChanges();
byte[] tiles = new byte[0];
if (!chunk.getBlockEntities().isEmpty()) {
List<CompoundTag> tagList = new ArrayList<>();
for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
if (blockEntity instanceof BlockEntitySpawnable) {
tagList.add(((BlockEntitySpawnable) blockEntity).getSpawnCompound());
}
}
try {
tiles = NBTIO.write(tagList, ByteOrder.LITTLE_ENDIAN);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Map<Integer, Integer> extra = chunk.getBlockExtraDataArray();
BinaryStream extraData;
if (!extra.isEmpty()) {
extraData = new BinaryStream();
extraData.putLInt(extra.size());
for (Integer key : extra.values()) {
extraData.putLInt(key);
extraData.putLShort(extra.get(key));
}
} else {
extraData = null;
}
BinaryStream stream = new BinaryStream();
stream.put(chunk.getBlockIdArray());
stream.put(chunk.getBlockDataArray());
stream.put(chunk.getBlockSkyLightArray());
stream.put(chunk.getBlockLightArray());
stream.put(chunk.getHeightMapArray());
for (int color : chunk.getBiomeColorArray()) {
stream.put(Binary.writeInt(color));
}
if (extraData != null) {
stream.put(extraData.getBuffer());
} else {
stream.putLInt(0);
}
stream.put(tiles);
this.getLevel().chunkRequestCallback(timestamp, x, z, stream.getBuffer());
return null;
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockTrappedChest method getWeakPower.
@Override
public int getWeakPower(BlockFace face) {
int playerCount = 0;
BlockEntity blockEntity = this.level.getBlockEntity(this);
if (blockEntity instanceof BlockEntityChest) {
playerCount = ((BlockEntityChest) blockEntity).getInventory().getViewers().size();
}
return playerCount < 0 ? 0 : playerCount > 15 ? 15 : playerCount;
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockTrappedChest method place.
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
int[] faces = { 2, 5, 3, 4 };
BlockEntityChest chest = null;
this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
for (BlockFace side : Plane.HORIZONTAL) {
if ((this.getDamage() == 4 || this.getDamage() == 5) && (side == BlockFace.WEST || side == BlockFace.EAST)) {
continue;
} else if ((this.getDamage() == 3 || this.getDamage() == 2) && (side == BlockFace.NORTH || side == BlockFace.SOUTH)) {
continue;
}
Block c = this.getSide(side);
if (c instanceof BlockTrappedChest && c.getDamage() == this.getDamage()) {
BlockEntity blockEntity = this.getLevel().getBlockEntity(c);
if (blockEntity instanceof BlockEntityChest && !((BlockEntityChest) blockEntity).isPaired()) {
chest = (BlockEntityChest) blockEntity;
break;
}
}
}
this.getLevel().setBlock(block, this, true, true);
CompoundTag nbt = new CompoundTag("").putList(new ListTag<>("Items")).putString("id", BlockEntity.CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
if (item.hasCustomName()) {
nbt.putString("CustomName", item.getCustomName());
}
if (item.hasCustomBlockData()) {
Map<String, Tag> customData = item.getCustomBlockData().getTags();
for (Map.Entry<String, Tag> tag : customData.entrySet()) {
nbt.put(tag.getKey(), tag.getValue());
}
}
BlockEntity blockEntity = new BlockEntityChest(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
if (chest != null) {
chest.pairWith(((BlockEntityChest) blockEntity));
((BlockEntityChest) blockEntity).pairWith(chest);
}
return true;
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockPistonBase method onUpdate.
@Override
public int onUpdate(int type) {
if (type != 6 && type != 1) {
return 0;
} else {
BlockEntity blockEntity = this.level.getBlockEntity(this);
if (blockEntity instanceof BlockEntityPistonArm) {
BlockEntityPistonArm arm = (BlockEntityPistonArm) blockEntity;
boolean powered = this.isPowered();
if (arm.powered != powered) {
this.level.getServer().getPluginManager().callEvent(new BlockPistonChangeEvent(this, powered ? 0 : 15, powered ? 15 : 0));
arm.powered = !arm.powered;
if (arm.chunk != null) {
arm.chunk.setChanged();
}
}
}
return type;
}
}
Aggregations