Added chest support. Double-width chests don't work too well yet.

This commit is contained in:
sk89q 2010-10-24 23:42:56 -07:00
parent cdfd4b81fa
commit ca1e522499
8 changed files with 435 additions and 9 deletions

View File

@ -359,13 +359,19 @@ public class CuboidClipboard {
int index = y * width * length + z * width + x;
BlockVector pt = new BlockVector(x, y, z);
BaseBlock block;
if (blocks[index] == 63 || blocks[index] == 68) {
block = new SignBlock(blocks[index], blockData[index]);
if (tileEntitiesMap.containsKey(pt)) {
((TileEntityBlock)block).fromTileEntityNBT(
tileEntitiesMap.get(pt));
}
} else if(blocks[index] == 54) {
block = new ChestBlock();
if (tileEntitiesMap.containsKey(pt)) {
((TileEntityBlock)block).fromTileEntityNBT(
tileEntitiesMap.get(pt));
}
} else {
block = new BaseBlock(blocks[index], blockData[index]);
}

View File

@ -116,19 +116,27 @@ public class EditSession {
private boolean rawSetBlock(Vector pt, BaseBlock block) {
boolean result = server.setBlockType(pt, block.getID());
if (block.getID() != 0) {
// Changes in block data don't take effect if the block type doesn't
// change, so here's a hack!
/*if (!result) {
server.setBlockType(pt, 0);
result = server.setBlockType(pt, block.getID());
}
server.setBlockData(pt, block.getData());*/
server.setBlockData(pt, block.getData());
// Signs
if (block instanceof SignBlock) {
SignBlock signBlock = (SignBlock)block;
String[] text = signBlock.getText();
server.setSignText(pt, text);
// Chests
} else if (block instanceof ChestBlock) {
ChestBlock chestBlock = (ChestBlock)block;
BaseItem blankItem = new BaseItem((short)1);
Map<Byte,Countable<BaseItem>> items = chestBlock.getItems();
for (byte i = 0; i <= 26; i++) {
Countable<BaseItem> item = items.get(i);
if (item != null) {
server.setChestSlot(pt, i, item.getID(),
item.getAmount());
} else {
server.setChestSlot(pt, i, blankItem, 0);
}
}
}
}
@ -234,6 +242,10 @@ public class EditSession {
if (type == 63 || type == 68) {
String[] text = server.getSignText(pt);
return new SignBlock(type, data, text);
// Chest
} else if (type == 54) {
Map<Byte,Countable<BaseItem>> items = server.getChestContents(pt);
return new ChestBlock(data, items);
} else {
return new BaseBlock(type, data);
}

View File

@ -18,6 +18,9 @@
*/
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseItem;
import java.util.Map;
import java.util.HashMap;
/**
*
@ -108,4 +111,55 @@ public class SMServerInterface implements ServerInterface {
}
return text;
}
/**
* Gets the contents of chests.
*
* @param pt
* @return
*/
public Map<Byte,Countable<BaseItem>> getChestContents(Vector pt) {
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) {
items.put(i, new Countable(new BaseItem((short)item.getItemId()), item.getAmount()));
}
}
return items;
}
/**
* Sets a chest slot.
*
* @param pt
* @param slot
* @param item
* @param amount
* @return
*/
public boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount) {
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;
}
}

View File

@ -0,0 +1,74 @@
// $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;
/**
*
* @author sk89q
*/
public class Countable<T> {
/**
* ID.
*/
private T id;
/**
* Amount.
*/
private int amount;
/**
* Construct the object.
*
* @param id
* @param amount
*/
public Countable(T id, int amount) {
this.id = id;
this.amount = amount;
}
/**
* @return the id
*/
public T getID() {
return id;
}
/**
* @param id the id to set
*/
public void setID(T id) {
this.id = id;
}
/**
* @return the amount
*/
public int getAmount() {
return amount;
}
/**
* @param amount the amount to set
*/
public void setAmount(int amount) {
this.amount = amount;
}
}

View File

@ -19,6 +19,10 @@
package com.sk89q.worldedit;
import java.util.Map;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseItem;
/**
*
* @author sk89q
@ -68,4 +72,21 @@ public interface ServerInterface {
* @return
*/
public String[] getSignText(Vector pt);
/**
* Gets the contents of chests.
*
* @param pt
* @return
*/
public Map<Byte,Countable<BaseItem>> getChestContents(Vector pt);
/**
* Sets a chest slot.
*
* @param pt
* @param slot
* @param item
* @param amount
* @return
*/
public boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount);
}

View File

