Skip to content

Commit

Permalink
Invisible trait: workaround Citizens/Paper bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 12, 2022
1 parent dab0650 commit d84c05e
Showing 1 changed file with 22 additions and 3 deletions.
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.Denizen;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
Expand All @@ -12,15 +13,17 @@
import org.bukkit.event.Listener;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

public class InvisibleTrait extends Trait implements Listener, Toggleable {

// <--[language]
// @name Invisible Trait
// @group NPC Traits
// @description
// The invisible trait will allow a NPC to remain invisible, even after a server restart. It permanently applies
// the invisible potion effect. Use '/trait invisible' or the 'invisible' script command to toggle this trait.
// The invisible trait will allow a NPC to remain invisible, even after a server restart.
// It permanently applies the invisible potion effect.
// Use '/npc invisible' or the 'invisible' script command to toggle this trait.
//
// Note that player-type NPCs must have '/npc playerlist' toggled to be turned invisible.
// Once invisible, the player-type NPCs can be taken off the playerlist.
Expand Down Expand Up @@ -83,7 +86,7 @@ public static void setInvisible(LivingEntity ent, NPC npc) {
}

private void setInvisible() {
if (npc.isSpawned() && npc.getEntity() instanceof LivingEntity) {
if (invisible && npc.isSpawned() && npc.getEntity() instanceof LivingEntity) {
setInvisible((LivingEntity) npc.getEntity(), npc);
}
}
Expand All @@ -96,6 +99,22 @@ public boolean isInvisible() {
public void onSpawn() {
if (invisible) {
setInvisible();
// Workaround bug in Citizens on Paper servers - NPCs don't seem to actually spawn at startup til several ticks *after* the spawn event (2022/03/12)
if (!npc.isSpawned()) {
new BukkitRunnable() {
int ticks = 0;
@Override
public void run() {
if (ticks++ > 80) {
return;
}
if (npc.isSpawned()) {
setInvisible();
cancel();
}
}
}.runTaskTimer(Denizen.getInstance(), 1, 1);
}
}
}

Expand Down

0 comments on commit d84c05e

Please sign in to comment.