Search in sources :

Example 1 with CommonPacket

use of com.bergerkiller.bukkit.common.protocol.CommonPacket in project BKCommonLib by bergerhealer.

the class ExtendedEntity method setPassengersSilent.

/**
     * Sets the passengers of this Vehicle without raising any events
     *
     * @param newPassenger to set to
     */
public void setPassengersSilent(List<org.bukkit.entity.Entity> newPassengers) {
    final EntityHandle handle = this.handle;
    // Generate a difference view between the expected list of passengers, and the current
    List<EntityHandle> removedPassengers = new ArrayList<EntityHandle>(handle.getPassengers().size());
    List<org.bukkit.entity.Entity> keptPassengers = new ArrayList<org.bukkit.entity.Entity>(newPassengers.size());
    for (EntityHandle oldPassenger : handle.getPassengers()) {
        boolean found = false;
        for (org.bukkit.entity.Entity p : newPassengers) {
            if (oldPassenger.getRaw() == Conversion.toEntityHandle.convert(p)) {
                found = true;
                keptPassengers.add(p);
                break;
            }
        }
        if (!found) {
            removedPassengers.add(oldPassenger);
        }
    }
    // Remove vehicle information and passenger information for all removed passengers
    for (EntityHandle passenger : removedPassengers) {
        passenger.setVehicle(null);
        handle.getPassengers().remove(passenger);
    }
    // Add new passengers as required
    List<EntityHandle> currPassengers = handle.getPassengers();
    for (org.bukkit.entity.Entity p : newPassengers) {
        EntityHandle passengerHandle = EntityHandle.fromBukkit(p);
        if (!currPassengers.contains(passengerHandle)) {
            currPassengers.add(passengerHandle);
            passengerHandle.setVehicle(handle);
            // Send mount packet
            CommonPacket packet = PacketType.OUT_MOUNT.newInstance(entity, keptPassengers);
            PacketUtil.broadcastEntityPacket(entity, packet);
        }
    }
    CommonPacket packet = PacketType.OUT_MOUNT.newInstanceHandles(entity, currPassengers);
    PacketUtil.broadcastEntityPacket(entity, packet);
    // Synchronize entity tracker of the vehicle to make sure it does not try to synchronize a second time
    Object entry = WorldUtil.getTracker(entity.getWorld()).getEntry(entity);
    if (entry != null) {
        NMSEntityTrackerEntry.passengers.set(entry, newPassengers);
    }
}
Also used : ArrayList(java.util.ArrayList) CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket) EntityHandle(com.bergerkiller.generated.net.minecraft.server.EntityHandle)

Example 2 with CommonPacket

use of com.bergerkiller.bukkit.common.protocol.CommonPacket in project BKCommonLib by bergerhealer.

the class MapDisplay method update.

/**
     * Performs all the updates required to display this Map Display to its viewers.
     * This is called automatically after having called start() on one of the implementations.
     * It should only be called when manually managing the map is required.
     */
public final void update() {
    if (this.info == null) {
        return;
    }
    // Update session. This will update the viewers, and update interception modes.
    if (!this.session.update()) {
        this.setRunning(false);
        return;
    }
    // Intercept player input when set
    if (this._receiveInputWhenHolding) {
        for (MapSession.Owner owner : this.session.onlineOwners) {
            owner.interceptInput = owner.controlling;
        }
    }
    // Perform the actual updates to the map
    if (this._updateWhenNotViewing || this.session.hasViewers) {
        //StopWatch.instance.start();
        this.onTick();
    //StopWatch.instance.stop().log("VirtualMap onTick()");
    }
    // Synchronize the map information to the clients
    if (this.clip.dirty) {
        // For all viewers watching, send map texture updates
        // For players that are in-sync, we can re-use the same packet
        CommonPacket syncPacket = null;
        for (MapSession.Owner owner : this.session.onlineOwners) {
            if (!owner.viewing) {
                // Update dirty clip only
                owner.clip.markDirty(this.clip);
                continue;
            }
            // When viewers have individual dirty areas, they need their own update packets
            if (owner.clip.dirty) {
                owner.clip.markDirty(this.clip);
                owner.updateMap(createPacket(owner.clip));
                continue;
            }
            // We can re-use the same update packet for all viewers
            if (syncPacket == null) {
                syncPacket = createPacket(this.clip);
            } else {
                syncPacket = syncPacket.clone();
            }
            owner.updateMap(syncPacket);
        }
        // Done updating, reset the dirty state
        this.clip.clearDirty();
    } else if (session.hasNewViewers) {
        // Send full changes to the dirty viewers
        for (MapSession.Owner owner : session.onlineOwners) {
            if (owner.isNewViewer()) {
                owner.updateMap(createPacket(owner.clip));
            }
        }
    }
}
Also used : CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket)

