Search in sources :

Example 1 with MemoryNPCDataStore

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);
}
Also used : MemoryNPCDataStore(net.citizensnpcs.api.npc.MemoryNPCDataStore) NPC(net.citizensnpcs.api.npc.NPC) LocationTag(com.denizenscript.denizen.objects.LocationTag) ChunkTag(com.denizenscript.denizen.objects.ChunkTag) ClickRedirectTrait(net.citizensnpcs.trait.ClickRedirectTrait) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ArmorStandTrait(net.citizensnpcs.trait.ArmorStandTrait) NPCRegistry(net.citizensnpcs.api.npc.NPCRegistry)

Example 2 with MemoryNPCDataStore

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();
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) MemoryNPCDataStore(net.citizensnpcs.api.npc.MemoryNPCDataStore)

Example 3 with MemoryNPCDataStore

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);
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MemoryNPCDataStore(net.citizensnpcs.api.npc.MemoryNPCDataStore) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Trait(net.citizensnpcs.api.trait.Trait) ListTag(com.denizenscript.denizencore.objects.core.ListTag) NPCRegistry(net.citizensnpcs.api.npc.NPCRegistry)

Aggregations

MemoryNPCDataStore (net.citizensnpcs.api.npc.MemoryNPCDataStore)3 LocationTag (com.denizenscript.denizen.objects.LocationTag)2 NPC (net.citizensnpcs.api.npc.NPC)2 NPCRegistry (net.citizensnpcs.api.npc.NPCRegistry)2 ChunkTag (com.denizenscript.denizen.objects.ChunkTag)1 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 Trait (net.citizensnpcs.api.trait.Trait)1 ArmorStandTrait (net.citizensnpcs.trait.ArmorStandTrait)1 ClickRedirectTrait (net.citizensnpcs.trait.ClickRedirectTrait)1 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1