2011-01-18 05:11:39 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
|
2011-01-18 05:11:39 +00:00
|
|
|
*
|
|
|
|
* 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.bukkit;
|
|
|
|
|
2012-02-08 01:24:22 +00:00
|
|
|
import com.sk89q.worldedit.WorldVector;
|
2011-01-18 05:11:39 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import com.sk89q.worldedit.bags.*;
|
2011-11-03 14:48:41 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseItem;
|
|
|
|
import com.sk89q.worldedit.blocks.BaseItemStack;
|
2011-09-03 16:54:20 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BlockID;
|
2011-11-03 23:41:19 +00:00
|
|
|
import com.sk89q.worldedit.blocks.ItemType;
|
2011-01-18 05:11:39 +00:00
|
|
|
|
|
|
|
public class BukkitPlayerBlockBag extends BlockBag {
|
|
|
|
/**
|
|
|
|
* Player instance.
|
|
|
|
*/
|
|
|
|
private Player player;
|
|
|
|
/**
|
|
|
|
* The player's inventory;
|
|
|
|
*/
|
|
|
|
private ItemStack[] items;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
/**
|
|
|
|
* Construct the object.
|
|
|
|
*
|
|
|
|
* @param player
|
|
|
|
*/
|
|
|
|
public BukkitPlayerBlockBag(Player player) {
|
|
|
|
this.player = player;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
/**
|
|
|
|
* Loads inventory on first use.
|
|
|
|
*/
|
|
|
|
private void loadInventory() {
|
|
|
|
if (items == null) {
|
|
|
|
items = player.getInventory().getContents();
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
/**
|
|
|
|
* Get the player.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Player getPlayer() {
|
|
|
|
return player;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
/**
|
|
|
|
* Get a block.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
*/
|
2011-02-18 23:49:50 +00:00
|
|
|
@Override
|
2011-11-04 15:56:53 +00:00
|
|
|
public void fetchItem(BaseItem item) throws BlockBagException {
|
|
|
|
final int id = item.getType();
|
2012-08-24 11:39:52 +00:00
|
|
|
final int damage = item.getData();
|
2011-11-04 15:56:53 +00:00
|
|
|
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
|
|
|
|
assert(amount == 1);
|
|
|
|
boolean usesDamageValue = ItemType.usesDamageValue(id);
|
|
|
|
|
2011-09-03 16:54:20 +00:00
|
|
|
if (id == BlockID.AIR) {
|
2011-01-18 05:11:39 +00:00
|
|
|
throw new IllegalArgumentException("Can't fetch air block");
|
|
|
|
}
|
2011-11-04 15:56:53 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
loadInventory();
|
2011-11-04 15:56:53 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
boolean found = false;
|
2011-11-04 15:56:53 +00:00
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int slot = 0; slot < items.length; ++slot) {
|
2011-11-04 15:56:53 +00:00
|
|
|
ItemStack bukkitItem = items[slot];
|
|
|
|
|
|
|
|
if (bukkitItem == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bukkitItem.getTypeId() != id) {
|
|
|
|
// Type id doesn't fit
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usesDamageValue && bukkitItem.getDurability() != damage) {
|
|
|
|
// Damage value doesn't fit.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int currentAmount = bukkitItem.getAmount();
|
|
|
|
if (currentAmount < 0) {
|
2011-01-18 05:11:39 +00:00
|
|
|
// Unlimited
|
2011-11-04 15:56:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentAmount > 1) {
|
|
|
|
bukkitItem.setAmount(currentAmount - 1);
|
|
|
|
found = true;
|
|
|
|
} else {
|
2011-11-23 01:29:48 +00:00
|
|
|
items[slot] = null;
|
2011-11-04 15:56:53 +00:00
|
|
|
found = true;
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
2011-11-04 15:56:53 +00:00
|
|
|
|
|
|
|
break;
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
2011-11-04 15:56:53 +00:00
|
|
|
|
|
|
|
if (!found) {
|
2011-01-18 05:11:39 +00:00
|
|
|
throw new OutOfBlocksException();
|
|
|
|
}
|
|
|
|
}
|
2011-11-04 15:56:53 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
/**
|
|
|
|
* Store a block.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
*/
|
2011-02-18 23:49:50 +00:00
|
|
|
@Override
|
2011-11-03 14:48:41 +00:00
|
|
|
public void storeItem(BaseItem item) throws BlockBagException {
|
|
|
|
final int id = item.getType();
|
2012-08-24 11:39:52 +00:00
|
|
|
final int damage = item.getData();
|
2011-11-03 14:48:41 +00:00
|
|
|
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
|
|
|
|
assert(amount <= 64);
|
2011-11-03 23:41:19 +00:00
|
|
|
boolean usesDamageValue = ItemType.usesDamageValue(id);
|
2011-11-03 14:48:41 +00:00
|
|
|
|
2011-09-03 16:54:20 +00:00
|
|
|
if (id == BlockID.AIR) {
|
2011-01-18 05:11:39 +00:00
|
|
|
throw new IllegalArgumentException("Can't store air block");
|
|
|
|
}
|
2011-11-03 14:48:41 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
loadInventory();
|
2011-11-03 14:48:41 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
int freeSlot = -1;
|
2011-11-03 14:48:41 +00:00
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int slot = 0; slot < items.length; ++slot) {
|
2011-11-03 14:48:41 +00:00
|
|
|
ItemStack bukkitItem = items[slot];
|
|
|
|
|
|
|
|
if (bukkitItem == null) {
|
|
|
|
// Delay using up a free slot until we know there are no stacks
|
|
|
|
// of this item to merge into
|
2011-11-03 23:41:19 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
if (freeSlot == -1) {
|
|
|
|
freeSlot = slot;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-03 14:48:41 +00:00
|
|
|
|
|
|
|
if (bukkitItem.getTypeId() != id) {
|
|
|
|
// Type id doesn't fit
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usesDamageValue && bukkitItem.getDurability() != damage) {
|
|
|
|
// Damage value doesn't fit.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int currentAmount = bukkitItem.getAmount();
|
|
|
|
if (currentAmount < 0) {
|
2011-01-18 05:11:39 +00:00
|
|
|
// Unlimited
|
2011-11-03 14:48:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (currentAmount >= 64) {
|
|
|
|
// Full stack
|
|
|
|
continue;
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 14:48:41 +00:00
|
|
|
int spaceLeft = 64 - currentAmount;
|
|
|
|
if (spaceLeft >= amount) {
|
|
|
|
bukkitItem.setAmount(currentAmount + amount);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bukkitItem.setAmount(64);
|
|
|
|
amount -= spaceLeft;
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
2011-11-03 14:48:41 +00:00
|
|
|
|
|
|
|
if (freeSlot > -1) {
|
|
|
|
items[freeSlot] = new ItemStack(id, amount);
|
|
|
|
return;
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
2011-11-03 14:48:41 +00:00
|
|
|
|
|
|
|
throw new OutOfSpaceException(id);
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
/**
|
|
|
|
* Flush any changes. This is called at the end.
|
|
|
|
*/
|
2011-02-18 23:49:50 +00:00
|
|
|
@Override
|
2011-01-18 05:11:39 +00:00
|
|
|
public void flushChanges() {
|
|
|
|
if (items != null) {
|
|
|
|
player.getInventory().setContents(items);
|
|
|
|
items = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a position to be used a source.
|
|
|
|
*
|
|
|
|
* @param pos
|
|
|
|
*/
|
2011-02-18 23:49:50 +00:00
|
|
|
@Override
|
2012-02-08 01:24:22 +00:00
|
|
|
public void addSourcePosition(WorldVector pos) {
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-18 05:11:39 +00:00
|
|
|
/**
|
|
|
|
* Adds a position to be used a source.
|
|
|
|
*
|
|
|
|
* @param pos
|
|
|
|
*/
|
2011-02-18 23:49:50 +00:00
|
|
|
@Override
|
2012-02-08 01:24:22 +00:00
|
|
|
public void addSingleSourcePosition(WorldVector pos) {
|
2011-01-18 05:11:39 +00:00
|
|
|
}
|
|
|
|
}
|