Search in sources :

Example 6 with Player

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

the class Entity method checkChunks.

protected void checkChunks() {
    if (this.chunk == null || (this.chunk.getX() != ((int) this.x >> 4)) || this.chunk.getZ() != ((int) this.z >> 4)) {
        if (this.chunk != null) {
            this.chunk.removeEntity(this);
        }
        this.chunk = this.level.getChunk((int) this.x >> 4, (int) this.z >> 4, true);
        if (!this.justCreated) {
            Map<Integer, Player> newChunk = this.level.getChunkPlayers((int) this.x >> 4, (int) this.z >> 4);
            for (Player player : new ArrayList<>(this.hasSpawned.values())) {
                if (!newChunk.containsKey(player.getLoaderId())) {
                    this.despawnFrom(player);
                } else {
                    newChunk.remove(player.getLoaderId());
                }
            }
            for (Player player : newChunk.values()) {
                this.spawnTo(player);
            }
        }
        if (this.chunk == null) {
            return;
        }
        this.chunk.addEntity(this);
    }
}
Also used : Player(cn.nukkit.Player)

Example 7 with Player

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

the class Entity method fall.

public void fall(float fallDistance) {
    float damage = (float) Math.floor(fallDistance - 3 - (this.hasEffect(Effect.JUMP) ? this.getEffect(Effect.JUMP).getAmplifier() + 1 : 0));
    if (damage > 0) {
        this.attack(new EntityDamageEvent(this, DamageCause.FALL, damage));
    }
    if (fallDistance > 0.75) {
        Block down = this.level.getBlock(this.floor().down());
        if (down.getId() == Item.FARMLAND) {
            Event ev;
            if (this instanceof Player) {
                ev = new PlayerInteractEvent((Player) this, null, down, null, Action.PHYSICAL);
            } else {
                ev = new EntityInteractEvent(this, down);
            }
            this.server.getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                return;
            }
            this.level.setBlock(down, new BlockDirt(), false, true);
        }
    }
}
Also used : Player(cn.nukkit.Player) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) Block(cn.nukkit.block.Block) PlayerInteractEvent(cn.nukkit.event.player.PlayerInteractEvent) Event(cn.nukkit.event.Event) PlayerTeleportEvent(cn.nukkit.event.player.PlayerTeleportEvent) BlockDirt(cn.nukkit.block.BlockDirt)

Example 8 with Player

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

the class Entity method sendData.

public void sendData(Player[] players, EntityMetadata data) {
    SetEntityDataPacket pk = new SetEntityDataPacket();
    pk.eid = this.getId();
    pk.metadata = data == null ? this.dataProperties : data;
    for (Player player : players) {
        if (player == this) {
            continue;
        }
        player.dataPacket(pk.clone());
    }
    if (this instanceof Player) {
        ((Player) this).dataPacket(pk);
    }
}
Also used : Player(cn.nukkit.Player)

Example 9 with Player

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

the class Entity method saveNBT.

public void saveNBT() {
    if (!(this instanceof Player)) {
        this.namedTag.putString("id", this.getSaveId());
        if (!this.getNameTag().equals("")) {
            this.namedTag.putString("CustomName", this.getNameTag());
            this.namedTag.putBoolean("CustomNameVisible", this.isNameTagVisible());
        } else {
            this.namedTag.remove("CustomName");
            this.namedTag.remove("CustomNameVisible");
        }
    }
    this.namedTag.putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("0", this.x)).add(new DoubleTag("1", this.y)).add(new DoubleTag("2", this.z)));
    this.namedTag.putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("0", this.motionX)).add(new DoubleTag("1", this.motionY)).add(new DoubleTag("2", this.motionZ)));
    this.namedTag.putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("0", (float) this.yaw)).add(new FloatTag("1", (float) this.pitch)));
    this.namedTag.putFloat("FallDistance", this.fallDistance);
    this.namedTag.putShort("Fire", this.fireTicks);
    this.namedTag.putShort("Air", this.getDataPropertyShort(DATA_AIR));
    this.namedTag.putBoolean("OnGround", this.onGround);
    this.namedTag.putBoolean("Invulnerable", this.invulnerable);
    this.namedTag.putFloat("Scale", this.scale);
    if (!this.effects.isEmpty()) {
        ListTag<CompoundTag> list = new ListTag<>("ActiveEffects");
        for (Effect effect : this.effects.values()) {
            list.add(new CompoundTag(String.valueOf(effect.getId())).putByte("Id", effect.getId()).putByte("Amplifier", effect.getAmplifier()).putInt("Duration", effect.getDuration()).putBoolean("Ambient", false).putBoolean("ShowParticles", effect.isVisible()));
        }
        this.namedTag.putList(list);
    } else {
        this.namedTag.remove("ActiveEffects");
    }
}
Also used : Player(cn.nukkit.Player) FloatTag(cn.nukkit.nbt.tag.FloatTag) Effect(cn.nukkit.potion.Effect) DoubleTag(cn.nukkit.nbt.tag.DoubleTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 10 with Player

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

the class EffectCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Player player = sender.getServer().getPlayer(args[0]);
    if (player == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        return true;
    }
    if (args[1].equalsIgnoreCase("clear")) {
        for (Effect effect : player.getEffects().values()) {
            player.removeEffect(effect.getId());
        }
        sender.sendMessage(new TranslationContainer("commands.effect.success.removed.all", player.getDisplayName()));
        return true;
    }
    Effect effect;
    try {
        effect = Effect.getEffect(Integer.parseInt(args[1]));
    } catch (NumberFormatException | ServerException a) {
        try {
            effect = Effect.getEffectByName(args[1]);
        } catch (Exception e) {
            sender.sendMessage(new TranslationContainer("commands.effect.notFound", args[1]));
            return true;
        }
    }
    int duration = 300;
    int amplification = 0;
    if (args.length >= 3) {
        try {
            duration = Integer.valueOf(args[2]);
        } catch (NumberFormatException a) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
        if (!(effect instanceof InstantEffect)) {
            duration *= 20;
        }
    } else if (effect instanceof InstantEffect) {
        duration = 1;
    }
    if (args.length >= 4) {
        try {
            amplification = Integer.valueOf(args[3]);
        } catch (NumberFormatException a) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
    }
    if (args.length >= 5) {
        String v = args[4].toLowerCase();
        if (v.matches("(?i)|on|true|t|1")) {
            effect.setVisible(false);
        }
    }
    if (duration == 0) {
        if (!player.hasEffect(effect.getId())) {
            if (player.getEffects().size() == 0) {
                sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive.all", player.getDisplayName()));
            } else {
                sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive", new String[] { effect.getName(), player.getDisplayName() }));
            }
            return true;
        }
        player.removeEffect(effect.getId());
        sender.sendMessage(new TranslationContainer("commands.effect.success.removed", new String[] { effect.getName(), player.getDisplayName() }));
    } else {
        effect.setDuration(duration).setAmplifier(amplification);
        player.addEffect(effect);
        Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.effect.success", new String[] { effect.getName(), String.valueOf(effect.getId()), String.valueOf(effect.getAmplifier()), player.getDisplayName(), String.valueOf(effect.getDuration() / 20) }));
    }
    return true;
}
Also used : InstantEffect(cn.nukkit.potion.InstantEffect) Player(cn.nukkit.Player) ServerException(cn.nukkit.utils.ServerException) TranslationContainer(cn.nukkit.lang.TranslationContainer) Effect(cn.nukkit.potion.Effect) InstantEffect(cn.nukkit.potion.InstantEffect) ServerException(cn.nukkit.utils.ServerException)

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