Search in sources :

Example 1 with NbtList

use of com.comphenix.protocol.wrappers.nbt.NbtList in project WheelCore by Plugindustry.

the class NBTBasedProvider method getOreDictionary.

@Nonnull
@Override
@SuppressWarnings("unchecked")
public Set<String> getOreDictionary(@Nullable ItemStack itemStack) {
    if (itemStack == null)
        return Collections.emptySet();
    Set<String> defaultOreDict = getInstance(itemStack) == null ? ItemMapping.getVanillaOreDict(itemStack.getType()) : Collections.emptySet();
    Optional<NbtWrapper<?>> nbtWrapperOptional = NbtFactory.fromItemOptional((ItemStack) ShadowManager.shadowUnpack(CraftItemStack.asCraftCopy(itemStack)));
    if (nbtWrapperOptional.isEmpty())
        return defaultOreDict;
    NbtWrapper<?> nbtWrapper = nbtWrapperOptional.get();
    if (nbtWrapper.getType() != NbtType.TAG_COMPOUND)
        return defaultOreDict;
    NbtCompound compound = NbtFactory.asCompound(nbtWrapper);
    if (!compound.containsKey("wheel_core_item_ore_dictionary"))
        return defaultOreDict;
    Object data = compound.getObject("wheel_core_item_ore_dictionary");
    if ((!(data instanceof NbtList)) || ((NbtList<?>) data).getElementType() != NbtType.TAG_STRING)
        return defaultOreDict;
    return ((NbtList<String>) data).asCollection().stream().map(NbtBase::getValue).collect(Collectors.toSet());
}
Also used : NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) NbtList(com.comphenix.protocol.wrappers.nbt.NbtList) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) Nonnull(javax.annotation.Nonnull)

Example 2 with NbtList

use of com.comphenix.protocol.wrappers.nbt.NbtList 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 3 with NbtList

use of com.comphenix.protocol.wrappers.nbt.NbtList 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)

Aggregations

NbtCompound (com.comphenix.protocol.wrappers.nbt.NbtCompound)3 NbtList (com.comphenix.protocol.wrappers.nbt.NbtList)3 NbtBase (com.comphenix.protocol.wrappers.nbt.NbtBase)2 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)1 NbtWrapper (com.comphenix.protocol.wrappers.nbt.NbtWrapper)1 ProfileProperty (com.destroystokyo.paper.profile.ProfileProperty)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1