Add new Bukkit implementation adapter system to access MC internals.

Replaces the old NMSBlocks.
This commit is contained in:
sk89q
2014-07-14 12:10:38 -07:00
parent c535ad8682
commit f033d87098
34 changed files with 528 additions and 1580 deletions

View File

@ -60,115 +60,11 @@ public abstract class LocalWorld extends AbstractWorld {
@Deprecated
protected Random random = new Random();
@Override
public BaseBlock getBlock(Vector pt) {
checkLoadedChunk(pt);
@SuppressWarnings("deprecation") int type = getBlockType(pt);
@SuppressWarnings("deprecation") int data = getBlockData(pt);
switch (type) {
case BlockID.WALL_SIGN:
case BlockID.SIGN_POST: {
SignBlock block = new SignBlock(type, data);
copyFromWorld(pt, block);
return block;
}
case BlockID.CHEST: {
ChestBlock block = new ChestBlock(data);
copyFromWorld(pt, block);
return block;
}
case BlockID.FURNACE:
case BlockID.BURNING_FURNACE: {
FurnaceBlock block = new FurnaceBlock(type, data);
copyFromWorld(pt, block);
return block;
}
case BlockID.DISPENSER: {
DispenserBlock block = new DispenserBlock(data);
copyFromWorld(pt, block);
return block;
}
case BlockID.MOB_SPAWNER: {
MobSpawnerBlock block = new MobSpawnerBlock(data);
copyFromWorld(pt, block);
return block;
}
case BlockID.NOTE_BLOCK: {
NoteBlock block = new NoteBlock(data);
copyFromWorld(pt, block);
return block;
}
case BlockID.HEAD: {
SkullBlock block = new SkullBlock(data);
copyFromWorld(pt, block);
return block;
}
default:
return new BaseBlock(type, data);
}
}
/**
* Given a block and a position, copy data from the world to the block
* based on the type of block.
* </p>
* The provided {@link BaseBlock} should match that of the one in the
* world.
*
* @param position the position
* @param block the block
* @return true if the copy operation succeeded, false otherwise
*/
public abstract boolean copyFromWorld(Vector position, BaseBlock block);
@Override
public BaseBlock getLazyBlock(Vector position) {
return getBlock(position);
}
@Override
public boolean setBlock(Vector pt, BaseBlock block, boolean notifyAdjacent) throws WorldEditException {
boolean successful;
// Default implementation will call the old deprecated methods
if (notifyAdjacent) {
successful = setTypeIdAndData(pt, block.getId(), block.getData());
} else {
successful = setTypeIdAndDataFast(pt, block.getId(), block.getData());
}
copyToWorld(pt, block);
return successful;
}
/**
* Given a block and a position, copy data to the world from the block
* based on the type of block.
* </p>
* The provided {@link BaseBlock} should match that of the one in the
* world.
*
* @param position the position
* @param block the block
* @return true if the copy operation succeeded, false otherwise
*/
public abstract boolean copyToWorld(Vector position, BaseBlock block);
@Override
public boolean setBlock(Vector pt, BaseBlock block) throws WorldEditException {
return setBlock(pt, block, true);
}
@Override
public boolean generateTree(TreeGenerator.TreeType type, EditSession editSession, Vector pt) throws MaxChangedBlocksException {
switch (type) {

View File

@ -100,42 +100,6 @@ public class LocalWorldAdapter extends LocalWorld {
return world.setBlock(position, block, notifyAndLight);
}
@Override
@Deprecated
public boolean setBlockType(Vector position, int type) {
return world.setBlockType(position, type);
}
@Override
@Deprecated
public boolean setBlockTypeFast(Vector position, int type) {
return world.setBlockTypeFast(position, type);
}
@Override
@Deprecated
public void setBlockData(Vector position, int data) {
world.setBlockData(position, data);
}
@Override
@Deprecated
public void setBlockDataFast(Vector position, int data) {
world.setBlockDataFast(position, data);
}
@Override
@Deprecated
public boolean setTypeIdAndData(Vector position, int type, int data) {
return world.setTypeIdAndData(position, type, data);
}
@Override
@Deprecated
public boolean setTypeIdAndDataFast(Vector position, int type, int data) {
return world.setTypeIdAndDataFast(position, type, data);
}
@Override
public int getBlockLightLevel(Vector position) {
return world.getBlockLightLevel(position);
@ -298,30 +262,11 @@ public class LocalWorldAdapter extends LocalWorld {
return world.getBlock(position);
}
@Override
public boolean copyFromWorld(Vector position, BaseBlock block) {
return false;
}
@Override
public boolean copyToWorld(Vector position, BaseBlock block) {
return false;
}
@Override
public BaseBlock getLazyBlock(Vector position) {
return world.getLazyBlock(position);
}
@Override
public boolean setBlock(Vector position, BaseBlock block) {
try {
return world.setBlock(position, block);
} catch (WorldEditException e) {
throw new RuntimeException(e);
}
}
@Override
@Nullable
public Operation commit() {

View File

@ -150,7 +150,7 @@ public class WorldEditExceptionConverter extends ExceptionConverterHelper {
@ExceptionMatch
public void convert(WorldEditException e) throws CommandException {
throw new CommandException(e.getMessage());
throw new CommandException(e.getMessage(), e);
}
}

View File

@ -0,0 +1,54 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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 Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Helper methods for enums.
*/
public final class Enums {
private Enums() {
}
/**
* Search the given enum for a value that is equal to the one of the
* given values, searching in an ascending manner.
*
* @param enumType the enum type
* @param values the list of values
* @param <T> the type of enum
* @return the found value or null
*/
@Nullable
public static <T extends Enum<T>> T findByValue(Class<T> enumType, String... values) {
checkNotNull(enumType);
checkNotNull(values);
for (String val : values) {
try {
return Enum.valueOf(enumType, val);
} catch (IllegalArgumentException ignored) {}
}
return null;
}
}

View File

@ -47,6 +47,37 @@ public abstract class AbstractWorld implements World {
private final PriorityQueue<QueuedEffect> effectQueue = new PriorityQueue<QueuedEffect>();
private int taskId = -1;
@Override
public final boolean setBlockType(Vector position, int type) {
try {
return setBlock(position, new BaseBlock(type));
} catch (WorldEditException ignored) {
return false;
}
}
@Override
public final void setBlockData(Vector position, int data) {
try {
setBlock(position, new BaseBlock(getLazyBlock(position).getType(), data));
} catch (WorldEditException ignored) {
}
}
@Override
public final boolean setTypeIdAndData(Vector position, int type, int data) {
try {
return setBlock(position, new BaseBlock(type, data));
} catch (WorldEditException ignored) {
return false;
}
}
@Override
public final boolean setBlock(Vector pt, BaseBlock block) throws WorldEditException {
return setBlock(pt, block, true);
}
@Override
public int getMaxY() {
return getMaximumPoint().getBlockY();
@ -82,56 +113,6 @@ public abstract class AbstractWorld implements World {
return getLazyBlock(pt).getData();
}
@Override
public boolean setBlock(Vector position, BaseBlock block) throws WorldEditException {
return setBlock(position, block, true);
}
@Override
public boolean setBlockType(Vector position, int type) {
try {
return setBlock(position, new BaseBlock(type));
} catch (WorldEditException e) {
throw new RuntimeException(e);
}
}
@Override
public void setBlockData(Vector position, int data) {
try {
setBlock(position, new BaseBlock(getLazyBlock(position).getId(), data));
} catch (WorldEditException e) {
throw new RuntimeException(e);
}
}
@Override
public void setBlockDataFast(Vector position, int data) {
setBlockData(position, data);
}
@SuppressWarnings("deprecation")
@Override
public boolean setBlockTypeFast(Vector pt, int type) {
return setBlockType(pt, type);
}
@SuppressWarnings("deprecation")
@Override
public boolean setTypeIdAndData(Vector pt, int type, int data) {
boolean ret = setBlockType(pt, type);
setBlockData(pt, data);
return ret;
}
@SuppressWarnings("deprecation")
@Override
public boolean setTypeIdAndDataFast(Vector pt, int type, int data) {
boolean ret = setBlockTypeFast(pt, type);
setBlockDataFast(pt, data);
return ret;
}
@Override
public void dropItem(Vector pt, BaseItemStack item, int times) {
for (int i = 0; i < times; ++i) {

View File

@ -122,36 +122,18 @@ public interface World extends Extent {
@Deprecated
boolean setBlockType(Vector position, int type);
/**
* @deprecated Use {@link #setBlock(Vector, BaseBlock)}
*/
@Deprecated
boolean setBlockTypeFast(Vector position, int type);
/**
* @deprecated Use {@link #setBlock(Vector, BaseBlock)}
*/
@Deprecated
void setBlockData(Vector position, int data);
/**
* @deprecated Use {@link #setBlock(Vector, BaseBlock)}
*/
@Deprecated
void setBlockDataFast(Vector position, int data);
/**
* @deprecated Use {@link #setBlock(Vector, BaseBlock)}
*/
@Deprecated
boolean setTypeIdAndData(Vector position, int type, int data);
/**
* @deprecated Use {@link #setBlock(Vector, BaseBlock)}
*/
@Deprecated
boolean setTypeIdAndDataFast(Vector position, int type, int data);
/**
* Get the light level at the given block.
*