Search in sources :

Example 1 with NbtBase

use of com.comphenix.protocol.wrappers.nbt.NbtBase in project VoxelGamesLibv2 by VoxelGamesLib.

the class NBTUtil method setPlayerProfile.

/**
 * Sets a player profile onto the given tag
 *
 * @param nbt           the tag to set the profile to
 * @param playerProfile the profile to set
 */
public static void setPlayerProfile(NbtCompound nbt, PlayerProfile playerProfile) {
    nbt.put("Id", (playerProfile.getId() == null ? UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerProfile.getName()).getBytes()) : playerProfile.getId()).toString());
    nbt.put("Name", playerProfile.getName());
    if (!nbt.containsKey("Properties"))
        return;
    NbtCompound properties = nbt.getCompound("Properties");
    NbtList list = properties.getList("textures");
    Map<String, NbtBase> texture = (Map<String, NbtBase>) list.getValue(0);
    for (ProfileProperty property : playerProfile.getProperties()) {
        texture.put("name", NbtFactory.of("name", property.getValue()));
        texture.put("Signature", NbtFactory.of("Signature", property.getSignature()));
        texture.put("Value", NbtFactory.of("Value", property.getValue()));
    }
}
Also used : NbtBase(com.comphenix.protocol.wrappers.nbt.NbtBase) ProfileProperty(com.destroystokyo.paper.profile.ProfileProperty) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) NbtList(com.comphenix.protocol.wrappers.nbt.NbtList) Map(java.util.Map)

Example 2 with NbtBase

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

the class DisguiseUtilities method serialize.

private static String serialize(int depth, NbtBase base) {
    switch(base.getType()) {
        case TAG_COMPOUND:
            StringBuilder builder = new StringBuilder();
            builder.append("{");
            for (String key : ((NbtCompound) base).getKeys()) {
                NbtBase<Object> nbt = ((NbtCompound) base).getValue(key);
                String val = serialize(depth + 1, nbt);
                // Skip root empty values
                if (depth == 0 && val.matches("0(\\.0)?")) {
                    continue;
                }
                if (builder.length() != 1) {
                    builder.append(",");
                }
                builder.append(key).append(":").append(val);
            }
            builder.append("}");
            return builder.toString();
        case TAG_LIST:
            Collection col = ((NbtList) base).asCollection();
            return "[" + StringUtils.join(col.stream().map(b -> serialize(depth + 1, (NbtBase) b)).toArray(), ",") + "]";
        case TAG_BYTE_ARRAY:
        case TAG_INT_ARRAY:
        case TAG_LONG_ARRAY:
            String[] str = new String[Array.getLength(base.getValue())];
            for (int i = 0; i < str.length; i++) {
                // + getChar(base.getType());
                str[i] = Array.get(base.getValue(), i).toString();
            }
            String c = "";
            switch(base.getType()) {
                case TAG_BYTE_ARRAY:
                    c = "B;";
                    break;
                case TAG_INT_ARRAY:
                    c = "I;";
                    break;
                case TAG_LONG_ARRAY:
                    c = "L;";
                    break;
            }
            return "[" + c + StringUtils.join(str, ",") + "]";
        case TAG_BYTE:
        case TAG_INT:
        case TAG_LONG:
        case TAG_FLOAT:
        case TAG_SHORT:
        case TAG_DOUBLE:
            // + getChar(base.getType());
            return base.getValue().toString();
        case TAG_STRING:
            String val = (String) base.getValue();
            return "\"" + val.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
        case TAG_END:
            return "";
        default:
            throw new IllegalArgumentException();
    }
}
Also used : NbtBase(com.comphenix.protocol.wrappers.nbt.NbtBase) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) Collection(java.util.Collection) NbtList(com.comphenix.protocol.wrappers.nbt.NbtList) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject)

Example 3 with NbtBase

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

the class MapChunk method process.

protected void process(PacketEvent packetEvent) {
    PsrUser user = getEventUser(packetEvent);
    if (user == null) {
        return;
    }
    PacketContainer packet = packetEvent.getPacket();
    List<NbtBase<?>> nbtBaseList = packet.getListNbtModifier().read(0);
    for (NbtBase<?> nbtBase : nbtBaseList) {
        NbtCompound nbtCompound = (NbtCompound) nbtBase;
        if (!nbtCompound.containsKey("id")) {
            continue;
        }
        if ("minecraft:sign".equals(nbtCompound.getString("id"))) {
            setSignText(packetEvent, nbtCompound, user, filter);
        }
    }
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) NbtBase(com.comphenix.protocol.wrappers.nbt.NbtBase) PsrUser(me.rothes.protocolstringreplacer.api.user.PsrUser) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound)

Example 4 with NbtBase

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

the class MapChunkUpper18 method processPacket.

private void processPacket(PacketEvent packetEvent, PsrUser user, Object packet) {
    if (!packet.getClass().equals(packetClass)) {
        return;
    }
    try {
        Object data = dataField.get(packet);
        EquivalentConverter<NbtBase<?>> converter = BukkitConverters.getNbtConverter();
        NbtCompound nbtCompound;
        for (Object obj : (List<?>) listField.get(data)) {
            if (subTypeField.get(obj).equals(signType)) {
                Object nbt = subNbtField.get(obj);
                if (nbt == null) {
                    continue;
                }
                nbtCompound = (NbtCompound) converter.getSpecific(nbt);
                setSignText(packetEvent, nbtCompound, user, filter);
            }
        }
        for (Object extra : (List<?>) extraField.get(data)) {
            processPacket(packetEvent, user, extra);
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
Also used : NbtBase(com.comphenix.protocol.wrappers.nbt.NbtBase) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) List(java.util.List)

Aggregations

NbtBase (com.comphenix.protocol.wrappers.nbt.NbtBase)4 NbtCompound (com.comphenix.protocol.wrappers.nbt.NbtCompound)4 NbtList (com.comphenix.protocol.wrappers.nbt.NbtList)2 PacketContainer (com.comphenix.protocol.events.PacketContainer)1 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)1 ProfileProperty (com.destroystokyo.paper.profile.ProfileProperty)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 PsrUser (me.rothes.protocolstringreplacer.api.user.PsrUser)1