2018-03-10 16:34:42 +01:00
|
|
|
package net.citizensnpcs.trait.waypoint;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
2018-08-08 10:08:38 +02:00
|
|
|
import net.citizensnpcs.api.event.SpawnReason;
|
2018-03-10 16:34:42 +01:00
|
|
|
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.api.npc.NPCRegistry;
|
2020-07-09 17:43:27 +02:00
|
|
|
import net.citizensnpcs.npc.ai.NPCHolder;
|
2018-03-10 16:34:42 +01:00
|
|
|
|
2019-05-16 13:58:29 +02:00
|
|
|
/**
|
|
|
|
* A helper class for storing a number of entity markers. By default an entity marker is a non-persisted EnderSignal.
|
|
|
|
*/
|
2018-03-10 16:34:42 +01:00
|
|
|
public class EntityMarkers<T> {
|
|
|
|
private final Map<T, Entity> markers = Maps.newHashMap();
|
2021-02-04 03:10:48 +01:00
|
|
|
private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
|
2019-09-22 14:14:33 +02:00
|
|
|
private EntityType type;
|
|
|
|
|
|
|
|
public EntityMarkers() {
|
2020-09-11 10:35:15 +02:00
|
|
|
this(DEFAULT_ENTITY_TYPE);
|
2019-09-22 14:14:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public EntityMarkers(EntityType type) {
|
|
|
|
this.type = type;
|
|
|
|
|
|
|
|
}
|
2018-03-10 16:34:42 +01:00
|
|
|
|
2019-05-16 13:58:29 +02:00
|
|
|
/**
|
|
|
|
* Creates and persists (in memory) an {@link Entity} marker.
|
|
|
|
*
|
|
|
|
* @param marker
|
|
|
|
* the storage marker
|
|
|
|
* @param at
|
|
|
|
* the spawn location
|
|
|
|
* @return the created entity
|
|
|
|
*/
|
2018-03-10 16:34:42 +01:00
|
|
|
public Entity createMarker(T marker, Location at) {
|
|
|
|
Entity entity = spawnMarker(at.getWorld(), at);
|
|
|
|
if (entity == null)
|
|
|
|
return null;
|
|
|
|
markers.put(marker, entity);
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void destroyMarkers() {
|
2020-07-09 17:43:27 +02:00
|
|
|
registry.deregisterAll();
|
2018-03-10 16:34:42 +01:00
|
|
|
markers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeMarker(T marker) {
|
|
|
|
Entity entity = markers.remove(marker);
|
|
|
|
if (entity != null) {
|
2020-07-09 17:43:27 +02:00
|
|
|
((NPCHolder) entity).getNPC().destroy();
|
2018-03-10 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:58:29 +02:00
|
|
|
/**
|
|
|
|
* Spawns a marker {@link Entity} without storing it for later use.
|
|
|
|
*
|
|
|
|
* @param world
|
|
|
|
* the world (unused currently)
|
|
|
|
* @param at
|
|
|
|
* the location
|
|
|
|
* @return the spawned entity
|
|
|
|
*/
|
2018-03-10 16:34:42 +01:00
|
|
|
public Entity spawnMarker(World world, Location at) {
|
2019-09-22 14:14:33 +02:00
|
|
|
NPC npc = registry.createNPC(type, "");
|
2018-10-31 09:52:18 +01:00
|
|
|
npc.spawn(at.clone().add(0.5, 0, 0.5), SpawnReason.CREATE);
|
2018-03-10 16:34:42 +01:00
|
|
|
return npc.getEntity();
|
|
|
|
}
|
2020-09-11 10:35:15 +02:00
|
|
|
|
|
|
|
private static EntityType DEFAULT_ENTITY_TYPE = EntityType.ENDER_SIGNAL;
|
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
DEFAULT_ENTITY_TYPE = EntityType.valueOf("SHULKER_BULLET");
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 16:34:42 +01:00
|
|
|
}
|