Changes to NBT-handling in operations and extents, changes to match the widely supported setBlock functionality, minor code cleanup

This commit is contained in:
IronApollo
2019-03-25 13:31:12 -04:00
parent 16c22b75da
commit 3236bdd78e
57 changed files with 347 additions and 417 deletions

View File

@ -73,7 +73,6 @@ public final class Spigot_v1_13_R2 extends CachedBukkitAdapter implements Bukkit
private final Field nbtListTagListField;
private final Method nbtCreateTagMethod;
private Method chunkSetTypeMethod;
static {
// A simple test
@ -92,13 +91,6 @@ public final class Spigot_v1_13_R2 extends CachedBukkitAdapter implements Bukkit
// The method to create an NBTBase tag given its type ID
nbtCreateTagMethod = NBTBase.class.getDeclaredMethod("createTag", byte.class);
nbtCreateTagMethod.setAccessible(true);
// 1.13.2 Adaptation to find the a/setType method
try {
chunkSetTypeMethod = Chunk.class.getMethod("setType", BlockPosition.class, IBlockData.class, boolean.class);
}catch(NoSuchMethodException e) {
chunkSetTypeMethod = Chunk.class.getMethod("a", BlockPosition.class, IBlockData.class, boolean.class);
}
}
private int[] idbToStateOrdinal;
@ -217,7 +209,7 @@ public final class Spigot_v1_13_R2 extends CachedBukkitAdapter implements Bukkit
@SuppressWarnings("deprecation")
@Override
public BlockState getBlock(Location location) {
public BaseBlock getBlock(Location location) {
checkNotNull(location);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
@ -233,11 +225,11 @@ public final class Spigot_v1_13_R2 extends CachedBukkitAdapter implements Bukkit
if (te != null) {
NBTTagCompound tag = new NBTTagCompound();
readTileEntityIntoTag(te, tag); // Load data
return new BaseBlock(state, (CompoundTag) toNative(tag)).toImmutableState();
return new BaseBlock(state, (CompoundTag) toNative(tag));
}
}
return state;
return state.toBaseBlock();
}
@Override
@ -265,7 +257,7 @@ public final class Spigot_v1_13_R2 extends CachedBukkitAdapter implements Bukkit
existing = section.getType(x & 15, y & 15, z & 15);
}
BlockPosition pos = null;
CompoundTag nativeTag = state.getNbtData();
CompoundTag nativeTag = state instanceof BaseBlock ? ((BaseBlock)state).getNbtData() : null;
if (nativeTag != null || existing instanceof TileEntityBlock) {
pos = new BlockPosition(x, y, z);
nmsWorld.setTypeAndData(pos, blockData, 0);
@ -289,12 +281,7 @@ public final class Spigot_v1_13_R2 extends CachedBukkitAdapter implements Bukkit
sections[y4] = section = new ChunkSection(y4 << 4, nmsWorld.worldProvider.g());
}
if (existing.e() != blockData.e() || existing.getMaterial().f() != blockData.getMaterial().f()) {
try {
chunkSetTypeMethod.invoke(nmsChunk, pos = new BlockPosition(x, y, z), blockData, false);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
logger.warning("Error when setting block!");
e.printStackTrace();
}
nmsChunk.setType(pos = new BlockPosition(x, y, z), blockData, false);
} else {
section.setType(x & 15, y & 15, z & 15, blockData);
}

View File

@ -356,12 +356,12 @@ public class BukkitChunk_All extends IntFaweChunk<Chunk, BukkitQueue_All> {
}
public void setBlock(BukkitImplAdapter adapter, Chunk chunk, Location location, int combinedId, boolean update) {
com.sk89q.worldedit.world.block.BlockState state = com.sk89q.worldedit.world.block.BlockState.getFromInternalId(combinedId);
com.sk89q.worldedit.world.block.BaseBlock base = com.sk89q.worldedit.world.block.BlockState.getFromInternalId(combinedId).toBaseBlock();
if (adapter != null) {
adapter.setBlock(chunk, (int) location.getX(), (int) location.getY(), (int) location.getZ(), state, update);
adapter.setBlock(chunk, (int) location.getX(), (int) location.getY(), (int) location.getZ(), base, update);
} else {
Block block = location.getWorld().getBlockAt(location);
block.setBlockData(BukkitAdapter.adapt(state), false);
block.setBlockData(BukkitAdapter.adapt(base), false);
}
}
}

View File

@ -22,6 +22,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes;
import org.bukkit.Chunk;
@ -333,7 +334,7 @@ public class BukkitQueue_All extends BukkitQueue_0<ChunkSnapshot, ChunkSnapshot,
return null;
}
Location loc = new Location(getWorld(), x, y, z);
BlockStateHolder block = getAdapter().getBlock(loc);
BaseBlock block = getAdapter().getBlock(loc);
return block.getNbtData();
}

View File

@ -1,5 +1,4 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
@ -461,9 +460,9 @@ public class BukkitWorld extends AbstractWorld {
int z = position.getBlockZ();
return adapter.setBlock(getWorld().getChunkAt(x >> 4, z >> 4), x, y, z, block, true);
} catch (Exception e) {
if (block.getNbtData() != null) {
if (block instanceof BaseBlock && ((BaseBlock)block).getNbtData() != null) {
logger.warning("Tried to set a corrupt tile entity at " + position.toString());
logger.warning(block.getNbtData().toString());
logger.warning(((BaseBlock)block).getNbtData().toString());
}
e.printStackTrace();
Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
@ -486,7 +485,7 @@ public class BukkitWorld extends AbstractWorld {
public BaseBlock getFullBlock(BlockVector3 position) {
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
if (adapter != null) {
return adapter.getBlock(BukkitAdapter.adapt(getWorld(), position)).toBaseBlock();
return adapter.getBlock(BukkitAdapter.adapt(getWorld(), position));
} else {
return getBlock(position).toBaseBlock();
}

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.bukkit.adapter;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.entity.BaseEntity;
@ -70,7 +71,7 @@ public interface BukkitImplAdapter<T> extends IBukkitAdapter {
* @param location the location
* @return the block
*/
BlockState getBlock(Location location);
BaseBlock getBlock(Location location);
boolean setBlock(Chunk chunk, int x, int y, int z, BlockStateHolder<?> state, boolean update);