/
CommonRegistries.java
441 lines (414 loc) · 20.4 KB
/
CommonRegistries.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
package com.denizenscript.denizen.utilities;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.objects.properties.bukkit.BukkitColorExtensions;
import com.denizenscript.denizen.tags.core.*;
import com.denizenscript.denizencore.objects.ObjectType;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.*;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.plugin.Plugin;
public class CommonRegistries {
// <--[language]
// @name ObjectTags
// @group Object System
// @description
// ObjectTags are a system put into place by Denizen that make working with things, or 'objects',
// in Minecraft and Denizen easier. Many parts of scripts will require some kind of object as an
// argument, identifier/type, or such as in world events, part of an event name. The ObjectTags notation
// system helps both you and Denizen know what type of objects are being referenced and worked with.
//
// So when should you use ObjectTags? In arguments, event names, replaceable tags, configs, flags, and
// more! If you're just a beginner, you've probably been using them without even realizing it!
//
// ObjectTag is a broader term for a 'type' of object that more specifically represents something,
// such as a LocationTag or ScriptTag, often times just referred to as a 'location' or 'script'. Denizen
// employs many object types that you should be familiar with. You'll notice that many times objects
// are referenced with their 'ObjectTag notation' which is in the format of 'x@', the x being the specific
// notation of an object type. Example: player objects use the p@ notation, and locations use l@.
// This notation is automatically generated when directly displaying objects, or saving them into data files.
// It should never be manually typed into a script.
//
// Let's take the tag system, for example. It uses the ObjectTags system pretty heavily. For instance,
// every time you use <player.name> or <npc.id>, you're using a ObjectTag, which brings us to a simple
// clarification: Why <player.name> and not <PlayerTag.name>? That's because Denizen allows Players,
// NPCs and other 'in-context objects' to be linked to certain scripts. In short, <player> already
// contains a reference to a specific player, such as the player that died in a world event 'on player dies'.
// <PlayerTag.name> is instead the format for documentation, with "PlayerTag" simply indicating 'any player object here'.
//
// ObjectTags can be used to CREATE new instances of objects, too! Though not all types allow 'new'
// objects to be created, many do, such as ItemTags. With the use of tags, it's easy to reference a specific
// item, say -- an item in the Player's hand -- items are also able to use a constructor to make a new item,
// and say, drop it in the world. Take the case of the command/usage '- drop diamond_ore'. The item object
// used is a brand new diamond_ore, which is then dropped by the command to a location of your choice -- just
// specify an additional location argument.
//
// There's a great deal more to learn about ObjectTags, so be sure to check out each object type for more
// specific information. While all ObjectTags share some features, many contain goodies on top of that!
// -->
// <--[language]
// @name Tick
// @group Common Terminology
// @description
// A 'tick' is usually referred to as 1/20th of a second, the speed at which Minecraft servers update
// and process everything on them.
// -->
public static void registerMainTagHandlers() {
// Objects
if (Depends.citizens != null) {
new NPCTagBase();
}
new PlayerTagBase();
// Other bases
new CustomColorTagBase();
new ServerTagBase();
new TextTagBase();
}
public static void registerMainObjects() {
registerObjectTypes();
registerNotables();
registerConversions();
registerSubtypeSets();
// Final debug
if (CoreConfiguration.debugVerbose) {
StringBuilder debug = new StringBuilder(256);
for (ObjectType<?> objectType : ObjectFetcher.objectsByPrefix.values()) {
debug.append(DebugInternals.getClassNameOpti(objectType.clazz)).append(" as ").append(objectType.prefix).append(", ");
}
Debug.echoApproval("Loaded core object types: [" + debug.substring(0, debug.length() - 2) + "]");
}
}
public static ObjectType<BiomeTag> TYPE_BIOME;
public static ObjectType<ChunkTag> TYPE_CHUNK;
public static ObjectType<CuboidTag> TYPE_CUBOID;
public static ObjectType<EllipsoidTag> TYPE_ELLIPSOID;
public static ObjectType<EnchantmentTag> TYPE_ENCHANTMENT;
public static ObjectType<EntityTag> TYPE_ENTITY;
public static ObjectType<InventoryTag> TYPE_INVENTORY;
public static ObjectType<ItemTag> TYPE_ITEM;
public static ObjectType<LocationTag> TYPE_LOCATION;
public static ObjectType<MaterialTag> TYPE_MATERIAL;
public static ObjectType<NPCTag> TYPE_NPC;
public static ObjectType<PlayerTag> TYPE_PLAYER;
public static ObjectType<PluginTag> TYPE_PLUGIN;
public static ObjectType<PolygonTag> TYPE_POLYGON;
public static ObjectType<TradeTag> TYPE_TRADE;
public static ObjectType<WorldTag> TYPE_WORLD;
private static void registerObjectTypes() {
// <--[tag]
// @attribute <biome[<biome>]>
// @returns BiomeTag
// @description
// Returns a biome object constructed from the input value.
// Refer to <@link objecttype BiomeTag>.
// -->
TYPE_BIOME = ObjectFetcher.registerWithObjectFetcher(BiomeTag.class, BiomeTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // b@
// <--[tag]
// @attribute <chunk[<chunk>]>
// @returns ChunkTag
// @description
// Returns a chunk object constructed from the input value.
// Refer to <@link objecttype ChunkTag>.
// -->
TYPE_CHUNK = ObjectFetcher.registerWithObjectFetcher(ChunkTag.class, ChunkTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // ch@
// <--[tag]
// @attribute <cuboid[<cuboid>]>
// @returns CuboidTag
// @description
// Returns a cuboid object constructed from the input value.
// Refer to <@link objecttype CuboidTag>.
// -->
TYPE_CUBOID = ObjectFetcher.registerWithObjectFetcher(CuboidTag.class, CuboidTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // cu@
// <--[tag]
// @attribute <ellipsoid[<ellipsoid>]>
// @returns EllipsoidTag
// @description
// Returns an ellipsoid object constructed from the input value.
// Refer to <@link objecttype EllipsoidTag>.
// -->
TYPE_ELLIPSOID = ObjectFetcher.registerWithObjectFetcher(EllipsoidTag.class, EllipsoidTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // ellipsoid@
// <--[tag]
// @attribute <enchantment[<enchantment>]>
// @returns EnchantmentTag
// @description
// Returns an enchantment object constructed from the input value.
// Refer to <@link objecttype EnchantmentTag>.
// -->
TYPE_ENCHANTMENT = ObjectFetcher.registerWithObjectFetcher(EnchantmentTag.class, EnchantmentTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // enchantment@
// <--[tag]
// @attribute <entity[<entity>]>
// @returns EntityTag
// @description
// Returns an entity object constructed from the input value.
// Refer to <@link objecttype EntityTag>.
// -->
TYPE_ENTITY = ObjectFetcher.registerWithObjectFetcher(EntityTag.class, EntityTag.tagProcessor).generateBaseTag(); // e@
TYPE_ENTITY.typeChecker = (inp) -> { // This is adapted 'no other type code' but for e@, p@, and n@
if (inp == null) {
return false;
}
if (inp instanceof PlayerTag || inp instanceof EntityTag || inp instanceof NPCTag) {
return true;
}
if (inp instanceof ElementTag) {
String simple = inp.identifySimple();
int atIndex = simple.indexOf('@');
if (atIndex != -1) {
String code = simple.substring(0, atIndex);
if (!code.equals("e") && !code.equals("p") && !code.equals("n") && !code.equals("el")) {
if (ObjectFetcher.objectsByPrefix.containsKey(code)) {
return false;
}
}
}
return true;
}
return false;
};
TYPE_ENTITY.typeConverter = (obj, context) -> {
if (obj instanceof PlayerTag) {
if (!((PlayerTag) obj).isOnline()) {
if (context.showErrors()) {
Debug.echoError("Player '" + obj.debuggable() + "' is offline, cannot convert to EntityTag.");
}
return null;
}
return new EntityTag(((PlayerTag) obj).getPlayerEntity());
}
else if (obj instanceof NPCTag) {
if (!((NPCTag) obj).isSpawned() && !EntityTag.allowDespawnedNpcs) {
if (context.showErrors()) {
Debug.echoError("NPC '" + obj.debuggable() + "' is unspawned, cannot convert to EntityTag.");
}
return null;
}
return new EntityTag((NPCTag) obj);
}
return EntityTag.valueOf(obj.toString(), context);
};
TYPE_ENTITY.typeShouldBeChecker = (obj) -> {
if (obj instanceof EntityFormObject) {
return true;
}
String raw = obj.toString();
if (raw.startsWith("p@") || raw.startsWith("e@") || raw.startsWith("n@")) {
return true;
}
return false;
};
// <--[tag]
// @attribute <inventory[<inventory>]>
// @returns InventoryTag
// @description
// Returns an inventory object constructed from the input value.
// Refer to <@link objecttype InventoryTag>.
// -->
// non-static due to notes and inventory scripts
TYPE_INVENTORY = ObjectFetcher.registerWithObjectFetcher(InventoryTag.class, InventoryTag.tagProcessor).setAsNOtherCode().generateBaseTag(); // in@
// <--[tag]
// @attribute <item[<item>]>
// @returns ItemTag
// @description
// Returns an item object constructed from the input value.
// Refer to <@link objecttype ItemTag>.
// -->
// non-static as item scripts can contain tags
TYPE_ITEM = ObjectFetcher.registerWithObjectFetcher(ItemTag.class, ItemTag.tagProcessor).setAsNOtherCode().generateBaseTag(); // i@
// <--[tag]
// @attribute <location[<location>]>
// @returns LocationTag
// @description
// Returns a location object constructed from the input value.
// Refer to <@link objecttype LocationTag>.
// -->
TYPE_LOCATION = ObjectFetcher.registerWithObjectFetcher(LocationTag.class, LocationTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // l@
// <--[tag]
// @attribute <material[<material>]>
// @returns MaterialTag
// @description
// Returns a material object constructed from the input value.
// Refer to <@link objecttype MaterialTag>.
// -->
TYPE_MATERIAL = ObjectFetcher.registerWithObjectFetcher(MaterialTag.class, MaterialTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // m@
if (Depends.citizens != null) {
// Tag generated externally as input is optional
TYPE_NPC = ObjectFetcher.registerWithObjectFetcher(NPCTag.class, NPCTag.tagProcessor); // n@
TYPE_NPC.typeChecker = (inp) -> { // This is adapted 'no other type code' but allows instanceof EntityTag
if (inp == null) {
return false;
}
if (inp instanceof NPCTag || inp instanceof EntityTag) {
return true;
}
if (inp instanceof ElementTag) {
String simple = inp.identifySimple();
int atIndex = simple.indexOf('@');
if (atIndex != -1) {
String code = simple.substring(0, atIndex);
if (!code.equals("n") && !code.equals("el")) {
if (ObjectFetcher.objectsByPrefix.containsKey(code)) {
return false;
}
}
}
return true;
}
return false;
};
TYPE_NPC.typeConverter = (obj, context) -> {
if (obj instanceof EntityTag && ((EntityTag) obj).isCitizensNPC()) {
return ((EntityTag) obj).getDenizenNPC();
}
return NPCTag.valueOf(obj.toString(), context);
};
}
// Tag generated externally as input is optional
TYPE_PLAYER = ObjectFetcher.registerWithObjectFetcher(PlayerTag.class, PlayerTag.tagProcessor); // p@
TYPE_PLAYER.typeChecker = (inp) -> { // This is adapted 'no other type code' but allows instanceof EntityTag
if (inp == null) {
return false;
}
if (inp instanceof PlayerTag || inp instanceof EntityTag) {
return true;
}
if (inp instanceof ElementTag) {
String simple = inp.identifySimple();
int atIndex = simple.indexOf('@');
if (atIndex != -1) {
String code = simple.substring(0, atIndex);
if (!code.equals("p") && !code.equals("el")) {
if (ObjectFetcher.objectsByPrefix.containsKey(code)) {
return false;
}
}
}
return true;
}
return false;
};
TYPE_PLAYER.typeConverter = (obj, context) -> {
if (obj instanceof EntityTag && ((EntityTag) obj).isPlayer()) {
return ((EntityTag) obj).getDenizenPlayer();
}
return PlayerTag.valueOf(obj.toString(), context);
};
// <--[tag]
// @attribute <plugin[<plugin>]>
// @returns PluginTag
// @description
// Returns a plugin object constructed from the input value.
// Refer to <@link objecttype PluginTag>.
// -->
TYPE_PLUGIN = ObjectFetcher.registerWithObjectFetcher(PluginTag.class, PluginTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // pl@
// <--[tag]
// @attribute <polygon[<polygon>]>
// @returns PolygonTag
// @description
// Returns a polygon object constructed from the input value.
// Refer to <@link objecttype PolygonTag>.
// -->
TYPE_POLYGON = ObjectFetcher.registerWithObjectFetcher(PolygonTag.class, PolygonTag.tagProcessor).setAsNOtherCode().setCanConvertStatic().generateBaseTag(); // polygon@
// <--[tag]
// @attribute <trade[<trade>]>
// @returns TradeTag
// @description
// Returns a trade object constructed from the input value.
// Refer to <@link objecttype TradeTag>.
// -->
// Non-static due to potential for dynamic items.
TYPE_TRADE = ObjectFetcher.registerWithObjectFetcher(TradeTag.class, TradeTag.tagProcessor).setAsNOtherCode().generateBaseTag(); // trade@
// <--[tag]
// @attribute <world[<world>]>
// @returns WorldTag
// @description
// Returns a world object constructed from the input value.
// Refer to <@link objecttype WorldTag>.
// -->
// non-static as worlds can be dynamically loaded
TYPE_WORLD = ObjectFetcher.registerWithObjectFetcher(WorldTag.class, WorldTag.tagProcessor).setAsNOtherCode().generateBaseTag(); // w@
}
private static void registerNotables() {
NoteManager.registerObjectTypeAsNotable(CuboidTag.class);
NoteManager.registerObjectTypeAsNotable(EllipsoidTag.class);
NoteManager.registerObjectTypeAsNotable(InventoryTag.class);
NoteManager.registerObjectTypeAsNotable(ItemTag.class);
NoteManager.registerObjectTypeAsNotable(LocationTag.class);
NoteManager.registerObjectTypeAsNotable(PolygonTag.class);
}
private static void registerConversions() {
CoreUtilities.objectConversions.add((obj) -> {
if (obj instanceof Biome) {
return new BiomeTag((Biome) obj);
}
if (obj instanceof Chunk) {
return new ChunkTag((Chunk) obj);
}
if (obj instanceof Color) {
return BukkitColorExtensions.fromColor((Color) obj);
}
if (obj instanceof Enchantment) {
return new EnchantmentTag((Enchantment) obj);
}
if (obj instanceof Entity) {
return new EntityTag((Entity) obj).getDenizenObject();
}
if (obj instanceof Inventory) {
return InventoryTag.mirrorBukkitInventory((Inventory) obj);
}
if (obj instanceof ItemStack) {
return new ItemTag((ItemStack) obj);
}
if (obj instanceof Location) {
return new LocationTag((Location) obj);
}
if (obj instanceof Material) {
return new MaterialTag((Material) obj);
}
if (obj instanceof BlockData) {
return new MaterialTag((BlockData) obj);
}
if (obj instanceof Block) {
return new LocationTag(((Block) obj).getLocation());
}
if (Depends.citizens != null && obj instanceof NPC) {
return new NPCTag((NPC) obj);
}
if (obj instanceof OfflinePlayer) {
return new PlayerTag((OfflinePlayer) obj);
}
if (obj instanceof Plugin) {
return new PluginTag((Plugin) obj);
}
if (obj instanceof MerchantRecipe) {
return new TradeTag((MerchantRecipe) obj);
}
if (obj instanceof World) {
return new WorldTag((World) obj);
}
return null;
});
}
private static void registerSubtypeSets() {
ObjectFetcher.registerCrossType(EntityTag.class, EntityFormObject.class);
ObjectFetcher.registerCrossType(PlayerTag.class, EntityTag.class);
ObjectFetcher.registerCrossType(PlayerTag.class, EntityFormObject.class);
if (Depends.citizens != null) {
ObjectFetcher.registerCrossType(NPCTag.class, EntityTag.class);
ObjectFetcher.registerCrossType(NPCTag.class, EntityFormObject.class);
}
}
}