// 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;
}