2011-12-04 19:58:00 +00:00
|
|
|
package com.earth2me.essentials.signs;
|
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n._;
|
2012-09-08 13:55:37 +00:00
|
|
|
import com.earth2me.essentials.*;
|
2011-12-07 13:23:56 +00:00
|
|
|
import java.util.Locale;
|
2011-12-04 19:58:00 +00:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
|
|
|
public class SignEnchant extends EssentialsSign
|
|
|
|
{
|
|
|
|
public SignEnchant()
|
|
|
|
{
|
|
|
|
super("Enchant");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
|
|
|
|
{
|
2011-12-07 13:23:56 +00:00
|
|
|
final ItemStack stack = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
|
2011-12-04 21:28:29 +00:00
|
|
|
final String[] enchantLevel = sign.getLine(2).split(":");
|
2011-12-04 19:58:00 +00:00
|
|
|
if (enchantLevel.length != 2)
|
|
|
|
{
|
2012-09-23 23:19:39 +00:00
|
|
|
sign.setLine(2, "§c<enchant>");
|
2011-12-07 13:23:56 +00:00
|
|
|
throw new SignException(_("invalidSignLine", 3));
|
2011-12-04 19:58:00 +00:00
|
|
|
}
|
2011-12-04 21:28:29 +00:00
|
|
|
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
|
|
|
|
if (enchantment == null)
|
2011-12-04 19:58:00 +00:00
|
|
|
{
|
2012-09-23 23:19:39 +00:00
|
|
|
sign.setLine(2, "§c<enchant>");
|
2011-12-04 19:58:00 +00:00
|
|
|
throw new SignException(_("enchantmentNotFound"));
|
|
|
|
}
|
|
|
|
int level;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
level = Integer.parseInt(enchantLevel[1]);
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
2012-09-23 23:19:39 +00:00
|
|
|
sign.setLine(2, "§c<enchant>");
|
2012-04-04 02:09:27 +00:00
|
|
|
throw new SignException(ex.getMessage(), ex);
|
2011-12-04 19:58:00 +00:00
|
|
|
}
|
2011-12-04 21:28:29 +00:00
|
|
|
if (level < 1 || level > enchantment.getMaxLevel())
|
2011-12-04 19:58:00 +00:00
|
|
|
{
|
2011-12-04 23:20:27 +00:00
|
|
|
level = enchantment.getMaxLevel();
|
|
|
|
sign.setLine(2, enchantLevel[0] + ":" + level);
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2011-12-07 13:23:56 +00:00
|
|
|
if (stack != null)
|
|
|
|
{
|
|
|
|
stack.addEnchantment(enchantment, level);
|
|
|
|
}
|
2011-12-04 23:20:27 +00:00
|
|
|
}
|
|
|
|
catch (Throwable ex)
|
|
|
|
{
|
2012-04-04 02:09:27 +00:00
|
|
|
throw new SignException(ex.getMessage(), ex);
|
2011-12-04 19:58:00 +00:00
|
|
|
}
|
|
|
|
getTrade(sign, 3, ess);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean onSignInteract(ISign sign, User player, String username, IEssentials ess) throws SignException, ChargeException
|
|
|
|
{
|
2011-12-07 13:23:56 +00:00
|
|
|
final ItemStack search = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
|
2011-12-07 12:44:51 +00:00
|
|
|
int slot = -1;
|
2011-12-04 21:28:29 +00:00
|
|
|
final Trade charge = getTrade(sign, 3, ess);
|
2011-12-04 19:58:00 +00:00
|
|
|
charge.isAffordableFor(player);
|
2011-12-04 21:28:29 +00:00
|
|
|
final String[] enchantLevel = sign.getLine(2).split(":");
|
2011-12-04 19:58:00 +00:00
|
|
|
if (enchantLevel.length != 2)
|
|
|
|
{
|
2011-12-07 13:23:56 +00:00
|
|
|
throw new SignException(_("invalidSignLine", 3));
|
2011-12-04 19:58:00 +00:00
|
|
|
}
|
2011-12-04 21:28:29 +00:00
|
|
|
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
|
|
|
|
if (enchantment == null)
|
2011-12-04 19:58:00 +00:00
|
|
|
{
|
2011-12-07 12:44:51 +00:00
|
|
|
throw new SignException(_("enchantmentNotFound"));
|
2011-12-04 19:58:00 +00:00
|
|
|
}
|
2011-12-07 13:23:56 +00:00
|
|
|
int level;
|
2011-12-04 21:28:29 +00:00
|
|
|
try
|
|
|
|
{
|
2011-12-07 13:23:56 +00:00
|
|
|
level = Integer.parseInt(enchantLevel[1]);
|
2011-12-04 21:28:29 +00:00
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
2011-12-07 13:23:56 +00:00
|
|
|
level = enchantment.getMaxLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
final ItemStack playerHand = player.getItemInHand();
|
|
|
|
if (playerHand == null
|
|
|
|
|| playerHand.getAmount() != 1
|
|
|
|
|| (playerHand.containsEnchantment(enchantment)
|
|
|
|
&& playerHand.getEnchantmentLevel(enchantment) == level))
|
|
|
|
{
|
|
|
|
throw new SignException(_("missingItems", 1, sign.getLine(1)));
|
|
|
|
}
|
|
|
|
if (search != null && playerHand.getType() != search.getType())
|
|
|
|
{
|
|
|
|
throw new SignException(_("missingItems", 1, search.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ')));
|
|
|
|
}
|
|
|
|
|
|
|
|
final ItemStack toEnchant = playerHand;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
toEnchant.addEnchantment(enchantment, level);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
throw new SignException(ex.getMessage(), ex);
|
2011-12-04 21:28:29 +00:00
|
|
|
}
|
|
|
|
|
2011-12-04 19:58:00 +00:00
|
|
|
charge.charge(player);
|
|
|
|
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
|
2011-12-04 21:28:29 +00:00
|
|
|
player.updateInventory();
|
2011-12-04 19:58:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|