Search in sources :

Example 31 with Player

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);
    }
}
Also used : EntityHuman(cn.nukkit.entity.EntityHuman) Player(cn.nukkit.Player)

Example 32 with Player

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);
}
Also used : Item(cn.nukkit.item.Item) Player(cn.nukkit.Player) InventoryContentPacket(cn.nukkit.network.protocol.InventoryContentPacket)

Example 33 with Player

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();
}
Also used : InventoryClickEvent(cn.nukkit.event.inventory.InventoryClickEvent) Player(cn.nukkit.Player) InventoryTransactionEvent(cn.nukkit.event.inventory.InventoryTransactionEvent) PlayerInventory(cn.nukkit.inventory.PlayerInventory) SlotChangeAction(cn.nukkit.inventory.transaction.action.SlotChangeAction) InventoryAction(cn.nukkit.inventory.transaction.action.InventoryAction)

Example 34 with Player

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;
}
Also used : Player(cn.nukkit.Player)

Example 35 with Player

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();
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) SoftReference(java.lang.ref.SoftReference) Player(cn.nukkit.Player) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap)

Aggregations

Player (cn.nukkit.Player)85 TranslationContainer (cn.nukkit.lang.TranslationContainer)24 Entity (cn.nukkit.entity.Entity)13 BlockEntity (cn.nukkit.blockentity.BlockEntity)10 Item (cn.nukkit.item.Item)10 Level (cn.nukkit.level.Level)7 IOException (java.io.IOException)6 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)5 Block (cn.nukkit.block.Block)4 ItemBlock (cn.nukkit.item.ItemBlock)4 BlockAir (cn.nukkit.block.BlockAir)3 EventHandler (cn.nukkit.event.EventHandler)3 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)3 InventoryContentPacket (cn.nukkit.network.protocol.InventoryContentPacket)3 InventorySlotPacket (cn.nukkit.network.protocol.InventorySlotPacket)3 User (me.lucko.luckperms.common.model.User)3 IPlayer (cn.nukkit.IPlayer)2 CommandSender (cn.nukkit.command.CommandSender)2 EntityItem (cn.nukkit.entity.item.EntityItem)2 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)2