From 255e3423530fffa4b725d7a9eb395e5d660cb3c1 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Sun, 27 Nov 2011 15:29:51 -0800 Subject: [PATCH] Added enchantment support --- pom.xml | 2 +- .../com/sk89q/worldedit/blocks/BaseItem.java | 9 +++++++++ .../com/sk89q/worldedit/bukkit/BukkitWorld.java | 17 +++++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 703b34656..aa66401f9 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ org.bukkit bukkit - 0.0.1-SNAPSHOT + 1.0.0-R1-SNAPSHOT diff --git a/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java b/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java index c9ef26083..8620c8ccb 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java +++ b/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java @@ -19,6 +19,9 @@ package com.sk89q.worldedit.blocks; +import java.util.HashMap; +import java.util.Map; + /** * Represents an item. * @@ -34,6 +37,8 @@ public class BaseItem { */ private short damage; + private Map enchantments = new HashMap(); + /** * Construct the object. * @@ -82,4 +87,8 @@ public class BaseItem { public void setDamage(short damage) { this.damage = damage; } + + public Map getEnchantments() { + return enchantments; + } } diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index 787260666..dc7f3aa75 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -24,12 +24,14 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Furnace; import org.bukkit.block.CreatureSpawner; import org.bukkit.block.Sign; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Arrow; import org.bukkit.entity.Boat; import org.bukkit.entity.Entity; @@ -660,6 +662,11 @@ public class BukkitWorld extends LocalWorld { bukkitStack.getTypeId(), bukkitStack.getAmount(), bukkitStack.getDurability()); + try { + for (Map.Entry entry : bukkitStack.getEnchantments().entrySet()) { + contents[i].getEnchantments().put(entry.getKey().getId(), entry.getValue()); + } + } catch (Throwable ignore) {} } } @@ -693,9 +700,15 @@ public class BukkitWorld extends LocalWorld { } if (contents[i] != null) { - inven.setItem(i, new ItemStack(contents[i].getType(), + ItemStack toAdd = new ItemStack(contents[i].getType(), contents[i].getAmount(), - (byte) contents[i].getDamage())); + (byte) contents[i].getDamage()); + try { + for (Map.Entry entry : contents[i].getEnchantments().entrySet()) { + toAdd.addEnchantment(Enchantment.getById(entry.getKey()), entry.getValue()); + } + } catch (Throwable ignore) {} + inven.setItem(i, toAdd); } else { inven.setItem(i, null); }