Worked around changes in hMod regarding chests.

This commit is contained in:
sk89q 2010-12-29 23:56:21 -08:00
parent 912468f8a1
commit 7499a04e65
2 changed files with 63 additions and 40 deletions

View File

@ -123,17 +123,7 @@ public class EditSession {
// Chests // Chests
} else if (block instanceof ChestBlock) { } else if (block instanceof ChestBlock) {
ChestBlock chestBlock = (ChestBlock)block; ChestBlock chestBlock = (ChestBlock)block;
BaseItem blankItem = new BaseItem((short)1); ServerInterface.setChestContents(pt, chestBlock.getItems());
Map<Byte,Countable<BaseItem>> items = chestBlock.getItems();
for (byte i = 0; i <= 26; i++) {
Countable<BaseItem> item = items.get(i);
if (item != null) {
ServerInterface.setChestSlot(pt, i, item.getID(),
item.getAmount());
} else {
ServerInterface.setChestSlot(pt, i, blankItem, 0);
}
}
// Mob spawners // Mob spawners
} else if (block instanceof MobSpawnerBlock) { } else if (block instanceof MobSpawnerBlock) {
MobSpawnerBlock mobSpawnerblock = (MobSpawnerBlock)block; MobSpawnerBlock mobSpawnerblock = (MobSpawnerBlock)block;
@ -248,7 +238,8 @@ public class EditSession {
return new SignBlock(type, data, text); return new SignBlock(type, data, text);
// Chest // Chest
} else if (type == 54) { } else if (type == 54) {
Map<Byte,Countable<BaseItem>> items = ServerInterface.getChestContents(pt); Map<Byte,Countable<BaseItem>> items =
ServerInterface.getChestContents(pt);
return new ChestBlock(data, items); return new ChestBlock(data, items);
// Mob spawner // Mob spawner
} else if (type == 52) { } else if (type == 52) {

View File

@ -19,6 +19,8 @@
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BlockType;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
@ -126,28 +128,34 @@ public class ServerInterface {
} }
/** /**
* Gets the contents of chests. * Gets the contents of chests. Will return null if the chest does not
* really exist or it is the second block for a double chest.
* *
* @param pt * @param pt
* @return * @return
*/ */
public static Map<Byte,Countable<BaseItem>> getChestContents(Vector pt) { public static Map<Byte,Countable<BaseItem>> getChestContents(Vector pt) {
ComplexBlock cblock = etc.getServer().getComplexBlock( ComplexBlock cblock = etc.getServer().getOnlyComplexBlock(
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (!(cblock instanceof Chest)) { Map<Byte,Countable<BaseItem>> items;
Item[] nativeItems;
if (cblock instanceof Chest) {
Chest chest = (Chest)cblock;
nativeItems = chest.getContents();
} else {
return null; return null;
} }
Chest chest = (Chest)cblock; items = new HashMap<Byte,Countable<BaseItem>>();
Map<Byte,Countable<BaseItem>> items =
new HashMap<Byte,Countable<BaseItem>>();
for (byte i = 0; i <= 26; i++) { for (byte i = 0; i < nativeItems.length; i++) {
Item item = chest.getItemFromSlot(i); Item item = nativeItems[i];
if (item != null) { if (item != null) {
items.put(i, new Countable<BaseItem>(new BaseItem((short)item.getItemId()), items.put(i,
item.getAmount())); new Countable<BaseItem>(
new BaseItem((short)item.getItemId()), item.getAmount()));
} }
} }
@ -158,23 +166,29 @@ public class ServerInterface {
* Sets a chest slot. * Sets a chest slot.
* *
* @param pt * @param pt
* @param slot * @param contents
* @param item
* @param amount
* @return * @return
*/ */
public static boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount) { public static boolean setChestContents(Vector pt,
ComplexBlock cblock = etc.getServer().getComplexBlock( Map<Byte,Countable<BaseItem>> contents) {
ComplexBlock cblock = etc.getServer().getOnlyComplexBlock(
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (!(cblock instanceof Chest)) { if (cblock instanceof Chest) {
return false; Chest chest = (Chest)cblock;
Item[] nativeItems = new Item[contents.size()];
for (Map.Entry<Byte,Countable<BaseItem>> entry : contents.entrySet()) {
nativeItems[entry.getKey()] =
new Item(entry.getValue().getID().getID(),
entry.getValue().getAmount());
}
setContents(chest, nativeItems);
} }
Chest chest = (Chest)cblock; return false;
chest.addItem(new Item(item.getID(), amount, slot));
chest.update();
return true;
} }
/** /**
@ -183,18 +197,36 @@ public class ServerInterface {
* @param pt * @param pt
*/ */
public static boolean clearChest(Vector pt) { public static boolean clearChest(Vector pt) {
ComplexBlock cblock = etc.getServer().getComplexBlock( ComplexBlock cblock = etc.getServer().getOnlyComplexBlock(
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (!(cblock instanceof Chest)) { if (cblock instanceof Chest) {
return false; Chest chest = (Chest)cblock;
chest.clearContents();
chest.update();
return true;
} }
Chest chest = (Chest)cblock;
chest.clearContents();
chest.update();
return true; return false;
}
/**
* Set the contents of an ItemArray.
*
* @param itemArray
* @param contents
*/
private static void setContents(ItemArray<?> itemArray, Item[] contents) {
int size = contents.length;
for (int i = 0; i < size; i++) {
if (contents[i] == null) {
itemArray.removeItem(i);
} else {
itemArray.setSlot(contents[i], i);
}
}
} }
/** /**