Search in sources :

Example 1 with Serializer

use of com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer in project HoloMobHealth by LOOHP.

the class ArmorstandDisplay method getData.

public static ArmorStandDisplayData getData(Player player, UUID entityUUID, World world, PacketContainer packet) {
    // ArmorStandDisplayCache cahcedData = cache.get(entityUUID);
    // if (cahcedData != null) {
    // return cahcedData;
    // }
    Entity entity = NMSUtils.getEntityFromUUID(entityUUID);
    if (entity == null || !EntityTypeUtils.getMobsTypesSet().contains(entity.getType())) {
        return null;
    }
    if (HoloMobHealth.disabledMobTypes.contains(entity.getType())) {
        return new ArmorStandDisplayData();
    }
    String customName = CustomNameUtils.getMobCustomName(entity);
    if (!HoloMobHealth.disabledWorlds.contains(world.getName())) {
        if (!HoloMobHealth.showCitizens && HoloMobHealth.citizensHook) {
            if (CitizensUtils.isNPC(entity)) {
                return null;
            }
        }
        if (!HoloMobHealth.showMythicMobs && HoloMobHealth.mythicHook) {
            if (MythicMobsUtils.isMythicMob(entity)) {
                return null;
            }
        }
        if (!HoloMobHealth.showShopkeepers && HoloMobHealth.shopkeepersHook) {
            if (ShopkeepersUtils.isShopkeeper(entity)) {
                return null;
            }
        }
        if (!HoloMobHealth.showMyPet && HoloMobHealth.myPetHook) {
            if (MyPetUtils.isMyPet(entity)) {
                return null;
            }
        }
        if (customName != null) {
            for (String each : HoloMobHealth.disabledMobNamesAbsolute) {
                if (customName.equals(ChatColorUtils.translateAlternateColorCodes('&', each))) {
                    return new ArmorStandDisplayData();
                }
            }
            for (String each : HoloMobHealth.disabledMobNamesContains) {
                if (ChatColorUtils.stripColor(customName.toLowerCase()).contains(ChatColorUtils.stripColor(ChatColorUtils.translateAlternateColorCodes('&', each).toLowerCase()))) {
                    return new ArmorStandDisplayData();
                }
            }
        }
        if (!HoloMobHealth.applyToNamed) {
            if (customName != null) {
                return null;
            }
        }
        boolean useIdle = false;
        if (HoloMobHealth.useAlterHealth && !HoloMobHealth.altShowHealth.containsKey(entityUUID)) {
            if (HoloMobHealth.idleUse) {
                useIdle = true;
            }
        }
        List<WrappedWatchableObject> data = packet.getWatchableCollectionModifier().read(0);
        WrappedDataWatcher watcher = new WrappedDataWatcher(data);
        List<Component> components;
        if (useIdle) {
            components = HoloMobHealth.idleDisplayText.stream().map(each -> ParsePlaceholders.parse(player, (LivingEntity) entity, each)).collect(Collectors.toList());
        } else {
            components = HoloMobHealth.displayText.stream().map(each -> ParsePlaceholders.parse(player, (LivingEntity) entity, each)).collect(Collectors.toList());
        }
        if (HoloMobHealth.version.isOld()) {
            watcher.setObject(2, "");
        } else if (HoloMobHealth.version.isLegacy()) {
            Serializer serializer = Registry.get(String.class);
            WrappedDataWatcherObject object = new WrappedDataWatcherObject(2, serializer);
            watcher.setObject(object, "");
        } else {
            Optional<?> opt = Optional.empty();
            watcher.setObject(new WrappedDataWatcherObject(2, Registry.getChatComponentSerializer(true)), opt);
        }
        if (HoloMobHealth.version.isOld()) {
            watcher.setObject(3, (byte) 0);
        } else {
            watcher.setObject(new WrappedDataWatcherObject(3, Registry.get(Boolean.class)), false);
        }
        ArmorStandDisplayData newData = new ArmorStandDisplayData(watcher, components, customName, entity);
        return newData;
    }
    return null;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Optional(java.util.Optional) LivingEntity(org.bukkit.entity.LivingEntity) WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) Component(net.kyori.adventure.text.Component) Serializer(com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)

Example 2 with Serializer

use of com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer in project HoloMobHealth by LOOHP.

the class ArmorStandPacket method buildWrappedDataWatcher.

