Package com.khorn.terraincontrol.util

Examples of com.khorn.terraincontrol.util.NamedBinaryTag


            // Found a cached one
            return loadedTags.get(path);
        }

        // Load from file
        NamedBinaryTag metadata;
        FileInputStream stream = null;
        try
        {
            // Read it from a file next to the BO3
            stream = new FileInputStream(path);
            // Get the tag
            metadata = NamedBinaryTag.readFrom(stream, true);
            stream.close();
        } catch (FileNotFoundException e)
        {
            // File not found
            TerrainControl.log(LogMarker.WARN, "NBT file {} not found", (Object) path);
            tryToClose(stream);
            return null;
        } catch (IOException e)
        {
            // Not a compressed NBT file, try uncompressed
            tryToClose(stream);
            try
            {
                // Read it from a file next to the BO3
                stream = new FileInputStream(path);
                // Get the tag
                metadata = NamedBinaryTag.readFrom(stream, false);
                stream.close();
            } catch (IOException corruptFile)
            {
                TerrainControl.log(LogMarker.FATAL, "Failed to read NBT meta file: ", e.getMessage());
                TerrainControl.printStackTrace(LogMarker.FATAL, corruptFile);
                tryToClose(stream);
                return null;
            }
        }

        // The file can be structured in two ways:
        // 1. chest.nbt with all the contents directly in it
        // 2. chest.nbt with a Compound tag in it with all the data

        // Check for type 1 by searching for an id tag
        NamedBinaryTag[] values = (NamedBinaryTag[]) metadata.getValue();
        for (NamedBinaryTag subTag : values)
        {
            if (subTag.getName() != null && subTag.getName().equals("id") && subTag.getType().equals(NamedBinaryTag.Type.TAG_String))
            {
                // Found id tag, so return the root tag
                return metadata;
            }
        }
        // No id tag found, so check for type 2
        try
        {
            return registerMetadata(path, ((NamedBinaryTag[]) metadata.getValue())[0]);
        } catch (Exception e)
        {
            TerrainControl.log(LogMarker.WARN, "Structure of NBT file is incorrect: ", e.getMessage());
            return null;
        }
View Full Code Here


            } catch (InvalidConfigException e)
            {
                // Maybe it's a NBT file?

                // Get the file
                NamedBinaryTag metaData = BO3Loader.loadMetadata(args.get(i), this.getHolder().getFile());
                if (metaData != null)
                {
                    metaDataNames[blockCount] = args.get(i);
                    metaDataTags[blockCount] = metaData;
                }
View Full Code Here

    @SuppressWarnings("unchecked")
    // ^ We know that NBTTagCompound.map is a Map<String, NBTBase>
    // So it is safe to suppress this warning
    public static NamedBinaryTag getNBTFromNMSTagCompound(String name, NBTTagCompound nmsTag)
    {
        NamedBinaryTag compoundTag = new NamedBinaryTag(NamedBinaryTag.Type.TAG_Compound, name, new NamedBinaryTag[] {new NamedBinaryTag(NamedBinaryTag.Type.TAG_End, null, null)});

        // Get the child tags using some reflection magic
        Field mapField;
        Map<String, NBTBase> nmsChildTags = null;
        try
        {
            mapField = NBTTagCompound.class.getDeclaredField("map");
            mapField.setAccessible(true);
            nmsChildTags = (Map<String, NBTBase>) mapField.get(nmsTag);
        } catch (Exception e)
        {
            TerrainControl.printStackTrace(LogMarker.FATAL, e);
        }

        if (nmsChildTags == null)
        {
            // Cannot load the tag, return an empty tag
            return compoundTag;
        }

        // Add all child tags to the compound tag
        for (Entry<String, NBTBase> entry : nmsChildTags.entrySet())
        {
            NBTBase nmsChildTag = entry.getValue();
            NamedBinaryTag.Type type = NamedBinaryTag.Type.values()[nmsChildTag.getId()];
            switch (type)
            {
                case TAG_End:
                    break;
                case TAG_Byte:
                case TAG_Short:
                case TAG_Int:
                case TAG_Long:
                case TAG_Float:
                case TAG_Double:
                case TAG_Byte_Array:
                case TAG_String:
                case TAG_Int_Array:
                    compoundTag.addTag(new NamedBinaryTag(type, entry.getKey(), getValueFromNms(nmsChildTag)));
                    break;
                case TAG_List:
                    NamedBinaryTag listChildTag = getNBTFromNMSTagList(entry.getKey(), (NBTTagList) nmsChildTag);
                    if (listChildTag != null)
                    {
                        compoundTag.addTag(listChildTag);
                    }
                    break;
View Full Code Here

            // Nothing to return
            return null;
        }

        NamedBinaryTag.Type listType = NamedBinaryTag.Type.values()[nmsListTag.func_150303_d()];
        NamedBinaryTag listTag = new NamedBinaryTag(name, listType);

        // Add all child tags
        for (int i = 0; i < nmsListTag.tagCount(); i++)
        {
            switch (listType)
            {
                case TAG_Int_Array:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.func_150306_c(i)));
                    break;
                case TAG_Float:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.func_150308_e(i)));
                    break;
                case TAG_Double:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.func_150309_d(i)));
                    break;
                case TAG_String:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.getStringTagAt(i)));
                    break;
                case TAG_Compound:
                    listTag.addTag(getNBTFromNMSTagCompound(null, nmsListTag.getCompoundTagAt(i)));
                    break;
                default:
                    TerrainControl.log(LogMarker.INFO, "Cannot convert list subtype {} from it's NMS value", new Object[] {listType});
                    break;
            }
