1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package com.earth2me.essentials.signs;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.Enchantments;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.api.IUser;
import java.util.Locale;
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 IUser player, final String username, final IEssentials ess) throws SignException, ChargeException
{
final ItemStack stack = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2)
{
throw new SignException(_("invalidSignLine", 3));
}
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null)
{
throw new SignException(_("enchantmentNotFound"));
}
int level;
try
{
level = Integer.parseInt(enchantLevel[1]);
}
catch (NumberFormatException ex)
{
throw new SignException(ex.getMessage());
}
if (level < 1 || level > enchantment.getMaxLevel())
{
level = enchantment.getMaxLevel();
sign.setLine(2, enchantLevel[0] + ":" + level);
}
try
{
if (stack != null)
{
stack.addEnchantment(enchantment, level);
}
}
catch (Throwable ex)
{
throw new SignException(ex.getMessage());
}
getTrade(sign, 3, ess);
return true;
}
@Override
protected boolean onSignInteract(ISign sign, IUser player, String username, IEssentials ess) throws SignException, ChargeException
{
final ItemStack search = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
int slot = -1;
final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player);
final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2)
{
throw new SignException(_("invalidSignLine", 3));
}
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null)
{
throw new SignException(_("enchantmentNotFound"));
}
int level;
try
{
level = Integer.parseInt(enchantLevel[1]);
}
catch (NumberFormatException ex)
{
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);
}
charge.charge(player);
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
player.updateInventory();
return true;
}
}
|