-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
AreaContainmentObject.java
323 lines (296 loc) · 13.2 KB
/
AreaContainmentObject.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
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.flags.LocationFlagSearchHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
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.ObjectTagProcessor;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.util.function.Predicate;
public interface AreaContainmentObject extends ObjectTag {
// <--[ObjectType]
// @name AreaObject
// @prefix None
// @base None
// @format
// N/A
//
// @description
// "AreaObject" is a pseudo-ObjectType that represents any object that indicates a world-space area, such as a CuboidTag.
//
// -->
String getNoteName();
boolean doesContainLocation(Location loc);
CuboidTag getCuboidBoundary();
WorldTag getWorld();
ListTag getShell();
ListTag getBlocks(Predicate<Location> test);
AreaContainmentObject withWorld(WorldTag world);
default ListTag getBlocksFlagged(String flagName, Attribute attribute) {
CuboidTag cuboid = getCuboidBoundary();
ListTag blocks = new ListTag();
for (CuboidTag.LocationPair pair : cuboid.pairs) {
ChunkTag minChunk = new ChunkTag(pair.low);
ChunkTag maxChunk = new ChunkTag(pair.high);
ChunkTag subChunk = new ChunkTag(pair.low);
for (int x = minChunk.getX(); x <= maxChunk.getX(); x++) {
subChunk.chunkX = x;
for (int z = minChunk.getZ(); z <= maxChunk.getZ(); z++) {
subChunk.chunkZ = z;
subChunk.cachedChunk = null;
if (subChunk.isLoadedSafe()) {
LocationFlagSearchHelper.getFlaggedLocations(subChunk.getChunkForTag(attribute), flagName, (loc) -> {
if (doesContainLocation(loc)) {
blocks.addObject(new LocationTag(loc));
}
});
}
}
}
}
return blocks;
}
static void registerTags(ObjectTagProcessor<? extends AreaContainmentObject> processor) {
// <--[tag]
// @attribute <AreaObject.bounding_box>
// @returns CuboidTag
// @description
// Returns a cuboid approximately representing the maximal bounding box of the area (anything this cuboid does not contain, is also not contained by the area, but not vice versa).
// For single-member CuboidTags, this tag returns a copy of the cuboid.
// -->
processor.registerTag("bounding_box", (attribute, area) -> {
return area.getCuboidBoundary();
});
// <--[tag]
// @attribute <AreaObject.world>
// @returns WorldTag
// @description
// Returns the area's world.
// -->
processor.registerTag("world", (attribute, area) -> {
return area.getWorld();
});
// <--[tag]
// @attribute <AreaObject.players>
// @returns ListTag(PlayerTag)
// @description
// Gets a list of all players currently within the area.
// -->
processor.registerTag("players", (attribute, area) -> {
ListTag result = new ListTag();
for (Player player : Bukkit.getOnlinePlayers()) {
if (area.doesContainLocation(player.getLocation())) {
result.addObject(PlayerTag.mirrorBukkitPlayer(player));
}
}
return result;
});
// <--[tag]
// @attribute <AreaObject.npcs>
// @returns ListTag(NPCTag)
// @description
// Gets a list of all NPCs currently within the area.
// -->
if (Depends.citizens != null) {
processor.registerTag("npcs", (attribute, area) -> {
ListTag result = new ListTag();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
NPCTag dnpc = new NPCTag(npc);
if (area.doesContainLocation(dnpc.getLocation())) {
result.addObject(dnpc);
}
}
return result;
});
}
// <--[tag]
// @attribute <AreaObject.entities[(<matcher>)]>
// @returns ListTag(EntityTag)
// @description
// Gets a list of all entities currently within the area, with an optional search parameter for the entity.
// -->
processor.registerTag("entities", (attribute, area) -> {
String matcher = attribute.hasContext(1) ? attribute.getContext(1) : null;
ListTag entities = new ListTag();
for (Entity ent : area.getCuboidBoundary().getEntitiesPossiblyWithinForTag()) {
if (area.doesContainLocation(ent.getLocation())) {
EntityTag current = new EntityTag(ent);
if (matcher == null || BukkitScriptEvent.tryEntity(current, matcher)) {
entities.addObject(current.getDenizenObject());
}
}
}
return entities;
});
// <--[tag]
// @attribute <AreaObject.living_entities>
// @returns ListTag(EntityTag)
// @description
// Gets a list of all living entities currently within the area.
// This includes Players, mobs, NPCs, etc., but excludes dropped items, experience orbs, etc.
// -->
processor.registerTag("living_entities", (attribute, area) -> {
ListTag result = new ListTag();
for (Entity ent : area.getCuboidBoundary().getEntitiesPossiblyWithinForTag()) {
if (ent instanceof LivingEntity && area.doesContainLocation(ent.getLocation()) && !EntityTag.isCitizensNPC(ent)) {
result.addObject(new EntityTag(ent).getDenizenObject());
}
}
return result;
});
// <--[tag]
// @attribute <AreaObject.contains[<location>]>
// @returns ElementTag(Boolean)
// @description
// Returns a boolean indicating whether the specified location is inside this area.
// -->
processor.registerTag("contains", (attribute, area) -> {
if (!attribute.hasContext(1)) {
return null;
}
LocationTag loc = attribute.contextAsType(1, LocationTag.class);
if (loc == null) {
return null;
}
return new ElementTag(area.doesContainLocation(loc));
}, "contains_location");
// <--[tag]
// @attribute <AreaObject.blocks[(<matcher>)]>
// @returns ListTag(LocationTag)
// @description
// Returns each block location within the area.
// Optionally, specify a material match to only return locations with that block type.
// -->
processor.registerTag("blocks", (attribute, area) -> {
if (attribute.hasContext(1)) {
NMSHandler.getChunkHelper().changeChunkServerThread(area.getWorld().getWorld());
try {
String matcher = attribute.getContext(1);
Predicate<Location> predicate = (l) -> BukkitScriptEvent.tryMaterial(l.getBlock().getType(), matcher);
return area.getBlocks(predicate);
}
finally {
NMSHandler.getChunkHelper().restoreServerThread(area.getWorld().getWorld());
}
}
return area.getBlocks(null);
}, "get_blocks");
// <--[tag]
// @attribute <AreaObject.spawnable_blocks[(<matcher>)]>
// @returns ListTag(LocationTag)
// @description
// Returns each LocationTag within the area that is safe for players or similar entities to spawn in.
// Optionally, specify a material matcher to only return locations with that block type.
// -->
processor.registerTag("spawnable_blocks", (attribute, area) -> {
NMSHandler.getChunkHelper().changeChunkServerThread(area.getWorld().getWorld());
try {
if (attribute.hasContext(1)) {
String matcher = attribute.getContext(1);
Predicate<Location> predicate = (l) -> isSpawnable(l) && BukkitScriptEvent.tryMaterial(l.getBlock().getType(), matcher);
return area.getBlocks(predicate);
}
return area.getBlocks(AreaContainmentObject::isSpawnable);
}
finally {
NMSHandler.getChunkHelper().restoreServerThread(area.getWorld().getWorld());
}
}, "get_spawnable_blocks");
// <--[tag]
// @attribute <AreaObject.blocks_flagged[<flag_name>]>
// @returns ListTag(LocationTag)
// @description
// Gets a list of all block locations with a specified flag within the area.
// Searches the internal flag lists, rather than through all possible blocks.
// -->
processor.registerTag("blocks_flagged", (attribute, area) -> {
if (!attribute.hasContext(1)) {
return null;
}
return area.getBlocksFlagged(CoreUtilities.toLowerCase(attribute.getContext(1)), attribute);
});
// <--[tag]
// @attribute <AreaObject.shell>
// @returns ListTag(LocationTag)
// @description
// Returns each block location on the 3D outer shell of the area.
// This tag is useful for displaying particles or blocks to mark the boundary of the area.
// -->
processor.registerTag("shell", (attribute, area) -> {
return area.getShell();
});
// <--[tag]
// @attribute <AreaObject.is_within[<cuboid>]>
// @returns ElementTag(Boolean)
// @description
// Returns whether this area is fully inside another cuboid.
// -->
processor.registerTag("is_within", (attribute, area) -> {
if (!attribute.hasContext(1)) {
return null;
}
CuboidTag cub2 = attribute.contextAsType(1, CuboidTag.class);
if (cub2 == null) {
return null;
}
CuboidTag cuboid = area instanceof CuboidTag ? (CuboidTag) area : area.getCuboidBoundary();
if (cub2 != null) {
boolean contains = true;
for (CuboidTag.LocationPair pair2 : cuboid.pairs) {
boolean contained = false;
for (CuboidTag.LocationPair pair : cub2.pairs) {
if (!pair.low.getWorld().getName().equalsIgnoreCase(pair2.low.getWorld().getName())) {
return new ElementTag(false);
}
if (pair2.low.getX() >= pair.low.getX()
&& pair2.low.getY() >= pair.low.getY()
&& pair2.low.getZ() >= pair.low.getZ()
&& pair2.high.getX() <= pair.high.getX()
&& pair2.high.getY() <= pair.high.getY()
&& pair2.high.getZ() <= pair.high.getZ()) {
contained = true;
break;
}
}
if (!contained) {
contains = false;
break;
}
}
return new ElementTag(contains);
}
return null;
});
// <--[tag]
// @attribute <AreaObject.with_world[<world>]>
// @returns AreaObject
// @description
// Returns a copy of the area, with the specified world.
// -->
processor.registerTag("with_world", (attribute, area) -> {
if (!attribute.hasContext(1)) {
return null;
}
WorldTag world = attribute.contextAsType(1, WorldTag.class);
if (world == null) {
return null;
}
return area.withWorld(world);
});
}
static boolean isSpawnable(Location loc) {
return loc.getBlock().getType().isAir()
&& (loc.clone().add(0, 1, 0).getBlock().getType().isAir()
&& (loc.clone().add(0, -1, 0)).getBlock().getType().isSolid());
}
}