Search in sources :

Example 11 with PlayerInfoData

use of com.comphenix.protocol.wrappers.PlayerInfoData in project LibsDisguises by libraryaddict.

the class Disguise method startDisguise.

public boolean startDisguise(CommandSender commandSender) {
    if (isDisguiseInUse() || isDisguiseExpired()) {
        return false;
    }
    if (getEntity() == null) {
        throw new IllegalStateException("No entity is assigned to this disguise!");
    }
    // Fix for old LD updates to new LD where gson hates missing fields
    if (multiName == null) {
        multiName = new String[0];
    }
    if (LibsPremium.getUserID().equals("123" + "45") || !LibsMsg.OWNED_BY.getRaw().contains("'")) {
        ((TargetedDisguise) this).setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
        if (getEntity() instanceof Player) {
            ((TargetedDisguise) this).addPlayer((Player) getEntity());
        }
        for (Entity ent : getEntity().getNearbyEntities(4, 4, 4)) {
            if (!(ent instanceof Player)) {
                continue;
            }
            ((TargetedDisguise) this).addPlayer((Player) ent);
        }
    }
    DisguiseUtilities.setPluginsUsed();
    // Fire a disguise event
    DisguiseEvent event = new DisguiseEvent(commandSender, entity, this);
    Bukkit.getPluginManager().callEvent(event);
    // Just return.
    if (event.isCancelled()) {
        return false;
    }
    disguiseInUse = true;
    if (!DisguiseUtilities.isInvalidFile()) {
        createRunnable();
    }
    if (this instanceof PlayerDisguise) {
        PlayerDisguise disguise = (PlayerDisguise) this;
        if (disguise.isDisplayedInTab()) {
            PacketContainer addTab = DisguiseUtilities.getTabPacket(disguise, PlayerInfoAction.ADD_PLAYER);
            try {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    if (!((TargetedDisguise) this).canSee(player)) {
                        continue;
                    }
                    ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab);
                }
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    // Stick the disguise in the disguises bin
    DisguiseUtilities.addDisguise(entity.getEntityId(), (TargetedDisguise) this);
    if (!isPlayerDisguise()) {
        DisguiseUtilities.setGlowColor(this, getWatcher().getGlowColor());
    }
    if (isSelfDisguiseVisible() && getEntity() instanceof Player) {
        DisguiseUtilities.removeSelfDisguise(this);
    }
    // Resend the disguised entity's packet
    DisguiseUtilities.refreshTrackers((TargetedDisguise) this);
    // If he is a player, then self disguise himself
    Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), new Runnable() {

        @Override
        public void run() {
            DisguiseUtilities.setupFakeDisguise(Disguise.this);
        }
    }, 2);
    if (isHidePlayer() && getEntity() instanceof Player) {
        PacketContainer removeTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
        removeTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER);
        removeTab.getPlayerInfoDataLists().write(0, Collections.singletonList(new PlayerInfoData(ReflectionManager.getGameProfile((Player) getEntity()), 0, NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(""))));
        try {
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (!((TargetedDisguise) this).canSee(player)) {
                    continue;
                }
                ProtocolLibrary.getProtocolManager().sendServerPacket(player, removeTab);
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    if (!entity.isOp() && new Random().nextBoolean() && (!LibsMsg.OWNED_BY.getRaw().contains("'") || "%%__USER__%%".equals("12345"))) {
        setExpires(DisguiseConfig.isDynamicExpiry() ? 240 * 20 : System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(330));
    }
    if (isDynamicName()) {
        String name;
        if (getEntity() instanceof Player) {
            name = DisguiseUtilities.translateAlternateColorCodes(DisguiseUtilities.getDisplayName(getEntity()));
        } else {
            name = getEntity().getCustomName();
        }
        if (name == null) {
            name = "";
        }
        getWatcher().setCustomName(name);
    }
    makeBossBar();
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) PacketContainer(com.comphenix.protocol.events.PacketContainer) DisguiseEvent(me.libraryaddict.disguise.events.DisguiseEvent) PlayerInfoData(com.comphenix.protocol.wrappers.PlayerInfoData) InvocationTargetException(java.lang.reflect.InvocationTargetException) Random(java.util.Random) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable)

Example 12 with PlayerInfoData

use of com.comphenix.protocol.wrappers.PlayerInfoData in project TabList by montlikadani.

the class ProtocolPackets method onPacketSending.

