Overhauled chest handling for MC beta. Now handles item damage as well.

This commit is contained in:
sk89q 2010-12-30 21:19:23 -08:00
parent 97835c97c9
commit 98de15fb4e
4 changed files with 114 additions and 31 deletions

View File

@ -238,7 +238,7 @@ 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 = BaseItemStack[] items =
ServerInterface.getChestContents(pt); ServerInterface.getChestContents(pt);
return new ChestBlock(data, items); return new ChestBlock(data, items);
// Mob spawner // Mob spawner

View File

@ -19,8 +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.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockType; 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;
@ -134,11 +134,11 @@ public class ServerInterface {
* @param pt * @param pt
* @return * @return
*/ */
public static Map<Byte,Countable<BaseItem>> getChestContents(Vector pt) { public static BaseItemStack[] getChestContents(Vector pt) {
ComplexBlock cblock = etc.getServer().getOnlyComplexBlock( ComplexBlock cblock = etc.getServer().getOnlyComplexBlock(
pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
Map<Byte,Countable<BaseItem>> items; BaseItemStack[] items;
Item[] nativeItems; Item[] nativeItems;
if (cblock instanceof Chest) { if (cblock instanceof Chest) {
@ -148,14 +148,14 @@ public class ServerInterface {
return null; return null;
} }
items = new HashMap<Byte,Countable<BaseItem>>(); items = new BaseItemStack[nativeItems.length];
for (byte i = 0; i < nativeItems.length; i++) { for (byte i = 0; i < nativeItems.length; i++) {
Item item = nativeItems[i]; Item item = nativeItems[i];
if (item != null) { if (item != null) {
items.put(i, items[i] = new BaseItemStack((short)item.getItemId(),
new Countable<BaseItem>( item.getAmount(), (short)item.getDamage());
new BaseItem((short)item.getItemId()), item.getAmount()));
} }
} }
@ -170,19 +170,24 @@ public class ServerInterface {
* @return * @return
*/ */
public static boolean setChestContents(Vector pt, public static boolean setChestContents(Vector pt,
Map<Byte,Countable<BaseItem>> contents) { BaseItemStack[] contents) {
ComplexBlock cblock = etc.getServer().getOnlyComplexBlock( 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) {
Chest chest = (Chest)cblock; Chest chest = (Chest)cblock;
Item[] nativeItems = new Item[contents.size()]; Item[] nativeItems = new Item[contents.length];
for (Map.Entry<Byte,Countable<BaseItem>> entry : contents.entrySet()) { for (int i = 0; i < contents.length; i++) {
nativeItems[entry.getKey()] = BaseItemStack item = contents[i];
new Item(entry.getValue().getID().getID(),
entry.getValue().getAmount()); if (item != null) {
Item nativeItem =
new Item(item.getID(), item.getAmount());
nativeItem.setDamage(item.getDamage());
nativeItems[i] = nativeItem;
}
} }
setContents(chest, nativeItems); setContents(chest, nativeItems);
@ -224,7 +229,8 @@ public class ServerInterface {
if (contents[i] == null) { if (contents[i] == null) {
itemArray.removeItem(i); itemArray.removeItem(i);
} else { } else {
itemArray.setSlot(contents[i], i); itemArray.setSlot(contents[i].getItemId(),
contents[i].getAmount(), contents[i].getDamage(), i);
} }
} }
} }

View File

@ -0,0 +1,75 @@
// $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/>.
*/
package com.sk89q.worldedit.blocks;
/**
* Represents a stack of BaseItems.
*
* @author sk89q
*/
public class BaseItemStack extends BaseItem {
/**
* Amount of an item.
*/
private int amount = 1;
/**
* Construct the object.
*
* @param id
*/
public BaseItemStack(short id) {
super(id);
}
/**
* Construct the object.
*
* @param id
*/
public BaseItemStack(short id, int amount) {
super(id);
this.amount = amount;
}
/**
* Construct the object.
*
* @param id
*/
public BaseItemStack(short id, int amount, short damage) {
super(id, damage);
this.amount = amount;
}
/**
* @return the amount
*/
public int getAmount() {
return amount;
}
/**
* @param amount the amount to set
*/
public void setAmount(int amount) {
this.amount = amount;
}
}

View File

@ -36,14 +36,14 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
/** /**
* Store the list of items. * Store the list of items.
*/ */
private Map<Byte,Countable<BaseItem>> items; private BaseItemStack[] items;
/** /**
* Construct the chest block. * Construct the chest block.
*/ */
public ChestBlock() { public ChestBlock() {
super(54); super(54);
items = new HashMap<Byte,Countable<BaseItem>>(); items = new BaseItemStack[27];
} }
/** /**
@ -53,7 +53,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
*/ */
public ChestBlock(int data) { public ChestBlock(int data) {
super(54, data); super(54, data);
items = new HashMap<Byte,Countable<BaseItem>>(); items = new BaseItemStack[27];
} }
/** /**
@ -62,7 +62,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
* @param data * @param data
* @param items * @param items
*/ */
public ChestBlock(int data, Map<Byte,Countable<BaseItem>> items) { public ChestBlock(int data, BaseItemStack[] items) {
super(54, data); super(54, data);
this.items = items; this.items = items;
} }
@ -72,7 +72,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
* *
* @return * @return
*/ */
public Map<Byte,Countable<BaseItem>> getItems() { public BaseItemStack[] getItems() {
return items; return items;
} }
@ -81,7 +81,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
* *
* @return * @return
*/ */
public void setItems(Map<Byte,Countable<BaseItem>> items) { public void setItems(BaseItemStack[] items) {
this.items = items; this.items = items;
} }
@ -103,15 +103,15 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
public Map<String,Tag> toTileEntityNBT() public Map<String,Tag> toTileEntityNBT()
throws DataException { throws DataException {
List<Tag> itemsList = new ArrayList<Tag>(); List<Tag> itemsList = new ArrayList<Tag>();
for (Map.Entry<Byte,Countable<BaseItem>> entry : items.entrySet()) { for (int i = 0; i < items.length; i++) {
BaseItemStack item = items[i];
Map<String,Tag> data = new HashMap<String,Tag>(); Map<String,Tag> data = new HashMap<String,Tag>();
CompoundTag item = new CompoundTag("Items", data); CompoundTag itemTag = new CompoundTag("Items", data);
BaseItem i = entry.getValue().getID(); data.put("id", new ShortTag("id", item.getID()));
data.put("id", new ShortTag("id", i.getID())); data.put("Damage", new ShortTag("Damage", item.getDamage()));
data.put("Damage", new ShortTag("Damage", i.getDamage())); data.put("Count", new ByteTag("Count", (byte)item.getAmount()));
data.put("Count", new ByteTag("Count", (byte)entry.getValue().getAmount())); data.put("Slot", new ByteTag("Slot", (byte)i));
data.put("Slot", new ByteTag("Slot", entry.getKey())); itemsList.add(itemTag);
itemsList.add(item);
} }
Map<String,Tag> values = new HashMap<String,Tag>(); Map<String,Tag> values = new HashMap<String,Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, itemsList)); values.put("Items", new ListTag("Items", CompoundTag.class, itemsList));
@ -130,7 +130,6 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
return; return;
} }
Map<Byte,Countable<BaseItem>> newItems = new HashMap<Byte,Countable<BaseItem>>();
Tag t = values.get("id"); Tag t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag)t).getValue().equals("Chest")) { if (!(t instanceof StringTag) || !((StringTag)t).getValue().equals("Chest")) {
@ -138,6 +137,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
} }
ListTag items = (ListTag)Chunk.getChildTag(values, "Items", ListTag.class); ListTag items = (ListTag)Chunk.getChildTag(values, "Items", ListTag.class);
BaseItemStack[] newItems = new BaseItemStack[27];
for (Tag tag : items.getValue()) { for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) { if (!(tag instanceof CompoundTag)) {
@ -156,7 +156,9 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock {
byte slot = (Byte)((ByteTag)Chunk.getChildTag(itemValues, "Slot", ByteTag.class)) byte slot = (Byte)((ByteTag)Chunk.getChildTag(itemValues, "Slot", ByteTag.class))
.getValue(); .getValue();
newItems.put(slot, new Countable<BaseItem>(new BaseItem(id, damage), count)); if (slot >= 0 && slot <= 26) {
newItems[slot] = new BaseItemStack(id, count, damage);
}
} }
this.items = newItems; this.items = newItems;