本文整理汇总了Java中org.jnbt.NBTInputStream类的典型用法代码示例。如果您正苦于以下问题:Java NBTInputStream类的具体用法?Java NBTInputStream怎么用?Java NBTInputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NBTInputStream类属于org.jnbt包,在下文中一共展示了NBTInputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: clone
import org.jnbt.NBTInputStream; //导入依赖的package包/类
@Override
public ItemStack clone() {
try {
ItemStack s = (ItemStack) super.clone();
if (compoundTag == null) return s;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (NBTOutputStream stream = new NBTOutputStream(bytes)) {
stream.writeTag(compoundTag);
}
try (NBTInputStream stream = new NBTInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
s.setCompoundTag((CompoundTag) stream.readTag());
}
return s;
} catch (CloneNotSupportedException | IOException err) {
throw new AssertionError(err);
}
}
示例2: readItemStack
import org.jnbt.NBTInputStream; //导入依赖的package包/类
public ItemStack readItemStack() {
short blockId = this.readShort();
if (blockId == -1) {
return null;
}
ItemStack stack = new ItemStack(blockId, (byte) -1, (short) -1);
stack.setAmount(this.readByte());
stack.setDamage(this.readShort());
int index = this.readerIndex();
if (this.readByte() == 0) {
return stack;
}
this.readerIndex(index);
try (NBTInputStream nbtInStream = new NBTInputStream(new DataInputStream(new ByteBufInputStream(this.buffer)))) {
stack.setCompoundTag((CompoundTag) nbtInStream.readTag());
} catch (Exception e) {
e.printStackTrace();
}
return stack;
}
示例3: getTag
import org.jnbt.NBTInputStream; //导入依赖的package包/类
/**
* Reads a tag from the chunk data.
* @param chunkX: Relative chunk x-coordinate of the chunk.
* @param chunkZ: Relative chunk x-coordinate of the chunk.
* @param tagName
* @return: The tag of the chunk with the given name.
* @throws ChunkNotFoundException: If the chunk at the given coordinates was not found.
* @throws TagNotFoundException: If the tag with the given name was not found.
*/
private Tag getTag(int chunkX, int chunkZ, String tagName) throws ChunkNotFoundException, TagNotFoundException {
try {
DataInputStream chunkData = getChunkAsStream(chunkX, chunkZ);
NBTInputStream nbtIs = new NBTInputStream(chunkData);
CompoundTag rootTag = (CompoundTag) nbtIs.readTag();
Map<String, Tag> rootMap = rootTag.getValue();
CompoundTag levelTag = (CompoundTag) rootMap.get("Level");
Map<String, Tag> levelMap = levelTag.getValue();
Tag tag = levelMap.get(tagName);
if(tag == null) {
throw new TagNotFoundException();
}
return levelMap.get(tagName);
} catch(IOException e) {
e.printStackTrace();
} return null;
}
示例4: load
import org.jnbt.NBTInputStream; //导入依赖的package包/类
/**
* Load a custom object in schematic format from an input stream. The stream
* is closed before exiting the method.
*
* @param name The name of the object.
* @param stream The input stream from which to load the object.
* @return A new <code>Schematic</code> containing the contents of the
* specified stream.
* @throws IOException If an I/O error occurred while reading the stream.
*/
public static Schematic load(String name, InputStream stream) throws IOException {
InputStream in = new BufferedInputStream(stream);
//noinspection TryFinallyCanBeTryWithResources // Not possible due to assignment of 'in' inside block
try {
byte[] magicNumber = new byte[2];
in.mark(2);
in.read(magicNumber);
in.reset();
if ((magicNumber[0] == (byte) 0x1f) && (magicNumber[1] == (byte) 0x8b)) {
in = new GZIPInputStream(in);
}
NBTInputStream nbtIn = new NBTInputStream(in);
CompoundTag tag = (CompoundTag) nbtIn.readTag();
return new Schematic(name, tag, null);
} finally {
in.close();
}
}
示例5: loadChildren
import org.jnbt.NBTInputStream; //导入依赖的package包/类
@Override
protected Node[] loadChildren() {
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
in.mark(2);
Tag tag;
if ((in.read() == 0x1f) && (in.read() == 0x8b)) {
// Gzip signature
in.reset();
tag = new NBTInputStream(new GZIPInputStream(in)).readTag();
} else {
tag = new NBTInputStream(in).readTag();
}
return new Node[] {new TagNode(tag)};
} catch (IOException e) {
throw new RuntimeException("I/O error while reading level.dat file", e);
}
}
示例6: loadSchematic
import org.jnbt.NBTInputStream; //导入依赖的package包/类
public static Schematic loadSchematic(File file) throws IOException {
FileInputStream stream = new FileInputStream(file);
NBTInputStream nbtStream = new NBTInputStream(stream);
CompoundTag schematicTag = (CompoundTag)nbtStream.readTag();
nbtStream.close();
if(!schematicTag.getName().equals("Schematic")) {
throw new IllegalArgumentException("Tag \"Schematic\" does not exist or is not first");
}
Map<String, Tag> schematic = schematicTag.getValue();
if(!schematic.containsKey("Blocks")) {
throw new IllegalArgumentException("Schematic file is missing a \"Blocks\" tag");
}
short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
if(!materials.equals("Alpha")) {
throw new IllegalArgumentException("Schematic file is not an Alpha schematic");
}
byte[] blocks = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
return new Schematic(blocks, blockData, width, length, height);
}
示例7: SchematicWrapper
import org.jnbt.NBTInputStream; //导入依赖的package包/类
/**
* Creates a new wrapper for a schematic file.
*
* @param path <b>String</b> path to file
* @throws FileNotFoundException
* @throws IOException
*/
// Can't be null (checked by Guava)
@SuppressWarnings("null")
public SchematicWrapper(final String path) throws FileNotFoundException, IOException {
if (path.equals("")) {
throw new IllegalArgumentException("Path is empty");
}
try (final NBTInputStream inputStream = new NBTInputStream(new FileInputStream(path))) {
this.root = checkNotNull((CompoundTag) inputStream.readTag());
}
}
示例8: loadChunk
import org.jnbt.NBTInputStream; //导入依赖的package包/类
private EnderChunk loadChunk(int x, int z) {
long key = ((long) calculateRegionPos(x) << 32) ^ calculateRegionPos(z);
EnderChunk c;
DataInputStream in;
synchronized (regionFileCache) {
RegionFile region = regionFileCache.get(Long.valueOf(key));
if (region == null) {
region = new RegionFile(new File(regionDirectory, "r." + calculateRegionPos(x) + "." + calculateRegionPos(z) + ".mca"));
regionFileCache.put(key, region);
}
in = region.getChunkDataInputStream(calculateChunkPos(x), calculateChunkPos(z));
}
if (true || in == null) { // todo: remove true when the chunk loading finaly works
return createChunk(x, z);
}
try (NBTInputStream indata = new NBTInputStream(in)) {
CompoundTag tag = (CompoundTag) indata.readTag();
// read tag to chunk, http://minecraft.gamepedia.com/Chunk_format
c = new EnderChunk(world, x, z);
c.loadFromNBT(tag);
c.chunkState.set(EnderChunk.ChunkState.LOADED);
} catch (IOException ex) {
EnderLogger.warn("Error while loading chunk");
EnderLogger.exception(ex);
return null;
}
EnderLogger.debug("Load: " + c);
return c;
}
示例9: getChunk
import org.jnbt.NBTInputStream; //导入依赖的package包/类
/**
* Load a chunk. Returns <code>null</code> if the chunk is outside the
* WorldPainter world boundaries.
*
* @param x The X coordinate in the Minecraft coordinate system of the chunk
* to load.
* @param z The Z coordinate in the Minecraft coordinate system of the chunk
* to load.
* @return The specified chunk, or <code>null</code> if the coordinates are
* outside the WorldPainter world boundaries.
*/
@Override
public Chunk getChunk(int x, int z) {
// updateStatistics();
// if ((x < lowestX) || (x > highestX) || (z < lowestZ) || (z > highestZ)) {
// return null;
// }
// long start = System.currentTimeMillis();
try {
RegionFile regionFile = getRegionFile(new Point(x >> 5, z >> 5));
if (regionFile == null) {
return null;
}
InputStream chunkIn = regionFile.getChunkDataInputStream(x & 31, z & 31);
if (chunkIn != null) {
// chunksLoaded++;
try (NBTInputStream in = new NBTInputStream(chunkIn)) {
CompoundTag tag = (CompoundTag) in.readTag();
// timeSpentLoading += System.currentTimeMillis() - start;
boolean readOnly = honourReadOnlyChunks && dimension.getBitLayerValueAt(ReadOnly.INSTANCE, x << 4, z << 4);
return platform.equals(DefaultPlugin.JAVA_MCREGION) ? new ChunkImpl(tag, maxHeight, readOnly) : new ChunkImpl2(tag, maxHeight, readOnly);
}
} else {
// timeSpentLoading += System.currentTimeMillis() - start;
return null;
}
} catch (IOException e) {
throw new RuntimeException("I/O error loading chunk", e);
}
}
示例10: loadTileEntity
import org.jnbt.NBTInputStream; //导入依赖的package包/类
private static TileEntity loadTileEntity(File bo3File, String nbtFileName) throws IOException {
File nbtFile = new File(bo3File.getParentFile(), nbtFileName);
try (NBTInputStream in = new NBTInputStream(new GZIPInputStream(new FileInputStream(nbtFile)))) {
CompoundTag tag = (CompoundTag) in.readTag();
Map<String, Tag> map = tag.getValue();
if ((map.size() == 1) && (map.values().iterator().next() instanceof CompoundTag)) {
// If the root tag is a CompoundTag which only contains another
// CompoundTag, assume that the nested CompoundTag actually
// contains the data
return TileEntity.fromNBT((CompoundTag) tag.getValue().values().iterator().next());
} else {
return TileEntity.fromNBT(tag);
}
}
}
示例11: loadChildren
import org.jnbt.NBTInputStream; //导入依赖的package包/类
@Override
protected Node[] loadChildren() {
try {
try (NBTInputStream in = new NBTInputStream(regionFile.getChunkDataInputStream(x, z))) {
Tag tag = in.readTag();
return new Node[]{new TagNode(tag)};
}
} catch (IOException e) {
throw new RuntimeException("I/O error reading from region file", e);
}
}
示例12: loadUUIDSFirstTime
import org.jnbt.NBTInputStream; //导入依赖的package包/类
private static void loadUUIDSFirstTime(LocalUUIDCache plugin, File uuidFile, final File[] files)
{
if(!plugin.getDataFolder().exists())
plugin.getDataFolder().mkdir();
else if(uuidFile.exists())
uuidFile.delete();
else
{
try
{
uuidFile.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
int size = 0;
for(File file : files)
{
if(isUUID(file.getName()))
{
try
{
FileInputStream fStream = new FileInputStream(file);
NBTInputStream stream = new NBTInputStream(fStream);
Tag playerData = stream.readTag();
stream.close();
fStream.close();
final CompoundTag playerDataCompound = (CompoundTag)playerData;
if(playerDataCompound.getValue().get("bukkit") != null)
{
final CompoundTag bukkit = (CompoundTag) playerDataCompound.getValue().get("bukkit");
if(bukkit.getValue().get("lastKnownName") != null)
{
final Tag name = bukkit.getValue().get("lastKnownName");
final String lastKnownName = (String) name.getValue();
final UUID uuid = UUID.fromString(file.getName().substring(0, file.getName().indexOf(".")));
if(uuid != null)
ids.put(uuid, lastKnownName);
size += 1;
if((size % 1000) == 0)
System.out.println(size + " uuids loaded in!");
}
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
LocalUUIDCache.getInstance().logger.log(Level.INFO, ids.values().size() + " uuids loaded");
}
示例13: loadSchematic
import org.jnbt.NBTInputStream; //导入依赖的package包/类
@SuppressWarnings("resource")
public static Schematic loadSchematic(File file) throws IOException
{
FileInputStream stream = new FileInputStream(file);
NBTInputStream nbtStream = new NBTInputStream(stream);
CompoundTag schematicTag = (CompoundTag) nbtStream.readTag();
if (!schematicTag.getName().equals("Schematic")) {
throw new IllegalArgumentException("Tag \"Schematic\" does not exist or is not first");
}
Map<String, Tag> schematic = schematicTag.getValue();
if (!schematic.containsKey("Blocks")) {
throw new IllegalArgumentException("Schematic file is missing a \"Blocks\" tag");
}
short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
// Get blocks
byte[] blockId = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
byte[] addId = new byte[0];
short[] blocks = new short[blockId.length]; // Have to later combine IDs
// We support 4096 block IDs using the same method as vanilla Minecraft, where
// the highest 4 bits are stored in a separate byte array.
if (schematic.containsKey("AddBlocks")) {
addId = getChildTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
}
// Combine the AddBlocks data with the first 8-bit block ID
for (int index = 0; index < blockId.length; index++) {
if ((index >> 1) >= addId.length) { // No corresponding AddBlocks index
blocks[index] = (short) (blockId[index] & 0xFF);
} else {
if ((index & 1) == 0) {
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
} else {
blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
}
}
}
return new Schematic(file.getName().replace(".schematic", ""), blocks, blockData, width, length, height);
}
示例14: getLevelTag
import org.jnbt.NBTInputStream; //导入依赖的package包/类
private CompoundTag getLevelTag(NBTInputStream nis) throws IOException {
CompoundTag rootTag = (CompoundTag) nis.readTag();
return getCompoundTagFromTag(rootTag, LEVEL);
}
示例15: loadSchematic
import org.jnbt.NBTInputStream; //导入依赖的package包/类
public Schematic loadSchematic(File file){
try{
if(file.exists()){
NBTInputStream nbtStream = new NBTInputStream(new FileInputStream(file));
CompoundTag compound = (CompoundTag) nbtStream.readTag();
Map<String, Tag> tags = compound.getValue();
Short width = ((ShortTag) tags.get("Width")).getValue();
Short height = ((ShortTag) tags.get("Height")).getValue();
Short length = ((ShortTag) tags.get("Length")).getValue();
String materials = ((StringTag) tags.get("Materials")).getValue();
byte[] blocksId = ((ByteArrayTag) tags.get("Blocks")).getValue();
byte[] data = ((ByteArrayTag) tags.get("Data")).getValue();
//ive found this at the github of the makers of worldedit so credits to them, it looks I was doing it wrong by using byte[] at the first place
//and the datavalues are hex while a byte is not hex but a short can accept hex as 16, now the only thing to learn is the Tag AddBlocks and why it is used.
short[] blocks = new short[blocksId.length];
//need to look a bit over this.
byte[] addId = new byte[0];
if (tags.containsKey("AddBlocks")) {
addId = ((ByteArrayTag) tags.get("AddBlocks")).getValue();
}
// Combine the AddBlocks data with the first 8-bit block ID
for (int index = 0; index < blocksId.length; index++) {
if ((index >> 1) >= addId.length) { // No corresponding AddBlocks index
blocks[index] = (short) (blocksId[index] & 0xFF);
} else {
if ((index & 1) == 0) {
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blocksId[index] & 0xFF));
} else {
blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blocksId[index] & 0xFF));
}
}
}
//end of worldedit snippet.
nbtStream.close();
Schematic schematic = new Schematic(file.getName().replace(".schematic", ""), width, height, length, materials, blocks, data);
return schematic;
}
} catch(Exception e){
ManCo.log(LogType.SEVERE, "could not load this file: " + file.getName());
e.printStackTrace();
}
return null;
}