View Full Code Here

    @SuppressWarnings("unchecked")
    // ^ We know that NBTTagCompound.map is a Map<String, NBTBase>
    //   So it is safe to suppress this warning
    public static NamedBinaryTag getNBTFromNMSTagCompound(String name, NBTTagCompound nmsTag)
    {
        NamedBinaryTag compoundTag = new NamedBinaryTag(NamedBinaryTag.Type.TAG_Compound, name, new NamedBinaryTag[] {new NamedBinaryTag(NamedBinaryTag.Type.TAG_End, null, null)});

        // Get the child tags using some reflection magic
        Field mapField;
        Map<String, NBTBase> nmsChildTags = null;
        try
        {
            mapField = NBTTagCompound.class.getDeclaredField("map");
            mapField.setAccessible(true);
            nmsChildTags = (Map<String, NBTBase>) mapField.get(nmsTag);
        } catch (Exception e)
        {
            TerrainControl.printStackTrace(LogMarker.FATAL, e);
        }

        if (nmsChildTags == null)
        {
            // Cannot load the tag, return an empty tag
            return compoundTag;
        }

        // Add all child tags to the compound tag
        for (Entry<String, NBTBase> entry : nmsChildTags.entrySet())
        {
            NBTBase nmsChildTag = entry.getValue();
            NamedBinaryTag.Type type = NamedBinaryTag.Type.values()[nmsChildTag.getTypeId()];
            switch (type)
            {
                case TAG_End:
                    break;
                case TAG_Byte:
                case TAG_Short:
                case TAG_Int:
                case TAG_Long:
                case TAG_Float:
                case TAG_Double:
                case TAG_Byte_Array:
                case TAG_String:
                case TAG_Int_Array:
                    compoundTag.addTag(new NamedBinaryTag(type, entry.getKey(), getValueFromNms(nmsChildTag)));
                    break;
                case TAG_List:
                    NamedBinaryTag listChildTag = getNBTFromNMSTagList(entry.getKey(), (NBTTagList) nmsChildTag);
                    if (listChildTag != null)
                    {
                        compoundTag.addTag(listChildTag);
                    }
                    break;
View Full Code Here

            // Nothing to return
            return null;
        }

        NamedBinaryTag.Type listType = NamedBinaryTag.Type.values()[nmsListTag.d()];
        NamedBinaryTag listTag = new NamedBinaryTag(name, listType);

        // Add all child tags
        for (int i = 0; i < nmsListTag.size(); i++)
        {
            switch (listType)
            {
                case TAG_Int_Array:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.c(i)));
                    break;
                case TAG_Float:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.e(i)));
                    break;
                case TAG_Double:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.d(i)));
                    break;
                case TAG_String:
                    listTag.addTag(new NamedBinaryTag(listType, null, nmsListTag.getString(i)));
                    break;
                case TAG_Compound:
                    listTag.addTag(getNBTFromNMSTagCompound(null, nmsListTag.get(i)));
                    break;
                default:
                    TerrainControl.log(LogMarker.INFO, "Cannot convert list subtype {} from it's NMS value", new Object[] {listType});
                    break;
            }
View Full Code Here

TOP

Related Classes of com.khorn.terraincontrol.util.NamedBinaryTag

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.