Java Code Examples for com.comphenix.protocol.ProtocolLibrary

The following examples show how to use com.comphenix.protocol.ProtocolLibrary. These examples are extracted from open source projects. 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 Project: SuperVanish   Source File: NightVision.java    License: Mozilla Public License 2.0 7 votes vote down vote up
private void sendAddPotionEffect(Player p, PotionEffect effect) {
    PacketContainer packet = new PacketContainer(ENTITY_EFFECT);
    //noinspection deprecation
    int effectID = effect.getType().getId();
    int amplifier = effect.getAmplifier();
    int duration = effect.getDuration();
    int entityID = p.getEntityId();
    packet.getIntegers().write(0, entityID);
    packet.getBytes().write(0, (byte) effectID);
    packet.getBytes().write(1, (byte) amplifier);
    packet.getIntegers().write(1, duration);
    // hide particles in 1.9
    packet.getBytes().write(2, (byte) 0);
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet);
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Cannot send packet", e);
    }
}
 
Example 2
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 6 votes vote down vote up
@Override
public void sendNamedEntitySpawnPacket(Player playerToSpawn, Player seer) {
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
    packet.getIntegers().write(0, playerToSpawn.getEntityId());
    packet.getUUIDs().write(0, playerToSpawn.getUniqueId());
    if (ReflectUtil.isVersionHigherThan(1, 8, 8)) {
        packet.getDoubles().write(0, playerToSpawn.getLocation().getX());
        packet.getDoubles().write(1, playerToSpawn.getLocation().getY());
        packet.getDoubles().write(2, playerToSpawn.getLocation().getZ());
    } else {
        packet.getIntegers().write(0, (int) Math.floor(playerToSpawn.getLocation().getX() * 32D));
        packet.getIntegers().write(1, (int) Math.floor(playerToSpawn.getLocation().getY() * 32D));
        packet.getIntegers().write(2, (int) Math.floor(playerToSpawn.getLocation().getZ() * 32D));
    }
    packet.getBytes().write(0, (byte) (playerToSpawn.getLocation().getYaw() * 256F / 360F));
    packet.getBytes().write(1, (byte) (playerToSpawn.getLocation().getPitch() * 256F / 360F));
    packet.getDataWatcherModifier().write(0, WrappedDataWatcher.getEntityWatcher(playerToSpawn));
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(seer, packet);
    } catch (InvocationTargetException e) {
        logMessage(Level.SEVERE, "Failed to send named entity spawn packet!", e);
    }
}
 
Example 3
@Override
public void onDisable()
{
    // Plugin is already disabled -> VerboseSender is not allowed to register a task
    VerboseSender.getInstance().setAllowedToRegisterTasks(false);

    // Remove all the Listeners, PacketListeners
    ProtocolLibrary.getProtocolManager().removePacketListeners(this);
    HandlerList.unregisterAll(AACAdditionPro.getInstance());

    DataUpdaterEvents.INSTANCE.unregister();

    VerboseSender.getInstance().sendVerboseMessage("AACAdditionPro disabled.", true, false);
    VerboseSender.getInstance().sendVerboseMessage(" ", true, false);

    // Task scheduling
    loaded = false;
}
 
Example 4
Source Project: AACAdditionPro   Source File: PlayerHider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void modifyInformation(final Player observer, final Entity entity)
{
    validate(observer, entity);

    if (setModifyInformation(observer, entity.getEntityId(), false)) {
        //Create new packet which destroys the entity
        final PacketContainer destroyEntity = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY);
        destroyEntity.getIntegerArrays().write(0, new int[]{entity.getEntityId()});

        // Make the entity disappear
        try {
            ProtocolLibrary.getProtocolManager().sendServerPacket(observer, destroyEntity);
        } catch (final InvocationTargetException e) {
            throw new RuntimeException("Cannot send server packet.", e);
        }
    }
}
 
Example 5
Source Project: ChatItem   Source File: ChatItem.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static void reload(CommandSender sender) {
    ChatItem obj = getInstance();
    obj.pm = ProtocolLibrary.getProtocolManager();
    obj.saveDefaultConfig();
    obj.reloadConfig();
    obj.storage = new Storage(obj.getConfig());
    obj.packetListener.setStorage(obj.storage);
    obj.packetValidator.setStorage(obj.storage);
    obj.chatEventListener.setStorage(obj.storage);
    obj.filter.setStorage(obj.storage);
    APIImplementation api = (APIImplementation) Bukkit.getServicesManager().getRegistration(ChatItemAPI.class).getProvider();
    api.setStorage(obj.storage);
    api.updateLogger();
    if (!obj.storage.RELOAD_MESSAGE.isEmpty())
        sender.sendMessage(obj.storage.RELOAD_MESSAGE);
}
 
