Permalink
Browse files

Update for latest changes to the NBT library in WorldEdit.

  • Loading branch information...
sk89q committed Nov 15, 2014
1 parent b31cbf3 commit 94f1839a35ef72e25be72c13b8de958c136da08c
View
@@ -33,7 +33,7 @@ subprojects {
}
dependencies {
compile "com.sk89q:worldedit:6.0.0-SNAPSHOT"
compile "com.sk89q.worldedit:worldedit-bukkit:6.0.0-SNAPSHOT"
testCompile "junit:junit:4.11"
}
@@ -256,7 +256,7 @@ public boolean setBlock(Location location, BaseBlock block, boolean notifyAndLig
// though we do not do this on the Forge version
TileEntity tileEntity = craftWorld.getHandle().getTileEntity(x, y, z);
if (tileEntity != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
NBTTagCompound tag = (NBTTagCompound) fromNative("", nativeTag);
tag.set("x", new NBTTagInt("x", x));
tag.set("y", new NBTTagInt("y", y));
tag.set("z", new NBTTagInt("z", z));
@@ -308,7 +308,7 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
if (createdEntity != null) {
CompoundTag nativeTag = state.getNbtData();
if (nativeTag != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
NBTTagCompound tag = (NBTTagCompound) fromNative(state.getTypeId(), nativeTag);
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
@@ -401,35 +401,32 @@ private Tag toNative(String name, NBTBase foreign) {
NBTBase base = ((NBTTagCompound) foreign).get(key);
values.put(key, toNative(key, base));
}
return new CompoundTag(name, values);
return new CompoundTag(values);
} else if (foreign instanceof NBTTagByte) {
return new ByteTag(name, ((NBTTagByte) foreign).data); // getByte
return new ByteTag(((NBTTagByte) foreign).data); // getByte
} else if (foreign instanceof NBTTagByteArray) {
return new ByteArrayTag(name,
((NBTTagByteArray) foreign).data); // data
return new ByteArrayTag(((NBTTagByteArray) foreign).data); // data
} else if (foreign instanceof NBTTagDouble) {
return new DoubleTag(name,
((NBTTagDouble) foreign).data); // getDouble
return new DoubleTag(((NBTTagDouble) foreign).data); // getDouble
} else if (foreign instanceof NBTTagFloat) {
return new FloatTag(name, ((NBTTagFloat) foreign).data); // getFloat
return new FloatTag(((NBTTagFloat) foreign).data); // getFloat
} else if (foreign instanceof NBTTagInt) {
return new IntTag(name, ((NBTTagInt) foreign).data); // getInt
return new IntTag(((NBTTagInt) foreign).data); // getInt
} else if (foreign instanceof NBTTagIntArray) {
return new IntArrayTag(name,
((NBTTagIntArray) foreign).data); // data
return new IntArrayTag(((NBTTagIntArray) foreign).data); // data
} else if (foreign instanceof NBTTagList) {
try {
return toNative(name, (NBTTagList) foreign);
return toNative((NBTTagList) foreign);
} catch (Throwable e) {
logger.log(Level.WARNING, "Failed to convert NBTTagList", e);
return new ListTag(name, ByteTag.class, new ArrayList<ByteTag>());
return new ListTag(ByteTag.class, new ArrayList<ByteTag>());
}
} else if (foreign instanceof NBTTagLong) {
return new LongTag(name, ((NBTTagLong) foreign).data); // getLong
return new LongTag(((NBTTagLong) foreign).data); // getLong
} else if (foreign instanceof NBTTagShort) {
return new ShortTag(name, ((NBTTagShort) foreign).data); // getShort
return new ShortTag(((NBTTagShort) foreign).data); // getShort
} else if (foreign instanceof NBTTagString) {
return new StringTag(name, ((NBTTagString) foreign).data); // data
return new StringTag(((NBTTagString) foreign).data); // data
} else if (foreign instanceof NBTTagEnd) {
return new EndTag();
} else {
@@ -440,15 +437,14 @@ private Tag toNative(String name, NBTBase foreign) {
/**
* Convert a foreign NBT list tag into a native WorldEdit one.
*
* @param name the field name
* @param foreign the foreign tag
* @return the converted tag
* @throws NoSuchFieldException on error
* @throws SecurityException on error
* @throws IllegalArgumentException on error
* @throws IllegalAccessException on error
*/
private ListTag toNative(String name, NBTTagList foreign) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
private ListTag toNative(NBTTagList foreign) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
List<Tag> values = new ArrayList<Tag>();
List foreignList;
@@ -461,7 +457,7 @@ private ListTag toNative(String name, NBTTagList foreign) throws NoSuchFieldExce
cls = tag.getClass();
}
return new ListTag(name, cls, values);
return new ListTag(cls, values);
}
/**
@@ -470,40 +466,40 @@ private ListTag toNative(String name, NBTTagList foreign) throws NoSuchFieldExce
* @param foreign structure to convert
* @return non-native structure
*/
private NBTBase fromNative(Tag foreign) {
private NBTBase fromNative(String name, Tag foreign) {
if (foreign == null) {
return null;
}
if (foreign instanceof CompoundTag) {
NBTTagCompound tag = new NBTTagCompound();
for (Map.Entry<String, Tag> entry : ((CompoundTag) foreign)
.getValue().entrySet()) {
tag.set(entry.getKey(), fromNative(entry.getValue()));
tag.set(entry.getKey(), fromNative(entry.getKey(), entry.getValue()));
}
return tag;
} else if (foreign instanceof ByteTag) {
return new NBTTagByte(foreign.getName(), ((ByteTag) foreign).getValue());
return new NBTTagByte(name, ((ByteTag) foreign).getValue());
} else if (foreign instanceof ByteArrayTag) {
return new NBTTagByteArray(foreign.getName(), ((ByteArrayTag) foreign).getValue());
return new NBTTagByteArray(name, ((ByteArrayTag) foreign).getValue());
} else if (foreign instanceof DoubleTag) {
return new NBTTagDouble(foreign.getName(), ((DoubleTag) foreign).getValue());
return new NBTTagDouble(name, ((DoubleTag) foreign).getValue());
} else if (foreign instanceof FloatTag) {
return new NBTTagFloat(foreign.getName(), ((FloatTag) foreign).getValue());
return new NBTTagFloat(name, ((FloatTag) foreign).getValue());
} else if (foreign instanceof IntTag) {
return new NBTTagInt(foreign.getName(), ((IntTag) foreign).getValue());
return new NBTTagInt(name, ((IntTag) foreign).getValue());
} else if (foreign instanceof IntArrayTag) {
return new NBTTagIntArray(foreign.getName(), ((IntArrayTag) foreign).getValue());
return new NBTTagIntArray(name, ((IntArrayTag) foreign).getValue());
} else if (foreign instanceof ListTag) {
NBTTagList tag = new NBTTagList();
ListTag foreignList = (ListTag) foreign;
for (Tag t : foreignList.getValue()) {
tag.add(fromNative(t));
tag.add(fromNative("", t));
}
return tag;
} else if (foreign instanceof LongTag) {
return new NBTTagLong(foreign.getName(), ((LongTag) foreign).getValue());
return new NBTTagLong(name, ((LongTag) foreign).getValue());
} else if (foreign instanceof ShortTag) {
return new NBTTagShort(foreign.getName(), ((ShortTag) foreign).getValue());
return new NBTTagShort(name, ((ShortTag) foreign).getValue());
} else if (foreign instanceof StringTag) {
return new NBTTagString(((StringTag) foreign).getValue());
} else if (foreign instanceof EndTag) {
@@ -317,71 +317,15 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
}
}
/**
* Get the tag name, which is used for a workaround for WorldEdit's
* NBT library, JNBT.
*
* @param i the number representing the tag type
* @return a string to represent the tag type
*/
private static String getTagName(int i) {
// seriously these two methods are hacky - our jnbt spec needs updating
// copied from NMS 1.7.5- code, since it was removed in 1.7.8
switch (i) {
case 0:
return "TAG_End";
case 1:
return "TAG_Byte";
case 2:
return "TAG_Short";
case 3:
return "TAG_Int";
case 4:
return "TAG_Long";
case 5:
return "TAG_Float";
case 6:
return "TAG_Double";
case 7:
return "TAG_Byte_Array";
case 8:
return "TAG_String";
case 9:
return "TAG_List";
case 10:
return "TAG_Compound";
case 11:
return "TAG_Int_Array";
case 99:
return "Any Numeric Tag";
default:
return "UNKNOWN";
}
}
/**
* Converts from a non-native NMS NBT structure to a native WorldEdit NBT
* structure.
*
* @param foreign non-native NMS NBT structure
* @return native WorldEdit NBT structure
*/
private Tag toNative(NBTBase foreign) {
// temporary fix since Mojang removed names from tags
// our nbt spec will need to be updated to theirs
return toNative(getTagName(foreign.getTypeId()), foreign);
}
/**
* Converts from a non-native NMS NBT structure to a native WorldEdit NBT
* structure.
*
* @param foreign non-native NMS NBT structure
* @param name name for the tag, if it has one
* @return native WorldEdit NBT structure
*/
@SuppressWarnings("unchecked")
private Tag toNative(String name, NBTBase foreign) {
private Tag toNative(NBTBase foreign) {
if (foreign == null) {
return null;
}
@@ -392,37 +336,34 @@ private Tag toNative(String name, NBTBase foreign) {
for (Object obj : foreignKeys) {
String key = (String) obj;
NBTBase base = ((NBTTagCompound) foreign).get(key);
values.put(key, toNative(key, base));
values.put(key, toNative(base));
}
return new CompoundTag(name, values);
return new CompoundTag(values);
} else if (foreign instanceof NBTTagByte) {
return new ByteTag(name, ((NBTTagByte) foreign).f()); // getByte
return new ByteTag(((NBTTagByte) foreign).f()); // getByte
} else if (foreign instanceof NBTTagByteArray) {
return new ByteArrayTag(name,
((NBTTagByteArray) foreign).c()); // data
return new ByteArrayTag(((NBTTagByteArray) foreign).c()); // data
} else if (foreign instanceof NBTTagDouble) {
return new DoubleTag(name,
((NBTTagDouble) foreign).g()); // getDouble
return new DoubleTag(((NBTTagDouble) foreign).g()); // getDouble
} else if (foreign instanceof NBTTagFloat) {
return new FloatTag(name, ((NBTTagFloat) foreign).h()); // getFloat
return new FloatTag(((NBTTagFloat) foreign).h()); // getFloat
} else if (foreign instanceof NBTTagInt) {
return new IntTag(name, ((NBTTagInt) foreign).d()); // getInt
return new IntTag(((NBTTagInt) foreign).d()); // getInt
} else if (foreign instanceof NBTTagIntArray) {
return new IntArrayTag(name,
((NBTTagIntArray) foreign).c()); // data
return new IntArrayTag(((NBTTagIntArray) foreign).c()); // data
} else if (foreign instanceof NBTTagList) {
try {
return toNative(name, (NBTTagList) foreign);
return toNativeList((NBTTagList) foreign);
} catch (Throwable e) {
logger.log(Level.WARNING, "Failed to convert NBTTagList", e);
return new ListTag(name, ByteTag.class, new ArrayList<ByteTag>());
return new ListTag(ByteTag.class, new ArrayList<ByteTag>());
}
} else if (foreign instanceof NBTTagLong) {
return new LongTag(name, ((NBTTagLong) foreign).c()); // getLong
return new LongTag(((NBTTagLong) foreign).c()); // getLong
} else if (foreign instanceof NBTTagShort) {
return new ShortTag(name, ((NBTTagShort) foreign).e()); // getShort
return new ShortTag(((NBTTagShort) foreign).e()); // getShort
} else if (foreign instanceof NBTTagString) {
return new StringTag(name, ((NBTTagString) foreign).a_()); // data
return new StringTag(((NBTTagString) foreign).a_()); // data
} else if (foreign instanceof NBTTagEnd) {
return new EndTag();
} else {
@@ -433,27 +374,26 @@ private Tag toNative(String name, NBTBase foreign) {
/**
* Convert a foreign NBT list tag into a native WorldEdit one.
*
* @param name the field name
* @param foreign the foreign tag
* @return the converted tag
* @throws NoSuchFieldException on error
* @throws SecurityException on error
* @throws IllegalArgumentException on error
* @throws IllegalAccessException on error
*/
private ListTag toNative(String name, NBTTagList foreign) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
private ListTag toNativeList(NBTTagList foreign) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
List<Tag> values = new ArrayList<Tag>();
int type = foreign.d();
List foreignList;
foreignList = (List) nbtListTagListField.get(foreign);
for (int i = 0; i < foreign.size(); i++) {
NBTBase element = (NBTBase) foreignList.get(i);
values.add(toNative(null, element)); // List elements shouldn't have names
values.add(toNative(element)); // List elements shouldn't have names
}
Class<? extends Tag> cls = NBTConstants.getClassFromType(type);
return new ListTag(name, cls, values);
return new ListTag(cls, values);
}
/**
@@ -509,4 +449,4 @@ private NBTBase fromNative(Tag foreign) {
}
}
}
}
Oops, something went wrong.

0 comments on commit 94f1839

Please sign in to comment.