/
TownTag.java
505 lines (452 loc) · 15.9 KB
/
TownTag.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package com.denizenscript.depenizen.bukkit.objects.towny;
import com.denizenscript.denizen.objects.ChunkTag;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.depenizen.bukkit.objects.factions.NationTag;
import com.palmergames.bukkit.towny.exceptions.EconomyException;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.*;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Location;
public class TownTag implements ObjectTag, Adjustable {
// <--[language]
// @name TownTag Objects
// @group Depenizen Object Types
// @plugin Depenizen, Towny
// @description
// A TownTag represents a Towny town in the world.
//
// These use the object notation "town@".
// The identity format for towns is <town_name>
// For example, 'town@mytown'.
//
// -->
/////////////////////
// OBJECT FETCHER
/////////////////
public static TownTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("town")
public static TownTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
string = string.replace("town@", "");
Town town = TownyUniverse.getInstance().getTown(string);
if (town == null) {
return null;
}
return new TownTag(town);
}
public static boolean matches(String arg) {
arg = arg.replace("town@", "");
return TownyUniverse.getInstance().hasTown(arg);
}
/////////////////////
// STATIC CONSTRUCTORS
/////////////////
Town town;
public TownTag(Town town) {
this.town = town;
}
public static TownTag fromWorldCoord(WorldCoord coord) {
if (coord == null) {
return null;
}
try {
return new TownTag(coord.getTownBlock().getTown());
}
catch (NotRegisteredException e) {
return null;
}
}
/////////////////////
// ObjectTag Methods
/////////////////
private String prefix = "Town";
@Override
public String getPrefix() {
return prefix;
}
@Override
public TownTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String getObjectType() {
return "Town";
}
@Override
public String identify() {
return "town@" + town.getName();
}
@Override
public String identifySimple() {
// TODO: Properties?
return identify();
}
public Town getTown() {
return town;
}
public boolean equals(TownTag town) {
return CoreUtilities.equalsIgnoreCase(town.getTown().getName(), this.getTown().getName());
}
@Override
public String toString() {
return identify();
}
@Override
public String getAttribute(Attribute attribute) {
// <--[tag]
// @attribute <TownTag.assistants>
// @returns ListTag
// @plugin Depenizen, Towny
// @description
// Returns a list of the town's assistants. Players will be valid PlayerTag instances, non-players will be plaintext of the name.
// -->
if (attribute.startsWith("assistants")) {
ListTag list = new ListTag();
for (Resident resident : town.getAssistants()) {
PlayerTag player = PlayerTag.valueOf(resident.getName(), attribute.context);
if (player != null) {
list.addObject(player);
}
else {
list.add(resident.getName());
}
}
return list.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.balance>
// @returns ElementTag(Decimal)
// @plugin Depenizen, Towny
// @mechanism TownTag.balance
// @description
// Returns the current money balance of the town.
// -->
else if (attribute.startsWith("balance")) {
try {
return new ElementTag(town.getAccount().getHoldingBalance()).getAttribute(attribute.fulfill(1));
}
catch (EconomyException e) {
if (!attribute.hasAlternative()) {
Debug.echoError("Invalid economy response!");
}
}
}
// <--[tag]
// @attribute <TownTag.board>
// @returns ElementTag
// @plugin Depenizen, Towny
// @description
// Returns the town's current board.
// -->
else if (attribute.startsWith("board")) {
return new ElementTag(town.getTownBoard())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.is_open>
// @returns ElementTag(Boolean)
// @plugin Depenizen, Towny
// @description
// Returns true if the town is currently open.
// -->
else if (attribute.startsWith("isopen") || attribute.startsWith("is_open")) {
return new ElementTag(town.isOpen())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.is_public>
// @returns ElementTag(Boolean)
// @plugin Depenizen, Towny
// @description
// Returns true if the town is currently public.
// -->
else if (attribute.startsWith("ispublic") || attribute.startsWith("is_public")) {
return new ElementTag(town.isPublic())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.mayor>
// @returns PlayerTag
// @plugin Depenizen, Towny
// @description
// Returns the mayor of the town.
// -->
else if (attribute.startsWith("mayor")) {
return PlayerTag.valueOf(town.getMayor().getName(), attribute.context)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.name>
// @returns ElementTag
// @plugin Depenizen, Towny
// @description
// Returns the town's name.
// -->
else if (attribute.startsWith("name")) {
return new ElementTag(town.getName())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.nation>
// @returns NationTag
// @plugin Depenizen, Towny
// @description
// Returns the nation that the town belongs to.
// -->
else if (attribute.startsWith("nation")) {
try {
return new NationTag(town.getNation())
.getAttribute(attribute.fulfill(1));
}
catch (NotRegisteredException e) {
}
}
// <--[tag]
// @attribute <TownTag.player_count>
// @returns ElementTag(Number)
// @plugin Depenizen, Towny
// @description
// Returns the number of players in the town.
// -->
else if (attribute.startsWith("playercount") || attribute.startsWith("player_count")) {
return new ElementTag(town.getNumResidents())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.residents>
// @returns ListTag
// @plugin Depenizen, Towny
// @description
// Returns a list of the town's residents. Players will be valid PlayerTag instances, non-players will be plaintext of the name.
// -->
else if (attribute.startsWith("residents")) {
ListTag list = new ListTag();
for (Resident resident : town.getResidents()) {
PlayerTag player = PlayerTag.valueOf(resident.getName(), attribute.context);
if (player != null) {
list.addObject(player);
}
else {
list.add(resident.getName());
}
}
return list.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.size>
// @returns ElementTag(Number)
// @plugin Depenizen, Towny
// @description
// Returns the number of blocks the town owns.
// -->
else if (attribute.startsWith("size")) {
return new ElementTag(town.getPurchasedBlocks())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.spawn>
// @returns LocationTag
// @plugin Depenizen, Towny
// @description
// Returns the spawn point of the town.
// -->
else if (attribute.startsWith("spawn")) {
try {
return new LocationTag(town.getSpawn()).getBlockLocation()
.getAttribute(attribute.fulfill(1));
}
catch (TownyException e) {
}
}
// <--[tag]
// @attribute <TownTag.tag>
// @returns ElementTag
// @plugin Depenizen, Towny
// @description
// Returns the town's tag.
// -->
else if (attribute.startsWith("tag")) {
return new ElementTag(town.getTag())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.taxes>
// @returns ElementTag(Decimal)
// @plugin Depenizen, Towny
// @description
// Returns the town's current taxes.
// -->
else if (attribute.startsWith("taxes")) {
return new ElementTag(town.getTaxes())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.outposts>
// @returns ListTag(LocationTag)
// @plugin Depenizen, Towny
// @description
// Returns a list of the town's outpost locations.
// -->
else if (attribute.startsWith("outposts")) {
ListTag posts = new ListTag();
for (Location p : town.getAllOutpostSpawns()) {
posts.addObject(new LocationTag(p));
}
return posts.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.has_explosions>
// @returns ElementTag(Boolean)
// @plugin Depenizen, Towny
// @description
// Returns if the town has explosions turned on.
// -->
else if (attribute.startsWith("has_explosions")) {
return new ElementTag(town.isBANG()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.has_mobs>
// @returns ElementTag(Boolean)
// @plugin Depenizen, Towny
// @description
// Returns if the town has mobs turned on.
// -->
else if (attribute.startsWith("has_mobs")) {
return new ElementTag(town.hasMobs()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.has_pvp>
// @returns ElementTag(Boolean)
// @plugin Depenizen, Towny
// @description
// Returns if the town has PvP turned on.
// -->
else if (attribute.startsWith("has_pvp")) {
return new ElementTag(town.isPVP()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.has_firespread>
// @returns ElementTag(Boolean)
// @plugin Depenizen, Towny
// @description
// Returns if the town has firespread turned on.
// -->
else if (attribute.startsWith("has_firespread")) {
return new ElementTag(town.isFire()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.has_taxpercent>
// @returns ElementTag(Boolean)
// @plugin Depenizen, Towny
// @description
// Returns if the town has taxes in percentage.
// -->
else if (attribute.startsWith("has_taxpercent")) {
return new ElementTag(town.isTaxPercentage()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.plot_object_group_names>
// @returns ListTag
// @plugin Depenizen, Towny
// @description
// Returns a list of the names of town plot object groups.
// -->
else if (attribute.startsWith("plot_object_group_names")) {
ListTag output = new ListTag();
if (!town.hasPlotGroups()) {
return null;
}
for (PlotGroup group : town.getPlotObjectGroups()) {
output.add(group.getName());
}
return output.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.plots>
// @returns ListTag(LocationTag)
// @plugin Depenizen, Towny
// @description
// Returns a list of the names of town plots.
// -->
else if (attribute.startsWith("plots")) {
ListTag output = new ListTag();
for (TownBlock block : town.getTownBlocks()) {
output.addObject(new ChunkTag(new WorldTag(block.getWorld().getName()), block.getX(), block.getZ()));
}
return output.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.plottax>
// @returns ElementTag(Decimal)
// @plugin Depenizen, Towny
// @description
// Returns the amount of taxes collected from plots.
// -->
else if (attribute.startsWith("plottax")) {
return new ElementTag(town.getPlotTax()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <TownTag.plotprice>
// @returns ElementTag(Decimal)
// @plugin Depenizen, Towny
// @description
// Returns the price of a plot.
// -->
else if (attribute.startsWith("plotprice")) {
return new ElementTag(town.getPlotPrice()).getAttribute(attribute.fulfill(1));
}
return new ElementTag(identify()).getAttribute(attribute);
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object TownTag
// @name balance
// @input ElementTag(Decimal)|ElementTag
// @description
// Sets the money balance of a town, with a reason for the change.
// @tags
// <TownTag.balance>
// -->
if (mechanism.matches("balance")) {
ListTag input = mechanism.valueAsType(ListTag.class);
if (input.size() != 2 || !ArgumentHelper.matchesDouble(input.get(0))) {
mechanism.echoError("Invalid balance mech input.");
return;
}
try {
town.getAccount().setBalance(new ElementTag(input.get(0)).asDouble(), input.get(1));
}
catch (EconomyException ex) {
Debug.echoError(ex);
}
}
}
@Override
public void applyProperty(Mechanism mechanism) {
Debug.echoError("Cannot apply properties to a Towny town!");
}
}