cn.nukkit.network.protocol.DataPacket Java Examples

The following examples show how to use cn.nukkit.network.protocol.DataPacket. 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: RakNetInterface.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private DataPacket getPacket(byte[] buffer) {
    int start = 0;

    if (buffer[0] == (byte) 0xfe) {
        start++;
    }
    DataPacket data = this.network.getPacket(ProtocolInfo.BATCH_PACKET);

    if (data == null) {
        return null;
    }

    data.setBuffer(buffer, start);

    return data;
}
 
Example #2
Source File: Server.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public static void broadcastPacket(Player[] players, DataPacket packet) {
    packet.encode();
    packet.isEncoded = true;

    if (packet.pid() == ProtocolInfo.BATCH_PACKET) {
        for (Player player : players) {
            player.dataPacket(packet);
        }
    } else {
        getInstance().batchPackets(players, new DataPacket[]{packet}, true);
    }

    if (packet.encapsulatedPacket != null) {
        packet.encapsulatedPacket = null;
    }
}
 
Example #3
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 #4
Source File: RakNetInterface.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
private DataPacket getPacket(byte[] buffer) {
    int start = 0;

    if (buffer[0] == (byte) 0xfe) {
        start++;
    }
    DataPacket data = this.network.getPacket(ProtocolInfo.BATCH_PACKET);

    if (data == null) {
        return null;
    }

    data.setBuffer(buffer, start);

    return data;
}
 
Example #5
Source File: Level.java    From Jupiter 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 #6
Source File: Server.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public static void broadcastPacket(Player[] players, DataPacket packet) {
    packet.encode();
    packet.isEncoded = true;

    if (packet.pid() == ProtocolInfo.BATCH_PACKET) {
        for (Player player : players) {
            player.dataPacket(packet);
        }
    } else {
        getInstance().batchPackets(players, new DataPacket[]{packet}, true);
    }

    if (packet.encapsulatedPacket != null) {
        packet.encapsulatedPacket = null;
    }
}
 
Example #7
Source File: BoneMealParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_BONEMEAL;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = 0;

    return new DataPacket[]{pk};
}
 
Example #8
Source File: DestroyBlockParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_DESTROY;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #9
Source File: EntityPainting.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket createAddEntityPacket() {
    AddPaintingPacket addPainting = new AddPaintingPacket();
    addPainting.entityUniqueId = this.getId();
    addPainting.entityRuntimeId = this.getId();
    addPainting.x = (float) this.x;
    addPainting.y = (float) this.y;
    addPainting.z = (float) this.z;
    addPainting.direction = this.getDirection().getHorizontalIndex();
    addPainting.title = this.namedTag.getString("Motive");
    return addPainting;
}
 
Example #10
Source File: PunchBlockParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_PUNCH_BLOCK;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #11
Source File: Server.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
/**
 * サーバーにいる人全員にパケットを送ります。
 * @param players プレイヤー
 * @param packet 送るパケット
 * @return void
 */
public static void broadcastPacket(Player[] players, DataPacket packet) {
    packet.encode();
    packet.isEncoded = true;

    for (Player player : players) {
        player.dataPacket(packet);
    }

    if (packet.encapsulatedPacket != null) {
        packet.encapsulatedPacket = null;
    }
}
 
Example #12
Source File: Level.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private void sendChunkFromCache(int x, int z) {
    Long index = Level.chunkHash(x, z);
    if (this.chunkSendTasks.containsKey(index)) {
        for (Player player : this.chunkSendQueue.get(index).values()) {
            if (player.isConnected() && player.usedChunks.containsKey(index)) {
                player.sendChunk(x, z, (DataPacket) this.chunkCache.get(index));
            }
        }

        this.chunkSendQueue.remove(index);
        this.chunkSendTasks.remove(index);
    }
}
 
Example #13
Source File: PunchBlockParticle.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_PUNCH_BLOCK;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #14
Source File: LevelSoundEventSound.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelSoundEventPacket pk = new LevelSoundEventPacket();
    pk.sound = this.type;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.extraData = this.extraData;
    pk.pitch = this.pitch;

    return new DataPacket[]{pk};
}
 
Example #15
Source File: BoneMealParticle.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_BONEMEAL;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = 0;

    return new DataPacket[]{pk};
}
 
Example #16
Source File: GenericParticle.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = (short) (LevelEventPacket.EVENT_ADD_PARTICLE_MASK | this.id);
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #17
Source File: Level.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void addChunkPacket(int chunkX, int chunkZ, DataPacket packet) {
    Long index = Level.chunkHash(chunkX, chunkZ);
    if (!this.chunkPackets.containsKey(index)) {
        this.chunkPackets.put(index, new ArrayList<>());
    }
    this.chunkPackets.get(index).add(packet);
}
 
