Java Code Examples for cn.nukkit.Server#broadcastPacket()

The following examples show how to use cn.nukkit.Server#broadcastPacket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Level.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void addSound(Sound sound, Player[] players) {
    DataPacket[] packets = sound.encode();

    if (players == null) {
        if (packets != null) {
            for (DataPacket packet : packets) {
                this.addChunkPacket((int) sound.x >> 4, (int) sound.z >> 4, packet);
            }
        }
    } else {
        if (packets != null) {
            if (packets.length == 1) {
                Server.broadcastPacket(players, packets[0]);
            } else {
                this.server.batchPackets(players, packets, false);
            }
        }
    }
}
 
Example 2
Source File: Level.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void addSound(Vector3 pos, Sound sound, float volume, float pitch, Player... players) {
    Preconditions.checkArgument(volume >= 0 && volume <= 1, "Sound volume must be between 0 and 1");
    Preconditions.checkArgument(pitch >= 0, "Sound pitch must be higher than 0");

    PlaySoundPacket packet = new PlaySoundPacket();
    packet.name = sound.getSound();
    packet.volume = 1;
    packet.pitch = 1;
    packet.x = pos.getFloorX();
    packet.y = pos.getFloorY();
    packet.z = pos.getFloorZ();

    if (players == null || players.length == 0) {
        addChunkPacket(pos.getFloorX() >> 4, pos.getFloorZ() >> 4, packet);
    } else {
        Server.broadcastPacket(players, packet);
    }
}
 
Example 3
Source File: Level.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void addParticle(Particle particle, Player[] players) {
    DataPacket[] packets = particle.encode();

    if (players == null) {
        if (packets != null) {
            for (DataPacket packet : packets) {
                this.addChunkPacket((int) particle.x >> 4, (int) particle.z >> 4, packet);
            }
        }
    } else {
        if (packets != null) {
            if (packets.length == 1) {
                Server.broadcastPacket(players, packets[0]);
            } else {
                this.server.batchPackets(players, packets, false);
            }
        }
    }
}
 
Example 4
Source File: Level.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void sendWeather(Player[] players) {
    if (players == null) {
        players = this.getPlayers().values().stream().toArray(Player[]::new);
    }

    LevelEventPacket pk = new LevelEventPacket();

    if (this.isRaining()) {
        pk.evid = LevelEventPacket.EVENT_START_RAIN;
        pk.data = rand.nextInt(50000) + 10000;
    } else {
        pk.evid = LevelEventPacket.EVENT_STOP_RAIN;
    }

    Server.broadcastPacket(players, pk);

    if (this.isThundering()) {
        pk.evid = LevelEventPacket.EVENT_START_THUNDER;
        pk.data = rand.nextInt(50000) + 10000;
    } else {
        pk.evid = LevelEventPacket.EVENT_STOP_THUNDER;
    }

    Server.broadcastPacket(players, pk);
}
 
Example 5
Source File: Level.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public boolean setThundering(boolean thundering) {
    ThunderChangeEvent ev = new ThunderChangeEvent(this, thundering);
    this.getServer().getPluginManager().callEvent(ev);

    if (ev.isCancelled()) {
        return false;
    }

    if (thundering && !isRaining()) {
        setRaining(true);
    }

    this.thundering = thundering;

    LevelEventPacket pk = new LevelEventPacket();
    // These numbers are from Minecraft
    if (thundering) {
        pk.evid = LevelEventPacket.EVENT_START_THUNDER;
        pk.data = rand.nextInt(50000) + 10000;
        setThunderTime(rand.nextInt(12000) + 3600);
    } else {
        pk.evid = LevelEventPacket.EVENT_STOP_THUNDER;
        setThunderTime(rand.nextInt(168000) + 12000);
    }

    Server.broadcastPacket(this.getPlayers().values(), pk);

    return true;
}
 
Example 6
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void broadcastLinkPacket(Entity rider, byte type) {
    SetEntityLinkPacket pk = new SetEntityLinkPacket();
    pk.vehicleUniqueId = getId();         // To the?
    pk.riderUniqueId = rider.getId(); // From who?
    pk.type = type;

    Server.broadcastPacket(this.hasSpawned.values(), pk);
}
 
Example 7
Source File: EntityLiving.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setHealth(float health) {
    boolean wasAlive = this.isAlive();
    super.setHealth(health);
    if (this.isAlive() && !wasAlive) {
        EntityEventPacket pk = new EntityEventPacket();
        pk.eid = this.getId();
        pk.event = EntityEventPacket.RESPAWN;
        Server.broadcastPacket(this.hasSpawned.values(), pk);
    }
}
 
