Add provisional support for basic item Meta

Adds /book command to allow reediting of signed books.
This commit is contained in:
KHobbits 2013-01-13 04:44:28 +00:00
parent ef1492a2a2
commit 18a15ca63c
7 changed files with 135 additions and 8 deletions

View file

@ -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]);
}
}

View file

@ -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<String> lore = new ArrayList<String>();
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:<red>|<green>|<blue> 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;
}
}

View file

@ -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.");
}
}
}

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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.

View file

@ -39,6 +39,10 @@ commands:
description: Bans an IP address.
usage: /<command> <address>
aliases: [ebanip]
book:
description: Allows reopening written books.
usage: /<command>
aliases: [ebook]
break:
description: Breaks the block you are looking at.
usage: /<command>