com.comphenix.protocol.events.PacketAdapter Java Examples

The following examples show how to use com.comphenix.protocol.events.PacketAdapter. 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: EntityHider.java    From BetonQuest with GNU General Public License v3.0 10 votes vote down vote up
/**
 * Construct the packet listener that will be used to intercept every
 * entity-related packet.
 *
 * @param plugin - the parent plugin.
 * @return The packet listener.
 */
private PacketAdapter constructProtocol(Plugin plugin) {
    return new PacketAdapter(plugin, ENTITY_PACKETS) {

        @Override
        public void onPacketSending(PacketEvent event) {
            int index = event.getPacketType() == COMBAT_EVENT ? 1 : 0;

            Integer entityID = event.getPacket().getIntegers().readSafely(index);
            if (entityID != null) {
                if (!isVisible(event.getPlayer(), entityID)) {
                    event.setCancelled(true);
                }
            }
        }
    };
}
 
Example #2
Source File: SkullPlaceHolders.java    From VoxelGamesLibv2 with MIT License 5 votes vote down vote up
public void init() {
    Bukkit.getPluginManager().registerEvents(this, voxelGamesLib);

    registerPlaceholders();

    // listener
    protocolManager.addPacketListener(new PacketAdapter(voxelGamesLib, PacketType.Play.Server.TILE_ENTITY_DATA) {
        @Override
        public void onPacketSending(PacketEvent event) {
            WrapperPlayServerTileEntityData packet = new WrapperPlayServerTileEntityData(event.getPacket());
            event.setPacket(modifySkull(packet, event.getPlayer()));
        }
    });

    // search for already loaded skulls
    Bukkit.getWorlds().stream()
            .flatMap(w -> Arrays.stream(w.getLoadedChunks()))
            .flatMap(s -> Arrays.stream(s.getTileEntities()))
            .filter(s -> s instanceof Skull)
            .map(s -> (Skull) s)
            .forEach(s -> lastSeenSkulls.put(s.getLocation(), s));

    // update task
    new BukkitRunnable() {

        @Override
        public void run() {
            lastSeenSkulls.forEach((loc, skull) -> skull.update());
        }
    }.runTaskTimer(voxelGamesLib, 20, 20);
}
 
Example #3
Source File: PacketInterceptor.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
public PacketInterceptor(Conversation conv, String playerID) {
    this.conv = conv;
    this.player = PlayerConverter.getPlayer(playerID);

    // Intercept Packets
    packetAdapter = new PacketAdapter(BetonQuest.getInstance(), ListenerPriority.HIGHEST,
            PacketType.Play.Server.CHAT

    ) {
        @Override
        public void onPacketSending(PacketEvent event) {
            if (event.getPlayer() != player) {
                return;
            }

            if (event.getPacketType().equals(PacketType.Play.Server.CHAT)) {
                PacketContainer packet = event.getPacket();
                BaseComponent[] bc = (BaseComponent[]) packet.getModifier().read(1);
                if (bc != null && bc.length > 0 && ((TextComponent) bc[0]).getText().contains("_bq_")) {
                    packet.getModifier().write(1, Arrays.copyOfRange(bc, 1, bc.length));
                    event.setPacket(packet);
                    return;
                }

                // Else save message to replay later
                WrapperPlayServerChat chat = new WrapperPlayServerChat(event.getPacket());
                event.setCancelled(true);
                messages.add(chat);
            }
        }
    };

    ProtocolLibrary.getProtocolManager().addPacketListener(packetAdapter);
}
 
Example #4
Source File: ProtocolUtils.java    From UhcCore with GNU General Public License v3.0 4 votes vote down vote up
private ProtocolUtils(){
    protocolUtils = this;

    ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(UhcCore.getPlugin(), PacketType.Play.Server.PLAYER_INFO) {

        @Override
        public void onPacketSending(PacketEvent event) {
            if (event.getPacket().getPlayerInfoAction().read(0) != EnumWrappers.PlayerInfoAction.ADD_PLAYER){
                return;
            }

            List<PlayerInfoData> newPlayerInfoDataList = new ArrayList<>();
            List<PlayerInfoData> playerInfoDataList = event.getPacket().getPlayerInfoDataLists().read(0);
            PlayersManager pm = GameManager.getGameManager().getPlayersManager();

            for (PlayerInfoData playerInfoData : playerInfoDataList) {
                if (
                        playerInfoData == null ||
                        playerInfoData.getProfile() == null ||
                        Bukkit.getPlayer(playerInfoData.getProfile().getUUID()) == null
                ){ // Unknown player
                    newPlayerInfoDataList.add(playerInfoData);
                    continue;
                }

                WrappedGameProfile profile = playerInfoData.getProfile();
                UhcPlayer uhcPlayer;

                try {
                    uhcPlayer = pm.getUhcPlayer(profile.getUUID());
                }catch (UhcPlayerDoesntExistException ex){ // UhcPlayer does not exist
                    newPlayerInfoDataList.add(playerInfoData);
                    continue;
                }

                // No display-name so don't change player data.
                if (!uhcPlayer.hasNickName()){
                    newPlayerInfoDataList.add(playerInfoData);
                    continue;
                }

                profile = profile.withName(uhcPlayer.getName());

                PlayerInfoData newPlayerInfoData = new PlayerInfoData(profile, playerInfoData.getPing(), playerInfoData.getGameMode(), playerInfoData.getDisplayName());
                newPlayerInfoDataList.add(newPlayerInfoData);
            }
            event.getPacket().getPlayerInfoDataLists().write(0, newPlayerInfoDataList);
        }

    });
}
 