Example 6
Source Project: SuperVanish   Source File: NightVision.java    License: Mozilla Public License 2.0 6 votes vote down vote up
private void sendRemovePotionEffect(Player p, PotionEffectType type) {
    PacketContainer packet = new PacketContainer(REMOVE_ENTITY_EFFECT);
    //noinspection deprecation
    final int effectID = type.getId();
    final int entityID = p.getEntityId();
    // 1.7 and below
    if (!plugin.getVersionUtil().isOneDotXOrHigher(8)) {
        packet.getIntegers().write(0, entityID);
        packet.getBytes().write(0, (byte) effectID);
    } else if (plugin.getVersionUtil().isOneDotX(8)) {
        // 1.8
        packet.getIntegers().write(0, entityID);
        packet.getIntegers().write(1, effectID);
    } else {
        // 1.9 and higher
        packet.getEffectTypes().write(0, type);
        packet.getIntegers().write(0, entityID);
    }
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet);
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Cannot send packet", e);
    }
}
 
Example 7
Source Project: SuperVanish   Source File: VanishIndication.java    License: Mozilla Public License 2.0 6 votes vote down vote up
private void sendPlayerInfoChangeGameModePacket(Player p, Player change, boolean spectator) {
    PacketContainer packet = new PacketContainer(PLAYER_INFO);
    packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE);
    List<PlayerInfoData> data = new ArrayList<>();
    int ping = ThreadLocalRandom.current().nextInt(20) + 15;
    data.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(change), ping,
            spectator ? EnumWrappers.NativeGameMode.SPECTATOR
                    : EnumWrappers.NativeGameMode.fromBukkit(change.getGameMode()),
            WrappedChatComponent.fromText(change.getPlayerListName())));
    packet.getPlayerInfoDataLists().write(0, data);
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet);
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Cannot send packet", e);
    }
}
 
Example 8
private void sendNbt(Vector pos, NbtCompound compound) {
    Player player = this.<Player>getPlayer().parent;
    ProtocolManager manager = ProtocolLibrary.getProtocolManager();

    PacketContainer blockNbt = new PacketContainer(PacketType.Play.Server.TILE_ENTITY_DATA);
    blockNbt.getBlockPositionModifier().write(0, new BlockPosition(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()));
    blockNbt.getIntegers().write(0, 7);
    blockNbt.getNbtModifier().write(0, compound);


    try {
        manager.sendServerPacket(player, blockNbt);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}
 
Example 9
Source Project: FastLogin   Source File: ProtocolLibLoginSource.java    License: MIT License 6 votes vote down vote up
@Override
public void setOnlineMode() throws Exception {
    verifyToken = EncryptionUtil.generateVerifyToken(random);

    /*
     * Packet Information: https://wiki.vg/Protocol#Encryption_Request
     *
     * ServerID="" (String) key=public server key verifyToken=random 4 byte array
     */
    PacketContainer newPacket = new PacketContainer(ENCRYPTION_BEGIN);

    newPacket.getStrings().write(0, serverId);
    newPacket.getSpecificModifier(PublicKey.class).write(0, publicKey);

    newPacket.getByteArrays().write(0, verifyToken);

    //serverId is a empty string
    ProtocolLibrary.getProtocolManager().sendServerPacket(player, newPacket);
}
 
Example 10
Source Project: FastLogin   Source File: ProtocolLibLoginSource.java    License: MIT License 6 votes vote down vote up
@Override
public void kick(String message) throws InvocationTargetException {
    ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();

    PacketContainer kickPacket = new PacketContainer(DISCONNECT);
    kickPacket.getChatComponents().write(0, WrappedChatComponent.fromText(message));

    try {
        //send kick packet at login state
        //the normal event.getPlayer.kickPlayer(String) method does only work at play state
        protocolManager.sendServerPacket(player, kickPacket);
    } finally {
        //tell the server that we want to close the connection
        player.kickPlayer("Disconnect");
    }
}
 
Example 11
/**
 * Sends a blanked out packet to the given player in order to hide the inventory.
 *
 * @param player the player to send the blank inventory packet to
 */
public void sendBlankInventoryPacket(Player player) {
    ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
    PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS);
    inventoryPacket.getIntegers().write(0, PLAYER_INVENTORY);
    int inventorySize = CRAFTING_SIZE + ARMOR_SIZE + MAIN_SIZE + HOTBAR_SIZE;

    ItemStack[] blankInventory = new ItemStack[inventorySize];
    Arrays.fill(blankInventory, new ItemStack(Material.AIR));

    //old minecraft versions
    StructureModifier<ItemStack[]> itemArrayModifier = inventoryPacket.getItemArrayModifier();
    if (itemArrayModifier.size() > 0) {
        itemArrayModifier.write(0, blankInventory);
    } else {
        //minecraft versions above 1.11
        StructureModifier<List<ItemStack>> itemListModifier = inventoryPacket.getItemListModifier();
        itemListModifier.write(0, Arrays.asList(blankInventory));
    }

    try {
        protocolManager.sendServerPacket(player, inventoryPacket, false);
    } catch (InvocationTargetException invocationExc) {
        logger.logException("Error during sending blank inventory", invocationExc);
    }
}
 
