Search in sources :

Example 76 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class BanIpCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length == 0) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }
    String value = args[0];
    String reason = "";
    for (int i = 1; i < args.length; i++) {
        reason += args[i] + " ";
    }
    if (reason.length() > 0) {
        reason = reason.substring(0, reason.length() - 1);
    }
    if (Pattern.matches("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", value)) {
        this.processIPBan(value, sender, reason);
        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.banip.success", value));
    } else {
        Player player = sender.getServer().getPlayer(value);
        if (player != null) {
            this.processIPBan(player.getAddress(), sender, reason);
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.banip.success.players", new String[] { player.getAddress(), player.getName() }));
        } else {
            String name = value.toLowerCase();
            String path = sender.getServer().getDataPath() + "players/";
            File file = new File(path + name + ".dat");
            CompoundTag nbt = null;
            if (file.exists()) {
                try {
                    nbt = NBTIO.readCompressed(new FileInputStream(file));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (nbt != null && nbt.contains("lastIP") && Pattern.matches("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", (value = nbt.getString("lastIP")))) {
                this.processIPBan(value, sender, reason);
                Command.broadcastCommandMessage(sender, new TranslationContainer("commands.banip.success", value));
            } else {
                sender.sendMessage(new TranslationContainer("commands.banip.invalid"));
                return false;
            }
        }
    }
    return true;
}
Also used : Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) IOException(java.io.IOException) File(java.io.File) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) FileInputStream(java.io.FileInputStream)

Example 77 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class BanIpCommand method processIPBan.

private void processIPBan(String ip, CommandSender sender, String reason) {
    sender.getServer().getIPBans().addBan(ip, reason, null, sender.getName());
    for (Player player : new ArrayList<>(sender.getServer().getOnlinePlayers().values())) {
        if (player.getAddress().equals(ip)) {
            player.kick(PlayerKickEvent.Reason.IP_BANNED, !reason.isEmpty() ? reason : "IP banned");
        }
    }
    sender.getServer().getNetwork().blockAddress(ip, -1);
}
Also used : Player(cn.nukkit.Player) ArrayList(java.util.ArrayList)

Example 78 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class Level method setSpawnLocation.

public void setSpawnLocation(Vector3 pos) {
    Position previousSpawn = this.getSpawnLocation();
    this.provider.setSpawn(pos);
    this.server.getPluginManager().callEvent(new SpawnChangeEvent(this, previousSpawn));
    SetSpawnPositionPacket pk = new SetSpawnPositionPacket();
    pk.spawnType = SetSpawnPositionPacket.TYPE_WORLD_SPAWN;
    pk.x = pos.getFloorX();
    pk.y = pos.getFloorY();
    pk.z = pos.getFloorZ();
    for (Player p : getPlayers().values()) p.dataPacket(pk);
}
Also used : Player(cn.nukkit.Player)

Example 79 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class Level method useItemOn.