private static WrappedDataWatcher buildWrappedDataWatcher(HoloMobArmorStand entity, Component entityNameComponent, boolean visible) {
    WrappedDataWatcher watcher = new WrappedDataWatcher();
    if (entity.getType().equals(EntityType.ARMOR_STAND)) {
        byte bitmask = 0x20;
        if (HoloMobHealth.version.isOld()) {
            watcher.setObject(0, bitmask);
        } else {
            watcher.setObject(new WrappedDataWatcherObject(0, Registry.get(Byte.class)), bitmask);
        }
        if (entityNameComponent != null && !entityNameComponent.equals(Component.empty())) {
            if (HoloMobHealth.version.isOld()) {
                watcher.setObject(2, LegacyComponentSerializer.legacySection().serialize(LanguageUtils.convert(entityNameComponent, HoloMobHealth.language)));
            } else if (HoloMobHealth.version.isLegacy()) {
                Serializer serializer = Registry.get(String.class);
                WrappedDataWatcherObject object = new WrappedDataWatcherObject(2, serializer);
                watcher.setObject(object, LegacyComponentSerializer.legacySection().serialize(LanguageUtils.convert(entityNameComponent, HoloMobHealth.language)));
            } else {
                Optional<?> opt = Optional.of(WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(entityNameComponent)).getHandle());
                watcher.setObject(new WrappedDataWatcherObject(2, Registry.getChatComponentSerializer(true)), opt);
            }
        } else {
            if (HoloMobHealth.version.isOld()) {
                watcher.setObject(2, "");
            } else if (HoloMobHealth.version.isLegacy()) {
                Serializer serializer = Registry.get(String.class);
                WrappedDataWatcherObject object = new WrappedDataWatcherObject(2, serializer);
                watcher.setObject(object, "");
            } else {
                Optional<?> opt = Optional.empty();
                watcher.setObject(new WrappedDataWatcherObject(2, Registry.getChatComponentSerializer(true)), opt);
            }
        }
        if (HoloMobHealth.version.isOld()) {
            watcher.setObject(3, (byte) (visible ? 1 : 0));
            watcher.setObject(5, (byte) 1);
        } else {
            watcher.setObject(new WrappedDataWatcherObject(3, Registry.get(Boolean.class)), entityNameComponent != null && (!entityNameComponent.equals(Component.empty()) && visible));
            watcher.setObject(new WrappedDataWatcherObject(5, Registry.get(Boolean.class)), true);
        }
        byte standbitmask = 0x01 | 0x10;
        if (HoloMobHealth.version.isNewerOrEqualTo(MCVersion.V1_17)) {
            watcher.setObject(new WrappedDataWatcherObject(15, Registry.get(Byte.class)), standbitmask);
        } else if (HoloMobHealth.version.isNewerOrEqualTo(MCVersion.V1_15)) {
            watcher.setObject(new WrappedDataWatcherObject(14, Registry.get(Byte.class)), standbitmask);
        } else if (HoloMobHealth.version.equals(MCVersion.V1_14)) {
            watcher.setObject(new WrappedDataWatcherObject(13, Registry.get(Byte.class)), standbitmask);
        } else if (!HoloMobHealth.version.isOld()) {
            watcher.setObject(new WrappedDataWatcherObject(11, Registry.get(Byte.class)), standbitmask);
        } else {
            watcher.setObject(10, standbitmask);
        }
    } else {
        byte bitmask = 0x20;
        if (HoloMobHealth.version.isOld()) {
            watcher.setObject(0, bitmask);
        } else {
            watcher.setObject(new WrappedDataWatcherObject(0, Registry.get(Byte.class)), bitmask);
        }
        if (!HoloMobHealth.version.isOld()) {
            watcher.setObject(new WrappedDataWatcherObject(4, Registry.get(Boolean.class)), true);
        } else {
            watcher.setObject(4, (byte) 1);
        }
        if (HoloMobHealth.version.isNewerOrEqualTo(MCVersion.V1_17)) {
            watcher.setObject(new WrappedDataWatcherObject(16, Registry.get(Boolean.class)), true);
        } else if (HoloMobHealth.version.isNewerOrEqualTo(MCVersion.V1_15)) {
            watcher.setObject(new WrappedDataWatcherObject(15, Registry.get(Boolean.class)), true);
        } else if (HoloMobHealth.version.equals(MCVersion.V1_14)) {
            watcher.setObject(new WrappedDataWatcherObject(14, Registry.get(Boolean.class)), true);
        } else if (!HoloMobHealth.version.isOld()) {
            watcher.setObject(new WrappedDataWatcherObject(12, Registry.get(Boolean.class)), true);
        } else {
            watcher.setObject(10, (byte) 1);
        }
    }
    return watcher;
}
Also used : WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) Optional(java.util.Optional) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) Serializer(com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)