Example 12
Source Project: Hawk   Source File: ServerUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static int getProtocolVersion(Player p) {
    if(Hawk.USING_PLIB)
        return ProtocolLibrary.getProtocolManager().getProtocolVersion(p);
    if(Hawk.getServerVersion() == 7) {
        net.minecraft.server.v1_7_R4.PlayerConnection pConnection = ((org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer) p).getHandle().playerConnection;
        if(pConnection == null)
            return 5;
        return pConnection.networkManager.getVersion();
    }
    return 47;
}
 
Example 13
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void sendTabListRemovePacket(Player playerToRemove, Player seer) {
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO);
    packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.REMOVE_PLAYER);
    PlayerInfoData playerInfoData = new PlayerInfoData(WrappedGameProfile.fromPlayer(playerToRemove), 0, EnumWrappers.NativeGameMode.NOT_SET, null);
    packet.getPlayerInfoDataLists().write(0, Collections.singletonList(playerInfoData));
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(seer, packet);
    } catch (InvocationTargetException e) {
        logMessage(Level.SEVERE, "Failed to send tab list remove packet!", e);
    }
}
 
Example 14
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void sendTabListAddPacket(Player playerToAdd, GameProfileWrapper newProfile, Player seer) {
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO);
    int ping = (int) ReflectUtil.getFieldValue(ReflectUtil.invokeMethod(playerToAdd, GET_HANDLE).getOrThrow(), PING).getOrThrow();
    packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.ADD_PLAYER);
    PlayerInfoData playerInfoData = new PlayerInfoData(getProtocolLibProfileWrapper(newProfile), ping, EnumWrappers.NativeGameMode.fromBukkit(playerToAdd.getGameMode()), WrappedChatComponent.fromText(playerToAdd.getPlayerListName()));
    packet.getPlayerInfoDataLists().write(0, Collections.singletonList(playerInfoData));
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(seer, packet);
    } catch (InvocationTargetException e) {
        logMessage(Level.SEVERE, "Failed to send tab list add packet!", e);
    }
}
 
Example 15
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void sendEntityDestroyPacket(Player playerToDestroy, Player seer) {
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_DESTROY);
    packet.getIntegerArrays().write(0, new int[] {playerToDestroy.getEntityId()});
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(seer, packet);
    } catch (InvocationTargetException e) {
        logMessage(Level.SEVERE, "Failed to send entity destroy packet!", e);
    }
}
 
Example 16
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 5 votes vote down vote up
private void doEquipmentPacketSend(int entityID, EnumWrappers.ItemSlot slot, ItemStack itemStack, Player recipient) {
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_EQUIPMENT);
    packet.getIntegers().write(0, entityID);
    packet.getItemSlots().write(0, slot);
    packet.getItemModifier().write(0, itemStack);
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(recipient, packet);
    } catch (InvocationTargetException e) {
        logMessage(Level.SEVERE, "Failed to send equipment packet!", e);
    }
}
 
Example 17
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void sendScoreboardRemovePacket(String playerToRemove, Player seer, String team) {
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(seer, getScoreboardPacket(team, playerToRemove, LEAVE_SCOREBOARD_TEAM_MODE));
    } catch (InvocationTargetException e) {
        logMessage(Level.SEVERE, "Failed to send scoreboard remove packet!", e);
    }
}
 
Example 18
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void sendScoreboardAddPacket(String playerToAdd, Player seer, String team) {
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(seer, getScoreboardPacket(team, playerToAdd, JOIN_SCOREBOARD_TEAM_MODE));
    } catch (InvocationTargetException e) {
        logMessage(Level.SEVERE, "Failed to send scoreboard add packet!", e);
    }
}
 
Example 19
Source Project: NameTagChanger   Source File: ProtocolLibPacketHandler.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private PacketContainer getScoreboardPacket(String team, String entryToAdd, int mode) {
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);
    packet.getStrings().write(0, team);
    packet.getIntegers().write(1, mode);
    ((Collection<String>) packet.getSpecificModifier(Collection.class).read(0)).add(entryToAdd);
    return packet;
}
 
Example 20
Source Project: UhcCore   Source File: ProtocolUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void register(){
    if (protocolUtils != null){
        ProtocolLibrary.getProtocolManager().removePacketListeners(UhcCore.getPlugin());
        protocolUtils = null;
    }

    new ProtocolUtils();
}
 
