2010-10-14 10:31:05 +02:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import com.sk89q.worldedit.*;
|
2010-10-25 08:42:56 +02:00
|
|
|
import com.sk89q.worldedit.blocks.BaseItem;
|
2010-11-07 05:03:34 +01:00
|
|
|
import java.util.logging.Logger;
|
2010-10-25 08:42:56 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.HashMap;
|
2010-11-07 05:03:34 +01:00
|
|
|
import java.util.Random;
|
2010-10-14 10:31:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public class ServerInterface {
|
2010-11-07 05:03:34 +01:00
|
|
|
/**
|
|
|
|
* Logger.
|
|
|
|
*/
|
|
|
|
private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
|
|
|
|
/**
|
|
|
|
* Random generator.
|
|
|
|
*/
|
|
|
|
private static Random random = new Random();
|
|
|
|
|
2010-10-14 10:31:05 +02:00
|
|
|
/**
|
|
|
|
* Set block type.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param type
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static boolean setBlockType(Vector pt, int type) {
|
2010-10-15 09:26:29 +02:00
|
|
|
// Can't set colored cloth or crash
|
|
|
|
if ((type >= 21 && type <= 34) || type == 36) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-31 02:28:04 +01:00
|
|
|
return etc.getServer().setBlockAt(type, pt.getBlockX(), pt.getBlockY(),
|
2010-10-31 02:40:02 +02:00
|
|
|
pt.getBlockZ());
|
2010-10-14 10:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get block type.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static int getBlockType(Vector pt) {
|
2010-10-31 02:40:02 +02:00
|
|
|
return etc.getServer().getBlockIdAt(pt.getBlockX(), pt.getBlockY(),
|
|
|
|
pt.getBlockZ());
|
2010-10-14 10:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set block data.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param data
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static void setBlockData(Vector pt, int data) {
|
2010-10-25 08:45:49 +02:00
|
|
|
etc.getServer().setBlockData(pt.getBlockX(), pt.getBlockY(),
|
2010-10-14 10:31:05 +02:00
|
|
|
pt.getBlockZ(), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get block data.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static int getBlockData(Vector pt) {
|
2010-10-31 02:40:02 +02:00
|
|
|
return etc.getServer().getBlockData(pt.getBlockX(), pt.getBlockY(),
|
|
|
|
pt.getBlockZ());
|
2010-10-14 10:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set sign text.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param text
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static void setSignText(Vector pt, String[] text) {
|
2010-10-14 10:31:05 +02:00
|
|
|
Sign signData = (Sign)etc.getServer().getComplexBlock(
|
|
|
|
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
2010-10-17 01:45:31 +02:00
|
|
|
if (signData == null) {
|
|
|
|
return;
|
|
|
|
}
|
2010-10-14 10:31:05 +02:00
|
|
|
for (byte i = 0; i < 4; i++) {
|
|
|
|
signData.setText(i, text[i]);
|
|
|
|
}
|
|
|
|
signData.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sign text.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static String[] getSignText(Vector pt) {
|
2010-10-14 10:31:05 +02:00
|
|
|
Sign signData = (Sign)etc.getServer().getComplexBlock(
|
|
|
|
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
2010-10-17 01:45:31 +02:00
|
|
|
if (signData == null) {
|
|
|
|
return new String[]{"", "", "", ""};
|
|
|
|
}
|
2010-10-14 10:31:05 +02:00
|
|
|
String[] text = new String[4];
|
|
|
|
for (byte i = 0; i < 4; i++) {
|
|
|
|
text[i] = signData.getText(i);
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
2010-10-25 08:42:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the contents of chests.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static Map<Byte,Countable<BaseItem>> getChestContents(Vector pt) {
|
2010-10-25 08:42:56 +02:00
|
|
|
ComplexBlock cblock = etc.getServer().getComplexBlock(
|
|
|
|
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
|
|
|
|
|
|
|
if (!(cblock instanceof Chest)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Chest chest = (Chest)cblock;
|
|
|
|
Map<Byte,Countable<BaseItem>> items =
|
|
|
|
new HashMap<Byte,Countable<BaseItem>>();
|
|
|
|
|
|
|
|
for (byte i = 0; i <= 26; i++) {
|
|
|
|
Item item = chest.getItemFromSlot(i);
|
|
|
|
if (item != null) {
|
2010-11-06 22:46:00 +01:00
|
|
|
items.put(i, new Countable<BaseItem>(new BaseItem((short)item.getItemId()),
|
|
|
|
item.getAmount()));
|
2010-10-25 08:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a chest slot.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param slot
|
|
|
|
* @param item
|
|
|
|
* @param amount
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public static boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount) {
|
2010-10-25 08:42:56 +02:00
|
|
|
ComplexBlock cblock = etc.getServer().getComplexBlock(
|
|
|
|
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
|
|
|
|
|
|
|
if (!(cblock instanceof Chest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Chest chest = (Chest)cblock;
|
|
|
|
chest.addItem(new Item(item.getID(), amount, slot));
|
|
|
|
chest.update();
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-07 05:03:34 +01:00
|
|
|
|
2010-11-16 09:15:06 +01:00
|
|
|
/**
|
|
|
|
* Clear a chest's contents.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
*/
|
|
|
|
public static boolean clearChest(Vector pt) {
|
|
|
|
ComplexBlock cblock = etc.getServer().getComplexBlock(
|
|
|
|
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
|
|
|
|
|
|
|
if (!(cblock instanceof Chest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Chest chest = (Chest)cblock;
|
2010-11-26 18:39:23 +01:00
|
|
|
chest.clearContents();
|
2010-11-16 09:15:06 +01:00
|
|
|
chest.update();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-27 04:33:28 +01:00
|
|
|
/**
|
|
|
|
* Checks if a mob type is valid.
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean isValidMobType(String type) {
|
|
|
|
return Mob.isValid(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set mob spawner mob type.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param mobType
|
|
|
|
*/
|
|
|
|
public static void setMobSpawnerType(Vector pt, String mobType) {
|
2010-11-28 23:11:15 +01:00
|
|
|
ComplexBlock cblock = etc.getServer().getComplexBlock(
|
2010-11-27 04:33:28 +01:00
|
|
|
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
2010-11-28 23:11:15 +01:00
|
|
|
|
|
|
|
if (!(cblock instanceof MobSpawner)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MobSpawner mobSpawner = (MobSpawner)cblock;
|
|
|
|
mobSpawner.setSpawn(mobType);
|
|
|
|
mobSpawner.update();
|
2010-11-27 04:33:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get mob spawner mob type. May return an empty string.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param mobType
|
|
|
|
*/
|
|
|
|
public static String getMobSpawnerType(Vector pt) {
|
|
|
|
ay o = etc.getMCServer().e.k(
|
|
|
|
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
|
|
|
|
|
|
|
if (o != null && o instanceof cf) {
|
|
|
|
String type = ((cf)o).f;
|
|
|
|
return type != null ? type : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2010-11-07 05:03:34 +01:00
|
|
|
/**
|
|
|
|
* Generate a tree at a location.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean generateTree(EditSession editSession, Vector pt) {
|
2010-12-01 05:14:01 +01:00
|
|
|
try {
|
|
|
|
return MinecraftServerInterface.generateTree(editSession, pt);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.severe("Failed to create tree (do you need to update WorldEdit due to a Minecraft update?): "
|
|
|
|
+ t.getMessage());
|
|
|
|
return false;
|
2010-11-07 05:03:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-27 06:23:51 +01:00
|
|
|
/**
|
|
|
|
* Drop an item.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param type
|
|
|
|
* @param count
|
|
|
|
* @param times
|
|
|
|
*/
|
|
|
|
public static void dropItem(Vector pt, int type, int count, int times) {
|
|
|
|
for (int i = 0; i < times; i++) {
|
|
|
|
etc.getServer().dropItem(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ(),
|
|
|
|
type, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drop an item.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param type
|
|
|
|
* @param count
|
|
|
|
* @param times
|
|
|
|
*/
|
|
|
|
public static void dropItem(Vector pt, int type, int count) {
|
|
|
|
etc.getServer().dropItem(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ(),
|
|
|
|
type, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drop an item.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @param type
|
|
|
|
* @param count
|
|
|
|
* @param times
|
|
|
|
*/
|
|
|
|
public static void dropItem(Vector pt, int type) {
|
|
|
|
etc.getServer().dropItem(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ(),
|
|
|
|
type, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simulate a block being mined.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
*/
|
|
|
|
public static void simulateBlockMine(Vector pt) {
|
|
|
|
int type = getBlockType(pt);
|
|
|
|
setBlockType(pt, 0);
|
|
|
|
|
|
|
|
if (type == 1) { dropItem(pt, 4); } // Stone
|
|
|
|
else if (type == 2) { dropItem(pt, 3); } // Grass
|
|
|
|
else if (type == 13) { // Gravel
|
|
|
|
dropItem(pt, type);
|
|
|
|
|
|
|
|
if (random.nextDouble() >= 0.9) {
|
|
|
|
dropItem(pt, 318);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == 16) { dropItem(pt, 263); } // Coal ore
|
|
|
|
else if (type == 18) { // Leaves
|
|
|
|
if (random.nextDouble() > 0.95) {
|
|
|
|
dropItem(pt, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == 20) { } // Glass
|
|
|
|
else if (type == 43) { dropItem(pt, 44); } // Double step
|
|
|
|
else if (type == 47) { } // Bookshelves
|
|
|
|
else if (type == 52) { } // Mob spawner
|
|
|
|
else if (type == 53) { dropItem(pt, 5); } // Wooden stairs
|
|
|
|
else if (type == 55) { dropItem(pt, 331); } // Redstone wire
|
|
|
|
else if (type == 56) { dropItem(pt, 264); } // Diamond ore
|
|
|
|
else if (type == 60) { dropItem(pt, 3); } // Soil
|
|
|
|
else if (type == 63) { dropItem(pt, 323); } // Sign post
|
|
|
|
else if (type == 67) { dropItem(pt, 4); } // Cobblestone stairs
|
|
|
|
else if (type == 68) { dropItem(pt, 323); } // Wall sign
|
|
|
|
else if (type == 73) { dropItem(pt, 331, 1, 4); } // Redstone ore
|
|
|
|
else if (type == 74) { dropItem(pt, 331, 1, 4); } // Glowing redstone ore
|
|
|
|
else if (type == 78) { } // Snow
|
|
|
|
else if (type == 79) { } // Ice
|
|
|
|
else if (type == 82) { dropItem(pt, 337, 1, 4); } // Clay
|
|
|
|
else if (type == 83) { dropItem(pt, 338); } // Reed
|
|
|
|
else if (type == 89) { dropItem(pt, 348); } // Lightstone
|
|
|
|
else if (type != 0) {
|
|
|
|
dropItem(pt, type);
|
|
|
|
}
|
|
|
|
}
|
2010-10-14 10:31:05 +02:00
|
|
|
}
|