Fixed NMS block set to copy tile entity data even if the original block did not change its id or data

This commit is contained in:
zml2008 2012-10-18 19:38:56 -07:00
parent 2221eae36a
commit 877f14f242

View File

@ -5,11 +5,11 @@
* Copyright (c) the WorldEdit team and contributors * Copyright (c) the WorldEdit team and contributors
* *
* This program is free software: you can redistribute it and/or modify it under the * This program 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 * terms of the GNU Lesser General Public License as published by the Free Software
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * This program 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 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License along with * You should have received a copy of the GNU Lesser General Public License along with
@ -80,7 +80,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
/** /**
* Create a new instance with a given type ID, data value, and previous * Create a new instance with a given type ID, data value, and previous
* {@link TileEntityBlock}-implementing object. * {@link TileEntityBlock}-implementing object.
* *
* @param type block type ID * @param type block type ID
* @param data data value * @param data data value
* @param tileEntityBlock tile entity block * @param tileEntityBlock tile entity block
@ -94,7 +94,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
/** /**
* Create a new instance with a given type ID, data value, and raw * Create a new instance with a given type ID, data value, and raw
* {@link NBTTagCompound} copy. * {@link NBTTagCompound} copy.
* *
* @param type block type ID * @param type block type ID
* @param data data value * @param data data value
* @param nbtData raw NBT data * @param nbtData raw NBT data
@ -107,7 +107,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
/** /**
* Build a {@link NBTTagCompound} that has valid coordinates. * Build a {@link NBTTagCompound} that has valid coordinates.
* *
* @param pt coordinates to set * @param pt coordinates to set
* @return the tag compound * @return the tag compound
*/ */
@ -115,14 +115,14 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
if (nbtData == null) { if (nbtData == null) {
return null; return null;
} }
nbtData.set("x", new NBTTagInt("x", pt.getBlockX())); nbtData.set("x", new NBTTagInt("x", pt.getBlockX()));
nbtData.set("y", new NBTTagInt("y", pt.getBlockY())); nbtData.set("y", new NBTTagInt("y", pt.getBlockY()));
nbtData.set("z", new NBTTagInt("z", pt.getBlockZ())); nbtData.set("z", new NBTTagInt("z", pt.getBlockZ()));
return nbtData; return nbtData;
} }
@Override @Override
public boolean hasNbtData() { public boolean hasNbtData() {
return nbtData != null; return nbtData != null;
@ -153,10 +153,10 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
} }
this.nbtData = (NBTTagCompound) fromNative(tag); this.nbtData = (NBTTagCompound) fromNative(tag);
} }
/** /**
* Build an instance from the given information. * Build an instance from the given information.
* *
* @param world world to get the block from * @param world world to get the block from
* @param position position to get the block at * @param position position to get the block at
* @param type type ID of block * @param type type ID of block
@ -166,19 +166,19 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
public static NmsBlock get(World world, Vector position, int type, int data) { public static NmsBlock get(World world, Vector position, int type, int data) {
TileEntity te = ((CraftWorld) world).getHandle().getTileEntity( TileEntity te = ((CraftWorld) world).getHandle().getTileEntity(
position.getBlockX(), position.getBlockY(), position.getBlockZ()); position.getBlockX(), position.getBlockY(), position.getBlockZ());
if (te != null) { if (te != null) {
NBTTagCompound tag = new NBTTagCompound(); NBTTagCompound tag = new NBTTagCompound();
te.b(tag); // Load data te.b(tag); // Load data
return new NmsBlock(type, data, tag); return new NmsBlock(type, data, tag);
} }
return null; return null;
} }
/** /**
* Set an instance or a {@link TileEntityBlock} to the given position. * Set an instance or a {@link TileEntityBlock} to the given position.
* *
* @param world world to set the block in * @param world world to set the block in
* @param position position to set the block at * @param position position to set the block at
* @param block the block to set * @param block the block to set
@ -186,7 +186,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
*/ */
public static boolean set(World world, Vector position, BaseBlock block) { public static boolean set(World world, Vector position, BaseBlock block) {
NBTTagCompound data = null; NBTTagCompound data = null;
if (block instanceof NmsBlock) { if (block instanceof NmsBlock) {
NmsBlock nmsProxyBlock = (NmsBlock) block; NmsBlock nmsProxyBlock = (NmsBlock) block;
data = nmsProxyBlock.getNmsData(position); data = nmsProxyBlock.getNmsData(position);
@ -195,7 +195,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
block.getType(), block.getData(), block); block.getType(), block.getData(), block);
data = nmsProxyBlock.getNmsData(position); data = nmsProxyBlock.getNmsData(position);
} }
if (data != null) { if (data != null) {
TileEntity te = ((CraftWorld) world).getHandle().getTileEntity( TileEntity te = ((CraftWorld) world).getHandle().getTileEntity(
position.getBlockX(), position.getBlockY(), position.getBlockZ()); position.getBlockX(), position.getBlockY(), position.getBlockZ());
@ -204,21 +204,21 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* Tries to set a block 'safely', as in setting the block data to the location, and * Tries to set a block 'safely', as in setting the block data to the location, and
* then triggering physics only at the end. * then triggering physics only at the end.
* *
* @param world world to set the block in * @param world world to set the block in
* @param position position to set the block at * @param position position to set the block at
* @param block the block to set * @param block the block to set
* @param notifyAdjacent true to notify physics and what not * @param notifyAdjacent true to notify physics and what not
* @return true if set * @return true if block id or data was changed
*/ */
public static boolean setSafely(BukkitWorld world, Vector position, public static boolean setSafely(BukkitWorld world, Vector position,
Block block, boolean notifyAdjacent) { Block block, boolean notifyAdjacent) {
int x = position.getBlockX(); int x = position.getBlockX();
@ -226,29 +226,29 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
int z = position.getBlockZ(); int z = position.getBlockZ();
CraftWorld craftWorld = ((CraftWorld) world.getWorld()); CraftWorld craftWorld = ((CraftWorld) world.getWorld());
boolean successful = craftWorld.getHandle().setRawTypeIdAndData( boolean changed = craftWorld.getHandle().setRawTypeIdAndData(
x, y, z, block.getId(), block.getData()); x, y, z, block.getId(), block.getData());
if (successful) { if (block instanceof BaseBlock) {
if (block instanceof BaseBlock) { world.copyToWorld(position, (BaseBlock) block);
world.copyToWorld(position, (BaseBlock) block); }
}
if (changed) {
if (notifyAdjacent) { if (notifyAdjacent) {
craftWorld.getHandle().update(x, y, z, block.getId()); craftWorld.getHandle().update(x, y, z, block.getId());
} else { } else {
craftWorld.getHandle().notify(x, y, z); craftWorld.getHandle().notify(x, y, z);
} }
} }
return successful; return changed;
} }
/** /**
* Converts from a non-native NMS NBT structure to a native WorldEdit NBT * Converts from a non-native NMS NBT structure to a native WorldEdit NBT
* structure. * structure.
* *
* @param foreign non-native NMS NBT structure * @param foreign non-native NMS NBT structure
* @return native WorldEdit NBT structure * @return native WorldEdit NBT structure
*/ */
@ -260,7 +260,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
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> foreignValues = null;
if (compoundMapField == null) { if (compoundMapField == null) {
try { try {
// Method name may change! // Method name may change!
@ -269,7 +269,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
try { try {
logger.warning("WorldEdit: Couldn't get NBTTagCompound.c(), " + logger.warning("WorldEdit: Couldn't get NBTTagCompound.c(), " +
"so we're going to try to get at the 'map' field directly from now on"); "so we're going to try to get at the 'map' field directly from now on");
if (compoundMapField == null) { if (compoundMapField == null) {
compoundMapField = NBTTagCompound.class.getDeclaredField("map"); compoundMapField = NBTTagCompound.class.getDeclaredField("map");
compoundMapField.setAccessible(true); compoundMapField.setAccessible(true);
@ -280,7 +280,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
} }
} }
} }
if (compoundMapField != null) { if (compoundMapField != null) {
try { try {
foreignValues = ((HashMap<Object, Object>) compoundMapField.get(foreign)).values(); foreignValues = ((HashMap<Object, Object>) compoundMapField.get(foreign)).values();
@ -289,7 +289,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
for (Object obj : foreignValues) { for (Object obj : foreignValues) {
NBTBase base = (NBTBase) obj; NBTBase base = (NBTBase) obj;
values.put(base.getName(), toNative(base)); values.put(base.getName(), toNative(base));
@ -338,7 +338,7 @@ class NmsBlock extends BaseBlock implements TileEntityBlock {
/** /**
* Converts a WorldEdit-native NBT structure to a NMS structure. * Converts a WorldEdit-native NBT structure to a NMS structure.
* *
* @param foreign structure to convert * @param foreign structure to convert
* @return non-native structure * @return non-native structure
*/ */