Example 21
Source Project: UhcCore   Source File: ProtocolUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
/***
 * This method can be used to change the tab header and footer.
 * @param player The player to change the header / footer for
 * @param header The new header
 * @param footer The new footer
 */
public static void setPlayerHeaderFooter(Player player, String header, String footer){
    PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_LIST_HEADER_FOOTER);
    packet.getChatComponents().write(0, WrappedChatComponent.fromText(header));
    packet.getChatComponents().write(1, WrappedChatComponent.fromText(footer));
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
    }catch (InvocationTargetException ex){
        ex.printStackTrace();
    }
}
 
Example 22
Source Project: StackMob-3   Source File: ProtocolLibHook.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void enable(){
    if(getStackMob().getCustomConfig().getBoolean("tag.show-player-nearby.enabled")){
        protocolManager = ProtocolLibrary.getProtocolManager();
        getHookManager().registerHook(PluginCompat.PROCOTOLLIB, this);
    }
}
 
Example 23
Source Project: VoxelGamesLibv2   Source File: AbstractPacket.java    License: MIT License 5 votes vote down vote up
/**
 * Send the current packet to the given receiver.
 *
 * @param receiver - the receiver.
 * @throws RuntimeException If the packet cannot be sent.
 */
public void sendPacket(Player receiver) {
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(receiver,
                getHandle());
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Cannot send packet.", e);
    }
}
 
Example 24
Source Project: VoxelGamesLibv2   Source File: AbstractPacket.java    License: MIT License 5 votes vote down vote up
/**
 * Simulate receiving the current packet from the given sender.
 *
 * @param sender - the sender.
 * @throws RuntimeException if the packet cannot be received.
 */
public void receivePacket(Player sender) {
    try {
        ProtocolLibrary.getProtocolManager().recieveClientPacket(sender,
                getHandle());
    } catch (Exception e) {
        throw new RuntimeException("Cannot recieve packet.", e);
    }
}
 
Example 25
private DataUpdaterEvents()
{
    this.keepAliveDataUpdater = new KeepAliveDataUpdater();
    this.velocityChangeDataUpdater = new VelocityChangeDataUpdater();
    ProtocolLibrary.getProtocolManager().addPacketListener(this.keepAliveDataUpdater);
    ProtocolLibrary.getProtocolManager().addPacketListener(this.velocityChangeDataUpdater);
}
 
Example 26
/**
 * Allow the observer to see an entity that was previously hidden.
 *
 * @param observer - the observer.
 * @param entity   - the entity to show.
 */
public final void unModifyInformation(final Player observer, final Entity entity)
{
    validate(observer, entity);
    final boolean hiddenBefore = !setModifyInformation(observer, entity.getEntityId(), true);

    // Resend packets
    if (ProtocolLibrary.getProtocolManager() != null && hiddenBefore) {
        ProtocolLibrary.getProtocolManager().updateEntity(entity, Collections.singletonList(observer));
    }
}
 
Example 27
/**
 * Send the current packet to the given receiver.
 *
 * @param receiver - the receiver.
 *
 * @throws RuntimeException If the packet cannot be sent.
 */
public void sendPacket(Player receiver)
{
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, getHandle());
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Cannot send packet.", e);
    }
}
 
Example 28
/**
 * Simulate receiving the current packet from the given sender.
 *
 * @param sender - the sender.
 *
 * @throws RuntimeException if the packet cannot be received.
 */
public void receivePacket(Player sender)
{
    try {
        ProtocolLibrary.getProtocolManager().recieveClientPacket(sender, getHandle());
    } catch (Exception e) {
        throw new RuntimeException("Cannot receive packet.", e);
    }
}
 
Example 29
Source Project: tabbed   Source File: TitledTabList.java    License: MIT License 5 votes vote down vote up
private void updateHeaderFooter() {
    PacketContainer packet = new PacketContainer(Server.PLAYER_LIST_HEADER_FOOTER);
    packet.getChatComponents().write(0, WrappedChatComponent.fromText(this.header == null ? "" : this.header));
    packet.getChatComponents().write(1, WrappedChatComponent.fromText(this.footer == null ? "" : this.footer));
    try {
        ProtocolLibrary.getProtocolManager().sendServerPacket(this.player, packet);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}
 
Example 30
Source Project: tabbed   Source File: Packets.java    License: MIT License 5 votes vote down vote up
/**
 * Creates a PLAYER_INFO packet from the params.
 * @param action
 * @param data
 * @return
 */

public static PacketContainer getPacket(PlayerInfoAction action, List<PlayerInfoData> data) {
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(Server.PLAYER_INFO);
    packet.getPlayerInfoAction().write(0, action);
    packet.getPlayerInfoDataLists().write(0, data);
    return packet;
}