Add support for adding item meta to [free] signs.

Fix NPE on [sign] creation.
This commit is contained in:
KHobbits 2013-12-07 20:25:23 +00:00
parent 427b97433e
commit 946051de71
3 changed files with 38 additions and 6 deletions

View file

@ -117,7 +117,7 @@ public class MetaItemStack
} }
} }
private void addStringMeta(final CommandSource sender, final boolean allowUnsafe, final String string, final IEssentials ess) throws Exception public void addStringMeta(final CommandSource sender, final boolean allowUnsafe, final String string, final IEssentials ess) throws Exception
{ {
final String[] split = splitPattern.split(string, 2); final String[] split = splitPattern.split(string, 2);
if (split.length < 1) if (split.length < 1)

View file

@ -100,7 +100,7 @@ public class EssentialsSign
showError(ess, user.getSource(), ex, signName); showError(ess, user.getSource(), ex, signName);
return false; return false;
} }
catch (SignException ex) catch (Exception ex)
{ {
showError(ess, user.getSource(), ex, signName); showError(ess, user.getSource(), ex, signName);
return false; return false;
@ -385,6 +385,26 @@ public class EssentialsSign
} }
} }
protected final ItemStack getItemMeta(final ItemStack item, final String meta, final IEssentials ess) throws SignException
{
ItemStack stack = item;
try
{
if (!meta.isEmpty())
{
MetaItemStack metaStack = new MetaItemStack(stack);
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
metaStack.addStringMeta(null, allowUnsafe, meta, ess);
stack = metaStack.getItemStack();
}
}
catch (Exception ex)
{
throw new SignException(ex.getMessage(), ex);
}
return stack;
}
protected final BigDecimal getMoney(final String line) throws SignException protected final BigDecimal getMoney(final String line) throws SignException
{ {
final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+$"); final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+$");

View file

@ -4,9 +4,11 @@ import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class SignFree extends EssentialsSign public class SignFree extends EssentialsSign
@ -20,7 +22,9 @@ public class SignFree extends EssentialsSign
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
try { try {
getItemStack(sign.getLine(1), 1, ess); ItemStack item = getItemStack(sign.getLine(1), 1, ess);
item = getItemMeta(item, sign.getLine(2), ess);
item = getItemMeta(item, sign.getLine(3), ess);
} }
catch (SignException ex) catch (SignException ex)
{ {
@ -33,14 +37,22 @@ public class SignFree extends EssentialsSign
@Override @Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
final ItemStack item = getItemStack(sign.getLine(1), 1, ess); ItemStack itemStack = getItemStack(sign.getLine(1), 1, ess);
itemStack = getItemMeta(itemStack, sign.getLine(2), ess);
final ItemStack item = getItemMeta(itemStack, sign.getLine(3), ess);
if (item.getType() == Material.AIR) if (item.getType() == Material.AIR)
{ {
throw new SignException(_("cantSpawnItem", "Air")); throw new SignException(_("cantSpawnItem", "Air"));
} }
item.setAmount(item.getType().getMaxStackSize()); item.setAmount(item.getType().getMaxStackSize());
Inventory invent = ess.getServer().createInventory(player.getBase(), 36, item.getItemMeta().getDisplayName());
ItemMeta meta = item.getItemMeta();
final String displayName = meta.hasDisplayName() ? meta.getDisplayName() : item.getType().toString();
Inventory invent = ess.getServer().createInventory(player.getBase(), 36, displayName);
for (int i = 0; i < 36; i++) { for (int i = 0; i < 36; i++) {
invent.addItem(item); invent.addItem(item);
} }