public Item useItemOn(Vector3 vector, Item item, BlockFace face, float fx, float fy, float fz, Player player, boolean playSound) {
    Block target = this.getBlock(vector);
    Block block = target.getSide(face);
    if (block.y > 255 || block.y < 0) {
        return null;
    }
    if (target.getId() == Item.AIR) {
        return null;
    }
    if (player != null) {
        PlayerInteractEvent ev = new PlayerInteractEvent(player, item, target, face, target.getId() == 0 ? Action.RIGHT_CLICK_AIR : Action.RIGHT_CLICK_BLOCK);
        if (player.getGamemode() > 2) {
            ev.setCancelled();
        }
        int distance = this.server.getSpawnRadius();
        if (!player.isOp() && distance > -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()) {
            target.onUpdate(BLOCK_UPDATE_TOUCH);
            if ((!player.isSneaking() || player.getInventory().getItemInHand().isNull()) && target.canBeActivated() && target.onActivate(item, player)) {
                return item;
            }
            if (item.canBeActivated() && item.onActivate(this, player, block, target, face, fx, fy, fz)) {
                if (item.getCount() <= 0) {
                    item = new ItemBlock(new BlockAir(), 0, 0);
                    return item;
                }
            }
        } else {
            return null;
        }
    } else if (target.canBeActivated() && target.onActivate(item, null)) {
        return item;
    }
    Block hand;
    if (item.canBePlaced()) {
        hand = item.getBlock();
        hand.position(block);
    } else {
        return null;
    }
    if (!(block.canBeReplaced() || (hand.getId() == Item.SLAB && block.getId() == Item.SLAB))) {
        return null;
    }
    if (target.canBeReplaced()) {
        block = target;
        hand.position(block);
    }
    if (!hand.canPassThrough() && hand.getBoundingBox() != null) {
        Entity[] entities = this.getCollidingEntities(hand.getBoundingBox());
        int realCount = 0;
        for (Entity e : entities) {
            if (e instanceof EntityArrow || e instanceof EntityItem || (e instanceof Player && ((Player) e).isSpectator())) {
                continue;
            }
            ++realCount;
        }
        if (player != null) {
            Vector3 diff = player.getNextPosition().subtract(player.getPosition());
            if (diff.lengthSquared() > 0.00001) {
                AxisAlignedBB bb = player.getBoundingBox().getOffsetBoundingBox(diff.x, diff.y, diff.z);
                if (hand.getBoundingBox().intersectsWith(bb)) {
                    ++realCount;
                }
            }
        }
        if (realCount > 0) {
            // Entity in block
            return null;
        }
    }
    Tag tag = item.getNamedTagEntry("CanPlaceOn");
    if (tag instanceof ListTag) {
        boolean canPlace = 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()) {
                    canPlace = true;
                    break;
                }
            }
        }
        if (!canPlace) {
            return null;
        }
    }
    if (player != null) {
        BlockPlaceEvent event = new BlockPlaceEvent(player, hand, block, target, item);
        int distance = this.server.getSpawnRadius();
        if (!player.isOp() && distance > -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) {
                event.setCancelled();
            }
        }
        this.server.getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            return null;
        }
    }
    if (!hand.place(item, block, target, face, fx, fy, fz, player)) {
        return null;
    }
    if (player != null) {
        if (!player.isCreative()) {
            item.setCount(item.getCount() - 1);
        }
    }
    if (playSound) {
        this.addLevelSoundEvent(hand, LevelSoundEventPacket.SOUND_PLACE, 1, item.getId(), false);
    }
    if (item.getCount() <= 0) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    return item;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) EntityArrow(cn.nukkit.entity.projectile.EntityArrow) Player(cn.nukkit.Player) BlockPlaceEvent(cn.nukkit.event.block.BlockPlaceEvent) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) ItemBlock(cn.nukkit.item.ItemBlock) EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block) EntityItem(cn.nukkit.entity.item.EntityItem)

Example 80 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class Level method unloadChunk.

public boolean unloadChunk(int x, int z, boolean safe, boolean trySave) {
    if (safe && this.isChunkInUse(x, z)) {
        return false;
    }
    if (!this.isChunkLoaded(x, z)) {
        return true;
    }
    this.timings.doChunkUnload.startTiming();
    long index = Level.chunkHash(x, z);
    BaseFullChunk chunk = this.getChunk(x, z);
    if (chunk != null && chunk.getProvider() != null) {
        ChunkUnloadEvent ev = new ChunkUnloadEvent(chunk);
        this.server.getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            this.timings.doChunkUnload.stopTiming();
            return false;
        }
    }
    try {
        if (chunk != null) {
            if (trySave && this.getAutoSave()) {
                int entities = 0;
                for (Entity e : chunk.getEntities().values()) {
                    if (e instanceof Player) {
                        continue;
                    }
                    ++entities;
                }
                if (chunk.hasChanged() || !chunk.getBlockEntities().isEmpty() || entities > 0) {
                    this.provider.setChunk(x, z, chunk);
                    this.provider.saveChunk(x, z);
                }
            }
            for (ChunkLoader loader : this.getChunkLoaders(x, z)) {
                loader.onChunkUnloaded(chunk);
            }
        }
        this.provider.unloadChunk(x, z, safe);
    } catch (Exception e) {
        MainLogger logger = this.server.getLogger();
        logger.error(this.server.getLanguage().translateString("nukkit.level.chunkUnloadError", e.toString()));
        logger.logException(e);
    }
    this.timings.doChunkUnload.stopTiming();
    return true;
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) Player(cn.nukkit.Player) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) IOException(java.io.IOException)

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