/
BiomeTag.java
362 lines (324 loc) · 13.2 KB
/
BiomeTag.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
package com.denizenscript.denizen.objects;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.RedirectionFlagTracker;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.BiomeNMS;
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.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.entity.EntityType;
import java.util.List;
public class BiomeTag implements ObjectTag, Adjustable, FlaggableObject {
// <--[ObjectType]
// @name BiomeTag
// @prefix b
// @base ElementTag
// @implements FlaggableObject
// @ExampleTagBase biome[desert]
// @ExampleValues desert
// @ExampleForReturns
// - adjust <player.location.to_ellipsoid[60,3,10].blocks> biome:%VALUE%
// @ExampleForReturns
// - adjust <player.location.chunk> set_all_biomes:%VALUE%
// @format
// The identity format for biomes is a world name, then a comma, then the biome key. For example: 'hub,desert', or 'space,minecraft:desert'.
//
// @description
// A BiomeTag represents a world biome type. Vanilla biomes are globally available, however some biomes are world-specific when added by datapacks.
//
// A list of all vanilla biomes can be found at <@link url https://minecraft.fandom.com/wiki/Biome#Biome_IDs>.
//
// BiomeTags without a specific world will work as though they are in the server's default world.
//
// This object type is flaggable.
// Flags on this object type will be stored in the server saves file, under special sub-key "__biomes"
//
// -->
//////////////////
// OBJECT FETCHER
////////////////
@Fetchable("b")
public static BiomeTag valueOf(String string, TagContext context) {
if (string.startsWith("b@")) {
string = string.substring(2);
}
string = CoreUtilities.toLowerCase(string);
int comma = string.indexOf(',');
String worldName = null, biomeName = string;
if (comma != -1) {
worldName = string.substring(0, comma);
biomeName = string.substring(comma + 1);
}
World world = Bukkit.getWorlds().get(0);
if (worldName != null) {
WorldTag worldTag = WorldTag.valueOf(worldName, context);
if (worldTag == null || worldTag.getWorld() == null) {
return null;
}
world = worldTag.getWorld();
}
BiomeNMS biome = NMSHandler.instance.getBiomeNMS(world, biomeName);
if (biome == null) {
return null;
}
return new BiomeTag(biome);
}
public static boolean matches(String arg) {
if (arg.startsWith("b@")) {
return true;
}
return valueOf(arg, CoreUtilities.noDebugContext) != null;
}
///////////////
// Constructors
/////////////
public BiomeTag(Biome biome) {
String key;
if (biome.getKey().getNamespace().equals("minecraft")) {
key = biome.getKey().getKey();
}
else {
key = biome.getKey().toString();
}
this.biome = NMSHandler.instance.getBiomeNMS(Bukkit.getWorlds().get(0), key);
}
public BiomeTag(BiomeNMS biome) {
this.biome = biome;
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
private BiomeNMS biome;
public BiomeNMS getBiome() {
return biome;
}
String prefix = "biome";
@Override
public String getPrefix() {
return prefix;
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String identify() {
return "b@" + biome.world.getName() + "," + biome.getName();
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public ObjectTag setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}
@Override
public AbstractFlagTracker getFlagTracker() {
return new RedirectionFlagTracker(DenizenCore.serverFlagMap, "__biomes." + biome.getName().replace(".", "&dot"));
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}
public static void register() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
// <--[tag]
// @attribute <BiomeTag.downfall_type>
// @returns ElementTag
// @mechanism BiomeTag.downfall_type
// @description
// Returns this biome's downfall type for when a world has weather.
// This can be RAIN, SNOW, or NONE.
// @example
// # In a plains biome, this fills with 'RAIN'.
// - narrate "The downfall type in plains biomes is: <biome[plains].downfall_type>!"
// -->
tagProcessor.registerTag(ElementTag.class, "downfall_type", (attribute, object) -> {
return new ElementTag(object.biome.getDownfallType());
});
// <--[tag]
// @attribute <BiomeTag.name>
// @returns ElementTag
// @description
// Returns this biome's name.
// @example
// # In a plains biome, this fills with 'plains'.
// - narrate "You are currently in a <biome[plains].name> biome!"
// -->
tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> {
return new ElementTag(CoreUtilities.toLowerCase(object.biome.getName()));
});
// <--[tag]
// @attribute <BiomeTag.humidity>
// @returns ElementTag(Decimal)
// @mechanism BiomeTag.humidity
// @description
// Returns the humidity of this biome.
// @example
// # In a plains biome, this fills with '0.4'.
// - narrate "Humidity in a plains biome is <biome[plains].humidity>! So humid!"
// -->
tagProcessor.registerTag(ElementTag.class, "humidity", (attribute, object) -> {
return new ElementTag(object.biome.getHumidity());
});
// <--[tag]
// @attribute <BiomeTag.temperature>
// @returns ElementTag(Decimal)
// @mechanism BiomeTag.temperature
// @description
// Returns the temperature of this biome.
// @example
// # In a plains biome, this fills with '0.8'.
// - narrate "Stay warm! In a plains biome, the temperature is <biome[plains].temperature>!"
// -->
tagProcessor.registerTag(ElementTag.class, "temperature", (attribute, object) -> {
return new ElementTag(object.biome.getTemperature());
});
// <--[tag]
// @attribute <BiomeTag.spawnable_entities[(<type>)]>
// @returns ListTag
// @description
// Returns all entities that spawn naturally in this biome.
// Optionally specify a type as: AMBIENT, CREATURES, MONSTERS, WATER, or ALL.
// (By default, will be "ALL").
// @example
// # Narrates the types of entities of type MONSTERS that can spawn in the player's biome.
// # For example, in a plains biome this could contain "SPIDER", "ZOMBIE", "CREEPER", etc.
// - narrate <player.location.biome.spawnable_entities[MONSTERS].formatted>
// -->
tagProcessor.registerTag(ListTag.class, "spawnable_entities", (attribute, object) -> {
List<EntityType> entityTypes;
if (attribute.startsWith("ambient", 2)) {
BukkitImplDeprecations.biomeSpawnableTag.warn(attribute.context);
attribute.fulfill(1);
entityTypes = object.biome.getAmbientEntities();
}
else if (attribute.startsWith("creatures", 2)) {
BukkitImplDeprecations.biomeSpawnableTag.warn(attribute.context);
attribute.fulfill(1);
entityTypes = object.biome.getCreatureEntities();
}
else if (attribute.startsWith("monsters", 2)) {
BukkitImplDeprecations.biomeSpawnableTag.warn(attribute.context);
attribute.fulfill(1);
entityTypes = object.biome.getMonsterEntities();
}
else if (attribute.startsWith("water", 2)) {
BukkitImplDeprecations.biomeSpawnableTag.warn(attribute.context);
attribute.fulfill(1);
entityTypes = object.biome.getWaterEntities();
}
else {
String type = attribute.hasParam() ? CoreUtilities.toLowerCase(attribute.getParam()) : "all";
switch (type) {
case "ambient":
entityTypes = object.biome.getAmbientEntities();
break;
case "creatures":
entityTypes = object.biome.getCreatureEntities();
break;
case "monsters":
entityTypes = object.biome.getMonsterEntities();
break;
case "water":
entityTypes = object.biome.getWaterEntities();
break;
default:
entityTypes = object.biome.getAllEntities();
break;
}
}
ListTag list = new ListTag();
for (EntityType entityType : entityTypes) {
list.add(entityType.name());
}
return list;
});
}
public static ObjectTagProcessor<BiomeTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to a biome!");
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object BiomeTag
// @name humidity
// @input ElementTag(Decimal)
// @description
// Sets the humidity for this biome server-wide.
// If this is greater than 0.85, fire has less chance to spread in this biome.
// Resets on server restart.
// @tags
// <BiomeTag.humidity>
// @example
// # Adjusts the humidity of the plains biome permanently, using a server start event to keep it applied.
// on server start:
// - adjust <biome[plains]> humidity:0.5
// -->
if (mechanism.matches("humidity") && mechanism.requireFloat()) {
biome.setHumidity(mechanism.getValue().asFloat());
}
// <--[mechanism]
// @object BiomeTag
// @name temperature
// @input ElementTag(Decimal)
// @description
// Sets the temperature for this biome server-wide.
// If this is less than 0.15, snow will form on the ground when weather occurs in the world and a layer of ice will form over water.
// Resets on server restart.
// @tags
// <BiomeTag.temperature>
// @example
// # Adjusts the temperature of the plains biome permanently, using a server start event to keep it applied.
// on server start:
// - adjust <biome[plains]> temperature:0.5
// -->
if (mechanism.matches("temperature") && mechanism.requireFloat()) {
biome.setTemperature(mechanism.getValue().asFloat());
}
// <--[mechanism]
// @object BiomeTag
// @name downfall_type
// @input ElementTag
// @description
// Sets the downfall-type for this biome server-wide.
// This can be RAIN, SNOW, or NONE.
// Resets on server restart.
// @tags
// <BiomeTag.temperature>
// @example
// # Adjusts the downfall type of the plains biome permanently, using a server start event to keep it applied.
// on server start:
// - adjust <biome[plains]> temperature:-0.2
// - adjust <biome[plains]> downfall_type:SNOW
// -->
if (mechanism.matches("downfall_type") && mechanism.requireEnum(BiomeNMS.DownfallType.class)) {
biome.setPrecipitation(BiomeNMS.DownfallType.valueOf(mechanism.getValue().asString().toUpperCase()));
}
tagProcessor.processMechanism(this, mechanism);
}
}