Search in sources :

Example 1 with UndisguiseEvent

use of me.libraryaddict.disguise.events.UndisguiseEvent 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)

Aggregations

PacketContainer (com.comphenix.protocol.events.PacketContainer)1 PlayerInfoData (com.comphenix.protocol.wrappers.PlayerInfoData)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 UndisguiseEvent (me.libraryaddict.disguise.events.UndisguiseEvent)1 Player (org.bukkit.entity.Player)1 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)1