1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-08 07:36:42 +02:00

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 de4ba9abdf
commit a7a42a3956
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);
if (split.length < 1)

View File

@@ -100,7 +100,7 @@ public class EssentialsSign
showError(ess, user.getSource(), ex, signName);
return false;
}
catch (SignException ex)
catch (Exception ex)
{
showError(ess, user.getSource(), ex, signName);
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
{
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.User;
import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
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
{
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)
{
@@ -33,14 +37,22 @@ public class SignFree extends EssentialsSign
@Override
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)
{
throw new SignException(_("cantSpawnItem", "Air"));
}
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++) {
invent.addItem(item);
}