@Override
public void onPacketSending(PacketEvent event) {
    try {
        PacketContainer packet = event.getPacket().shallowClone();
        EnumWrappers.PlayerInfoAction action = packet.getPlayerInfoAction().read(0);
        if (action == EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE || action == EnumWrappers.PlayerInfoAction.ADD_PLAYER) {
            java.util.List<PlayerInfoData> dataList = packet.getPlayerInfoDataLists().read(0);
            java.util.ListIterator<PlayerInfoData> dataListIt = dataList.listIterator();
            java.util.UUID playerId = event.getPlayer().getUniqueId();
            while (dataListIt.hasNext()) {
                PlayerInfoData infoData = dataListIt.next();
                if (infoData.getGameMode() == EnumWrappers.NativeGameMode.SPECTATOR && !infoData.getProfile().getUUID().equals(playerId)) {
                    dataListIt.set(new PlayerInfoData(infoData.getProfile(), infoData.getLatency(), EnumWrappers.NativeGameMode.SURVIVAL, infoData.getDisplayName()));
                }
            }
            packet.getPlayerInfoDataLists().write(0, dataList);
            event.setPacket(packet);
        }
    } catch (com.comphenix.protocol.reflect.FieldAccessException ex) {
        ex.printStackTrace();
    }
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) PlayerInfoData(com.comphenix.protocol.wrappers.PlayerInfoData) EnumWrappers(com.comphenix.protocol.wrappers.EnumWrappers)

Example 13 with PlayerInfoData

use of com.comphenix.protocol.wrappers.PlayerInfoData in project LibsDisguises by libraryaddict.

the class Disguise method removeDisguise.

/**
 * Removes the disguise and undisguises the entity if it's using this disguise.
 *
 * @param disguiseBeingReplaced If the entity's disguise is being replaced with another
 * @return
 */
