2011-12-04 19:58:00 +00:00
|
|
|
package com.earth2me.essentials.signs;
|
|
|
|
|
|
|
|
import com.earth2me.essentials.ChargeException;
|
2011-12-04 21:28:29 +00:00
|
|
|
import com.earth2me.essentials.Enchantments;
|
2011-12-04 19:58:00 +00:00
|
|
|
import com.earth2me.essentials.IEssentials;
|
|
|
|
import com.earth2me.essentials.User;
|
|
|
|
import static com.earth2me.essentials.I18n._;
|
|
|
|
import com.earth2me.essentials.Trade;
|
2011-12-07 12:44:51 +00:00
|
|
|
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
2011-12-04 19:58:00 +00:00
|
|
|
import org.bukkit.Material;
|
|
|
|
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-04 23:20:27 +00:00
|
|
|
final ItemStack stack = 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)
|
|
|
|
{
|
|
|
|
throw new SignException(_("invalidSignLine", 2));
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
throw new SignException(_("enchantmentNotFound"));
|
|
|
|
}
|
|
|
|
int level;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
level = Integer.parseInt(enchantLevel[1]);
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
|
|
|
throw new SignException(ex.getMessage());
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
stack.addEnchantment(enchantment, level);
|
|
|
|
}
|
|
|
|
catch (Throwable ex)
|
|
|
|
{
|
|
|
|
throw new SignException(ex.getMessage());
|
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 12:44:51 +00:00
|
|
|
final ItemStack search = getItemStack(sign.getLine(1), 1, ess);
|
|
|
|
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-07 12:44:51 +00:00
|
|
|
if (InventoryWorkaround.containsItem(player.getInventory(), false, search))
|
2011-12-04 19:58:00 +00:00
|
|
|
{
|
2011-12-07 12:44:51 +00:00
|
|
|
slot = InventoryWorkaround.first(player.getInventory(), search, false, true);
|
2011-12-04 19:58:00 +00:00
|
|
|
}
|
2011-12-07 12:44:51 +00:00
|
|
|
if (slot == -1)
|
2011-12-04 19:58:00 +00:00
|
|
|
{
|
2011-12-07 12:44:51 +00:00
|
|
|
throw new SignException(_("missingItems", 1, search.toString()));
|
2011-12-04 19:58:00 +00:00
|
|
|
}
|
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 12:44:51 +00:00
|
|
|
throw new SignException(_("invalidSignLine", 2));
|
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-04 23:20:27 +00:00
|
|
|
|
2011-12-04 21:28:29 +00:00
|
|
|
final ItemStack toEnchant = player.getInventory().getItem(slot);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
toEnchant.addEnchantment(enchantment, Integer.parseInt(enchantLevel[1]));
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
|
|
|
toEnchant.addEnchantment(enchantment, enchantment.getMaxLevel());
|
|
|
|
}
|
|
|
|
|
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.getInventory().setItem(slot, toEnchant);
|
|
|
|
player.updateInventory();
|
2011-12-04 19:58:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|