From 9e1571a8e56bd605ab5cdfe85e5d0cc4dc7e0d8a Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 6 Dec 2011 17:42:55 +0100 Subject: More cleanup --- .../com/earth2me/essentials/EssentialsUpgrade.java | 1 + .../src/com/earth2me/essentials/FakeInventory.java | 190 -------- .../src/com/earth2me/essentials/FakeWorld.java | 525 -------------------- .../earth2me/essentials/InventoryWorkaround.java | 348 -------------- .../src/com/earth2me/essentials/TargetBlock.java | 527 --------------------- Essentials/src/com/earth2me/essentials/Trade.java | 1 + .../earth2me/essentials/commands/Commandgive.java | 2 +- .../earth2me/essentials/commands/Commanditem.java | 2 +- .../earth2me/essentials/commands/Commandjump.java | 8 +- .../earth2me/essentials/commands/Commandkit.java | 2 +- .../earth2me/essentials/commands/Commandsell.java | 2 +- .../essentials/commands/Commandunlimited.java | 2 +- .../essentials/craftbukkit/FakeInventory.java | 190 ++++++++ .../earth2me/essentials/craftbukkit/FakeWorld.java | 524 ++++++++++++++++++++ .../craftbukkit/InventoryWorkaround.java | 349 ++++++++++++++ .../essentials/craftbukkit/ShowInventory.java | 1 - .../test/com/earth2me/essentials/FakeServer.java | 1 + 17 files changed, 1073 insertions(+), 1602 deletions(-) delete mode 100644 Essentials/src/com/earth2me/essentials/FakeInventory.java delete mode 100644 Essentials/src/com/earth2me/essentials/FakeWorld.java delete mode 100644 Essentials/src/com/earth2me/essentials/InventoryWorkaround.java delete mode 100644 Essentials/src/com/earth2me/essentials/TargetBlock.java create mode 100644 Essentials/src/com/earth2me/essentials/craftbukkit/FakeInventory.java create mode 100644 Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java create mode 100644 Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java diff --git a/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java b/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java index dc2845414..ae444af2e 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.craftbukkit.FakeWorld; import com.earth2me.essentials.settings.Spawns; import com.earth2me.essentials.storage.YamlStorageWriter; import static com.earth2me.essentials.I18n._; diff --git a/Essentials/src/com/earth2me/essentials/FakeInventory.java b/Essentials/src/com/earth2me/essentials/FakeInventory.java deleted file mode 100644 index 6496fb75b..000000000 --- a/Essentials/src/com/earth2me/essentials/FakeInventory.java +++ /dev/null @@ -1,190 +0,0 @@ -package com.earth2me.essentials; - -import java.util.HashMap; -import org.bukkit.Material; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - - -public class FakeInventory implements Inventory -{ - ItemStack[] items; - - public FakeInventory(ItemStack[] items) - { - this.items = new ItemStack[items.length]; - for (int i = 0; i < items.length; i++) - { - if (items[i] == null) - { - continue; - } - this.items[i] = items[i].clone(); - } - } - - @Override - public int getSize() - { - return items.length; - } - - @Override - public String getName() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ItemStack getItem(int i) - { - return items[i]; - } - - @Override - public void setItem(int i, ItemStack is) - { - items[i] = is; - } - - @Override - public HashMap addItem(ItemStack... iss) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap removeItem(ItemStack... iss) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ItemStack[] getContents() - { - return items; - } - - @Override - public void setContents(ItemStack[] iss) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(Material mtrl, int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(ItemStack is, int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap all(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap all(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap all(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int first(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int first(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int first(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int firstEmpty() - { - for (int i = 0; i < items.length; i++) - { - if (items[i] == null || items[i].getTypeId() == 0) { - return i; - } - } - return -1; - } - - @Override - public void remove(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void remove(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void remove(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void clear(int i) - { - items[i] = null; - } - - @Override - public void clear() - { - for (int i = 0; i < items.length; i++) - { - items[i] = null; - } - } -} diff --git a/Essentials/src/com/earth2me/essentials/FakeWorld.java b/Essentials/src/com/earth2me/essentials/FakeWorld.java deleted file mode 100644 index 0032b6845..000000000 --- a/Essentials/src/com/earth2me/essentials/FakeWorld.java +++ /dev/null @@ -1,525 +0,0 @@ -package com.earth2me.essentials; - -import java.io.File; -import java.util.List; -import java.util.UUID; -import org.bukkit.*; -import org.bukkit.block.Biome; -import org.bukkit.block.Block; -import org.bukkit.entity.*; -import org.bukkit.generator.BlockPopulator; -import org.bukkit.generator.ChunkGenerator; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - - -public class FakeWorld implements World -{ - - private final String name; - private final Environment env; - FakeWorld(String string, Environment environment) - { - this.name = string; - this.env = environment; - } - - @Override - public Block getBlockAt(int i, int i1, int i2) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Block getBlockAt(Location lctn) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getBlockTypeIdAt(int i, int i1, int i2) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getBlockTypeIdAt(Location lctn) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getHighestBlockYAt(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getHighestBlockYAt(Location lctn) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Chunk getChunkAt(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Chunk getChunkAt(Location lctn) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Chunk getChunkAt(Block block) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean isChunkLoaded(Chunk chunk) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Chunk[] getLoadedChunks() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void loadChunk(Chunk chunk) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean isChunkLoaded(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void loadChunk(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean loadChunk(int i, int i1, boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean unloadChunk(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean unloadChunk(int i, int i1, boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean unloadChunk(int i, int i1, boolean bln, boolean bln1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean unloadChunkRequest(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean unloadChunkRequest(int i, int i1, boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean regenerateChunk(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean refreshChunk(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Item dropItem(Location lctn, ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Item dropItemNaturally(Location lctn, ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Arrow spawnArrow(Location lctn, Vector vector, float f, float f1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean generateTree(Location lctn, TreeType tt) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean generateTree(Location lctn, TreeType tt, BlockChangeDelegate bcd) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public LivingEntity spawnCreature(Location lctn, CreatureType ct) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public LightningStrike strikeLightning(Location lctn) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public LightningStrike strikeLightningEffect(Location lctn) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public List getEntities() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public List getLivingEntities() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public List getPlayers() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public String getName() - { - return name; - } - - @Override - public long getId() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Location getSpawnLocation() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean setSpawnLocation(int i, int i1, int i2) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public long getTime() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setTime(long l) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public long getFullTime() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setFullTime(long l) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean hasStorm() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setStorm(boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getWeatherDuration() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setWeatherDuration(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean isThundering() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setThundering(boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getThunderDuration() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setThunderDuration(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Environment getEnvironment() - { - return env; - } - - @Override - public long getSeed() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean getPVP() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setPVP(boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void save() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean createExplosion(double d, double d1, double d2, float f) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean createExplosion(Location lctn, float f) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ChunkGenerator getGenerator() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public List getPopulators() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void playEffect(Location lctn, Effect effect, int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void playEffect(Location lctn, Effect effect, int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean createExplosion(double d, double d1, double d2, float f, boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean createExplosion(Location lctn, float f, boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public T spawn(Location lctn, Class type) throws IllegalArgumentException - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ChunkSnapshot getEmptyChunkSnapshot(int i, int i1, boolean bln, boolean bln1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setSpawnFlags(boolean bln, boolean bln1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean getAllowAnimals() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean getAllowMonsters() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public UUID getUID() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Block getHighestBlockAt(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Block getHighestBlockAt(Location lctn) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - - @Override - public Biome getBiome(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public double getTemperature(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public double getHumidity(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean unloadChunk(Chunk chunk) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getMaxHeight() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean getKeepSpawnInMemory() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setKeepSpawnInMemory(boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean isAutoSave() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setAutoSave(boolean bln) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Difficulty getDifficulty() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setDifficulty(Difficulty difficulty) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getSeaLevel() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public File getWorldFolder() - { - throw new UnsupportedOperationException("Not supported yet."); - } -} diff --git a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java deleted file mode 100644 index 5d020f08b..000000000 --- a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java +++ /dev/null @@ -1,348 +0,0 @@ -package com.earth2me.essentials; - -import java.util.HashMap; -import java.util.Map; -import org.bukkit.Location; -import org.bukkit.entity.Item; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -/* - * This class can be removed when - * https://github.com/Bukkit/CraftBukkit/pull/193 - * is accepted to CraftBukkit - */ - -public final class InventoryWorkaround -{ - private InventoryWorkaround() - { - } - - public static int first(final Inventory inventory, final ItemStack item, final boolean forceDurability, final boolean forceAmount) - { - return next(inventory, item, 0, forceDurability, forceAmount); - } - - public static int next(final Inventory cinventory, final ItemStack item, final int start, final boolean forceDurability, final boolean forceAmount) - { - final ItemStack[] inventory = cinventory.getContents(); - for (int i = start; i < inventory.length; i++) - { - final ItemStack cItem = inventory[i]; - if (cItem == null) - { - continue; - } - if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments())) - { - return i; - } - } - return -1; - } - - public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability) - { - return firstPartial(cinventory, item, forceDurability, item.getType().getMaxStackSize()); - } - - public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability, final int maxAmount) - { - if (item == null) - { - return -1; - } - final ItemStack[] inventory = cinventory.getContents(); - for (int i = 0; i < inventory.length; i++) - { - final ItemStack cItem = inventory[i]; - if (cItem == null) - { - continue; - } - if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < maxAmount && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments())) - { - return i; - } - } - return -1; - } - - public static boolean addAllItems(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) - { - final Inventory fake = new FakeInventory(cinventory.getContents()); - if (addItem(fake, forceDurability, items).isEmpty()) - { - addItem(cinventory, forceDurability, items); - return true; - } - else - { - return false; - } - } - - public static Map addItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) - { - return addItem(cinventory, forceDurability, 0, items); - } - - public static Map addItem(final Inventory cinventory, final boolean forceDurability, final int oversizedStacks, final ItemStack... items) - { - final Map leftover = new HashMap(); - - /* TODO: some optimization - * - Create a 'firstPartial' with a 'fromIndex' - * - Record the lastPartial per Material - * - Cache firstEmpty result - */ - - // combine items - - ItemStack[] combined = new ItemStack[items.length]; - for (int i = 0; i < items.length; i++) - { - if (items[i] == null || items[i].getAmount() < 1) - { - continue; - } - for (int j = 0; j < combined.length; j++) - { - if (combined[j] == null) - { - combined[j] = items[i].clone(); - break; - } - if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments())) - { - combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); - break; - } - } - } - - - for (int i = 0; i < combined.length; i++) - { - final ItemStack item = combined[i]; - if (item == null) - { - continue; - } - - while (true) - { - // Do we already have a stack of it? - final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize(); - final int firstPartial = firstPartial(cinventory, item, forceDurability, maxAmount); - - // Drat! no partial stack - if (firstPartial == -1) - { - // Find a free spot! - final int firstFree = cinventory.firstEmpty(); - - if (firstFree == -1) - { - // No space at all! - leftover.put(i, item); - break; - } - else - { - // More than a single stack! - if (item.getAmount() > maxAmount) - { - final ItemStack stack = item.clone(); - stack.setAmount(maxAmount); - cinventory.setItem(firstFree, stack); - item.setAmount(item.getAmount() - maxAmount); - } - else - { - // Just store it - cinventory.setItem(firstFree, item); - break; - } - } - } - else - { - // So, apparently it might only partially fit, well lets do just that - final ItemStack partialItem = cinventory.getItem(firstPartial); - - final int amount = item.getAmount(); - final int partialAmount = partialItem.getAmount(); - - // Check if it fully fits - if (amount + partialAmount <= maxAmount) - { - partialItem.setAmount(amount + partialAmount); - break; - } - - // It fits partially - partialItem.setAmount(maxAmount); - item.setAmount(amount + partialAmount - maxAmount); - } - } - } - return leftover; - } - - public static Map removeItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) - { - final Map leftover = new HashMap(); - - // TODO: optimization - - for (int i = 0; i < items.length; i++) - { - final ItemStack item = items[i]; - if (item == null) - { - continue; - } - int toDelete = item.getAmount(); - - while (true) - { - - // Bail when done - if (toDelete <= 0) - { - break; - } - - // get first Item, ignore the amount - final int first = first(cinventory, item, forceDurability, false); - - // Drat! we don't have this type in the inventory - if (first == -1) - { - item.setAmount(toDelete); - leftover.put(i, item); - break; - } - else - { - final ItemStack itemStack = cinventory.getItem(first); - final int amount = itemStack.getAmount(); - - if (amount <= toDelete) - { - toDelete -= amount; - // clear the slot, all used up - cinventory.clear(first); - } - else - { - // split the stack and store - itemStack.setAmount(amount - toDelete); - cinventory.setItem(first, itemStack); - toDelete = 0; - } - } - } - } - return leftover; - } - - public static boolean containsItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) - { - final Map leftover = new HashMap(); - - // TODO: optimization - - // combine items - - ItemStack[] combined = new ItemStack[items.length]; - for (int i = 0; i < items.length; i++) - { - if (items[i] == null) - { - continue; - } - for (int j = 0; j < combined.length; j++) - { - if (combined[j] == null) - { - combined[j] = items[i].clone(); - break; - } - if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments())) - { - combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); - break; - } - } - } - - for (int i = 0; i < combined.length; i++) - { - final ItemStack item = combined[i]; - if (item == null) - { - continue; - } - int mustHave = item.getAmount(); - int position = 0; - - while (true) - { - // Bail when done - if (mustHave <= 0) - { - break; - } - - final int slot = next(cinventory, item, position, forceDurability, false); - - // Drat! we don't have this type in the inventory - if (slot == -1) - { - leftover.put(i, item); - break; - } - else - { - final ItemStack itemStack = cinventory.getItem(slot); - final int amount = itemStack.getAmount(); - - if (amount <= mustHave) - { - mustHave -= amount; - } - else - { - mustHave = 0; - } - position = slot + 1; - } - } - } - return leftover.isEmpty(); - } - - public static Item[] dropItem(final Location loc, final ItemStack itm) - { - final int maxStackSize = itm.getType().getMaxStackSize(); - final int stacks = itm.getAmount() / maxStackSize; - final int leftover = itm.getAmount() % maxStackSize; - final Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)]; - for (int i = 0; i < stacks; i++) - { - final ItemStack stack = itm.clone(); - stack.setAmount(maxStackSize); - itemStacks[i] = loc.getWorld().dropItem(loc, stack); - } - if (leftover > 0) - { - final ItemStack stack = itm.clone(); - stack.setAmount(leftover); - itemStacks[stacks] = loc.getWorld().dropItem(loc, stack); - } - return itemStacks; - } -} diff --git a/Essentials/src/com/earth2me/essentials/TargetBlock.java b/Essentials/src/com/earth2me/essentials/TargetBlock.java deleted file mode 100644 index fb6bfabb6..000000000 --- a/Essentials/src/com/earth2me/essentials/TargetBlock.java +++ /dev/null @@ -1,527 +0,0 @@ -package com.earth2me.essentials; - -import java.util.List; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; - - -/** - * Original authors: toi & Raphfrk - */ -@Deprecated -public class TargetBlock -{ - private transient final Location location; - private transient final double viewHeight; - private transient final int maxDistance; - private transient final int[] blockToIgnore; - private transient final double checkDistance; - private transient double curDistance; - private transient double targetPositionX; - private transient double targetPositionY; - private transient double targetPositionZ; - private transient int itargetPositionX; - private transient int itargetPositionY; - private transient int itargetPositionZ; - private transient int prevPositionX; - private transient int prevPositionY; - private transient int prevPositionZ; - private transient final double offsetX; - private transient final double offsetY; - private transient final double offsetZ; - - /** - * Constructor requiring a player, uses default values - * - * @param player Player to work with - */ - public TargetBlock(final Player player) - { - this(player.getLocation(), 300, 1.65, 0.2, null); - } - - /** - * Constructor requiring a location, uses default values - * - * @param loc Location to work with - */ - public TargetBlock(final Location loc) - { - this(loc, 300, 0, 0.2, null); - } - - /** - * Constructor requiring a player, max distance and a checking distance - * - * @param player Player to work with - * @param maxDistance How far it checks for blocks - * @param checkDistance How often to check for blocks, the smaller the more precise - */ - public TargetBlock(final Player player, final int maxDistance, final double checkDistance) - { - this(player.getLocation(), maxDistance, 1.65, checkDistance, null); - } - - /** - * Constructor requiring a location, max distance and a checking distance - * - * @param loc What location to work with - * @param maxDistance How far it checks for blocks - * @param checkDistance How often to check for blocks, the smaller the more precise - */ - public TargetBlock(final Location loc, final int maxDistance, final double checkDistance) - { - this(loc, maxDistance, 0, checkDistance, null); - } - - /** - * Constructor requiring a player, max distance, checking distance and an array of blocks to ignore - * - * @param player What player to work with - * @param maxDistance How far it checks for blocks - * @param checkDistance How often to check for blocks, the smaller the more precise - * @param blocksToIgnore Integer array of what block ids to ignore while checking for viable targets - */ - public TargetBlock(final Player player, final int maxDistance, final double checkDistance, final int[] blocksToIgnore) - { - this(player.getLocation(), maxDistance, 1.65, checkDistance, blocksToIgnore); - } - - /** - * Constructor requiring a location, max distance, checking distance and an array of blocks to ignore - * - * @param loc What location to work with - * @param maxDistance How far it checks for blocks - * @param checkDistance How often to check for blocks, the smaller the more precise - * @param blocksToIgnore Array of what block ids to ignore while checking for viable targets - */ - public TargetBlock(final Location loc, final int maxDistance, final double checkDistance, final int[] blocksToIgnore) - { - this(loc, maxDistance, 0, checkDistance, blocksToIgnore); - } - - /** - * Constructor requiring a player, max distance, checking distance and an array of blocks to ignore - * - * @param player What player to work with - * @param maxDistance How far it checks for blocks - * @param checkDistance How often to check for blocks, the smaller the more precise - * @param blocksToIgnore String ArrayList of what block ids to ignore while checking for viable targets - */ - public TargetBlock(final Player player, final int maxDistance, final double checkDistance, final List blocksToIgnore) - { - this(player.getLocation(), maxDistance, 1.65, checkDistance, TargetBlock.convertStringArraytoIntArray(blocksToIgnore)); - } - - /** - * Constructor requiring a location, max distance, checking distance and an array of blocks to ignore - * - * @param loc What location to work with - * @param maxDistance How far it checks for blocks - * @param checkDistance How often to check for blocks, the smaller the more precise - * @param blocksToIgnore String ArrayList of what block ids to ignore while checking for viable targets - */ - public TargetBlock(final Location loc, final int maxDistance, final double checkDistance, final List blocksToIgnore) - { - this(loc, maxDistance, 0, checkDistance, TargetBlock.convertStringArraytoIntArray(blocksToIgnore)); - } - - /** - * Set the values, all constructors uses this function - * - * @param loc Location of the view - * @param maxDistance How far it checks for blocks - * @param viewPos Where the view is positioned in y-axis - * @param checkDistance How often to check for blocks, the smaller the more precise - * @param blocksToIgnore Ids of blocks to ignore while checking for viable targets - */ - private TargetBlock(final Location loc, final int maxDistance, final double viewHeight, final double checkDistance, final int[] blocksToIgnore) - { - this.location = loc; - this.maxDistance = maxDistance; - this.viewHeight = viewHeight; - this.checkDistance = checkDistance; - if (blocksToIgnore == null || blocksToIgnore.length == 0) - { - this.blockToIgnore = new int[0]; - } - else - { - this.blockToIgnore = new int[blocksToIgnore.length]; - System.arraycopy(blocksToIgnore, 0, this.blockToIgnore, 0, this.blockToIgnore.length); - } - - final double xRotation = (loc.getYaw() + 90) % 360; - final double yRotation = loc.getPitch() * -1; - - final double hypotenuse = (checkDistance * Math.cos(Math.toRadians(yRotation))); - offsetX = hypotenuse * Math.cos(Math.toRadians(xRotation)); - offsetY = checkDistance * Math.sin(Math.toRadians(yRotation)); - offsetZ = hypotenuse * Math.sin(Math.toRadians(xRotation)); - - reset(); - } - - /** - * Call this to reset checking position to allow you to check for a new target with the same TargetBlock instance. - */ - public final void reset() - { - targetPositionX = location.getX(); - targetPositionY = location.getY() + viewHeight; - targetPositionZ = location.getZ(); - itargetPositionX = (int)Math.floor(targetPositionX); - itargetPositionY = (int)Math.floor(targetPositionY); - itargetPositionZ = (int)Math.floor(targetPositionZ); - prevPositionX = itargetPositionX; - prevPositionY = itargetPositionY; - prevPositionZ = itargetPositionZ; - this.curDistance = 0; - } - - /** - * Gets the distance to a block. Measures from the block underneath the player to the targetblock - * Should only be used when passing player as an constructor parameter - * - * @return double - */ - public double getDistanceToBlock() - { - final double blockUnderPlayerX = Math.floor(location.getX() + 0.5); - final double blockUnderPlayerY = Math.floor(location.getY() - 0.5); - final double blockUnderPlayerZ = Math.floor(location.getZ() + 0.5); - - final Block block = getTargetBlock(); - final double distX = block.getX() - blockUnderPlayerX; - final double distY = block.getY() - blockUnderPlayerY; - final double distZ = block.getZ() - blockUnderPlayerZ; - - return Math.sqrt(distX*distX + distY*distY + distZ*distZ); - } - - /** - * Gets the rounded distance to a block. Measures from the block underneath the player to the targetblock - * Should only be used when passing player as an constructor parameter - * - * @return int - */ - public int getDistanceToBlockRounded() - { - return (int)Math.round(getDistanceToBlock()); - } - - /** - * Gets the floored x distance to a block. - * - * @return int - */ - public int getXDistanceToBlock() - { - return (int)Math.floor(getTargetBlock().getX() - location.getBlockX() + 0.5); - } - - /** - * Gets the floored y distance to a block - * - * @return int - */ - public int getYDistanceToBlock() - { - return (int)Math.floor(getTargetBlock().getY() - location.getBlockY() + viewHeight); - } - - /** - * Gets the floored z distance to a block - * - * @return int - */ - public int getZDistanceToBlock() - { - return (int)Math.floor(getTargetBlock().getZ() - location.getBlockZ() + 0.5); - } - - /** - * Returns the block at the sight. Returns null if out of range or if no viable target was found - * - * @return Block - */ - public Block getTargetBlock() - { - this.reset(); - Block block; - do - { - block = getNextBlock(); - } - while (block != null && ((block.getTypeId() == 0) || this.blockIsIgnored(block.getTypeId()))); - - return block; - } - - /** - * Sets the type of the block at the sight. Returns false if the block wasn't set. - * - * @param typeID ID of type to set the block to - * @return boolean - */ - public boolean setTargetBlock(final int typeID) - { - return setTargetBlock(Material.getMaterial(typeID)); - } - - /** - * Sets the type of the block at the sight. Returns false if the block wasn't set. - * - * @param type Material to set the block to - * @return boolean - */ - @SuppressWarnings("empty-statement") - public boolean setTargetBlock(final Material type) - { - if (type == null) - { - return false; - } - final Block block = getTargetBlock(); - if (block != null) - { - block.setType(type); - return true; - } - return false; - } - - /** - * Sets the type of the block at the sight. Returns false if the block wasn't set. - * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented - * - * @param type Name of type to set the block to - * @return boolean - */ - public boolean setTargetBlock(final String type) - { - return setTargetBlock(Material.valueOf(type)); - } - - /** - * Returns the block attached to the face at the sight. Returns null if out of range or if no viable target was found - * - * @return Block - */ - public Block getFaceBlock() - { - final Block block = getTargetBlock(); - if (block == null) - { - return null; - } - return getPreviousBlock(); - } - - /** - * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set. - * - * @param typeID - * @return boolean - */ - public boolean setFaceBlock(final int typeID) - { - return setFaceBlock(Material.getMaterial(typeID)); - } - - /** - * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set. - * - * @param type - * @return boolean - */ - public boolean setFaceBlock(final Material type) - { - if (type == null) - { - return false; - } - if (getCurrentBlock() != null) - { - final Block blk = location.getWorld().getBlockAt(prevPositionX, prevPositionY, prevPositionZ); - blk.setType(type); - return true; - } - return false; - } - - /** - * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set. - * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented - * - * @param type - * @return boolean - */ - public boolean setFaceBlock(final String type) - { - return setFaceBlock(Material.valueOf(type)); - } - - /** - * Get next block - * - * @return Block - */ - public Block getNextBlock() - { - prevPositionX = itargetPositionX; - prevPositionY = itargetPositionY; - prevPositionZ = itargetPositionZ; - do - { - curDistance += checkDistance; - - targetPositionX += offsetX; - targetPositionY += offsetY; - targetPositionZ += offsetZ; - itargetPositionX = (int)Math.floor(targetPositionX); - itargetPositionY = (int)Math.floor(targetPositionY); - itargetPositionZ = (int)Math.floor(targetPositionZ); - } - while (curDistance <= maxDistance && itargetPositionX == prevPositionX && itargetPositionY == prevPositionY && itargetPositionZ == prevPositionZ); - if (curDistance > maxDistance) - { - return null; - } - - return this.location.getWorld().getBlockAt(itargetPositionX, itargetPositionY, itargetPositionZ); - } - - /** - * Returns the current block along the line of vision - * - * @return Block - */ - public Block getCurrentBlock() - { - Block block; - if (curDistance <= maxDistance) - { - block = this.location.getWorld().getBlockAt(itargetPositionX, itargetPositionY, itargetPositionZ); - } - else - { - block = null; - } - return block; - } - - /** - * Sets current block type. Returns false if the block wasn't set. - * - * @param typeID - */ - public boolean setCurrentBlock(final int typeID) - { - return setCurrentBlock(Material.getMaterial(typeID)); - } - - /** - * Sets current block type. Returns false if the block wasn't set. - * - * @param type - */ - public boolean setCurrentBlock(final Material type) - { - final Block blk = getCurrentBlock(); - if (blk != null && type != null) - { - blk.setType(type); - return true; - } - return false; - } - - /** - * Sets current block type. Returns false if the block wasn't set. - * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented - * - * @param type - */ - public boolean setCurrentBlock(final String type) - { - return setCurrentBlock(Material.valueOf(type)); - } - - /** - * Returns the previous block in the aimed path - * - * @return Block - */ - public Block getPreviousBlock() - { - return this.location.getWorld().getBlockAt(prevPositionX, prevPositionY, prevPositionZ); - } - - /** - * Sets previous block type id. Returns false if the block wasn't set. - * - * @param typeID - */ - public boolean setPreviousBlock(final int typeID) - { - return setPreviousBlock(Material.getMaterial(typeID)); - } - - /** - * Sets previous block type id. Returns false if the block wasn't set. - * - * @param type - */ - public boolean setPreviousBlock(final Material type) - { - final Block blk = getPreviousBlock(); - if (blk != null && type != null) - { - blk.setType(type); - return true; - } - return false; - } - - /** - * Sets previous block type id. Returns false if the block wasn't set. - * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented - * - * @param type - */ - public boolean setPreviousBlock(final String type) - { - return setPreviousBlock(Material.valueOf(type)); - } - - private static int[] convertStringArraytoIntArray(final List array) - { - final int intarray[] = new int[array == null ? 0 : array.size()]; - for (int i = 0; i < intarray.length; i++) - { - try - { - intarray[i] = Integer.parseInt(array.get(i)); - } - catch (NumberFormatException nfe) - { - } - } - return intarray; - } - - private boolean blockIsIgnored(final int value) - { - for (int i : this.blockToIgnore) - { - if (i == value) - { - return true; - } - } - return false; - } -} \ No newline at end of file diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 7d2480660..cf4c3080d 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import static com.earth2me.essentials.I18n._; import java.io.File; import java.io.FileWriter; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java index 6b5720cce..d6e3d8f98 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.InventoryWorkaround; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.User; import java.util.Locale; import org.bukkit.ChatColor; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java index 8628c3741..405b8b799 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.InventoryWorkaround; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; import java.util.Locale; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java index 93ce45fd4..8e37da0c8 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java @@ -1,9 +1,9 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.TargetBlock; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import com.earth2me.essentials.Util; import org.bukkit.Location; import org.bukkit.Server; @@ -15,7 +15,6 @@ public class Commandjump extends EssentialsCommand super("jump"); } - //TODO: Update to use new target methods @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { @@ -24,10 +23,7 @@ public class Commandjump extends EssentialsCommand try { - loc = new TargetBlock(user, 100, 2.65).getTargetBlock().getLocation(); - loc.setYaw(cloc.getYaw()); - loc.setPitch(cloc.getPitch()); - loc = new TargetBlock(loc).getPreviousBlock().getLocation(); + loc = Util.getTarget(user); loc.setYaw(cloc.getYaw()); loc.setPitch(cloc.getPitch()); loc.setY(loc.getY() + 1); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandkit.java b/Essentials/src/com/earth2me/essentials/commands/Commandkit.java index ebfec2d2d..8392d4759 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandkit.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandkit.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.InventoryWorkaround; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java index f0e1cb150..ceec6b79d 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java @@ -1,7 +1,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.InventoryWorkaround; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java b/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java index 8891036c6..ec4730aff 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java @@ -1,7 +1,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.InventoryWorkaround; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.User; import java.util.List; import java.util.Locale; diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/FakeInventory.java b/Essentials/src/com/earth2me/essentials/craftbukkit/FakeInventory.java new file mode 100644 index 000000000..992791710 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/FakeInventory.java @@ -0,0 +1,190 @@ +package com.earth2me.essentials.craftbukkit; + +import java.util.HashMap; +import org.bukkit.Material; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + + +public class FakeInventory implements Inventory +{ + ItemStack[] items; + + public FakeInventory(ItemStack[] items) + { + this.items = new ItemStack[items.length]; + for (int i = 0; i < items.length; i++) + { + if (items[i] == null) + { + continue; + } + this.items[i] = items[i].clone(); + } + } + + @Override + public int getSize() + { + return items.length; + } + + @Override + public String getName() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public ItemStack getItem(int i) + { + return items[i]; + } + + @Override + public void setItem(int i, ItemStack is) + { + items[i] = is; + } + + @Override + public HashMap addItem(ItemStack... iss) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public HashMap removeItem(ItemStack... iss) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public ItemStack[] getContents() + { + return items; + } + + @Override + public void setContents(ItemStack[] iss) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean contains(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean contains(Material mtrl) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean contains(ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean contains(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean contains(Material mtrl, int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean contains(ItemStack is, int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public HashMap all(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public HashMap all(Material mtrl) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public HashMap all(ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int first(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int first(Material mtrl) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int first(ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int firstEmpty() + { + for (int i = 0; i < items.length; i++) + { + if (items[i] == null || items[i].getTypeId() == 0) { + return i; + } + } + return -1; + } + + @Override + public void remove(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void remove(Material mtrl) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void remove(ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void clear(int i) + { + items[i] = null; + } + + @Override + public void clear() + { + for (int i = 0; i < items.length; i++) + { + items[i] = null; + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java b/Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java new file mode 100644 index 000000000..bd4d09111 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java @@ -0,0 +1,524 @@ +package com.earth2me.essentials.craftbukkit; + +import java.io.File; +import java.util.List; +import java.util.UUID; +import org.bukkit.*; +import org.bukkit.block.Biome; +import org.bukkit.block.Block; +import org.bukkit.entity.*; +import org.bukkit.generator.BlockPopulator; +import org.bukkit.generator.ChunkGenerator; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + + +public class FakeWorld implements World +{ + private final String name; + private final Environment env; + + public FakeWorld(String string, Environment environment) + { + this.name = string; + this.env = environment; + } + + @Override + public Block getBlockAt(int i, int i1, int i2) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Block getBlockAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getBlockTypeIdAt(int i, int i1, int i2) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getBlockTypeIdAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getHighestBlockYAt(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getHighestBlockYAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Chunk getChunkAt(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Chunk getChunkAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Chunk getChunkAt(Block block) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean isChunkLoaded(Chunk chunk) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Chunk[] getLoadedChunks() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void loadChunk(Chunk chunk) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean isChunkLoaded(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void loadChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean loadChunk(int i, int i1, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean unloadChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean unloadChunk(int i, int i1, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean unloadChunk(int i, int i1, boolean bln, boolean bln1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean unloadChunkRequest(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean unloadChunkRequest(int i, int i1, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean regenerateChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean refreshChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Item dropItem(Location lctn, ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Item dropItemNaturally(Location lctn, ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Arrow spawnArrow(Location lctn, Vector vector, float f, float f1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean generateTree(Location lctn, TreeType tt) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean generateTree(Location lctn, TreeType tt, BlockChangeDelegate bcd) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public LivingEntity spawnCreature(Location lctn, CreatureType ct) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public LightningStrike strikeLightning(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public LightningStrike strikeLightningEffect(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public List getEntities() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public List getLivingEntities() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public List getPlayers() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public String getName() + { + return name; + } + + @Override + public long getId() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Location getSpawnLocation() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean setSpawnLocation(int i, int i1, int i2) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public long getTime() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setTime(long l) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public long getFullTime() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setFullTime(long l) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean hasStorm() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setStorm(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getWeatherDuration() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setWeatherDuration(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean isThundering() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setThundering(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getThunderDuration() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setThunderDuration(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Environment getEnvironment() + { + return env; + } + + @Override + public long getSeed() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean getPVP() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setPVP(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void save() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean createExplosion(double d, double d1, double d2, float f) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean createExplosion(Location lctn, float f) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public ChunkGenerator getGenerator() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public List getPopulators() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void playEffect(Location lctn, Effect effect, int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void playEffect(Location lctn, Effect effect, int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean createExplosion(double d, double d1, double d2, float f, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean createExplosion(Location lctn, float f, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public T spawn(Location lctn, Class type) throws IllegalArgumentException + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public ChunkSnapshot getEmptyChunkSnapshot(int i, int i1, boolean bln, boolean bln1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setSpawnFlags(boolean bln, boolean bln1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean getAllowAnimals() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean getAllowMonsters() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public UUID getUID() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Block getHighestBlockAt(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Block getHighestBlockAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Biome getBiome(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public double getTemperature(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public double getHumidity(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean unloadChunk(Chunk chunk) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getMaxHeight() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean getKeepSpawnInMemory() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setKeepSpawnInMemory(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean isAutoSave() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setAutoSave(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Difficulty getDifficulty() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setDifficulty(Difficulty difficulty) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int getSeaLevel() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public File getWorldFolder() + { + throw new UnsupportedOperationException("Not supported yet."); + } +} diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java new file mode 100644 index 000000000..e58b6a16a --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java @@ -0,0 +1,349 @@ +package com.earth2me.essentials.craftbukkit; + +import com.earth2me.essentials.craftbukkit.FakeInventory; +import java.util.HashMap; +import java.util.Map; +import org.bukkit.Location; +import org.bukkit.entity.Item; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +/* + * This class can be removed when + * https://github.com/Bukkit/CraftBukkit/pull/193 + * is accepted to CraftBukkit + */ + +public final class InventoryWorkaround +{ + private InventoryWorkaround() + { + } + + public static int first(final Inventory inventory, final ItemStack item, final boolean forceDurability, final boolean forceAmount) + { + return next(inventory, item, 0, forceDurability, forceAmount); + } + + public static int next(final Inventory cinventory, final ItemStack item, final int start, final boolean forceDurability, final boolean forceAmount) + { + final ItemStack[] inventory = cinventory.getContents(); + for (int i = start; i < inventory.length; i++) + { + final ItemStack cItem = inventory[i]; + if (cItem == null) + { + continue; + } + if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments())) + { + return i; + } + } + return -1; + } + + public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability) + { + return firstPartial(cinventory, item, forceDurability, item.getType().getMaxStackSize()); + } + + public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability, final int maxAmount) + { + if (item == null) + { + return -1; + } + final ItemStack[] inventory = cinventory.getContents(); + for (int i = 0; i < inventory.length; i++) + { + final ItemStack cItem = inventory[i]; + if (cItem == null) + { + continue; + } + if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < maxAmount && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments())) + { + return i; + } + } + return -1; + } + + public static boolean addAllItems(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) + { + final Inventory fake = new FakeInventory(cinventory.getContents()); + if (addItem(fake, forceDurability, items).isEmpty()) + { + addItem(cinventory, forceDurability, items); + return true; + } + else + { + return false; + } + } + + public static Map addItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) + { + return addItem(cinventory, forceDurability, 0, items); + } + + public static Map addItem(final Inventory cinventory, final boolean forceDurability, final int oversizedStacks, final ItemStack... items) + { + final Map leftover = new HashMap(); + + /* TODO: some optimization + * - Create a 'firstPartial' with a 'fromIndex' + * - Record the lastPartial per Material + * - Cache firstEmpty result + */ + + // combine items + + ItemStack[] combined = new ItemStack[items.length]; + for (int i = 0; i < items.length; i++) + { + if (items[i] == null || items[i].getAmount() < 1) + { + continue; + } + for (int j = 0; j < combined.length; j++) + { + if (combined[j] == null) + { + combined[j] = items[i].clone(); + break; + } + if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments())) + { + combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); + break; + } + } + } + + + for (int i = 0; i < combined.length; i++) + { + final ItemStack item = combined[i]; + if (item == null) + { + continue; + } + + while (true) + { + // Do we already have a stack of it? + final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize(); + final int firstPartial = firstPartial(cinventory, item, forceDurability, maxAmount); + + // Drat! no partial stack + if (firstPartial == -1) + { + // Find a free spot! + final int firstFree = cinventory.firstEmpty(); + + if (firstFree == -1) + { + // No space at all! + leftover.put(i, item); + break; + } + else + { + // More than a single stack! + if (item.getAmount() > maxAmount) + { + final ItemStack stack = item.clone(); + stack.setAmount(maxAmount); + cinventory.setItem(firstFree, stack); + item.setAmount(item.getAmount() - maxAmount); + } + else + { + // Just store it + cinventory.setItem(firstFree, item); + break; + } + } + } + else + { + // So, apparently it might only partially fit, well lets do just that + final ItemStack partialItem = cinventory.getItem(firstPartial); + + final int amount = item.getAmount(); + final int partialAmount = partialItem.getAmount(); + + // Check if it fully fits + if (amount + partialAmount <= maxAmount) + { + partialItem.setAmount(amount + partialAmount); + break; + } + + // It fits partially + partialItem.setAmount(maxAmount); + item.setAmount(amount + partialAmount - maxAmount); + } + } + } + return leftover; + } + + public static Map removeItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) + { + final Map leftover = new HashMap(); + + // TODO: optimization + + for (int i = 0; i < items.length; i++) + { + final ItemStack item = items[i]; + if (item == null) + { + continue; + } + int toDelete = item.getAmount(); + + while (true) + { + + // Bail when done + if (toDelete <= 0) + { + break; + } + + // get first Item, ignore the amount + final int first = first(cinventory, item, forceDurability, false); + + // Drat! we don't have this type in the inventory + if (first == -1) + { + item.setAmount(toDelete); + leftover.put(i, item); + break; + } + else + { + final ItemStack itemStack = cinventory.getItem(first); + final int amount = itemStack.getAmount(); + + if (amount <= toDelete) + { + toDelete -= amount; + // clear the slot, all used up + cinventory.clear(first); + } + else + { + // split the stack and store + itemStack.setAmount(amount - toDelete); + cinventory.setItem(first, itemStack); + toDelete = 0; + } + } + } + } + return leftover; + } + + public static boolean containsItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) + { + final Map leftover = new HashMap(); + + // TODO: optimization + + // combine items + + ItemStack[] combined = new ItemStack[items.length]; + for (int i = 0; i < items.length; i++) + { + if (items[i] == null) + { + continue; + } + for (int j = 0; j < combined.length; j++) + { + if (combined[j] == null) + { + combined[j] = items[i].clone(); + break; + } + if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments())) + { + combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); + break; + } + } + } + + for (int i = 0; i < combined.length; i++) + { + final ItemStack item = combined[i]; + if (item == null) + { + continue; + } + int mustHave = item.getAmount(); + int position = 0; + + while (true) + { + // Bail when done + if (mustHave <= 0) + { + break; + } + + final int slot = next(cinventory, item, position, forceDurability, false); + + // Drat! we don't have this type in the inventory + if (slot == -1) + { + leftover.put(i, item); + break; + } + else + { + final ItemStack itemStack = cinventory.getItem(slot); + final int amount = itemStack.getAmount(); + + if (amount <= mustHave) + { + mustHave -= amount; + } + else + { + mustHave = 0; + } + position = slot + 1; + } + } + } + return leftover.isEmpty(); + } + + public static Item[] dropItem(final Location loc, final ItemStack itm) + { + final int maxStackSize = itm.getType().getMaxStackSize(); + final int stacks = itm.getAmount() / maxStackSize; + final int leftover = itm.getAmount() % maxStackSize; + final Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)]; + for (int i = 0; i < stacks; i++) + { + final ItemStack stack = itm.clone(); + stack.setAmount(maxStackSize); + itemStacks[i] = loc.getWorld().dropItem(loc, stack); + } + if (leftover > 0) + { + final ItemStack stack = itm.clone(); + stack.setAmount(leftover); + itemStacks[stacks] = loc.getWorld().dropItem(loc, stack); + } + return itemStacks; + } +} diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/ShowInventory.java b/Essentials/src/com/earth2me/essentials/craftbukkit/ShowInventory.java index 9d6bffcd1..fb2050ce4 100644 --- a/Essentials/src/com/earth2me/essentials/craftbukkit/ShowInventory.java +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/ShowInventory.java @@ -1,6 +1,5 @@ package com.earth2me.essentials.craftbukkit; -import com.earth2me.essentials.InventoryWorkaround; import java.util.logging.Level; import java.util.logging.Logger; import net.minecraft.server.EntityPlayer; diff --git a/Essentials/test/com/earth2me/essentials/FakeServer.java b/Essentials/test/com/earth2me/essentials/FakeServer.java index 22a8f0224..eb549ca37 100644 --- a/Essentials/test/com/earth2me/essentials/FakeServer.java +++ b/Essentials/test/com/earth2me/essentials/FakeServer.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.craftbukkit.FakeWorld; import com.avaje.ebean.config.ServerConfig; import java.io.File; import java.util.*; -- cgit v1.2.3