-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
SpawnEntityHelper.java
35 lines (31 loc) · 1.49 KB
/
SpawnEntityHelper.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
package com.denizenscript.denizen.utilities.entity;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.scripts.containers.core.EntityScriptHelper;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.util.Consumer;
import java.util.ArrayList;
/**
* Applies mechanisms to an entity before spawning it
*/
public class SpawnEntityHelper {
@SuppressWarnings("unchecked")
public static <T extends Entity> Entity spawn(Location location, EntityType bukkitEntityType, final ArrayList<Mechanism> mechanisms, final String scriptName) {
Consumer<? extends Entity> consumer = (Consumer<Entity>) bukkitEntity -> {
if (scriptName != null) {
EntityScriptHelper.setEntityScript(bukkitEntity, scriptName);
}
EntityTag entity = new EntityTag(bukkitEntity);
for (Mechanism mechanism : new ArrayList<>(mechanisms)) {
if (EntityTag.earlyValidMechanisms.contains(CoreUtilities.toLowerCase(mechanism.getName()))) {
entity.safeAdjustDuplicate(mechanism);
//mechanisms.remove(mechanism);
}
}
};
return location.getWorld().spawn(location, (Class<T>) bukkitEntityType.getEntityClass(), (Consumer<T>) consumer);
}
}