Skip to content

Commit

Permalink
NBT part 1: added a Spout lib called SimpleNBT
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Oct 14, 2014
1 parent 3447505 commit a7c47c5
Show file tree
Hide file tree
Showing 36 changed files with 4,227 additions and 0 deletions.
115 changes: 115 additions & 0 deletions src/main/java/org/spout/nbt/ByteArrayTag.java
@@ -0,0 +1,115 @@
/*
* This file is part of SimpleNBT.
*
* Copyright (c) 2011 Spout LLC <http://www.spout.org/>
* SimpleNBT is licensed under the Spout License Version 1.
*
* SimpleNBT is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* In addition, 180 days after any changes are published, you can use the
* software, incorporating those changes, under the terms of the MIT license,
* as described in the Spout License Version 1.
*
* SimpleNBT is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License,
* the MIT license and the Spout License Version 1 along with this program.
* If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public
* License and see <http://spout.in/licensev1> for the full license, including
* the MIT license.
*/
package org.spout.nbt;

import java.util.Arrays;

/**
* The {@code TAG_Byte_Array} tag.
*/
public final class ByteArrayTag extends Tag<byte[]>
{
/**
* The value.
*/
private final byte[] value;

/**
* Creates the tag.
*
* @param name The name.
* @param value The value.
*/
public ByteArrayTag(String name, byte[] value)
{
super(TagType.TAG_BYTE_ARRAY, name);
this.value = value;
}

@Override
public byte[] getValue()
{
return value;
}

@Override
public boolean equals(Object other)
{
if (!(other instanceof ByteArrayTag))
{
return false;
}

ByteArrayTag tag = (ByteArrayTag) other;
return Arrays.equals(value, tag.value) && getName().equals(tag.getName());
}

public ByteArrayTag clone()
{
byte[] clonedArray = cloneArray(value);

return new ByteArrayTag(getName(), clonedArray);
}

@Override
public String toString()
{
StringBuilder hex = new StringBuilder();
for (byte b : value)
{
String hexDigits = Integer.toHexString(b).toUpperCase();
if (hexDigits.length() == 1)
{
hex.append("0");
}
hex.append(hexDigits).append(" ");
}

String name = getName();
String append = "";
if (name != null && !name.equals(""))
{
append = "(\"" + this.getName() + "\")";
}
return "TAG_Byte_Array" + append + ": " + hex.toString();
}

private byte[] cloneArray(byte[] byteArray)
{
if (byteArray == null)
{
return null;
}
else
{
int length = byteArray.length;
byte[] newArray = new byte[length];
System.arraycopy(byteArray, 0, newArray, 0, length);
return newArray;
}
}
}
106 changes: 106 additions & 0 deletions src/main/java/org/spout/nbt/ByteTag.java
@@ -0,0 +1,106 @@
/*
* This file is part of SimpleNBT.
*
* Copyright (c) 2011 Spout LLC <http://www.spout.org/>
* SimpleNBT is licensed under the Spout License Version 1.
*
* SimpleNBT is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* In addition, 180 days after any changes are published, you can use the
* software, incorporating those changes, under the terms of the MIT license,
* as described in the Spout License Version 1.
*
* SimpleNBT is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License,
* the MIT license and the Spout License Version 1 along with this program.
* If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public
* License and see <http://spout.in/licensev1> for the full license, including
* the MIT license.
*/
package org.spout.nbt;

/**
* The {@code TAG_Byte} tag.
*/
public final class ByteTag extends Tag<Byte>
{
/**
* The value.
*/
private final byte value;

/**
* Creates the tag.<br> Boolean true is stored as 1 and boolean false is stored as 0.
*
* @param name The name.
* @param value The value.
*/
public ByteTag(String name, boolean value)
{
this(name, (byte) (value ? 1 : 0));
}

/**
* Creates the tag.
*
* @param name The name.
* @param value The value.
*/
public ByteTag(String name, byte value)
{
super(TagType.TAG_BYTE, name);
this.value = value;
}

public static Boolean getBooleanValue(Tag<?> t)
{
if (t == null)
{
return null;
}
try
{
ByteTag byteTag = (ByteTag) t;
return byteTag.getBooleanValue();
}
catch (ClassCastException e)
{
return null;
}
}

@Override
public Byte getValue()
{
return value;
}

public ByteTag clone()
{
return new ByteTag(getName(), value);
}

public boolean getBooleanValue()
{
return value != 0;
}

@Override
public String toString()
{
String name = getName();
String append = "";
if (name != null && !name.equals(""))
{
append = "(\"" + this.getName() + "\")";
}
return "TAG_Byte" + append + ": " + value;
}
}

0 comments on commit a7c47c5

Please sign in to comment.