Skip to content
This repository has been archived by the owner on Jun 26, 2019. It is now read-only.

Support the long array tag #10

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/main/java/com/flowpowered/nbt/LongArrayTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* This file is part of Flow NBT, licensed under the MIT License (MIT).
*
* Copyright (c) 2011 Flow Powered <https://flowpowered.com/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.flowpowered.nbt;

import java.util.Arrays;

public class LongArrayTag extends Tag<long[]> {
/**
* The value.
*/
private final long[] value;

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

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

@Override
public String toString() {
StringBuilder hex = new StringBuilder();
for (long s : value) {
String hexDigits = Long.toHexString(s).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_Long_Array" + append + ": " + hex.toString();
}

public LongArrayTag clone() {
long[] clonedArray = cloneArray(value);

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

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

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

private long[] cloneArray(long[] longArray) {
if (longArray == null) {
return null;
} else {
int length = longArray.length;
byte[] newArray = new byte[length];
System.arraycopy(longArray, 0, newArray, 0, length);
return longArray;
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/flowpowered/nbt/NBTConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class NBTConstants {
TYPE_LIST = TagType.TAG_LIST.getId(),
TYPE_COMPOUND = TagType.TAG_COMPOUND.getId(),
TYPE_INT_ARRAY = TagType.TAG_INT_ARRAY.getId(),
TYPE_LONG_ARRAY = TagType.TAG_LONG_ARRAY.getId(),
TYPE_SHORT_ARRAY = TagType.TAG_SHORT_ARRAY.getId();

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/flowpowered/nbt/TagType.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum TagType {
// Java generics, y u so suck
TAG_COMPOUND(CompoundTag.class, "TAG_Compound", 10),
TAG_INT_ARRAY(IntArrayTag.class, "TAG_Int_Array", 11),
TAG_LONG_ARRAY(LongArrayTag.class, "TAG_Long_Array", 12),
TAG_SHORT_ARRAY(ShortArrayTag.class, "TAG_Short_Array", 100),;
private static final Map<Class<? extends Tag<?>>, TagType> BY_CLASS = new HashMap<Class<? extends Tag<?>>, TagType>();
private static final Map<String, TagType> BY_NAME = new HashMap<String, TagType>();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/flowpowered/nbt/stream/NBTInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.flowpowered.nbt.IntArrayTag;
import com.flowpowered.nbt.IntTag;
import com.flowpowered.nbt.ListTag;
import com.flowpowered.nbt.LongArrayTag;
import com.flowpowered.nbt.LongTag;
import com.flowpowered.nbt.NBTConstants;
import com.flowpowered.nbt.ShortArrayTag;
Expand Down Expand Up @@ -215,6 +216,14 @@ private Tag readTagPayload(TagType type, String name, int depth) throws IOExcept
}
return new IntArrayTag(name, ints);

case TAG_LONG_ARRAY:
length = is.readInt();
long[] longs = new long[length];
for (int i = 0; i < length; i++) {
longs[i] = is.readLong();
}
return new LongArrayTag(name, longs);

case TAG_SHORT_ARRAY:
length = is.readInt();
short[] shorts = new short[length];
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/flowpowered/nbt/stream/NBTOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.flowpowered.nbt.IntArrayTag;
import com.flowpowered.nbt.IntTag;
import com.flowpowered.nbt.ListTag;
import com.flowpowered.nbt.LongArrayTag;
import com.flowpowered.nbt.LongTag;
import com.flowpowered.nbt.NBTConstants;
import com.flowpowered.nbt.ShortArrayTag;
Expand Down Expand Up @@ -167,6 +168,10 @@ private void writeTagPayload(Tag<?> tag) throws IOException {
writeIntArrayTagPayload((IntArrayTag) tag);
break;

case TAG_LONG_ARRAY:
writeLongArrayTagPayload((LongArrayTag) tag);
break;

case TAG_SHORT_ARRAY:
writeShortArrayTagPayload((ShortArrayTag) tag);
break;
Expand Down Expand Up @@ -306,6 +311,20 @@ private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException {
}
}

/**
* Writes a {@code TAG_Long_Array} tag.
*
* @param tag The tag.
* @throws java.io.IOException if an I/O error occurs.
*/
private void writeLongArrayTagPayload(LongArrayTag tag) throws IOException {
long[] longs = tag.getValue();
os.writeInt(longs.length);
for (int i = 0; i < longs.length; i++) {
os.writeLong(longs[i]);
}
}

/**
* Writes a {@code TAG_Short_Array} tag.
*
Expand Down