Search in sources :

Example 1 with ClickRedirectTrait

use of net.citizensnpcs.trait.ClickRedirectTrait 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)

Aggregations

ChunkTag (com.denizenscript.denizen.objects.ChunkTag)1 LocationTag (com.denizenscript.denizen.objects.LocationTag)1 MemoryNPCDataStore (net.citizensnpcs.api.npc.MemoryNPCDataStore)1 NPC (net.citizensnpcs.api.npc.NPC)1 NPCRegistry (net.citizensnpcs.api.npc.NPCRegistry)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