Example 3 with CommonPacket

use of com.bergerkiller.bukkit.common.protocol.CommonPacket in project BKCommonLib by bergerhealer.

the class CommonPlayerMeta method syncRemoveQueue.

/**
     * Sends out destroy packets for all entity ids in the removal queue
     */
public void syncRemoveQueue() {
    if (!this.removeQueue.isEmpty()) {
        CommonPacket packet = PacketType.OUT_ENTITY_DESTROY.newInstance(this.removeQueue);
        this.removeQueue.clear();
        Player p = this.playerRef.get();
        if (p != null) {
            PacketUtil.sendPacket(p, packet);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket)

Example 4 with CommonPacket

use of com.bergerkiller.bukkit.common.protocol.CommonPacket in project BKCommonLib by bergerhealer.

the class CommonMapController method onPacketReceive.

@Override
public void onPacketReceive(PacketReceiveEvent event) {
    // Handle input coming from the player for the map
    if (event.getType() == PacketType.IN_STEER_VEHICLE) {
        Player p = event.getPlayer();
        MapPlayerInput input = playerInputs.get(p);
        if (input != null) {
            CommonPacket packet = event.getPacket();
            int dx = (int) -Math.signum(packet.read(PacketType.IN_STEER_VEHICLE.sideways));
            int dy = (int) -Math.signum(packet.read(PacketType.IN_STEER_VEHICLE.forwards));
            int dz = 0;
            if (packet.read(PacketType.IN_STEER_VEHICLE.unmount)) {
                dz -= 1;
            }
            if (packet.read(PacketType.IN_STEER_VEHICLE.jump)) {
                dz += 1;
            }
            // Receive input. If it will be handled, it will cancel further handling of this packet
            event.setCancelled(input.receiveInput(dx, dy, dz));
        }
    }
}
Also used : Player(org.bukkit.entity.Player) MapPlayerInput(com.bergerkiller.bukkit.common.map.MapPlayerInput) CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket)

Example 5 with CommonPacket

use of com.bergerkiller.bukkit.common.protocol.CommonPacket in project BKCommonLib by bergerhealer.

the class CommonObjective method display.

private void display() {
    CommonPacket packet = new CommonPacket(PacketType.OUT_SCOREBOARD_DISPLAY_OBJECTIVE);
    packet.write(PacketType.OUT_SCOREBOARD_DISPLAY_OBJECTIVE.name, this.name);
    packet.write(PacketType.OUT_SCOREBOARD_DISPLAY_OBJECTIVE.display, this.display.getId());
    PacketUtil.sendPacket(this.scoreboard.getPlayer(), packet);
}
Also used : CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket)

Aggregations

CommonPacket (com.bergerkiller.bukkit.common.protocol.CommonPacket)17 Player (org.bukkit.entity.Player)3 PacketListener (com.bergerkiller.bukkit.common.protocol.PacketListener)2 PacketType (com.bergerkiller.bukkit.common.protocol.PacketType)2 ArrayList (java.util.ArrayList)2 PacketReceiveEvent (com.bergerkiller.bukkit.common.events.PacketReceiveEvent)1 PacketSendEvent (com.bergerkiller.bukkit.common.events.PacketSendEvent)1 MapPlayerInput (com.bergerkiller.bukkit.common.map.MapPlayerInput)1 PacketMonitor (com.bergerkiller.bukkit.common.protocol.PacketMonitor)1 DataWatcher (com.bergerkiller.bukkit.common.wrappers.DataWatcher)1 EntityHandle (com.bergerkiller.generated.net.minecraft.server.EntityHandle)1 NMSEntityPlayer (com.bergerkiller.reflection.net.minecraft.server.NMSEntityPlayer)1 Location (org.bukkit.Location)1