/
Trades.java
452 lines (388 loc) · 14 KB
/
Trades.java
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package com.laytonsmith.core.functions;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCMerchant;
import com.laytonsmith.abstraction.MCMerchantRecipe;
import com.laytonsmith.abstraction.MCPlayer;
import com.laytonsmith.abstraction.StaticLayer;
import com.laytonsmith.abstraction.entities.MCVillager;
import com.laytonsmith.abstraction.enums.MCRecipeType;
import com.laytonsmith.annotations.api;
import com.laytonsmith.core.CHVersion;
import com.laytonsmith.core.ObjectGenerator;
import com.laytonsmith.core.Static;
import com.laytonsmith.core.constructs.CArray;
import com.laytonsmith.core.constructs.CBoolean;
import com.laytonsmith.core.constructs.CInt;
import com.laytonsmith.core.constructs.CNull;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.constructs.CVoid;
import com.laytonsmith.core.constructs.Construct;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.exceptions.CRE.CREBadEntityException;
import com.laytonsmith.core.exceptions.CRE.CRECastException;
import com.laytonsmith.core.exceptions.CRE.CREFormatException;
import com.laytonsmith.core.exceptions.CRE.CREIllegalArgumentException;
import com.laytonsmith.core.exceptions.CRE.CRELengthException;
import com.laytonsmith.core.exceptions.CRE.CRENotFoundException;
import com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException;
import com.laytonsmith.core.exceptions.CRE.CRERangeException;
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Trades {
public static String docs() {
return "Functions related to the management of trades and merchants. A trade is a special kind of recipe"
+ " accessed through the merchant interface. A merchant is a provider of trades,"
+ " which may or may not be attached to a Villager.";
}
@api
public static class get_merchant_trades extends Recipes.recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREIllegalArgumentException.class, CREFormatException.class, CREBadEntityException.class};
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
CArray ret = new CArray(t);
for(MCMerchantRecipe mr : GetMerchant(args[0], t).getRecipes()) {
ret.push(trade(mr, t), t);
}
return ret;
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "get_merchant_trades";
}
@Override
public Integer[] numArgs() {
return new Integer[]{1};
}
@Override
public String docs() {
return "array {specifier} Returns a list of trades used by the specified merchant."
+ " Specifier can be the UUID of an entity or a virtual merchant ID.";
}
}
@api
public static class set_merchant_trades extends Recipes.recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREIllegalArgumentException.class, CREFormatException.class, CRECastException.class,
CREBadEntityException.class, CRENotFoundException.class};
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
MCMerchant merchant = GetMerchant(args[0], t);
CArray trades = Static.getArray(args[1], t);
List<MCMerchantRecipe> recipes = new ArrayList<>();
for(Construct trade : trades.asList()) {
recipes.add(trade(trade, t));
}
merchant.setRecipes(recipes);
return CVoid.VOID;
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "set_merchant_trades";
}
@Override
public Integer[] numArgs() {
return new Integer[]{2};
}
@Override
public String docs() {
return "void {specifier, array} Sets the list of trades the specified merchant can use to the provided"
+ " array of TradeArrays. The specifier can be the UUID of a physical entity or the ID"
+ " of a user-created virtual merchant. ----"
+ " TradeArrays are similar to RecipeArray format and contain the following keys:"
+ " <pre>"
+ " result: The result item array of the trade.\n"
+ " ingredients: Items the player must provide. Must be 1 or 2 itemstacks.\n"
+ " uses: (Optional) The number of times the recipe has been used. Defaults to 0."
+ " Note: this number is not kept in sync between merchants and the master list.\n"
+ " maxuses: (Optional) The maximum number of times this trade can be made before it is disabled."
+ " Defaults to " + Integer.MAX_VALUE + ".\n"
+ " hasxpreward: (Optional) Whether xp is given to the player for making this trade."
+ " Defaults to true."
+ " </pre>"
+ " Example 1: Turns 9 stone into obsidian."
+ " <pre>"
+ "{\n"
+ " result: {name: OBSIDIAN},\n"
+ " ingredients: {{name: STONE, qty: 9}}\n"
+ "}"
+ "</pre>"
+ " Example 2: Combines a diamond and dirt to make grass, but only once."
+ "<pre>"
+ "{\n"
+ " result: {name: 'GRASS'},\n"
+ " ingredients: {{name: 'DIRT'}, {name: 'DIAMOND'}}\n"
+ " maxuses: 1\n"
+ "}"
+ "</pre>";
}
}
@api
public static class get_virtual_merchants extends Recipes.recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[0];
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
CArray ret = CArray.GetAssociativeArray(t);
for(Map.Entry<String, MCMerchant> entry : VIRTUAL_MERCHANTS.entrySet()) {
if(entry.getValue() == null) {
VIRTUAL_MERCHANTS.remove(entry.getKey());
continue;
}
ret.set(entry.getKey(), entry.getValue().getTitle(), t);
}
return ret;
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "get_virtual_merchants";
}
@Override
public Integer[] numArgs() {
return new Integer[]{0};
}
@Override
public String docs() {
return "array {} Returns an array where the keys are currently registered merchant IDs and the values are"
+ " the corresponding window titles of those merchants.";
}
}
@api
public static class create_virtual_merchant extends Recipes.recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[] {CREIllegalArgumentException.class};
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
if(VIRTUAL_MERCHANTS.containsKey(args[0].val())) {
throw new CREIllegalArgumentException("There is already a merchant with id " + args[0].val(), t);
} else {
VIRTUAL_MERCHANTS.put(args[0].val(), Static.getServer().createMerchant(args[1].val()));
return CVoid.VOID;
}
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "create_virtual_merchant";
}
@Override
public Integer[] numArgs() {
return new Integer[]{2};
}
@Override
public String docs() {
return "void {ID, title} Creates a merchant that can be traded with by players but is not attached to"
+ " a physical entity. The ID given should not be a UUID. The title is the text that will display"
+ " at the top of the window while a player is trading with it. Created merchants will persist"
+ " across recompiles, but not across server restarts. An exception will be thrown if a merchant"
+ " already exists using the given ID.";
}
}
@api
public static class delete_virtual_merchant extends Recipes.recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[0];
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
return CBoolean.get(VIRTUAL_MERCHANTS.remove(args[0].val()) != null);
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "delete_virtual_merchant";
}
@Override
public Integer[] numArgs() {
return new Integer[]{1};
}
@Override
public String docs() {
return "boolean {string} Deletes a virtual merchant if one by the given ID exists. Returns true if"
+ " one was removed, or false if there was no match for the ID.";
}
}
@api
public static class popen_trading extends Recipes.recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREPlayerOfflineException.class, CRELengthException.class,
CREIllegalArgumentException.class, CREBadEntityException.class, CREFormatException.class};
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
MCPlayer player;
boolean force = false;
if(args.length > 1) {
player = Static.GetPlayer(args[1], t);
} else {
player = Static.getPlayer(env, t);
}
if(args.length == 3) {
force = Static.getBoolean(args[2], t);
}
MCMerchant merchant = GetMerchant(args[0], t);
if(!force && merchant.isTrading()) {
return CBoolean.FALSE;
}
return CBoolean.get(player.openMerchant(merchant, force) != null);
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "popen_trading";
}
@Override
public Integer[] numArgs() {
return new Integer[]{1, 2, 3};
}
@Override
public String docs() {
return "boolean {specifier, [player], [force]} Opens a trading interface for the current player,"
+ " or the one specified. Only one player can trade with a merchant at a time."
+ " If the merchant is already being traded with, the function will do nothing."
+ " When true, force will make the merchant trade with the player, closing the trade with"
+ " the previous player if there was one. Function returns true if trading was successfully"
+ " opened, and false if not.";
}
}
@api
public static class merchant_trader extends Recipes.recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREBadEntityException.class, CREFormatException.class, CREFormatException.class,
CREIllegalArgumentException.class, CRELengthException.class};
}
@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCMerchant merchant = GetMerchant(args[0], t);
return merchant.isTrading() ? new CString(merchant.getTrader().getUniqueId().toString(), t) : CNull.NULL;
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "merchant_trader";
}
@Override
public Integer[] numArgs() {
return new Integer[]{1};
}
@Override
public String docs() {
return "UUID {specifier} Returns the UUID of the user trading with the merchant, or null if no one is.";
}
}
private static final HashMap<String, MCMerchant> VIRTUAL_MERCHANTS = new HashMap<>();
/**
* Returns the merchant specified.
* @param specifier The string representing the merchant, whether entity UUID or virtual id.
* @param t
* @return abstracted merchant
*/
private static MCMerchant GetMerchant(Construct specifier, Target t) {
MCMerchant merchant;
if(specifier.val().length() == 36 || specifier.val().length() == 32) {
try {
MCEntity entity = Static.getEntity(specifier, t);
if(!(entity instanceof MCVillager)) {
throw new CREIllegalArgumentException("The entity specified is not capable of being an merchant.", t);
}
return ((MCVillager) entity).asMerchant();
} catch (CREFormatException iae) {
// not a UUID
}
}
merchant = VIRTUAL_MERCHANTS.get(specifier.val());
if(merchant == null) {
throw new CREIllegalArgumentException("A merchant named \"" + specifier.val() + "\" does not exist.", t);
}
return merchant;
}
private static MCMerchantRecipe trade(Construct c, Target t) {
if(!(c instanceof CArray)) {
throw new CRECastException("Expected array but received " + c.getCType().name(), t);
}
CArray recipe = (CArray) c;
MCItemStack result = ObjectGenerator.GetGenerator().item(recipe.get("result", t), t);
MCMerchantRecipe mer = (MCMerchantRecipe) StaticLayer.GetNewRecipe(null, MCRecipeType.MERCHANT, result);
if(recipe.containsKey("maxuses")) {
mer.setMaxUses(Static.getInt32(recipe.get("maxuses", t), t));
}
if(recipe.containsKey("uses")) {
mer.setUses(Static.getInt32(recipe.get("uses", t), t));
}
if(recipe.containsKey("hasxpreward")) {
mer.setHasExperienceReward(Static.getBoolean(recipe.get("hasxpreward", t), t));
}
CArray ingredients = Static.getArray(recipe.get("ingredients", t), t);
if(ingredients.inAssociativeMode()) {
throw new CREFormatException("Ingredients array is invalid.", t);
}
if(ingredients.size() < 1 || ingredients.size() > 2) {
throw new CRERangeException("Ingredients for merchants must contain 1 or 2 items, found "
+ ingredients.size(), t);
}
List<MCItemStack> mcIngredients = new ArrayList<>();
for(Construct ingredient : ingredients.asList()) {
mcIngredients.add(ObjectGenerator.GetGenerator().item(ingredient, t));
}
mer.setIngredients(mcIngredients);
return mer;
}
private static Construct trade(MCMerchantRecipe r, Target t) {
if(r == null) {
return CNull.NULL;
}
CArray ret = CArray.GetAssociativeArray(t);
ret.set("result", ObjectGenerator.GetGenerator().item(r.getResult(), t), t);
CArray il = new CArray(t);
for(MCItemStack i : r.getIngredients()) {
il.push(ObjectGenerator.GetGenerator().item(i, t), t);
}
ret.set("ingredients", il, t);
ret.set("maxuses", new CInt(r.getMaxUses(), t), t);
ret.set("uses", new CInt(r.getUses(), t), t);
ret.set("hasxpreward", CBoolean.get(r.hasExperienceReward()), t);
return ret;
}
}