summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsnowleo <schneeleo@gmail.com>2011-11-27 10:02:41 +0100
committersnowleo <schneeleo@gmail.com>2011-11-27 10:02:41 +0100
commit76a8f794ef56320adf9559695743e856fd472f15 (patch)
treea1e90865acd420f150dfcfcf88a8e72e39fdb28c
parentcbba3639b9a3862c06db263d8a3b054d1b4362ac (diff)
downloadEssentials-76a8f794ef56320adf9559695743e856fd472f15.tar
Essentials-76a8f794ef56320adf9559695743e856fd472f15.tar.gz
Essentials-76a8f794ef56320adf9559695743e856fd472f15.tar.lz
Essentials-76a8f794ef56320adf9559695743e856fd472f15.tar.xz
Essentials-76a8f794ef56320adf9559695743e856fd472f15.zip
Enchantments in CraftBukkit are still broken, but it works with Essentials :)
-rw-r--r--Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java3
-rw-r--r--Essentials/src/com/earth2me/essentials/InventoryWorkaround.java7
-rw-r--r--Essentials/src/com/earth2me/essentials/commands/Commandenchant.java3
-rw-r--r--Essentials/src/com/earth2me/essentials/commands/Commandgive.java3
-rw-r--r--Essentials/src/com/earth2me/essentials/commands/Commandinvsee.java5
-rw-r--r--Essentials/src/com/earth2me/essentials/commands/Commanditem.java3
-rw-r--r--Essentials/src/com/earth2me/essentials/commands/Commandkit.java3
-rw-r--r--Essentials/src/com/earth2me/essentials/craftbukkit/EnchantmentFix.java66
8 files changed, 83 insertions, 10 deletions
diff --git a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
index 2bf989b5c..671f1a479 100644
--- a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
+++ b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
@@ -1,5 +1,6 @@
package com.earth2me.essentials;
+import com.earth2me.essentials.craftbukkit.EnchantmentFix;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
@@ -114,7 +115,7 @@ public class EssentialsPlayerListener extends PlayerListener
}
if (user.getSavedInventory() != null)
{
- user.getInventory().setContents(user.getSavedInventory());
+ EnchantmentFix.setContents(user.getInventory(), user.getSavedInventory());
user.setSavedInventory(null);
}
user.updateActivity(false);
diff --git a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java
index bca932ad5..a5cfdd036 100644
--- a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java
+++ b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java
@@ -1,5 +1,6 @@
package com.earth2me.essentials;
+import com.earth2me.essentials.craftbukkit.EnchantmentFix;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
@@ -145,13 +146,13 @@ public final class InventoryWorkaround
{
ItemStack stack = item.clone();
stack.setAmount(item.getType().getMaxStackSize());
- cinventory.setItem(firstFree, stack);
+ EnchantmentFix.setItem(cinventory, firstFree, stack);
item.setAmount(item.getAmount() - item.getType().getMaxStackSize());
}
else
{
// Just store it
- cinventory.setItem(firstFree, item);
+ EnchantmentFix.setItem(cinventory, firstFree, item);
break;
}
}
@@ -230,7 +231,7 @@ public final class InventoryWorkaround
{
// split the stack and store
itemStack.setAmount(amount - toDelete);
- cinventory.setItem(first, itemStack);
+ EnchantmentFix.setItem(cinventory, first, itemStack);
toDelete = 0;
}
}
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java
index ef68b63a9..962ab54de 100644
--- a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java
+++ b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java
@@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
+import com.earth2me.essentials.craftbukkit.EnchantmentFix;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -97,7 +98,7 @@ public class Commandenchant extends EssentialsCommand
level = enchantment.getMaxLevel();
}
stack.addEnchantment(enchantment, level);
- user.setItemInHand(stack);
+ EnchantmentFix.setItemInHand(user.getInventory(), stack);
user.updateInventory();
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' ')));
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java
index fb0b78c40..a9d9bdd6c 100644
--- a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java
+++ b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java
@@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
+import com.earth2me.essentials.InventoryWorkaround;
import com.earth2me.essentials.User;
import java.util.Locale;
import org.bukkit.ChatColor;
@@ -76,7 +77,7 @@ public class Commandgive extends EssentialsCommand
final User giveTo = getPlayer(server, args, 0);
final String itemName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
sender.sendMessage(ChatColor.BLUE + "Giving " + stack.getAmount() + " of " + itemName + " to " + giveTo.getDisplayName() + ".");
- giveTo.getInventory().addItem(stack);
+ InventoryWorkaround.addItem(giveTo.getInventory(), true, stack);
giveTo.updateInventory();
}
}
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandinvsee.java b/Essentials/src/com/earth2me/essentials/commands/Commandinvsee.java
index 1f5c32faa..d53fe8a77 100644
--- a/Essentials/src/com/earth2me/essentials/commands/Commandinvsee.java
+++ b/Essentials/src/com/earth2me/essentials/commands/Commandinvsee.java
@@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
+import com.earth2me.essentials.craftbukkit.EnchantmentFix;
import java.util.Arrays;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
@@ -29,7 +30,7 @@ public class Commandinvsee extends EssentialsCommand
}
if (invUser == user && user.getSavedInventory() != null)
{
- invUser.getInventory().setContents(user.getSavedInventory());
+ EnchantmentFix.setContents(invUser.getInventory(), user.getSavedInventory());
user.setSavedInventory(null);
user.sendMessage(_("invRestored"));
throw new NoChargeException();
@@ -49,7 +50,7 @@ public class Commandinvsee extends EssentialsCommand
{
throw new Exception(_("invBigger"));
}
- user.getInventory().setContents(invUserStack);
+ EnchantmentFix.setContents(user.getInventory(), invUserStack);
user.sendMessage(_("invSee", invUser.getDisplayName()));
user.sendMessage(_("invSeeHelp"));
throw new NoChargeException();
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java
index c17f851e4..8bdec5e4f 100644
--- a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java
+++ b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java
@@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
+import com.earth2me.essentials.InventoryWorkaround;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import java.util.Locale;
@@ -71,7 +72,7 @@ public class Commanditem extends EssentialsCommand
final String displayName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
user.sendMessage(_("itemSpawn", stack.getAmount(), displayName));
- user.getInventory().addItem(stack);
+ InventoryWorkaround.addItem(user.getInventory(), true, stack);
user.updateInventory();
}
}
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandkit.java b/Essentials/src/com/earth2me/essentials/commands/Commandkit.java
index f1aad4394..2e09f28ba 100644
--- a/Essentials/src/com/earth2me/essentials/commands/Commandkit.java
+++ b/Essentials/src/com/earth2me/essentials/commands/Commandkit.java
@@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
+import com.earth2me.essentials.InventoryWorkaround;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
@@ -113,7 +114,7 @@ public class Commandkit extends EssentialsCommand
final int id = Material.getMaterial(Integer.parseInt(parts[0])).getId();
final int amount = parts.length > 1 ? Integer.parseInt(parts[parts.length > 2 ? 2 : 1]) : 1;
final short data = parts.length > 2 ? Short.parseShort(parts[1]) : 0;
- final HashMap<Integer, ItemStack> overfilled = user.getInventory().addItem(new ItemStack(id, amount, data));
+ final Map<Integer, ItemStack> overfilled = InventoryWorkaround.addItem(user.getInventory(), true, new ItemStack(id, amount, data));
for (ItemStack itemStack : overfilled.values())
{
user.getWorld().dropItemNaturally(user.getLocation(), itemStack);
diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/EnchantmentFix.java b/Essentials/src/com/earth2me/essentials/craftbukkit/EnchantmentFix.java
new file mode 100644
index 000000000..b6e18cb3f
--- /dev/null
+++ b/Essentials/src/com/earth2me/essentials/craftbukkit/EnchantmentFix.java
@@ -0,0 +1,66 @@
+package com.earth2me.essentials.craftbukkit;
+
+import org.bukkit.craftbukkit.inventory.CraftInventory;
+import org.bukkit.craftbukkit.inventory.CraftInventoryPlayer;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.inventory.Inventory;
+import org.bukkit.inventory.ItemStack;
+
+
+public class EnchantmentFix
+{
+ public static void setContents(Inventory inventory, ItemStack[] items)
+ {
+ CraftInventory cInventory = (CraftInventory)inventory;
+ if (cInventory.getContents().length != items.length)
+ {
+ throw new IllegalArgumentException("Invalid inventory size; expected " + cInventory.getContents().length);
+ }
+
+ net.minecraft.server.ItemStack[] mcItems = cInventory.getInventory().getContents();
+
+ for (int i = 0; i < items.length; i++)
+ {
+ ItemStack item = items[i];
+ if (item == null || item.getTypeId() <= 0)
+ {
+ mcItems[i] = null;
+ }
+ else
+ {
+ mcItems[i] = new net.minecraft.server.ItemStack(item.getTypeId(), item.getAmount(), item.getDurability());
+ new CraftItemStack(mcItems[i]).addUnsafeEnchantments(item.getEnchantments());
+ }
+ }
+ }
+
+ public static void setItem(Inventory inventory, int index, ItemStack item)
+ {
+ CraftInventory cInventory = (CraftInventory)inventory;
+ if (item == null)
+ {
+ cInventory.getInventory().setItem(index, null);
+ }
+ else
+ {
+ net.minecraft.server.ItemStack stack = new net.minecraft.server.ItemStack(item.getTypeId(), item.getAmount(), item.getDurability());
+ new CraftItemStack(stack).addUnsafeEnchantments(item.getEnchantments());
+ cInventory.getInventory().setItem(index, stack);
+ }
+ }
+
+ public static void setItemInHand(Inventory inventory, ItemStack item)
+ {
+ CraftInventoryPlayer cInventory = (CraftInventoryPlayer)inventory;
+ if (item == null)
+ {
+ cInventory.getInventory().setItem(cInventory.getInventory().itemInHandIndex, null);
+ }
+ else
+ {
+ net.minecraft.server.ItemStack stack = new net.minecraft.server.ItemStack(item.getTypeId(), item.getAmount(), item.getDurability());
+ new CraftItemStack(stack).addUnsafeEnchantments(item.getEnchantments());
+ cInventory.getInventory().setItem(cInventory.getInventory().itemInHandIndex, stack);
+ }
+ }
+}