@ -0,0 +1,84 @@
// $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 an item.
*
* @author sk89q
*/
public class BaseItem {
/**
* Item ID.
*/
private short id;
/**
* Item damage.
*/
private short damage;
/**
* Construct the object.
*
* @param id
*/
public BaseItem(short id) {
this.id = id;
this.damage = 0;
}
/**
* Construct the object.
*
* @param id
*/
public BaseItem(short id, short damage) {
this.id = id;
this.damage = damage;
}
/**
* @return the id
*/
public short getID() {
return id;
}
/**
* @param id the id to set
*/
public void setID(short id) {
this.id = id;
}
/**
* @return the damage
*/
public short getDamage() {
return damage;
}
/**
* @param damage the damage to set
*/
public void setDamage(short damage) {
this.damage = damage;
}
}

View File

@ -0,0 +1,164 @@
// $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;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.data.*;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import org.jnbt.*;
/**
* Represents chests.
*
* @author sk89q
*/
public class ChestBlock extends BaseBlock implements TileEntityBlock {
/**
* Store the list of items.
*/
private Map<Byte,Countable<BaseItem>> items;
/**
* Construct the chest block.
*/
public ChestBlock() {
super(54);
items = new HashMap<Byte,Countable<BaseItem>>();
}
/**
* Construct the chest block.
*
* @param data
*/
public ChestBlock(int data) {
super(54, data);
items = new HashMap<Byte,Countable<BaseItem>>();
}
/**
* Construct the chest block.
*
* @param data
* @param items
*/
public ChestBlock(int data, Map<Byte,Countable<BaseItem>> items) {
super(54, data);
this.items = items;
}
/**
* Get the list of items.
*
* @return
*/
public Map<Byte,Countable<BaseItem>> getItems() {
return items;
}
/**
* Set the list of items.
*
* @return
*/
public void setItems(Map<Byte,Countable<BaseItem>> items) {
this.items = items;
}
/**
* Get the tile entity ID.
*
* @return
*/
public String getTileEntityID() {
return "Chest";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
public Map<String,Tag> toTileEntityNBT()
throws DataException {
List<Tag> itemsList = new ArrayList<Tag>();
for (Map.Entry<Byte,Countable<BaseItem>> entry : items.entrySet()) {
Map<String,Tag> data = new HashMap<String,Tag>();
CompoundTag item = new CompoundTag("Items", data);
BaseItem i = entry.getValue().getID();
data.put("id", new ShortTag("id", i.getID()));
data.put("Damage", new ShortTag("Damage", i.getDamage()));
data.put("Count", new ByteTag("Count", (byte)entry.getValue().getAmount()));
data.put("Slot", new ByteTag("Slot", entry.getKey()));
itemsList.add(item);
}
Map<String,Tag> values = new HashMap<String,Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, itemsList));
return values;
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
public void fromTileEntityNBT(Map<String,Tag> values)
throws DataException {
if (values == null) {
return;
}
Map<Byte,Countable<BaseItem>> newItems = new HashMap<Byte,Countable<BaseItem>>();
Tag t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag)t).getValue().equals("Chest")) {
throw new DataException("'Chest' tile entity expected");
}
ListTag items = (ListTag)Chunk.getChildTag(values, "Items", ListTag.class);
for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Chest's Items");
}
CompoundTag item = (CompoundTag)tag;
Map<String,Tag> itemValues = item.getValue();
short id = (Short)((ShortTag)Chunk.getChildTag(itemValues, "id", ShortTag.class))
.getValue();
short damage = (Short)((ShortTag)Chunk.getChildTag(itemValues, "Damage", ShortTag.class))
.getValue();
byte count = (Byte)((ByteTag)Chunk.getChildTag(itemValues, "Count", ByteTag.class))
.getValue();
byte slot = (Byte)((ByteTag)Chunk.getChildTag(itemValues, "Slot", ByteTag.class))
.getValue();
newItems.put(slot, new Countable<BaseItem>(new BaseItem(id, damage), count));
}
this.items = newItems;
}
}

View File

@ -199,6 +199,17 @@ public class Chunk {
((TileEntityBlock)block).fromTileEntityNBT(tileEntity);
}
return block;
// Chests
} else if (id == 54) {
ChestBlock block = new ChestBlock();
Map<String,Tag> tileEntity = getBlockTileEntity(pos);
if (tileEntity != null) {
((TileEntityBlock)block).fromTileEntityNBT(tileEntity);
}
return block;
} else {
return new BaseBlock(id, data);
@ -214,7 +225,7 @@ public class Chunk {
* @return child tag
* @throws InvalidFormatException
*/
private static Tag getChildTag(Map<String,Tag> items, String key, Class expected)
public static Tag getChildTag(Map<String,Tag> items, String key, Class expected)
throws InvalidFormatException {
if (!items.containsKey(key)) {
throw new InvalidFormatException("Missing a \"" + key + "\" tag");