Example 3 with Serializer

use of com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer in project HoloMobHealth by LOOHP.

the class NameTagDisplay method getWatcher.

public static WrappedDataWatcher getWatcher(Player player, UUID entityUUID, World world, PacketContainer packet) {
    // WrappedDataWatcher cachedWatcher = cache.get(entityUUID);
    // if (cachedWatcher != null) {
    // return cachedWatcher;
    // }
    Entity entity = NMSUtils.getEntityFromUUID(entityUUID);
    if (entity == null || !EntityTypeUtils.getMobsTypesSet().contains(entity.getType())) {
        return null;
    }
    if (HoloMobHealth.disabledMobTypes.contains(entity.getType())) {
        return null;
    }
    String customName = CustomNameUtils.getMobCustomName(entity);
    if (HoloMobHealth.rangeEnabled && !RangeModule.isEntityInRangeOfPlayer(player, entity)) {
        return null;
    }
    boolean useIdle = false;
    if (HoloMobHealth.useAlterHealth && !HoloMobHealth.altShowHealth.containsKey(entityUUID)) {
        if (!HoloMobHealth.idleUse) {
            return null;
        }
        useIdle = true;
    }
    if (!HoloMobHealth.disabledWorlds.contains(world.getName())) {
        if (!HoloMobHealth.showCitizens && HoloMobHealth.citizensHook) {
            if (CitizensUtils.isNPC(entity)) {
                return null;
            }
        }
        if (!HoloMobHealth.showMythicMobs && HoloMobHealth.mythicHook) {
            if (MythicMobsUtils.isMythicMob(entity)) {
                return null;
            }
        }
        if (!HoloMobHealth.showShopkeepers && HoloMobHealth.shopkeepersHook) {
            if (ShopkeepersUtils.isShopkeeper(entity)) {
                return null;
            }
        }
        if (!HoloMobHealth.showMyPet && HoloMobHealth.myPetHook) {
            if (MyPetUtils.isMyPet(entity)) {
                return null;
            }
        }
        if (customName != null) {
            for (String each : HoloMobHealth.disabledMobNamesAbsolute) {
                if (customName.equals(ChatColorUtils.translateAlternateColorCodes('&', each))) {
                    return null;
                }
            }
            for (String each : HoloMobHealth.disabledMobNamesContains) {
                if (ChatColorUtils.stripColor(customName.toLowerCase()).contains(ChatColorUtils.stripColor(ChatColorUtils.translateAlternateColorCodes('&', each).toLowerCase()))) {
                    return null;
                }
            }
        }
        if (!HoloMobHealth.applyToNamed) {
            if (customName != null) {
                return null;
            }
        }
        List<WrappedWatchableObject> data = packet.getWatchableCollectionModifier().read(0);
        WrappedDataWatcher watcher = new WrappedDataWatcher(data);
        Component component = ParsePlaceholders.parse(player, (LivingEntity) entity, useIdle ? HoloMobHealth.idleDisplayText.get(0) : HoloMobHealth.displayText.get(0));
        boolean visible = HoloMobHealth.alwaysShow;
        if (component != null && !component.equals(Component.empty())) {
            if (HoloMobHealth.version.isOld()) {
                watcher.setObject(2, LegacyComponentSerializer.legacySection().serialize(LanguageUtils.convert(component, HoloMobHealth.language)));
            } else if (HoloMobHealth.version.isLegacy()) {
                Serializer serializer = Registry.get(String.class);
                WrappedDataWatcherObject object = new WrappedDataWatcherObject(2, serializer);
                watcher.setObject(object, LegacyComponentSerializer.legacySection().serialize(LanguageUtils.convert(component, HoloMobHealth.language)));
            } else {
                Optional<?> opt = Optional.of(WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(component)).getHandle());
                watcher.setObject(new WrappedDataWatcherObject(2, Registry.getChatComponentSerializer(true)), opt);
            }
        } else {
            if (HoloMobHealth.version.isOld()) {
                watcher.setObject(2, "");
            } else if (HoloMobHealth.version.isLegacy()) {
                Serializer serializer = Registry.get(String.class);
                WrappedDataWatcherObject object = new WrappedDataWatcherObject(2, serializer);
                watcher.setObject(object, "");
            } else {
                Optional<?> opt = Optional.empty();
                watcher.setObject(new WrappedDataWatcherObject(2, Registry.getChatComponentSerializer(true)), opt);
            }
        }
        if (HoloMobHealth.version.isOld()) {
            watcher.setObject(3, (byte) (visible ? 1 : 0));
        } else {
            watcher.setObject(new WrappedDataWatcherObject(3, Registry.get(Boolean.class)), visible);
        }
        return watcher;
    }
    return null;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) Optional(java.util.Optional) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent) Component(net.kyori.adventure.text.Component) Serializer(com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)

