use of net.citizensnpcs.api.npc.MemoryNPCDataStore in project Denizen-For-Bukkit by DenizenScript.
the class SittingTrait method forceEntitySit.
public void forceEntitySit(Entity entity, Location location, boolean isRetry) {
if (sitStandNPC != null) {
sitStandNPC.destroy();
}
entity.setMetadata("denizen.sitting", new FixedMetadataValue(Denizen.getInstance(), true));
NPCRegistry registry = CitizensAPI.getNamedNPCRegistry("DenizenSitRegistry");
if (registry == null) {
registry = CitizensAPI.createNamedNPCRegistry("DenizenSitRegistry", new MemoryNPCDataStore());
}
NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
final NPC holder = registry.createNPC(EntityType.ARMOR_STAND, SIT_STAND_NAME);
sitStandNPC = holder;
if (npc != null) {
holder.addTrait(new ClickRedirectTrait(npc));
Messaging.debug("(Denizen) SittingTrait: Spawning chair for", npc.getId(), "as id", holder.getId());
}
ArmorStandTrait trait = holder.getOrAddTrait(ArmorStandTrait.class);
trait.setGravity(false);
trait.setHasArms(false);
trait.setHasBaseplate(false);
trait.setSmall(true);
trait.setMarker(true);
trait.setVisible(false);
holder.data().set(NPC.NAMEPLATE_VISIBLE_METADATA, false);
holder.data().set(NPC.DEFAULT_PROTECTED_METADATA, true);
boolean spawned = holder.spawn(location);
if (!spawned || !holder.isSpawned()) {
if (isRetry) {
Debug.echoError("NPC " + (npc == null ? "null" : npc.getId()) + " sit failed (" + spawned + "," + holder.isSpawned() + "): cannot spawn chair id " + holder.getId() + " at " + new LocationTag(location).identifySimple() + " ChunkIsLoaded=" + new ChunkTag(location).isLoaded());
holder.destroy();
sitStandNPC = null;
} else {
Messaging.debug("(Denizen) SittingTrait: retrying failed sit for", npc.getId());
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
if (npc.isSpawned()) {
forceEntitySit(entity, location, true);
}
}, 5);
}
return;
}
holder.data().set("is-denizen-seat", true);
new BukkitRunnable() {
@Override
public void cancel() {
super.cancel();
if (entity.isValid() && entity.hasMetadata("denizen.sitting")) {
entity.removeMetadata("denizen.sitting", Denizen.getInstance());
}
if (holder.getTraits().iterator().hasNext()) {
// Hacky NPC-already-removed test
holder.destroy();
}
if (sitStandNPC == holder) {
sitStandNPC = null;
}
}
@Override
public void run() {
if (holder != sitStandNPC) {
cancel();
} else if (!holder.getTraits().iterator().hasNext()) {
// Hacky NPC-already-removed test
cancel();
} else if (!entity.isValid() || !entity.hasMetadata("denizen.sitting") || !entity.getMetadata("denizen.sitting").get(0).asBoolean()) {
cancel();
} else if (npc != null && !npc.isSpawned()) {
cancel();
} else if (!holder.isSpawned()) {
cancel();
} else if (!NMS.getPassengers(holder.getEntity()).contains(entity)) {
holder.getEntity().addPassenger(entity);
}
}
}.runTaskTimer(Denizen.getInstance(), 0, 1);
}
use of net.citizensnpcs.api.npc.MemoryNPCDataStore in project Citizens2 by CitizensDev.
the class WaypointMarkers method spawnMarker.
public Entity spawnMarker(World world, Location at) {
NPC npc = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore()).createNPC(EntityType.ENDER_SIGNAL, "");
npc.spawn(at);
return npc.getEntity();
}
use of net.citizensnpcs.api.npc.MemoryNPCDataStore in project Denizen-For-Bukkit by DenizenScript.
the class CreateCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
ElementTag name = scriptEntry.getElement("name");
EntityTag type = scriptEntry.getObjectTag("entity_type");
LocationTag loc = scriptEntry.getObjectTag("spawn_location");
ListTag traits = scriptEntry.getObjectTag("traits");
ElementTag registry = scriptEntry.getElement("registry");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), name, type, loc, traits, registry);
}
NPCTag created;
if (!type.isGeneric() && type.isCitizensNPC()) {
created = new NPCTag(type.getDenizenNPC().getCitizen().clone());
created.getCitizen().setName(name.asString());
} else {
NPCRegistry actualRegistry = CitizensAPI.getNPCRegistry();
if (registry != null) {
actualRegistry = NPCTag.getRegistryByName(registry.asString());
if (actualRegistry == null) {
actualRegistry = CitizensAPI.createNamedNPCRegistry(registry.asString(), new MemoryNPCDataStore());
}
}
created = new NPCTag(actualRegistry.createNPC(type.getBukkitEntityType(), name.asString()));
}
// Add the created NPC into the script entry so it can be utilized if need be.
scriptEntry.addObject("created_npc", created);
if (created.isSpawned()) {
if (loc != null) {
created.getCitizen().teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
} else {
created.getCitizen().despawn();
}
} else {
if (loc != null) {
created.getCitizen().spawn(loc);
}
}
if (traits != null) {
for (String trait_name : traits) {
Trait trait = CitizensAPI.getTraitFactory().getTrait(trait_name);
if (trait != null) {
created.getCitizen().addTrait(trait);
} else {
Debug.echoError(scriptEntry, "Could not add trait to NPC: " + trait_name);
}
}
}
for (Mechanism mechanism : type.getWaitingMechanisms()) {
created.safeAdjust(mechanism);
}
}
Aggregations