Example #5
Source File: SignPlaceholders.java    From VoxelGamesLibv2 with MIT License 4 votes vote down vote up
public void init() {
    registerPlaceholders();

    // search for already loaded signs
    Bukkit.getWorlds().stream()
            .flatMap(w -> Arrays.stream(w.getLoadedChunks()))
            .flatMap(s -> Arrays.stream(s.getTileEntities()))
            .filter(s -> s instanceof Sign)
            .map(s -> (Sign) s)
            .forEach(s -> lastSeenSigns.put(s.getLocation(), s));

    // modify update packets
    protocolManager.addPacketListener(new PacketAdapter(voxelGamesLib, PacketType.Play.Server.TILE_ENTITY_DATA) {

        @Override
        public void onPacketSending(@Nonnull PacketEvent event) {
            int action = event.getPacket().getIntegers().read(0);
            // 9 = set sign text action
            if (action != 9) {
                return;
            }

            NbtCompound data = (NbtCompound) event.getPacket().getNbtModifier().read(0);
            // read data
            Component[] lines = new Component[4];
            String[] rawLines = new String[4];
            for (int i = 0; i < lines.length; i++) {
                lines[i] = ComponentSerializers.JSON.deserialize(data.getString("Text" + (i + 1)));
                rawLines[i] = ChatUtil.toPlainText(lines[i]);
            }

            // sometimes its a double, sometimes its an int...
            double x;
            double y;
            double z;
            try {
                x = data.getDouble("x");
                y = data.getDouble("y");
                z = data.getDouble("z");
            } catch (ClassCastException ex) {
                x = data.getInteger("x");
                y = data.getInteger("y");
                z = data.getInteger("z");
            }

            Location loc = new Location(event.getPlayer().getWorld(), x, y, z);
            if (event.getPlayer().getLocation().distanceSquared(loc) > 200 * 200) {
                return;
            }

            if (!(rawLines[0].equals("") && rawLines[1].equals("") && rawLines[2].equals("") && rawLines[3].equals(""))) {
                Block b = loc.getBlock();
                if (!(b.getState() instanceof Sign)) {
                    return;
                }
                Sign sign = (Sign) b.getState();
                lastSeenSigns.put(loc, sign);
            }

            Optional<User> user = userHandler.getUser(event.getPlayer().getUniqueId());
            if (!user.isPresent()) {
                return;
            }

            // call sign placeholders
            modifySign(user.get(), loc, rawLines, lines);

            // modify packet
            for (int i = 0; i < lines.length; i++) {
                data.put("Text" + (i + 1), ComponentSerializers.JSON.serialize(lines[i]));
            }
        }
    });

    // update task
    new BukkitRunnable() {

        @Override
        public void run() {
            lastSeenSigns.forEach((loc, sign) -> sign.update());
        }
    }.runTaskTimer(voxelGamesLib, 20, 20);
}
 
Example #6
Source File: ForceField.java    From CombatLogX with GNU General Public License v3.0 4 votes vote down vote up
public void registerProtocol() {
    ProtocolManager manager = ProtocolLibrary.getProtocolManager();
    PacketAdapter adapter = new ForceFieldAdapter(this);
    manager.addPacketListener(adapter);
}
 
Example #7
Source File: VanishIndication.java    From SuperVanish with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void onEnable() {
    ProtocolLibrary.getProtocolManager().addPacketListener(
            new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.PLAYER_INFO) {
                @Override
                public void onPacketSending(PacketEvent event) {
                    // multiple events share same packet object
                    event.setPacket(event.getPacket().shallowClone());
                    List<PlayerInfoData> infoDataList = new ArrayList<>(
                            event.getPacket().getPlayerInfoDataLists().read(0));
                    Player receiver = event.getPlayer();
                    for (PlayerInfoData infoData : ImmutableList.copyOf(infoDataList)) {
                        try {
                            if (!VanishIndication.this.plugin.getVisibilityChanger().getHider()
                                    .isHidden(infoData.getProfile().getUUID(), receiver)
                                    && VanishIndication.this.plugin.getVanishStateMgr()
                                    .isVanished(infoData.getProfile().getUUID())) {
                                if (!receiver.getUniqueId().equals(infoData.getProfile().getUUID()))
                                    if (infoData.getGameMode() != EnumWrappers.NativeGameMode.SPECTATOR) {
                                        int latency;
                                        try {
                                            latency = infoData.getLatency();
                                        } catch (NoSuchMethodError e) {
                                            latency = 21;
                                        }
                                        if (event.getPacket().getPlayerInfoAction().read(0)
                                                != EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE) {
                                            continue;
                                        }
                                        PlayerInfoData newData = new PlayerInfoData(infoData.getProfile(),
                                                latency, EnumWrappers.NativeGameMode.SPECTATOR,
                                                infoData.getDisplayName());
                                        infoDataList.remove(infoData);
                                        infoDataList.add(newData);
                                    }
                            }
                        } catch (UnsupportedOperationException ignored) {
                            // Ignore temporary players
                        }
                    }
                    event.getPacket().getPlayerInfoDataLists().write(0, infoDataList);
                }
            });
}
 
Example #8
Source File: ProtocolLibHandler.java    From ServerListPlus with GNU General Public License v3.0 4 votes vote down vote up
public StatusPacketListener() {
    super(PacketAdapter.params(bukkit, PacketType.Status.Server.OUT_SERVER_INFO,
            PacketType.Handshake.Client.SET_PROTOCOL).optionAsync());
}