use of cn.nukkit.Player in project Nukkit by Nukkit.
the class PlayerInventory method onSlotChange.
@Override
public void onSlotChange(int index, Item before, boolean send) {
EntityHuman holder = this.getHolder();
if (holder instanceof Player && !((Player) holder).spawned) {
return;
}
if (index >= this.getSize()) {
this.sendArmorSlot(index, this.getViewers());
this.sendArmorSlot(index, this.getHolder().getViewers().values());
} else {
super.onSlotChange(index, before, send);
}
}
use of cn.nukkit.Player in project Nukkit by Nukkit.
the class PlayerInventory method sendCreativeContents.
public void sendCreativeContents() {
if (!(this.getHolder() instanceof Player)) {
return;
}
Player p = (Player) this.getHolder();
InventoryContentPacket pk = new InventoryContentPacket();
pk.inventoryId = ContainerIds.CREATIVE;
if (!p.isSpectator()) {
// fill it for all gamemodes except spectator
pk.slots = Item.getCreativeItems().stream().toArray(Item[]::new);
}
p.dataPacket(pk);
}
use of cn.nukkit.Player in project Nukkit by Nukkit.
the class InventoryTransaction method callExecuteEvent.
protected boolean callExecuteEvent() {
InventoryTransactionEvent ev = new InventoryTransactionEvent(this);
this.source.getServer().getPluginManager().callEvent(ev);
SlotChangeAction from = null;
SlotChangeAction to = null;
Player who = null;
for (InventoryAction action : this.actions) {
if (!(action instanceof SlotChangeAction)) {
continue;
}
SlotChangeAction slotChange = (SlotChangeAction) action;
if (slotChange.getInventory() instanceof PlayerInventory) {
who = (Player) slotChange.getInventory().getHolder();
}
if (from == null) {
from = slotChange;
} else {
to = slotChange;
}
}
if (who != null && to != null) {
if (from.getTargetItem().getCount() > from.getSourceItem().getCount()) {
from = to;
}
InventoryClickEvent ev2 = new InventoryClickEvent(who, from.getInventory(), from.getSlot(), from.getSourceItem(), from.getTargetItem());
this.source.getServer().getPluginManager().callEvent(ev2);
if (ev2.isCancelled()) {
return false;
}
}
return !ev.isCancelled();
}
use of cn.nukkit.Player in project Nukkit by Nukkit.
the class Level method unload.
public boolean unload(boolean force) {
LevelUnloadEvent ev = new LevelUnloadEvent(this);
if (this == this.server.getDefaultLevel() && !force) {
ev.setCancelled();
}
this.server.getPluginManager().callEvent(ev);
if (!force && ev.isCancelled()) {
return false;
}
this.server.getLogger().info(this.server.getLanguage().translateString("nukkit.level.unloading", TextFormat.GREEN + this.getName() + TextFormat.WHITE));
Level defaultLevel = this.server.getDefaultLevel();
for (Player player : new ArrayList<>(this.getPlayers().values())) {
if (this == defaultLevel || defaultLevel == null) {
player.close(player.getLeaveMessage(), "Forced default level unload");
} else {
player.teleport(this.server.getDefaultLevel().getSafeSpawn());
}
}
if (this == defaultLevel) {
this.server.setDefaultLevel(null);
}
this.close();
return true;
}
use of cn.nukkit.Player in project Nukkit by Nukkit.
the class Level method doTick.
public void doTick(int currentTick) {
this.timings.doTick.startTiming();
updateBlockLight(lightQueue);
this.checkTime();
// Tick Weather
this.rainTime--;
if (this.rainTime <= 0) {
if (!this.setRaining(!this.raining)) {
if (this.raining) {
setRainTime(ThreadLocalRandom.current().nextInt(12000) + 12000);
} else {
setRainTime(ThreadLocalRandom.current().nextInt(168000) + 12000);
}
}
}
this.thunderTime--;
if (this.thunderTime <= 0) {
if (!this.setThundering(!this.thundering)) {
if (this.thundering) {
setThunderTime(ThreadLocalRandom.current().nextInt(12000) + 3600);
} else {
setThunderTime(ThreadLocalRandom.current().nextInt(168000) + 12000);
}
}
}
if (this.isThundering()) {
Map<Long, ? extends FullChunk> chunks = getChunks();
if (chunks instanceof Long2ObjectOpenHashMap) {
Long2ObjectOpenHashMap<? extends FullChunk> fastChunks = (Long2ObjectOpenHashMap) chunks;
ObjectIterator<? extends Long2ObjectMap.Entry<? extends FullChunk>> iter = fastChunks.long2ObjectEntrySet().fastIterator();
while (iter.hasNext()) {
Long2ObjectMap.Entry<? extends FullChunk> entry = iter.next();
performThunder(entry.getLongKey(), entry.getValue());
}
} else {
for (Map.Entry<Long, ? extends FullChunk> entry : getChunks().entrySet()) {
performThunder(entry.getKey(), entry.getValue());
}
}
}
this.skyLightSubtracted = this.calculateSkylightSubtracted(1);
this.levelCurrentTick++;
this.unloadChunks();
this.timings.doTickPending.startTiming();
int polled = 0;
this.updateQueue.tick(this.getCurrentTick());
this.timings.doTickPending.stopTiming();
TimingsHistory.entityTicks += this.updateEntities.size();
this.timings.entityTick.startTiming();
if (!this.updateEntities.isEmpty()) {
for (long id : new ArrayList<>(this.updateEntities.keySet())) {
Entity entity = this.updateEntities.get(id);
if (entity.closed || !entity.onUpdate(currentTick)) {
this.updateEntities.remove(id);
}
}
}
this.timings.entityTick.stopTiming();
TimingsHistory.tileEntityTicks += this.updateBlockEntities.size();
this.timings.blockEntityTick.startTiming();
if (!this.updateBlockEntities.isEmpty()) {
for (long id : new ArrayList<>(this.updateBlockEntities.keySet())) {
if (!this.updateBlockEntities.get(id).onUpdate()) {
this.updateBlockEntities.remove(id);
}
}
}
this.timings.blockEntityTick.stopTiming();
this.timings.tickChunks.startTiming();
this.tickChunks();
this.timings.tickChunks.stopTiming();
if (!this.changedBlocks.isEmpty()) {
if (!this.players.isEmpty()) {
ObjectIterator<Long2ObjectMap.Entry<SoftReference<Map<Character, Object>>>> iter = changedBlocks.long2ObjectEntrySet().fastIterator();
while (iter.hasNext()) {
Long2ObjectMap.Entry<SoftReference<Map<Character, Object>>> entry = iter.next();
long index = entry.getKey();
Map<Character, Object> blocks = entry.getValue().get();
int chunkX = Level.getHashX(index);
int chunkZ = Level.getHashZ(index);
if (blocks == null || blocks.size() > MAX_BLOCK_CACHE) {
FullChunk chunk = this.getChunk(chunkX, chunkZ);
for (Player p : this.getChunkPlayers(chunkX, chunkZ).values()) {
p.onChunkChanged(chunk);
}
} else {
Collection<Player> toSend = this.getChunkPlayers(chunkX, chunkZ).values();
Player[] playerArray = toSend.toArray(new Player[toSend.size()]);
Vector3[] blocksArray = new Vector3[blocks.size()];
int i = 0;
for (char blockHash : blocks.keySet()) {
Vector3 hash = getBlockXYZ(index, blockHash);
blocksArray[i++] = hash;
}
this.sendBlocks(playerArray, blocksArray, UpdateBlockPacket.FLAG_ALL);
}
}
}
this.changedBlocks.clear();
}
this.processChunkRequest();
if (this.sleepTicks > 0 && --this.sleepTicks <= 0) {
this.checkSleep();
}
for (long index : this.chunkPackets.keySet()) {
int chunkX = Level.getHashX(index);
int chunkZ = Level.getHashZ(index);
Player[] chunkPlayers = this.getChunkPlayers(chunkX, chunkZ).values().stream().toArray(Player[]::new);
if (chunkPlayers.length > 0) {
for (DataPacket pk : this.chunkPackets.get(index)) {
Server.broadcastPacket(chunkPlayers, pk);
}
}
}
if (gameRules.isStale()) {
GameRulesChangedPacket packet = new GameRulesChangedPacket();
packet.gameRules = gameRules;
Server.broadcastPacket(players.values().toArray(new Player[players.size()]), packet);
gameRules.refresh();
}
this.chunkPackets.clear();
this.timings.doTick.stopTiming();
}
Aggregations