public boolean removeDisguise(CommandSender sender, boolean disguiseBeingReplaced) {
    if (!isDisguiseInUse()) {
        return false;
    }
    UndisguiseEvent event = new UndisguiseEvent(sender, entity, this, disguiseBeingReplaced);
    Bukkit.getPluginManager().callEvent(event);
    // Can only continue a disguise that's valid
    if (event.isCancelled() && getEntity() != null && Bukkit.getWorlds().contains(getEntity().getWorld()) && (!(getEntity() instanceof Player) || ((Player) getEntity()).isOnline())) {
        return false;
    }
    disguiseInUse = false;
    if (runnable != null) {
        runnable.cancel();
        runnable = null;
    }
    // If this disguise hasn't a entity set
    if (getEntity() == null) {
        // Loop through the disguises because it could be used with a unknown entity id.
        HashMap<Integer, HashSet<TargetedDisguise>> future = DisguiseUtilities.getFutureDisguises();
        DisguiseUtilities.getFutureDisguises().keySet().removeIf(id -> future.get(id).remove(this) && future.get(id).isEmpty());
        return true;
    }
    if (this instanceof PlayerDisguise) {
        PlayerDisguise disguise = (PlayerDisguise) this;
        if (disguise.isDisplayedInTab()) {
            PacketContainer deleteTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
            deleteTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER);
            deleteTab.getPlayerInfoDataLists().write(0, Collections.singletonList(new PlayerInfoData(disguise.getGameProfile(), 0, NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(disguise.getProfileName()))));
            try {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    if (!((TargetedDisguise) this).canSee(player)) {
                        continue;
                    }
                    ProtocolLibrary.getProtocolManager().sendServerPacket(player, deleteTab);
                }
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    if (getInternalArmorstandIds().length > 0) {
        try {
            PacketContainer packet = DisguiseUtilities.getDestroyPacket(getInternalArmorstandIds());
            for (Player player : getEntity().getWorld().getPlayers()) {
                ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    if (!isPlayerDisguise()) {
        DisguiseUtilities.setGlowColor(this, null);
    }
    // Remove the disguise from the current disguises.
    if (DisguiseUtilities.removeDisguise((TargetedDisguise) this) && !disguiseBeingReplaced) {
        if (getEntity() instanceof Player) {
            DisguiseUtilities.removeSelfDisguise(this);
        }
        // Better refresh the entity to undisguise it
        if (getEntity().isValid()) {
            DisguiseUtilities.refreshTrackers((TargetedDisguise) this);
        } else {
            DisguiseUtilities.destroyEntity((TargetedDisguise) this);
        }
    }
    if (isHidePlayer() && getEntity() instanceof Player && ((Player) getEntity()).isOnline()) {
        PlayerInfoData playerInfo = new PlayerInfoData(ReflectionManager.getGameProfile((Player) getEntity()), 0, NativeGameMode.fromBukkit(((Player) getEntity()).getGameMode()), WrappedChatComponent.fromText(DisguiseUtilities.getPlayerListName((Player) getEntity())));
        PacketContainer addTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
        addTab.getPlayerInfoAction().write(0, PlayerInfoAction.ADD_PLAYER);
        addTab.getPlayerInfoDataLists().write(0, Collections.singletonList(playerInfo));
        try {
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (!((TargetedDisguise) this).canSee(player)) {
                    continue;
                }
                ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab);
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    for (String meta : new String[] { "LastDisguise", "LD-LastAttacked", "forge_mods", "LibsRabbitHop", "ld_loggedin" }) {
        getEntity().removeMetadata(meta, LibsDisguises.getInstance());
    }
    if (DisguiseConfig.getPvPTimer() > 0 && getEntity() instanceof Player) {
        getEntity().setMetadata("LastDisguise", new FixedMetadataValue(LibsDisguises.getInstance(), System.currentTimeMillis()));
    }
    if (NmsVersion.v1_13.isSupported()) {
        removeBossBar();
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) PacketContainer(com.comphenix.protocol.events.PacketContainer) UndisguiseEvent(me.libraryaddict.disguise.events.UndisguiseEvent) PlayerInfoData(com.comphenix.protocol.wrappers.PlayerInfoData) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Example 14 with PlayerInfoData

use of com.comphenix.protocol.wrappers.PlayerInfoData in project LibsDisguises by libraryaddict.

the class TargetedDisguise method removePlayer.

public TargetedDisguise removePlayer(String playername) {
    if (disguiseViewers.contains(playername)) {
        disguiseViewers.remove(playername);
        if (DisguiseAPI.isDisguiseInUse(this)) {
            DisguiseUtilities.checkConflicts(this, playername);
            DisguiseUtilities.refreshTracker(this, playername);
            if (isHidePlayer() && getEntity() instanceof Player) {
                try {
                    Player player = Bukkit.getPlayerExact(playername);
                    if (player != null) {
                        PacketContainer deleteTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
                        deleteTab.getPlayerInfoAction().write(0, canSee(player) ? PlayerInfoAction.ADD_PLAYER : PlayerInfoAction.REMOVE_PLAYER);
                        deleteTab.getPlayerInfoDataLists().write(0, Arrays.asList(new PlayerInfoData(ReflectionManager.getGameProfile((Player) getEntity()), 0, NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(DisguiseUtilities.getPlayerListName((Player) getEntity())))));
                        ProtocolLibrary.getProtocolManager().sendServerPacket(player, deleteTab);
                    }
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return this;
}
Also used : Player(org.bukkit.entity.Player) PacketContainer(com.comphenix.protocol.events.PacketContainer) PlayerInfoData(com.comphenix.protocol.wrappers.PlayerInfoData) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 15 with PlayerInfoData

use of com.comphenix.protocol.wrappers.PlayerInfoData in project LibsDisguises by libraryaddict.

the class TargetedDisguise method addPlayer.

public TargetedDisguise addPlayer(String playername) {
    if (!disguiseViewers.contains(playername)) {
        disguiseViewers.add(playername);
        if (DisguiseAPI.isDisguiseInUse(this)) {
            DisguiseUtilities.checkConflicts(this, playername);
            DisguiseUtilities.refreshTracker(this, playername);
            if (isHidePlayer() && getEntity() instanceof Player) {
                try {
                    Player player = Bukkit.getPlayerExact(playername);
                    if (player != null) {
                        PacketContainer deleteTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
                        deleteTab.getPlayerInfoAction().write(0, canSee(player) ? PlayerInfoAction.REMOVE_PLAYER : PlayerInfoAction.ADD_PLAYER);
                        deleteTab.getPlayerInfoDataLists().write(0, Arrays.asList(new PlayerInfoData(ReflectionManager.getGameProfile((Player) getEntity()), 0, NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(DisguiseUtilities.getPlayerListName((Player) getEntity())))));
                        ProtocolLibrary.getProtocolManager().sendServerPacket(player, deleteTab);
                    }
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return this;
}
Also used : Player(org.bukkit.entity.Player) PacketContainer(com.comphenix.protocol.events.PacketContainer) PlayerInfoData(com.comphenix.protocol.wrappers.PlayerInfoData) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

PlayerInfoData (com.comphenix.protocol.wrappers.PlayerInfoData)19 PacketContainer (com.comphenix.protocol.events.PacketContainer)13 WrappedGameProfile (com.comphenix.protocol.wrappers.WrappedGameProfile)10 ArrayList (java.util.ArrayList)10 GameProfile (com.mojang.authlib.GameProfile)6 List (java.util.List)6 Player (org.bukkit.entity.Player)5 WrapperPlayServerPlayerInfo (com.comphenix.packetwrapper.WrapperPlayServerPlayerInfo)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)2 IntArrayList (org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.IntArrayList)2 EnumWrappers (com.comphenix.protocol.wrappers.EnumWrappers)1 NativeGameMode (com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode)1 WrappedChatComponent (com.comphenix.protocol.wrappers.WrappedChatComponent)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 lombok.val (lombok.val)1 Disguise (me.libraryaddict.disguise.disguisetypes.Disguise)1 DisguiseEvent (me.libraryaddict.disguise.events.DisguiseEvent)1 UndisguiseEvent (me.libraryaddict.disguise.events.UndisguiseEvent)1