use of cn.nukkit.Player in project Nukkit by Nukkit.
the class Level method sendChunk.
private void sendChunk(int x, int z, long index, DataPacket packet) {
if (this.chunkSendTasks.containsKey(index)) {
for (Player player : this.chunkSendQueue.get(index).values()) {
if (player.isConnected() && player.usedChunks.containsKey(index)) {
player.sendChunk(x, z, packet);
}
}
this.chunkSendQueue.remove(index);
this.chunkSendTasks.remove(index);
}
}
use of cn.nukkit.Player in project Nukkit by Nukkit.
the class Level method useBreakOn.
public Item useBreakOn(Vector3 vector, Item item, Player player, boolean createParticles) {
if (player != null && player.getGamemode() > 1) {
return null;
}
Block target = this.getBlock(vector);
Item[] drops;
if (item == null) {
item = new ItemBlock(new BlockAir(), 0, 0);
}
if (player != null) {
double breakTime = target.getBreakTime(item, player);
if (player.isCreative() && breakTime > 0.15) {
breakTime = 0.15;
}
if (player.hasEffect(Effect.SWIFTNESS)) {
breakTime *= 1 - (0.2 * (player.getEffect(Effect.SWIFTNESS).getAmplifier() + 1));
}
if (player.hasEffect(Effect.MINING_FATIGUE)) {
breakTime *= 1 - (0.3 * (player.getEffect(Effect.MINING_FATIGUE).getAmplifier() + 1));
}
Enchantment eff = item.getEnchantment(Enchantment.ID_EFFICIENCY);
if (eff != null && eff.getLevel() > 0) {
breakTime *= 1 - (0.3 * eff.getLevel());
}
breakTime -= 0.15;
BlockBreakEvent ev = new BlockBreakEvent(player, target, item, player.isCreative(), (player.lastBreak + breakTime * 1000) > System.currentTimeMillis());
double distance;
if (player.isSurvival() && !target.isBreakable(item)) {
ev.setCancelled();
} else if (!player.isOp() && (distance = this.server.getSpawnRadius()) > -1) {
Vector2 t = new Vector2(target.x, target.z);
Vector2 s = new Vector2(this.getSpawnLocation().x, this.getSpawnLocation().z);
if (!this.server.getOps().getAll().isEmpty() && t.distance(s) <= distance) {
ev.setCancelled();
}
}
this.server.getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
return null;
}
if (!ev.getInstaBreak() && ev.isFastBreak()) {
return null;
}
player.lastBreak = System.currentTimeMillis();
drops = ev.getDrops();
} else if (!target.isBreakable(item)) {
return null;
} else {
drops = target.getDrops(item);
}
Block above = this.getBlock(new Vector3(target.x, target.y + 1, target.z));
if (above != null) {
if (above.getId() == Item.FIRE) {
this.setBlock(above, new BlockAir(), true);
}
}
Tag tag = item.getNamedTagEntry("CanDestroy");
if (tag instanceof ListTag) {
boolean canBreak = false;
for (Tag v : ((ListTag<Tag>) tag).getAll()) {
if (v instanceof StringTag) {
Item entry = Item.fromString(((StringTag) v).data);
if (entry.getId() > 0 && entry.getBlock() != null && entry.getBlock().getId() == target.getId()) {
canBreak = true;
break;
}
}
}
if (!canBreak) {
return null;
}
}
if (createParticles) {
Map<Integer, Player> players = this.getChunkPlayers((int) target.x >> 4, (int) target.z >> 4);
this.addParticle(new DestroyBlockParticle(target.add(0.5), target), players.values());
if (player != null) {
players.remove(player.getLoaderId());
}
}
target.onBreak(item);
BlockEntity blockEntity = this.getBlockEntity(target);
if (blockEntity != null) {
if (blockEntity instanceof InventoryHolder) {
if (blockEntity instanceof BlockEntityChest) {
((BlockEntityChest) blockEntity).unpair();
}
for (Item chestItem : ((InventoryHolder) blockEntity).getInventory().getContents().values()) {
this.dropItem(target, chestItem);
}
}
blockEntity.close();
this.updateComparatorOutputLevel(target);
}
item.useOn(target);
if (item.isTool() && item.getDamage() >= item.getMaxDurability()) {
item = new ItemBlock(new BlockAir(), 0, 0);
}
if (this.gameRules.getBoolean(GameRule.DO_TILE_DROPS)) {
int dropExp = target.getDropExp();
if (player != null) {
player.addExperience(dropExp);
if (player.isSurvival()) {
for (int ii = 1; ii <= dropExp; ii++) {
this.dropExpOrb(target, 1);
}
}
}
if (player == null || player.isSurvival()) {
for (Item drop : drops) {
if (drop.getCount() > 0) {
this.dropItem(vector.add(0.5, 0.5, 0.5), drop);
}
}
}
}
return item;
}
use of cn.nukkit.Player 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());
ListTag<CompoundTag> sectionList = new ListTag<>("Sections");
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());
sectionList.add(s);
}
nbt.putList(sectionList);
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);
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 Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of cn.nukkit.Player in project Nukkit by Nukkit.
the class Chunk method toBinary.
public byte[] toBinary(boolean saveExtra) {
try {
LevelProvider provider = this.getProvider();
if (saveExtra && provider instanceof LevelDB) {
List<CompoundTag> entities = new ArrayList<>();
for (Entity entity : this.getEntities().values()) {
if (!(entity instanceof Player) && !entity.closed) {
entity.saveNBT();
entities.add(entity.namedTag);
}
}
EntitiesKey entitiesKey = EntitiesKey.create(this.getX(), this.getZ());
if (!entities.isEmpty()) {
((LevelDB) provider).getDatabase().put(entitiesKey.toArray(), NBTIO.write(entities));
} else {
((LevelDB) provider).getDatabase().delete(entitiesKey.toArray());
}
List<CompoundTag> tiles = new ArrayList<>();
for (BlockEntity blockEntity : this.getBlockEntities().values()) {
if (!blockEntity.closed) {
blockEntity.saveNBT();
entities.add(blockEntity.namedTag);
}
}
TilesKey tilesKey = TilesKey.create(this.getX(), this.getZ());
if (!tiles.isEmpty()) {
((LevelDB) provider).getDatabase().put(tilesKey.toArray(), NBTIO.write(tiles));
} else {
((LevelDB) provider).getDatabase().delete(tilesKey.toArray());
}
ExtraDataKey extraDataKey = ExtraDataKey.create(this.getX(), this.getZ());
if (!this.getBlockExtraDataArray().isEmpty()) {
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));
}
((LevelDB) provider).getDatabase().put(extraDataKey.toArray(), extraData.getBuffer());
} else {
((LevelDB) provider).getDatabase().delete(extraDataKey.toArray());
}
}
byte[] heightMap = this.getHeightMapArray();
byte[] biomeColors = new byte[this.getBiomeColorArray().length * 4];
for (int i = 0; i < this.getBiomeColorArray().length; i++) {
byte[] bytes = Binary.writeInt(this.getBiomeColorArray()[i]);
biomeColors[i * 4] = bytes[0];
biomeColors[i * 4 + 1] = bytes[1];
biomeColors[i * 4 + 2] = bytes[2];
biomeColors[i * 4 + 3] = bytes[3];
}
return Binary.appendBytes(Binary.writeLInt(this.getX()), Binary.writeLInt(this.getZ()), this.getBlockIdArray(), this.getBlockDataArray(), this.getBlockSkyLightArray(), this.getBlockLightArray(), heightMap, biomeColors, new byte[] { (byte) (((this.isLightPopulated ? 0x04 : 0) | (this.isPopulated() ? 0x02 : 0) | (this.isGenerated() ? 0x01 : 0)) & 0xff) });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of cn.nukkit.Player in project LuckPerms by lucko.
the class NukkitConnectionListener method onPlayerLogin.
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerLogin(PlayerLoginEvent e) {
/* Called when the player starts logging into the server.
At this point, the users data should be present and loaded. */
final Player player = e.getPlayer();
if (this.plugin.getConfiguration().get(ConfigKeys.DEBUG_LOGINS)) {
this.plugin.getLogger().info("Processing login for " + player.getUniqueId() + " - " + player.getName());
}
final User user = this.plugin.getUserManager().getIfLoaded(player.getUniqueId());
/* User instance is null for whatever reason. Could be that it was unloaded between asyncpre and now. */
if (user == null) {
this.deniedLogin.add(e.getPlayer().getUniqueId());
this.plugin.getLogger().warn("User " + player.getUniqueId() + " - " + player.getName() + " doesn't have data pre-loaded. - denying login.");
e.setCancelled();
e.setKickMessage(Message.LOADING_ERROR.asString(this.plugin.getLocaleManager()));
return;
}
// Care should be taken at this stage to ensure that async tasks which manipulate nukkit data check that the player is still online.
try {
// Make a new permissible for the user
LPPermissible lpPermissible = new LPPermissible(player, user, this.plugin);
// Inject into the player
PermissibleInjector.inject(player, lpPermissible);
} catch (Throwable t) {
t.printStackTrace();
}
this.plugin.refreshAutoOp(user, player);
}
Aggregations