Example #18
Source File: GenericParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = (short) (LevelEventPacket.EVENT_ADD_PARTICLE_MASK | this.id);
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #19
Source File: RakNetInterface.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void handleEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
        if (this.players.containsKey(identifier)) {
            DataPacket pk = null;
            try {
                if (packet.buffer.length > 0) {
                    if (packet.buffer[0] == PING_DataPacket.ID) {
                        PING_DataPacket pingPacket = new PING_DataPacket();
                        pingPacket.buffer = packet.buffer;
                        pingPacket.decode();

                        this.networkLatency.put(identifier, (int) pingPacket.pingID);
                        return;
                    }

                    pk = this.getPacket(packet.buffer);
                    if (pk != null) {
                        pk.decode();
                        this.players.get(identifier).handleDataPacket(pk);
                    }
                }
            } catch (Exception e) {
                this.server.getLogger().logException(e);
                if (Nukkit.DEBUG > 1 && pk != null) {
                    MainLogger logger = this.server.getLogger();
//                    if (logger != null) {
                    logger.debug("Packet " + pk.getClass().getName() + " 0x" + Binary.bytesToHexString(packet.buffer));
                    //logger.logException(e);
//                    }
                }

                if (this.players.containsKey(identifier)) {
                    this.handler.blockAddress(this.players.get(identifier).getAddress(), 5);
                }
            }
        }
    }
 
Example #20
Source File: MobSpawnParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket packet = new LevelEventPacket();
    packet.evid = LevelEventPacket.EVENT_PARTICLE_SPAWN;
    packet.x = (float) this.x;
    packet.y = (float) this.y;
    packet.z = (float) this.z;
    packet.data = (this.width & 0xff) + ((this.height & 0xff) << 8);

    return new DataPacket[]{packet};
}
 
Example #21
Source File: PunchBlockParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_PUNCH_BLOCK;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #22
Source File: MobSpawnParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket packet = new LevelEventPacket();
    packet.evid = LevelEventPacket.EVENT_PARTICLE_SPAWN;
    packet.x = (float) this.x;
    packet.y = (float) this.y;
    packet.z = (float) this.z;
    packet.data = (this.width & 0xff) + ((this.height & 0xff) << 8);

    return new DataPacket[]{packet};
}
 
Example #23
Source File: SpellParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_SPLASH;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #24
Source File: SynLibInterface.java    From SynapseAPI with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Integer putPacket(Player player, DataPacket packet, boolean needACK, boolean immediate) {
    RedirectPacket pk = new RedirectPacket();
    pk.uuid = player.getUniqueId();
    pk.direct = immediate;
    pk.mcpeBuffer = packet instanceof BatchPacket ? Binary.appendBytes((byte) 0xfe, ((BatchPacket) packet).payload) : packet.getBuffer();

    this.synapseInterface.putPacket(pk);
    return 0;  //这个返回值在nk中并没有被用到
}
 
Example #25
Source File: DestroyBlockParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_DESTROY;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #26
Source File: SynapseEntryPutPacketThread.java    From SynapseAPI with GNU General Public License v3.0 5 votes vote down vote up
public void addMainToThreadBroadcast(Map<Player, DataPacket[]> packets) {
    if (packets == null || packets.isEmpty()) {
        return;
    }

    this.broadcastQueue.offer(packets);
}
 
Example #27
Source File: SynapseAPI.java    From SynapseAPI with GNU General Public License v3.0 5 votes vote down vote up
public DataPacket getPacket(byte[] buffer) {
    byte pid = buffer[0] == (byte) 0xfe ? (byte) 0xff : buffer[0];

    byte start = 1;
    DataPacket data;
    data = this.getServer().getNetwork().getPacket(pid);

    if (data == null) {
        Server.getInstance().getLogger().notice("C => S Unknown packet with PID 0x" + String.format("%02x", pid));
        return null;
    }
    data.setBuffer(buffer, start);
    return data;
}
 
Example #28
Source File: RakNetInterface.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void handleEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
        if (this.players.containsKey(identifier)) {
            DataPacket pk = null;
            try {
                if (packet.buffer.length > 0) {
                    if (packet.buffer[0] == PING_DataPacket.ID) {
                        PING_DataPacket pingPacket = new PING_DataPacket();
                        pingPacket.buffer = packet.buffer;
                        pingPacket.decode();

                        this.networkLatency.put(identifier, (int) pingPacket.pingID);
                        return;
                    }

                    pk = this.getPacket(packet.buffer);
                    if (pk != null) {
                        pk.decode();
                        this.players.get(identifier).handleDataPacket(pk);
                    }
                }
            } catch (Exception e) {
                this.server.getLogger().logException(e);
                if (Nukkit.DEBUG > 1 && pk != null) {
                    MainLogger logger = this.server.getLogger();
//                    if (logger != null) {
                    logger.debug("Packet " + pk.getClass().getName() + " 0x" + Binary.bytesToHexString(packet.buffer));
                    //logger.logException(e);
//                    }
                }

                if (this.players.containsKey(identifier)) {
                    this.handler.blockAddress(this.players.get(identifier).getAddress(), 5);
                }
            }
        }
    }
 
Example #29
Source File: SpellParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_SPLASH;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;

    return new DataPacket[]{pk};
}
 
Example #30
Source File: BoneMealParticle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_BONEMEAL;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = 0;

    return new DataPacket[]{pk};
}