Example 8
Source File: EntityLiving.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (this.attackTime > 0 || this.noDamageTicks > 0) {
        EntityDamageEvent lastCause = this.getLastDamageCause();
        if (lastCause != null && lastCause.getDamage() >= source.getDamage()) {
            return false;
        }
    }

    if (super.attack(source)) {
        if (source instanceof EntityDamageByEntityEvent) {
            Entity e = ((EntityDamageByEntityEvent) source).getDamager();
            if (source instanceof EntityDamageByChildEntityEvent) {
                e = ((EntityDamageByChildEntityEvent) source).getChild();
            }

            if (e.isOnFire() && !(e instanceof Player)) {
                this.setOnFire(2 * this.server.getDifficulty());
            }

            double deltaX = this.x - e.x;
            double deltaZ = this.z - e.z;
            this.knockBack(e, source.getDamage(), deltaX, deltaZ, ((EntityDamageByEntityEvent) source).getKnockBack());
        }

        EntityEventPacket pk = new EntityEventPacket();
        pk.eid = this.getId();
        pk.event = this.getHealth() <= 0 ? EntityEventPacket.DEATH_ANIMATION : EntityEventPacket.HURT_ANIMATION;
        Server.broadcastPacket(this.hasSpawned.values(), pk);

        this.attackTime = 10;

        return true;
    } else {
        return false;
    }
}
 
Example 9
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public boolean setThundering(boolean thundering) {
    ThunderChangeEvent ev = new ThunderChangeEvent(this, thundering);
    this.getServer().getPluginManager().callEvent(ev);

    if (ev.isCancelled()) {
        return false;
    }

    if (thundering && !isRaining()) {
        setRaining(true);
    }

    this.thundering = thundering;

    LevelEventPacket pk = new LevelEventPacket();
    // These numbers are from Minecraft
    if (thundering) {
        pk.evid = LevelEventPacket.EVENT_START_THUNDER;
        pk.data = ThreadLocalRandom.current().nextInt(50000) + 10000;
        setThunderTime(ThreadLocalRandom.current().nextInt(12000) + 3600);
    } else {
        pk.evid = LevelEventPacket.EVENT_STOP_THUNDER;
        setThunderTime(ThreadLocalRandom.current().nextInt(168000) + 12000);
    }

    Server.broadcastPacket(this.getPlayers().values(), pk);

    return true;
}
 
Example 10
Source File: BlockEntityJukebox.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void play() {
    if (this.recordItem instanceof ItemRecord) {
        LevelSoundEventPacket pk = new LevelSoundEventPacket();
        pk.sound = ((ItemRecord) this.recordItem).getSoundId();
        pk.pitch = 1;
        pk.extraData = -1;
        pk.x = (float) this.x;
        pk.y = (float) this.y;
        pk.z = (float) this.z;

        Server.broadcastPacket(this.level.getPlayers().values(), pk);
    }
}
 
Example 11
Source File: EntityItem.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCollideWithPlayer(Player player) {
    if(this.getPickupDelay() > 0) {
        return;
    }

    Item item = this.getItem();
    PlayerInventory playerInventory = player.getInventory();

    if(!(item instanceof Item) || (player.isSurvival() && !playerInventory.canAddItem(item))) {
        return;
    }

    InventoryPickupItemEvent ev;
    this.server.getPluginManager().callEvent(ev = new InventoryPickupItemEvent(playerInventory, this));
    if(ev.isCancelled()) {
        return;
    }

    TakeItemEntityPacket pk = new TakeItemEntityPacket();
    pk.entityRuntimeId = player.getId();
    pk.target = this.getId();
    Server.broadcastPacket(this.getViewers().values(), pk);

    playerInventory.addItem(item);
    this.kill();
}
 
Example 12
Source File: EntityLiving.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setHealth(float health) {
    boolean wasAlive = this.isAlive();
    super.setHealth(health);
    if (this.isAlive() && !wasAlive) {
        EntityEventPacket pk = new EntityEventPacket();
        pk.eid = this.getId();
        pk.eid = EntityEventPacket.RESPAWN;
        Server.broadcastPacket(this.hasSpawned.values(), pk);
    }
}
 
Example 13
Source File: Level.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void sendBlockExtraData(int x, int y, int z, int id, int data, Collection<Player> players) {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_SET_DATA;
    pk.x = x + 0.5f;
    pk.y = y + 0.5f;
    pk.z = z + 0.5f;
    pk.data = (data << 8) | id;

    Server.broadcastPacket(players, pk);
}
 
Example 14
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public boolean setRaining(boolean raining) {
    WeatherChangeEvent ev = new WeatherChangeEvent(this, raining);
    this.getServer().getPluginManager().callEvent(ev);

    if (ev.isCancelled()) {
        return false;
    }

    this.raining = raining;

    LevelEventPacket pk = new LevelEventPacket();
    // These numbers are from Minecraft

    if (raining) {
        pk.evid = LevelEventPacket.EVENT_START_RAIN;
        int time = ThreadLocalRandom.current().nextInt(12000) + 12000;
        pk.data = time;
        setRainTime(time);
    } else {
        pk.evid = LevelEventPacket.EVENT_STOP_RAIN;
        setRainTime(ThreadLocalRandom.current().nextInt(168000) + 12000);
    }

    Server.broadcastPacket(this.getPlayers().values(), pk);

    return true;
}
 
