Search in sources :

Example 6 with WrappedChatComponent

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

the class PacketListenerScoreboardTeam method onPacketSending.

@Override
public void onPacketSending(PacketEvent event) {
    PacketContainer packet = event.getPacket();
    if (NmsVersion.v1_17.isSupported()) {
        int type = packet.getIntegers().read(0);
        if (type != 0 && type != 2) {
            return;
        }
    }
    String name = packet.getStrings().read(0);
    if (name == null || !name.startsWith("LD_") || name.equals("LD_NoName") || name.startsWith("LD_Color_")) {
        return;
    }
    DisguiseUtilities.DScoreTeam team = DisguiseUtilities.getTeams().get(name);
    if (team == null) {
        return;
    }
    StructureModifier<WrappedChatComponent> chats;
    if (NmsVersion.v1_17.isSupported()) {
        // Might need to do sanity checks but eh
        chats = packet.getOptionalStructures().read(0).get().getChatComponents();
    } else {
        chats = packet.getChatComponents();
    }
    BaseComponent[] prefix = DisguiseUtilities.getColoredChat(team.getPrefix());
    BaseComponent[] suffix = DisguiseUtilities.getColoredChat(team.getSuffix());
    chats.write(1, WrappedChatComponent.fromJson(ComponentSerializer.toString(prefix)));
    chats.write(2, WrappedChatComponent.fromJson(ComponentSerializer.toString(suffix)));
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) DisguiseUtilities(me.libraryaddict.disguise.utilities.DisguiseUtilities)

Example 7 with WrappedChatComponent

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

the class ReflectionManager method convertInvalidMeta.

