From e2edbcda23f062976eb9e2aa1819f34f8fb7255c Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 17 Jan 2011 21:11:39 -0800 Subject: [PATCH] Inventory support now works in Bukkit. --- .../sk89q/worldedit/bukkit/BukkitPlayer.java | 3 +- .../bukkit/BukkitPlayerBlockBag.java | 189 ++++++++++++++++++ 2 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 src/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java diff --git a/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java index 334f409c2..6c1d8b961 100644 --- a/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -149,8 +149,7 @@ public class BukkitPlayer extends LocalPlayer { @Override public BlockBag getInventoryBlockBag() { - // TODO Auto-generated method stub - return null; + return new BukkitPlayerBlockBag(player); } @Override diff --git a/src/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java b/src/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java new file mode 100644 index 000000000..35d1b914e --- /dev/null +++ b/src/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java @@ -0,0 +1,189 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +package com.sk89q.worldedit.bukkit; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.bags.*; + +public class BukkitPlayerBlockBag extends BlockBag { + /** + * Player instance. + */ + private Player player; + /** + * The player's inventory; + */ + private ItemStack[] items; + + /** + * Construct the object. + * + * @param player + */ + public BukkitPlayerBlockBag(Player player) { + this.player = player; + } + + /** + * Loads inventory on first use. + */ + private void loadInventory() { + if (items == null) { + items = player.getInventory().getContents(); + } + } + + /** + * Get the player. + * + * @return + */ + public Player getPlayer() { + return player; + } + + /** + * Get a block. + * + * @param id + */ + public void fetchBlock(int id) throws BlockBagException { + if (id == 0) { + throw new IllegalArgumentException("Can't fetch air block"); + } + + loadInventory(); + + boolean found = false; + + for (int slot = 0; slot < items.length; slot++) { + ItemStack item = items[slot]; + + if (item == null) continue; + + if (item.getTypeId() == id) { + int amount = item.getAmount(); + + // Unlimited + if (amount < 0) { + return; + } + + if (amount > 1) { + item.setAmount(amount - 1); + found = true; + } else { + items[slot] = null; + found = true; + } + + break; + } + } + + if (found) { + } else { + throw new OutOfBlocksException(); + } + } + + /** + * Store a block. + * + * @param id + */ + public void storeBlock(int id) throws BlockBagException { + if (id == 0) { + throw new IllegalArgumentException("Can't store air block"); + } + + loadInventory(); + + boolean found = false; + int freeSlot = -1; + + for (int slot = 0; slot < items.length; slot++) { + ItemStack item = items[slot]; + + // Delay using up a free slot until we know there are no stacks + // of this item to merge into + if (item == null) { + if (freeSlot == -1) { + freeSlot = slot; + } + continue; + } + + if (item.getTypeId() == id) { + int amount = item.getAmount(); + + // Unlimited + if (amount < 0) { + return; + } + + if (amount < 64) { + item.setAmount(amount + 1); + found = true; + break; + } + } + } + + if (!found && freeSlot > -1) { + items[freeSlot] = new ItemStack(id, 1); + found = true; + } + + if (found) { + } else { + throw new OutOfSpaceException(id); + } + } + + /** + * Flush any changes. This is called at the end. + */ + public void flushChanges() { + if (items != null) { + player.getInventory().setContents(items); + items = null; + } + } + + /** + * Adds a position to be used a source. + * + * @param pos + * @return + */ + public void addSourcePosition(Vector pos) { + } + /** + * Adds a position to be used a source. + * + * @param pos + * @return + */ + public void addSingleSourcePosition(Vector pos) { + } +}