Example 4 with Serializer

use of com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer in project HoloMobHealth by LOOHP.

the class EntityMetadata method sendMetadataPacket.

public static void sendMetadataPacket(Entity entity, Component entityNameComponent, boolean visible, List<Player> players, boolean quiet) {
    Bukkit.getScheduler().runTask(HoloMobHealth.plugin, () -> {
        PacketContainer packet = HoloMobHealth.protocolManager.createPacket(PacketType.Play.Server.ENTITY_METADATA);
        // Set packet's entity id
        packet.getIntegers().write(0, entity.getEntityId());
        // Create data watcher, the Entity Metadata packet requires this
        WrappedDataWatcher watcher = new WrappedDataWatcher();
        if (entityNameComponent != null && !entityNameComponent.equals(Component.empty())) {
            if (HoloMobHealth.version.isOld()) {
                watcher.setObject(2, LegacyComponentSerializer.legacySection().serialize(LanguageUtils.convert(entityNameComponent, HoloMobHealth.language)));
            } else if (HoloMobHealth.version.isLegacy()) {
                Serializer serializer = Registry.get(String.class);
                WrappedDataWatcherObject object = new WrappedDataWatcherObject(2, serializer);
                watcher.setObject(object, LegacyComponentSerializer.legacySection().serialize(LanguageUtils.convert(entityNameComponent, HoloMobHealth.language)));
            } else {
                Optional<?> opt = Optional.of(WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(entityNameComponent)).getHandle());
                watcher.setObject(new WrappedDataWatcherObject(2, Registry.getChatComponentSerializer(true)), opt);
            }
        } else {
            if (HoloMobHealth.version.isOld()) {
                watcher.setObject(2, "");
            } else if (HoloMobHealth.version.isLegacy()) {
                Serializer serializer = Registry.get(String.class);
                WrappedDataWatcherObject object = new WrappedDataWatcherObject(2, serializer);
                watcher.setObject(object, "");
            } else {
                Optional<?> opt = Optional.empty();
                watcher.setObject(new WrappedDataWatcherObject(2, Registry.getChatComponentSerializer(true)), opt);
            }
        }
        if (HoloMobHealth.version.isOld()) {
            watcher.setObject(3, (byte) (visible ? 1 : 0));
        } else {
            watcher.setObject(new WrappedDataWatcherObject(3, Registry.get(Boolean.class)), visible);
        }
        packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
        try {
            for (Player player : players) {
                if (player.hasPermission("holomobhealth.use")) {
                    HoloMobHealth.protocolManager.sendServerPacket(player, packet, !quiet);
                }
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    });
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) Player(org.bukkit.entity.Player) WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) Optional(java.util.Optional) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) InvocationTargetException(java.lang.reflect.InvocationTargetException) Serializer(com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer) GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)

Aggregations

WrappedDataWatcher (com.comphenix.protocol.wrappers.WrappedDataWatcher)4 Serializer (com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer)4 WrappedDataWatcherObject (com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject)4 Optional (java.util.Optional)4 LegacyComponentSerializer (net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)4 GsonComponentSerializer (net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)3 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)2 Component (net.kyori.adventure.text.Component)2 Entity (org.bukkit.entity.Entity)2 LivingEntity (org.bukkit.entity.LivingEntity)2 PacketContainer (com.comphenix.protocol.events.PacketContainer)1 WrappedChatComponent (com.comphenix.protocol.wrappers.WrappedChatComponent)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Player (org.bukkit.entity.Player)1