public static Object convertInvalidMeta(Object value) {
    if (value instanceof Optional) {
        Optional opt = (Optional) value;
        if (!opt.isPresent()) {
            return NmsVersion.v1_13.isSupported() ? value : com.google.common.base.Optional.absent();
        }
        if (nmsReflection != null) {
            return nmsReflection.convertOptional(opt.get());
        }
        Object val = opt.get();
        if (val instanceof BlockPosition) {
            BlockPosition pos = (BlockPosition) val;
            try {
                Object obj = blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
                return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (val instanceof WrappedBlockData) {
            try {
                Object obj = ((WrappedBlockData) val).getHandle();
                return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (val instanceof ItemStack) {
            val = getNmsItem((ItemStack) val);
            if (val == null) {
                return NmsVersion.v1_13.isSupported() ? Optional.empty() : com.google.common.base.Optional.absent();
            } else {
                return Optional.of(val);
            }
        } else if (val instanceof WrappedChatComponent) {
            Object obj = ((WrappedChatComponent) val).getHandle();
            return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
        } else if (!NmsVersion.v1_13.isSupported()) {
            return com.google.common.base.Optional.of(val);
        } else {
            return Optional.of(val);
        }
    } else if (value instanceof Vector3F) {
        Vector3F angle = (Vector3F) value;
        if (nmsReflection != null) {
            return nmsReflection.convertVec3(angle);
        }
        try {
            return vector3FConstructor.newInstance(angle.getX(), angle.getY(), angle.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof EulerAngle) {
        EulerAngle angle = (EulerAngle) value;
        if (nmsReflection != null) {
            return nmsReflection.convertVec3(angle);
        }
        try {
            return vector3FConstructor.newInstance((float) angle.getX(), (float) angle.getY(), (float) angle.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof Direction) {
        if (nmsReflection != null) {
            return nmsReflection.convertDirection((Direction) value);
        }
        try {
            return enumDirectionFrom.invoke(null, ((Direction) value).ordinal());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof BlockPosition) {
        BlockPosition pos = (BlockPosition) value;
        if (nmsReflection != null) {
            return nmsReflection.getBlockPosition(pos.getX(), pos.getY(), pos.getZ());
        }
        try {
            return blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof ItemStack) {
        return getNmsItem((ItemStack) value);
    } else if (value instanceof Double) {
        return ((Double) value).floatValue();
    } else if (value instanceof NbtWrapper) {
        return ((NbtWrapper) value).getHandle();
    } else if (value instanceof WrappedParticle) {
        return ((WrappedParticle) value).getHandle();
    } else if (value instanceof EntityPose) {
        return getNmsEntityPose((EntityPose) value);
    } else if (value instanceof VillagerData) {
        return getNmsVillagerData((VillagerData) value);
    } else if (value instanceof WrappedChatComponent) {
        return ((WrappedChatComponent) value).getHandle();
    }
    return value;
}
Also used : WrappedBlockData(com.comphenix.protocol.wrappers.WrappedBlockData) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent) Optional(java.util.Optional) BlockPosition(com.comphenix.protocol.wrappers.BlockPosition) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) Direction(com.comphenix.protocol.wrappers.EnumWrappers.Direction) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FieldAccessException(com.comphenix.protocol.reflect.FieldAccessException) Vector3F(com.comphenix.protocol.wrappers.Vector3F) VillagerData(me.libraryaddict.disguise.disguisetypes.VillagerData) WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) AccessibleObject(java.lang.reflect.AccessibleObject) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) ItemStack(org.bukkit.inventory.ItemStack) EulerAngle(org.bukkit.util.EulerAngle) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) EntityPose(me.libraryaddict.disguise.disguisetypes.EntityPose)

Example 8 with WrappedChatComponent

use of com.comphenix.protocol.wrappers.WrappedChatComponent in project SwornAPI by dmulloy2.

the class ProtocolLibProvider method sendMessage.

@Override
public boolean sendMessage(Player player, ChatPosition position, BaseComponent... message) {
    try {
        PacketContainer packet = manager.createPacket(PacketType.Play.Server.CHAT);
        // Write our message to the packet
        WrappedChatComponent component = WrappedChatComponent.fromJson(ComponentSerializer.toString(message));
        packet.getChatComponents().write(0, component);
        // Nullify Spigot's components
        packet.getModifier().write(1, null);
        // Write the position
        packet.getBytes().write(0, position != null ? position.getValue() : 1);
        // Send the packet
        manager.sendServerPacket(player, packet);
        return true;
    } catch (Throwable ex) {
        LogHandler.globalDebug(Util.getUsefulStack(ex, "sending chat packet to {0}", player.getName()));
        return false;
    }
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent)

Example 9 with WrappedChatComponent

use of com.comphenix.protocol.wrappers.WrappedChatComponent in project ProtocolStringReplacer by Rothes.

the class AbstractTitleListener method process.

protected void process(PacketEvent packetEvent) {
    PsrUser user = getEventUser(packetEvent);
    if (user == null) {
        return;
    }
    PacketContainer packet = packetEvent.getPacket();
    StructureModifier<WrappedChatComponent> wrappedChatComponentStructureModifier = packet.getChatComponents();
    WrappedChatComponent wrappedChatComponent = wrappedChatComponentStructureModifier.read(0);
    if (wrappedChatComponent != null) {
        String json = wrappedChatComponent.getJson();
        WrappedChatComponent replaced = getReplacedJsonWrappedComponent(packetEvent, user, listenType, json, filter);
        if (replaced != null) {
            wrappedChatComponentStructureModifier.write(0, replaced);
        }
    }
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent) PsrUser(me.rothes.protocolstringreplacer.api.user.PsrUser)

Example 10 with WrappedChatComponent

use of com.comphenix.protocol.wrappers.WrappedChatComponent in project ProtocolStringReplacer by Rothes.

the class EntityMetadata method process.

protected void process(PacketEvent packetEvent) {
    PsrUser user = getEventUser(packetEvent);
    if (user == null) {
        return;
    }
    PacketContainer packet = packetEvent.getPacket();
    Entity entity;
    WrapperPlayServerEntityMetadata wrapperPlayServerEntityMetadata;
    try {
        entity = packet.getEntityModifier(packetEvent).read(0);
        if (entity == null) {
            return;
        }
        wrapperPlayServerEntityMetadata = new WrapperPlayServerEntityMetadata(packet.deepClone());
    } catch (RuntimeException e) {
        if (exceptionTimes < ProtocolStringReplacer.getInstance().getConfigManager().protocollibSideStackPrintCount) {
            e.printStackTrace();
            ProtocolStringReplacer.warn("This may be a ProtocolLib side problem.");
            exceptionTimes++;
        }
        return;
    }
    List<WrappedWatchableObject> metadataList = wrapperPlayServerEntityMetadata.getMetadata();
    if (metadataList != null) {
        for (WrappedWatchableObject watchableObject : metadataList) {
            if (watchableObject.getValue() instanceof Optional<?>) {
                Optional<?> value = (Optional<?>) watchableObject.getValue();
                if (value.isPresent() && MinecraftReflection.getIChatBaseComponentClass().isInstance(value.get())) {
                    WrappedChatComponent wrappedChatComponent = WrappedChatComponent.fromHandle(value.get());
                    if (wrappedChatComponent != null) {
                        String replacedJson = getReplacedJson(packetEvent, user, listenType, wrappedChatComponent.getJson(), filter);
                        if (replacedJson != null) {
                            wrappedChatComponent.setJson(replacedJson);
                            watchableObject.setValue(Optional.of(wrappedChatComponent.getHandle()));
                        } else {
                            return;
                        }
                    }
                }
            } else if (BukkitConverters.getItemStackConverter().getSpecificType().isInstance(watchableObject.getValue())) {
                Object value = watchableObject.getValue();
                if (BukkitConverters.getItemStackConverter().getSpecificType().isInstance(value)) {
                    ItemStack itemStack = BukkitConverters.getItemStackConverter().getSpecific(value);
                    replaceItemStack(packetEvent, user, listenType, itemStack, filter);
                }
            }
            packetEvent.setPacket(wrapperPlayServerEntityMetadata.getHandle());
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) PacketContainer(com.comphenix.protocol.events.PacketContainer) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent) PsrUser(me.rothes.protocolstringreplacer.api.user.PsrUser) Optional(java.util.Optional) WrapperPlayServerEntityMetadata(me.rothes.protocolstringreplacer.libs.com.comphenix.packetwrapper.WrapperPlayServerEntityMetadata) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

WrappedChatComponent (com.comphenix.protocol.wrappers.WrappedChatComponent)12 PacketContainer (com.comphenix.protocol.events.PacketContainer)9 PsrUser (me.rothes.protocolstringreplacer.api.user.PsrUser)8 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)2 Optional (java.util.Optional)2 BaseComponent (net.md_5.bungee.api.chat.BaseComponent)2 ItemStack (org.bukkit.inventory.ItemStack)2 FieldAccessException (com.comphenix.protocol.reflect.FieldAccessException)1 BlockPosition (com.comphenix.protocol.wrappers.BlockPosition)1 Direction (com.comphenix.protocol.wrappers.EnumWrappers.Direction)1 NativeGameMode (com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode)1 PlayerInfoData (com.comphenix.protocol.wrappers.PlayerInfoData)1 Vector3F (com.comphenix.protocol.wrappers.Vector3F)1 WrappedBlockData (com.comphenix.protocol.wrappers.WrappedBlockData)1 WrappedDataWatcherObject (com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject)1 WrappedParticle (com.comphenix.protocol.wrappers.WrappedParticle)1 NbtWrapper (com.comphenix.protocol.wrappers.nbt.NbtWrapper)1 IOException (java.io.IOException)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 Field (java.lang.reflect.Field)1