-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTradeCraft.java
More file actions
296 lines (234 loc) · 8.53 KB
/
TradeCraft.java
File metadata and controls
296 lines (234 loc) · 8.53 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TradeCraft extends Plugin {
// The plugin name.
static final String pluginName = "TradeCraft";
// The plugin version. The first part is the version of hMod this is built against.
// The second part is the release number built against that version of hMod.
// A "+" at the end means this is a development version that hasn't been released yet.
static final String version = "133.3+";
private static final Pattern ratePattern = Pattern.compile("\\s*(\\d+)\\s*:\\s*(\\d+)\\s*");
// Stuff used to interact with the server.
final Logger log = Logger.getLogger("Minecraft");
final Server server = etc.getServer();
// Objects used by the plugin.
TradeCraftPropertiesFile properties = new TradeCraftPropertiesFile();
TradeCraftConfigurationFile configuration = new TradeCraftConfigurationFile(this);
TradeCraftDataFile data = new TradeCraftDataFile(this);
private final TradeCraftListener listener = new TradeCraftListener(this);
public void enable() {
}
public void disable() {
}
public void initialize() {
log.info(pluginName + " " + version + " initialized");
properties = new TradeCraftPropertiesFile();
configuration = new TradeCraftConfigurationFile(this);
data = new TradeCraftDataFile(this);
configuration.load();
data.load();
etc.getLoader().addListener(
PluginLoader.Hook.BLOCK_RIGHTCLICKED,
listener,
this,
PluginListener.Priority.MEDIUM);
etc.getLoader().addListener(
PluginLoader.Hook.BLOCK_BROKEN,
listener,
this,
PluginListener.Priority.MEDIUM);
etc.getLoader().addListener(
PluginLoader.Hook.SIGN_CHANGE,
listener,
this,
PluginListener.Priority.MEDIUM);
etc.getLoader().addListener(
PluginLoader.Hook.COMMAND,
listener,
this,
PluginListener.Priority.LOW);
}
void sendMessage(Player player, String format, Object... args) {
String message = String.format(format, args);
player.sendMessage(message);
}
void trace(Player player, String format, Object... args) {
if (properties.getEnableDebugMessages()) {
sendMessage(player, format, args);
}
}
public boolean playerIsInGroup(Player player, String group) {
if (group.equals("*")) {
return true;
}
return player.isInGroup(group);
}
TradeCraftShop getShopFromSignOrChestBlock(Player player, Block block) {
if (block.getType() == Block.Type.Chest.getType()) {
block = server.getBlockAt(block.getX(), block.getY() + 1, block.getZ());
}
return getShopFromSignBlock(player, block);
}
TradeCraftShop getShopFromSignBlock(Player player, Block block) {
if (block.getType() != Block.Type.WallSign.getType()) {
return null;
}
int x = block.getX();
int y = block.getY();
int z = block.getZ();
trace(player, "You clicked a sign at %d, %d, %d.", x, y, z);
Sign sign = (Sign)server.getComplexBlock(x, y, z);
// The sign at this location can be null if it was just destroyed.
if (sign == null) {
trace(player, "The sign is no longer there.");
return null;
}
String itemName = getItemName(sign);
if (itemName == null) {
trace(player, "There is no item name on the sign.");
return null;
}
trace(player, "The item name on the sign is %s.", itemName);
Block blockBelowSign = server.getBlockAt(x, y - 1, z);
if (blockBelowSign.getType() != Block.Type.Chest.getType()) {
trace(player, "There is no chest beneath the sign.");
return null;
}
Chest chest = (Chest)server.getComplexBlock(x, y - 1, z);
if (itemName.toLowerCase().equals("repair")) {
if (!properties.getRepairShopsEnabled()) {
trace(player, "Repair shops are not enabled.");
return null;
}
if (!this.playerIsInGroup(player, properties.getGroupRequiredToUseRepairShops())) {
trace(player, "You can't use repair shops.");
return null;
}
trace(player, "This is a repair shop.");
return new TradeCraftRepairShop(this, sign, chest);
}
if (!configuration.isConfigured(itemName)) {
trace(player, "The item name %s is not configured in TradeCraft.txt.", itemName);
return null;
}
String ownerName = getOwnerName(sign);
if (ownerName == null) {
trace(player, "There is no owner name on the sign.");
if (!properties.getInfiniteShopsEnabled()) {
trace(player, "Ininite shops are not enabled.");
return null;
}
trace(player, "This is an infinite shop.");
return new TradeCraftInfiniteShop(this, sign, chest);
}
trace(player, "The owner name on the sign is %s.", ownerName);
if (!properties.getPlayerOwnedShopsEnabled()) {
trace(player, "Player-owned shops are not enabled.");
return null;
}
trace(player, "This is a player-owned shop.");
return new TradeCraftPlayerOwnedShop(this, sign, chest);
}
String getItemName(Sign sign) {
return getSpecialText(sign, "[", "]");
}
String getOwnerName(Sign sign) {
return getSpecialTextOnLine(sign, "-", "-", 3);
}
private String getSpecialText(Sign sign, String prefix, String suffix) {
for (int i = 0; i < 4; i++) {
String text = getSpecialTextOnLine(sign, prefix, suffix, i);
if (text != null) {
return text;
}
}
return null;
}
private String getSpecialTextOnLine(Sign sign, String prefix, String suffix, int lineNumber) {
String signText = sign.getText(lineNumber);
if (signText == null) {
return null;
}
signText = signText.trim();
if (signText.startsWith(prefix) &&
signText.endsWith(suffix) &&
signText.length() > 2) {
String text = signText.substring(1, signText.length() - 1);
text = text.trim();
if (text.equals("")) {
return null;
}
return text;
}
return null;
}
TradeCraftExchangeRate getExchangeRate(Sign sign, int lineNumber) {
TradeCraftExchangeRate rate = new TradeCraftExchangeRate();
String signText = sign.getText(lineNumber);
Matcher matcher = ratePattern.matcher(signText);
if (matcher.find()) {
rate.amount = Integer.parseInt(matcher.group(1));
rate.value = Integer.parseInt(matcher.group(2));
}
return rate;
}
static int getMaxStackSize(int itemType) {
switch (Item.Type.fromId(itemType)) {
case Apple:
case GoldenApple:
case Pork:
case GrilledPork:
case Bread:
case Bucket:
case WaterBucket:
case LavaBucket:
case MilkBucket:
case WoodSword:
case WoodSpade:
case WoodPickaxe:
case WoodAxe:
case WoodHoe:
case StoneSword:
case StoneSpade:
case StonePickaxe:
case StoneAxe:
case StoneHoe:
case IronSword:
case IronSpade:
case IronPickaxe:
case IronAxe:
case IronHoe:
case DiamondSword:
case DiamondSpade:
case DiamondPickaxe:
case DiamondAxe:
case DiamondHoe:
case GoldSword:
case GoldSpade:
case GoldPickaxe:
case GoldAxe:
case GoldHoe:
case LeatherHelmet:
case LeatherChestplate:
case LeatherLeggings:
case LeatherBoots:
case IronHelmet:
case IronChestplate:
case IronLeggings:
case IronBoots:
case DiamondHelmet:
case DiamondChestplate:
case DiamondLeggings:
case DiamondBoots:
case GoldHelmet:
case GoldChestplate:
case GoldLeggings:
case GoldBoots:
return 1;
case SnowBall:
return 16;
}
return 64;
}
}