Update NMS access to 1.7.2.

This commit is contained in:
wizjany 2013-12-01 14:13:34 -05:00
parent 81a76c5d4f
commit 8574f7bb36
3 changed files with 105 additions and 82 deletions

View File

@ -125,7 +125,7 @@
<dependency> <!-- NMS blocks --> <dependency> <!-- NMS blocks -->
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId> <artifactId>craftbukkit</artifactId>
<version>1.6.4-R0.1-SNAPSHOT</version> <version>1.7.2-R0.1-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
<type>jar</type> <type>jar</type>
<optional>true</optional> <optional>true</optional>

View File

@ -18,6 +18,7 @@ package com.sk89q.worldedit.bukkit;
*/ */
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -25,23 +26,23 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.minecraft.server.v1_6_R3.NBTBase; import net.minecraft.server.v1_7_R1.NBTBase;
import net.minecraft.server.v1_6_R3.NBTTagByte; import net.minecraft.server.v1_7_R1.NBTTagByte;
import net.minecraft.server.v1_6_R3.NBTTagByteArray; import net.minecraft.server.v1_7_R1.NBTTagByteArray;
import net.minecraft.server.v1_6_R3.NBTTagCompound; import net.minecraft.server.v1_7_R1.NBTTagCompound;
import net.minecraft.server.v1_6_R3.NBTTagDouble; import net.minecraft.server.v1_7_R1.NBTTagDouble;
import net.minecraft.server.v1_6_R3.NBTTagEnd; import net.minecraft.server.v1_7_R1.NBTTagEnd;
import net.minecraft.server.v1_6_R3.NBTTagFloat; import net.minecraft.server.v1_7_R1.NBTTagFloat;
import net.minecraft.server.v1_6_R3.NBTTagInt; import net.minecraft.server.v1_7_R1.NBTTagInt;
import net.minecraft.server.v1_6_R3.NBTTagIntArray; import net.minecraft.server.v1_7_R1.NBTTagIntArray;
import net.minecraft.server.v1_6_R3.NBTTagList; import net.minecraft.server.v1_7_R1.NBTTagList;
import net.minecraft.server.v1_6_R3.NBTTagLong; import net.minecraft.server.v1_7_R1.NBTTagLong;
import net.minecraft.server.v1_6_R3.NBTTagShort; import net.minecraft.server.v1_7_R1.NBTTagShort;
import net.minecraft.server.v1_6_R3.NBTTagString; import net.minecraft.server.v1_7_R1.NBTTagString;
import net.minecraft.server.v1_6_R3.TileEntity; import net.minecraft.server.v1_7_R1.TileEntity;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.craftbukkit.v1_6_R3.CraftWorld; import org.bukkit.craftbukkit.v1_7_R1.CraftWorld;
import com.sk89q.jnbt.ByteArrayTag; import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.ByteTag; import com.sk89q.jnbt.ByteTag;
@ -81,7 +82,7 @@ public class DefaultNmsBlock extends NmsBlock {
static { static {
Field field; Field field;
try { try {
field = net.minecraft.server.v1_6_R3.Block.class.getDeclaredField("isTileEntity"); field = net.minecraft.server.v1_7_R1.Block.class.getDeclaredField("isTileEntity");
field.setAccessible(true); field.setAccessible(true);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
// logger.severe("Could not find NMS block tile entity field!"); // logger.severe("Could not find NMS block tile entity field!");
@ -133,9 +134,9 @@ public class DefaultNmsBlock extends NmsBlock {
return null; return null;
} }
nbtData.set("x", new NBTTagInt("x", pt.getBlockX())); nbtData.set("x", new NBTTagInt(pt.getBlockX()));
nbtData.set("y", new NBTTagInt("y", pt.getBlockY())); nbtData.set("y", new NBTTagInt(pt.getBlockY()));
nbtData.set("z", new NBTTagInt("z", pt.getBlockZ())); nbtData.set("z", new NBTTagInt(pt.getBlockZ()));
return nbtData; return nbtData;
} }
@ -253,7 +254,7 @@ public class DefaultNmsBlock extends NmsBlock {
// TileEntity te = craftWorld.getHandle().getTileEntity(x, y, z); // TileEntity te = craftWorld.getHandle().getTileEntity(x, y, z);
// craftWorld.getHandle().tileEntityList.remove(te); // craftWorld.getHandle().tileEntityList.remove(te);
boolean changed = craftWorld.getHandle().setTypeIdAndData(x, y, z, block.getId(), block.getData(), 0); boolean changed = craftWorld.getHandle().setTypeAndData(x, y, z, getNmsBlock(block.getId()), block.getData(), 0);
if (block instanceof BaseBlock) { if (block instanceof BaseBlock) {
world.copyToWorld(position, (BaseBlock) block); world.copyToWorld(position, (BaseBlock) block);
@ -262,14 +263,14 @@ public class DefaultNmsBlock extends NmsBlock {
changed = craftWorld.getHandle().setData(x, y, z, block.getData(), 0) || changed; changed = craftWorld.getHandle().setData(x, y, z, block.getData(), 0) || changed;
if (changed && notifyAdjacent) { if (changed && notifyAdjacent) {
craftWorld.getHandle().notify(x, y, z); craftWorld.getHandle().notify(x, y, z);
craftWorld.getHandle().update(x, y, z, block.getId()); craftWorld.getHandle().update(x, y, z, getNmsBlock(block.getId()));
} }
return changed; return changed;
} }
public static boolean hasTileEntity(int type) { public static boolean hasTileEntity(int type) {
net.minecraft.server.v1_6_R3.Block nmsBlock = getNmsBlock(type); net.minecraft.server.v1_7_R1.Block nmsBlock = getNmsBlock(type);
if (nmsBlock == null) { if (nmsBlock == null) {
return false; return false;
} }
@ -281,11 +282,8 @@ public class DefaultNmsBlock extends NmsBlock {
} }
} }
public static net.minecraft.server.v1_6_R3.Block getNmsBlock(int type) { public static net.minecraft.server.v1_7_R1.Block getNmsBlock(int type) {
if (type < 0 || type >= net.minecraft.server.v1_6_R3.Block.byId.length) { return net.minecraft.server.v1_7_R1.Block.e(type);
return null;
}
return net.minecraft.server.v1_6_R3.Block.byId[type];
} }
/** /**
@ -295,19 +293,33 @@ public class DefaultNmsBlock extends NmsBlock {
* @param foreign non-native NMS NBT structure * @param foreign non-native NMS NBT structure
* @return native WorldEdit NBT structure * @return native WorldEdit NBT structure
*/ */
@SuppressWarnings("unchecked")
private static Tag toNative(NBTBase foreign) { private static Tag toNative(NBTBase foreign) {
// temporary fix since mojang removed names from tags
// our nbt spec will need to be updated to theirs
return toNative(NBTBase.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 static Tag toNative(String name, NBTBase foreign) {
if (foreign == null) { if (foreign == null) {
return null; return null;
} }
if (foreign instanceof NBTTagCompound) { if (foreign instanceof NBTTagCompound) {
Map<String, Tag> values = new HashMap<String, Tag>(); Map<String, Tag> values = new HashMap<String, Tag>();
Collection<Object> foreignValues = null; Collection<Object> foreignKeys = null;
if (compoundMapField == null) { if (compoundMapField == null) {
try { try {
// Method name may change! // Method name may change!
foreignValues = ((NBTTagCompound) foreign).c(); foreignKeys = ((NBTTagCompound) foreign).c();
} catch (Throwable t) { } catch (Throwable t) {
try { try {
logger.warning("WorldEdit: Couldn't get NBTTagCompound.c(), " + logger.warning("WorldEdit: Couldn't get NBTTagCompound.c(), " +
@ -326,51 +338,49 @@ public class DefaultNmsBlock extends NmsBlock {
if (compoundMapField != null) { if (compoundMapField != null) {
try { try {
foreignValues = ((HashMap<Object, Object>) compoundMapField.get(foreign)).values(); foreignKeys = ((HashMap<Object, Object>) compoundMapField.get(foreign)).keySet();
} catch (Throwable e) { } catch (Throwable e) {
// Can't do much beyond this // Can't do much beyond this
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
for (Object obj : foreignValues) { for (Object obj : foreignKeys) {
NBTBase base = (NBTBase) obj; String key = (String) obj;
values.put(base.getName(), toNative(base)); NBTBase base = (NBTBase) ((NBTTagCompound) foreign).get(key);
values.put(key, toNative(key, base));
} }
return new CompoundTag(foreign.getName(), values); return new CompoundTag(name, values);
} else if (foreign instanceof NBTTagByte) { } else if (foreign instanceof NBTTagByte) {
return new ByteTag(foreign.getName(), ((NBTTagByte) foreign).data); return new ByteTag(name, ((NBTTagByte) foreign).f()); // getByte
} else if (foreign instanceof NBTTagByteArray) { } else if (foreign instanceof NBTTagByteArray) {
return new ByteArrayTag(foreign.getName(), return new ByteArrayTag(name,
((NBTTagByteArray) foreign).data); ((NBTTagByteArray) foreign).c()); // data
} else if (foreign instanceof NBTTagDouble) { } else if (foreign instanceof NBTTagDouble) {
return new DoubleTag(foreign.getName(), return new DoubleTag(name,
((NBTTagDouble) foreign).data); ((NBTTagDouble) foreign).g()); // getDouble
} else if (foreign instanceof NBTTagFloat) { } else if (foreign instanceof NBTTagFloat) {
return new FloatTag(foreign.getName(), ((NBTTagFloat) foreign).data); return new FloatTag(name, ((NBTTagFloat) foreign).h()); // getFloat
} else if (foreign instanceof NBTTagInt) { } else if (foreign instanceof NBTTagInt) {
return new IntTag(foreign.getName(), ((NBTTagInt) foreign).data); return new IntTag(name, ((NBTTagInt) foreign).d()); // getInt
} else if (foreign instanceof NBTTagIntArray) { } else if (foreign instanceof NBTTagIntArray) {
return new IntArrayTag(foreign.getName(), return new IntArrayTag(name,
((NBTTagIntArray) foreign).data); ((NBTTagIntArray) foreign).c()); // data
} else if (foreign instanceof NBTTagList) { } else if (foreign instanceof NBTTagList) {
List<Tag> values = new ArrayList<Tag>(); try {
NBTTagList foreignList = (NBTTagList) foreign; return transmorgifyList(name, (NBTTagList) foreign);
int type = NBTConstants.TYPE_BYTE; } catch (NoSuchFieldException e) {
for (int i = 0; i < foreignList.size(); i++) { } catch (SecurityException e) {
NBTBase foreignTag = foreignList.get(i); } catch (IllegalArgumentException e) {
values.add(toNative(foreignTag)); } catch (IllegalAccessException e) {}
type = foreignTag.getTypeId(); return new ListTag(name, ByteTag.class, new ArrayList<ByteTag>());
}
Class<? extends Tag> cls = NBTConstants.getClassFromType(type);
return new ListTag(foreign.getName(), cls, values);
} else if (foreign instanceof NBTTagLong) { } else if (foreign instanceof NBTTagLong) {
return new LongTag(foreign.getName(), ((NBTTagLong) foreign).data); return new LongTag(name, ((NBTTagLong) foreign).c()); // getLong
} else if (foreign instanceof NBTTagShort) { } else if (foreign instanceof NBTTagShort) {
return new ShortTag(foreign.getName(), ((NBTTagShort) foreign).data); return new ShortTag(name, ((NBTTagShort) foreign).e()); // getShort
} else if (foreign instanceof NBTTagString) { } else if (foreign instanceof NBTTagString) {
return new StringTag(foreign.getName(), return new StringTag(name,
((NBTTagString) foreign).data); ((NBTTagString) foreign).a_()); // data
} else if (foreign instanceof NBTTagEnd) { } else if (foreign instanceof NBTTagEnd) {
return new EndTag(); return new EndTag();
} else { } else {
@ -379,6 +389,22 @@ public class DefaultNmsBlock extends NmsBlock {
} }
} }
private static ListTag transmorgifyList(String name, NBTTagList foreign)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
List<Tag> values = new ArrayList<Tag>();
int type = foreign.d();
Field listField = NBTTagList.class.getDeclaredField("list");
listField.setAccessible(true);
List foreignList;
foreignList = (List) listField.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
}
Class<? extends Tag> cls = NBTConstants.getClassFromType(type);
return new ListTag(name, cls, values);
}
/** /**
* Converts a WorldEdit-native NBT structure to a NMS structure. * Converts a WorldEdit-native NBT structure to a NMS structure.
* *
@ -390,48 +416,45 @@ public class DefaultNmsBlock extends NmsBlock {
return null; return null;
} }
if (foreign instanceof CompoundTag) { if (foreign instanceof CompoundTag) {
NBTTagCompound tag = new NBTTagCompound(foreign.getName()); NBTTagCompound tag = new NBTTagCompound();
for (Map.Entry<String, Tag> entry : ((CompoundTag) foreign) for (Map.Entry<String, Tag> entry : ((CompoundTag) foreign)
.getValue().entrySet()) { .getValue().entrySet()) {
tag.set(entry.getKey(), fromNative(entry.getValue())); tag.set(entry.getKey(), fromNative(entry.getValue()));
} }
return tag; return tag;
} else if (foreign instanceof ByteTag) { } else if (foreign instanceof ByteTag) {
return new NBTTagByte(foreign.getName(), return new NBTTagByte(((ByteTag) foreign).getValue());
((ByteTag) foreign).getValue());
} else if (foreign instanceof ByteArrayTag) { } else if (foreign instanceof ByteArrayTag) {
return new NBTTagByteArray(foreign.getName(), return new NBTTagByteArray(((ByteArrayTag) foreign).getValue());
((ByteArrayTag) foreign).getValue());
} else if (foreign instanceof DoubleTag) { } else if (foreign instanceof DoubleTag) {
return new NBTTagDouble(foreign.getName(), return new NBTTagDouble(((DoubleTag) foreign).getValue());
((DoubleTag) foreign).getValue());
} else if (foreign instanceof FloatTag) { } else if (foreign instanceof FloatTag) {
return new NBTTagFloat(foreign.getName(), return new NBTTagFloat(((FloatTag) foreign).getValue());
((FloatTag) foreign).getValue());
} else if (foreign instanceof IntTag) { } else if (foreign instanceof IntTag) {
return new NBTTagInt(foreign.getName(), return new NBTTagInt(((IntTag) foreign).getValue());
((IntTag) foreign).getValue());
} else if (foreign instanceof IntArrayTag) { } else if (foreign instanceof IntArrayTag) {
return new NBTTagIntArray(foreign.getName(), return new NBTTagIntArray(((IntArrayTag) foreign).getValue());
((IntArrayTag) foreign).getValue());
} else if (foreign instanceof ListTag) { } else if (foreign instanceof ListTag) {
NBTTagList tag = new NBTTagList(foreign.getName()); NBTTagList tag = new NBTTagList();
ListTag foreignList = (ListTag) foreign; ListTag foreignList = (ListTag) foreign;
for (Tag t : foreignList.getValue()) { for (Tag t : foreignList.getValue()) {
tag.add(fromNative(t)); tag.add(fromNative(t));
} }
return tag; return tag;
} else if (foreign instanceof LongTag) { } else if (foreign instanceof LongTag) {
return new NBTTagLong(foreign.getName(), return new NBTTagLong(((LongTag) foreign).getValue());
((LongTag) foreign).getValue());
} else if (foreign instanceof ShortTag) { } else if (foreign instanceof ShortTag) {
return new NBTTagShort(foreign.getName(), return new NBTTagShort(((ShortTag) foreign).getValue());
((ShortTag) foreign).getValue());
} else if (foreign instanceof StringTag) { } else if (foreign instanceof StringTag) {
return new NBTTagString(foreign.getName(), return new NBTTagString(((StringTag) foreign).getValue());
((StringTag) foreign).getValue());
} else if (foreign instanceof EndTag) { } else if (foreign instanceof EndTag) {
return new NBTTagEnd(); try {
Method tagMaker = NBTBase.class.getDeclaredMethod("createTag", byte.class);
tagMaker.setAccessible(true);
return (NBTBase) tagMaker.invoke(null, (byte) 0);
} catch (Exception e) {
return null;
}
} else { } else {
throw new IllegalArgumentException("Don't know how to make NMS " throw new IllegalArgumentException("Don't know how to make NMS "
+ foreign.getClass().getCanonicalName()); + foreign.getClass().getCanonicalName());
@ -439,7 +462,7 @@ public class DefaultNmsBlock extends NmsBlock {
} }
public static boolean isValidBlockType(int type) throws NoClassDefFoundError { public static boolean isValidBlockType(int type) throws NoClassDefFoundError {
return type == 0 || (type >= 1 && type < net.minecraft.server.v1_6_R3.Block.byId.length return type == 0 || (type >= 1 && net.minecraft.server.v1_7_R1.Block.e(type) != null);
&& net.minecraft.server.v1_6_R3.Block.byId[type] != null);
} }
} }

Binary file not shown.