Example 15
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void sendBlockExtraData(int x, int y, int z, int id, int data, Player[] players) {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_SET_DATA;
    pk.x = x + 0.5f;
    pk.y = y + 0.5f;
    pk.z = z + 0.5f;
    pk.data = (data << 8) | id;

    Server.broadcastPacket(players, pk);
}
 
Example 16
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void sendBlockExtraData(int x, int y, int z, int id, int data, Collection<Player> players) {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_SET_DATA;
    pk.x = x + 0.5f;
    pk.y = y + 0.5f;
    pk.z = z + 0.5f;
    pk.data = (data << 8) | id;

    Server.broadcastPacket(players, pk);
}
 
Example 17
Source File: EntityLiving.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (this.attackTime > 0 || this.noDamageTicks > 0) {
        EntityDamageEvent lastCause = this.getLastDamageCause();
        if (lastCause != null && lastCause.getDamage() >= source.getDamage()) {
            return false;
        }
    }

    if (super.attack(source)) {
        if (source instanceof EntityDamageByEntityEvent) {
            Entity e = ((EntityDamageByEntityEvent) source).getDamager();
            if (source instanceof EntityDamageByChildEntityEvent) {
                e = ((EntityDamageByChildEntityEvent) source).getChild();
            }

            if (e.isOnFire() && !(e instanceof Player)) {
                this.setOnFire(2 * this.server.getDifficulty());
            }

            double deltaX = this.x - e.x;
            double deltaZ = this.z - e.z;
            this.knockBack(e, source.getDamage(), deltaX, deltaZ, ((EntityDamageByEntityEvent) source).getKnockBack());
        }

        EntityEventPacket pk = new EntityEventPacket();
        pk.entityRuntimeId = this.getId();
        pk.event = this.getHealth() <= 0 ? EntityEventPacket.DEATH_ANIMATION : EntityEventPacket.HURT_ANIMATION;
        Server.broadcastPacket(this.level.getPlayers().values(), pk);

        this.attackTime = 10;

        return true;
    } else {
        return false;
    }
}
 
Example 18
Source File: EntityLiving.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setHealth(float health) {
    boolean wasAlive = this.isAlive();
    super.setHealth(health);
    if (this.isAlive() && !wasAlive) {
        EntityEventPacket pk = new EntityEventPacket();
        pk.entityRuntimeId = this.getId();
        pk.event = EntityEventPacket.RESPAWN;
        Server.broadcastPacket(this.level.getPlayers().values(), pk);
    }
}
 
Example 19
Source File: EntityLiving.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (this.noDamageTicks > 0) {
        return false;
    } else if (this.attackTime > 0) {
        EntityDamageEvent lastCause = this.getLastDamageCause();
        if (lastCause != null && lastCause.getDamage() >= source.getDamage()) {
            return false;
        }
    }

    if (super.attack(source)) {
        if (source instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
            if (source instanceof EntityDamageByChildEntityEvent) {
                damager = ((EntityDamageByChildEntityEvent) source).getChild();
            }

            //Critical hit
            if (damager instanceof Player && !damager.onGround) {
                AnimatePacket animate = new AnimatePacket();
                animate.action = AnimatePacket.Action.CRITICAL_HIT;
                animate.eid = getId();

                this.getLevel().addChunkPacket(damager.getChunkX(), damager.getChunkZ(), animate);
                this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ATTACK_STRONG);

                source.setDamage(source.getDamage() * 1.5f);
            }

            if (damager.isOnFire() && !(damager instanceof Player)) {
                this.setOnFire(2 * this.server.getDifficulty());
            }

            double deltaX = this.x - damager.x;
            double deltaZ = this.z - damager.z;
            this.knockBack(damager, source.getDamage(), deltaX, deltaZ, ((EntityDamageByEntityEvent) source).getKnockBack());
        }

        EntityEventPacket pk = new EntityEventPacket();
        pk.eid = this.getId();
        pk.event = this.getHealth() <= 0 ? EntityEventPacket.DEATH_ANIMATION : EntityEventPacket.HURT_ANIMATION;
        Server.broadcastPacket(this.hasSpawned.values(), pk);

        this.attackTime = source.getAttackCooldown();

        return true;
    } else {
        return false;
    }
}
 
Example 20
Source File: Level.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void sendTime(Player... players) {
    SetTimePacket pk = new SetTimePacket();
    pk.time = (int) this.time;

    Server.broadcastPacket(players, pk);
}