From 18a15ca63c2c68b45b8004c6f2d1f2693407e16b Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sun, 13 Jan 2013 04:44:28 +0000 Subject: [PATCH] Add provisional support for basic item Meta Adds /book command to allow reediting of signed books. --- .../src/com/earth2me/essentials/Kit.java | 2 +- .../earth2me/essentials/MetaItemStack.java | 79 ++++++++++++++++++- .../essentials/commands/Commandbook.java | 45 +++++++++++ .../essentials/commands/Commandgive.java | 2 +- .../essentials/commands/Commanditem.java | 2 +- Essentials/src/config.yml | 9 ++- Essentials/src/plugin.yml | 4 + 7 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/commands/Commandbook.java diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index 35b105612..4ff59a948 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -122,7 +122,7 @@ public class Kit { for (int i = 2; i < parts.length; i++) { - metaStack.addStringEnchantment(null, allowUnsafe, parts[i]); + metaStack.addStringMeta(null, allowUnsafe, parts[i]); } } diff --git a/Essentials/src/com/earth2me/essentials/MetaItemStack.java b/Essentials/src/com/earth2me/essentials/MetaItemStack.java index cf4aa70ec..0dea8c63d 100644 --- a/Essentials/src/com/earth2me/essentials/MetaItemStack.java +++ b/Essentials/src/com/earth2me/essentials/MetaItemStack.java @@ -1,12 +1,19 @@ package com.earth2me.essentials; import static com.earth2me.essentials.I18n._; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; +import java.util.logging.Logger; import java.util.regex.Pattern; +import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.inventory.meta.SkullMeta; public class MetaItemStack @@ -18,15 +25,78 @@ public class MetaItemStack { this.stack = stack.clone(); } - + public ItemStack getItemStack() { return stack; } + //TODO: TL this public void addStringMeta(final User user, final boolean allowUnsafe, final String string) throws Exception { + final String[] split = splitPattern.split(string, 2); + if (split.length < 1) + { + return; + } + if (split.length > 1 && split[0].equalsIgnoreCase("name")) + { + final String displayName = split[1].replace('_', ' '); + final ItemMeta meta = stack.getItemMeta(); + meta.setDisplayName(displayName); + stack.setItemMeta(meta); + } + else if (split.length > 1 && (split[0].equalsIgnoreCase("lore") || split[0].equalsIgnoreCase("desc"))) + { + final List lore = new ArrayList(); + for (String line : split[1].split("\\|")) + { + lore.add(line.replace('_', ' ')); + } + final ItemMeta meta = stack.getItemMeta(); + meta.setLore(lore); + stack.setItemMeta(meta); + } + else if (split.length > 1 && (split[0].equalsIgnoreCase("player") || split[0].equalsIgnoreCase("owner")) && stack.getType() == Material.SKULL_ITEM) + { + if (stack.getDurability() == 3) + { + final String owner = split[1]; + final SkullMeta meta = (SkullMeta)stack.getItemMeta(); + boolean result = meta.setOwner(owner); + stack.setItemMeta(meta); + } + else + { + throw new Exception("You can only set the owner of player skulls (397:3)"); + } + } + else if (split.length > 1 && (split[0].equalsIgnoreCase("color") || split[0].equalsIgnoreCase("colour")) + && (stack.getType() == Material.LEATHER_BOOTS + || stack.getType() == Material.LEATHER_CHESTPLATE + || stack.getType() == Material.LEATHER_HELMET + || stack.getType() == Material.LEATHER_LEGGINGS)) + { + final String[] color = split[1].split("\\|"); + if (color.length == 3) + { + final int red = Util.isInt(color[0]) ? Integer.parseInt(color[0]) : 0; + final int green = Util.isInt(color[1]) ? Integer.parseInt(color[1]) : 0; + final int blue = Util.isInt(color[2]) ? Integer.parseInt(color[2]) : 0; + final LeatherArmorMeta meta = (LeatherArmorMeta)stack.getItemMeta(); + meta.setColor(Color.fromRGB(red, green, blue)); + stack.setItemMeta(meta); + } + else + { + throw new Exception("Leather Color Syntax: color:|| eg: color:255|0|0"); + } + } + else + { + parseEnchantmentStrings(user, allowUnsafe, split); + } } public void addStringEnchantment(final User user, final boolean allowUnsafe, final String string) throws Exception @@ -37,6 +107,11 @@ public class MetaItemStack return; } + parseEnchantmentStrings(user, allowUnsafe, split); + } + + private void parseEnchantmentStrings(final User user, final boolean allowUnsafe, final String[] split) throws Exception + { Enchantment enchantment = getEnchantment(user, split[0]); int level = -1; @@ -116,6 +191,4 @@ public class MetaItemStack } return enchantment; } - - } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbook.java b/Essentials/src/com/earth2me/essentials/commands/Commandbook.java new file mode 100644 index 000000000..da46dbe7a --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbook.java @@ -0,0 +1,45 @@ +package com.earth2me.essentials.commands; + +import com.earth2me.essentials.User; +import org.bukkit.Material; +import org.bukkit.Server; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + + +public class Commandbook extends EssentialsCommand +{ + public Commandbook() + { + super("book"); + } + + + //TODO: Translate this + @Override + public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception + { + + ItemStack item = user.getItemInHand(); + if (item.getType() == Material.WRITTEN_BOOK) + { + ItemMeta meta = item.getItemMeta(); + ItemStack newItem = new ItemStack(Material.BOOK_AND_QUILL, item.getAmount()); + newItem.setItemMeta(meta); + user.setItemInHand(newItem); + user.sendMessage("You can now edit the contents of this book."); + } + else if (item.getType() == Material.BOOK_AND_QUILL) + { + ItemMeta meta = item.getItemMeta(); + ItemStack newItem = new ItemStack(Material.WRITTEN_BOOK, item.getAmount()); + newItem.setItemMeta(meta); + user.setItemInHand(newItem); + user.sendMessage("This book is now locked and signed."); + } + else + { + throw new Exception("You are not holding a book."); + } + } +} \ No newline at end of file diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java index c80ff0d1b..49b60a8e7 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java @@ -80,7 +80,7 @@ public class Commandgive extends EssentialsCommand for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++) { - metaStack.addStringEnchantment(null, allowUnsafe, args[i]); + metaStack.addStringMeta(null, allowUnsafe, args[i]); } stack = metaStack.getItemStack(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java index 0acd1f60f..b69c73f2c 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java @@ -62,7 +62,7 @@ public class Commanditem extends EssentialsCommand for (int i = 2; i < args.length; i++) { - metaStack.addStringEnchantment(null, allowUnsafe, args[i]); + metaStack.addStringMeta(null, allowUnsafe, args[i]); } stack = metaStack.getItemStack(); } diff --git a/Essentials/src/config.yml b/Essentials/src/config.yml index 4453f355c..724a1b82f 100644 --- a/Essentials/src/config.yml +++ b/Essentials/src/config.yml @@ -212,8 +212,9 @@ kits: dtools: delay: 10 items: - - 277 1 efficiency:1 durability:1 - - 278 1 + - 277 1 digspeed:3 name:Dwarf lore:Diggy|Diggy|Hole + - 278 1 efficiency:1 durability:1 fortune:1 name:Gigadrill lore:The_drill_that_pierces|the_heavens + - 298 1 color:255|0|0 - 279:780 1 tools: delay: 10 @@ -222,6 +223,10 @@ kits: - 273 1 - 274 1 - 275 1 + notch: + delay: 1000 + items: + - 397:3 1 player:Notch # Essentials Sign Control # See http://wiki.ess3.net/wiki/Sign_Tutorial for instructions on how to use these. diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index 8c66921d0..10ed7da21 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -39,6 +39,10 @@ commands: description: Bans an IP address. usage: /
aliases: [ebanip] + book: + description: Allows reopening written books. + usage: / + aliases: [ebook] break: description: Breaks the block you are looking at. usage: /