Search in sources :

Example 1 with InvisibleTrait

use of net.aufdemrand.denizen.npc.traits.InvisibleTrait in project Denizen-For-Bukkit by DenizenScript.

the class InvisibleCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    Element state = scriptEntry.getElement("state");
    dEntity target = (dEntity) scriptEntry.getObject("target");
    // Report to dB
    dB.report(scriptEntry, getName(), state.debug() + target.debug());
    if (target.isCitizensNPC()) {
        NPC npc = target.getDenizenNPC().getCitizen();
        if (!npc.hasTrait(InvisibleTrait.class)) {
            npc.addTrait(InvisibleTrait.class);
        }
        InvisibleTrait trait = npc.getTrait(InvisibleTrait.class);
        switch(Action.valueOf(state.asString().toUpperCase())) {
            case FALSE:
                trait.setInvisible(false);
                break;
            case TRUE:
                trait.setInvisible(true);
                break;
            case TOGGLE:
                trait.toggle();
                break;
        }
    } else {
        switch(Action.valueOf(state.asString().toUpperCase())) {
            case FALSE:
                if (target.getBukkitEntity() instanceof ArmorStand) {
                    ((ArmorStand) target.getBukkitEntity()).setVisible(true);
                } else {
                    target.getLivingEntity().removePotionEffect(PotionEffectType.INVISIBILITY);
                }
                break;
            case TRUE:
                if (target.getBukkitEntity() instanceof ArmorStand) {
                    ((ArmorStand) target.getBukkitEntity()).setVisible(false);
                } else {
                    new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(target.getLivingEntity());
                }
                break;
            case TOGGLE:
                if (target.getBukkitEntity() instanceof ArmorStand) {
                    if (((ArmorStand) target.getBukkitEntity()).isVisible()) {
                        ((ArmorStand) target.getBukkitEntity()).setVisible(true);
                    } else {
                        ((ArmorStand) target.getBukkitEntity()).setVisible(false);
                    }
                } else {
                    if (target.getLivingEntity().hasPotionEffect(PotionEffectType.INVISIBILITY)) {
                        target.getLivingEntity().removePotionEffect(PotionEffectType.INVISIBILITY);
                    } else {
                        new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(target.getLivingEntity());
                    }
                }
                break;
        }
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) ArmorStand(org.bukkit.entity.ArmorStand) PotionEffect(org.bukkit.potion.PotionEffect) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Element(net.aufdemrand.denizencore.objects.Element) InvisibleTrait(net.aufdemrand.denizen.npc.traits.InvisibleTrait)

Aggregations

InvisibleTrait (net.aufdemrand.denizen.npc.traits.InvisibleTrait)1 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)1 Element (net.aufdemrand.denizencore.objects.Element)1 NPC (net.citizensnpcs.api.npc.NPC)1 ArmorStand (org.bukkit.entity.ArmorStand)1 PotionEffect (org.bukkit.potion.PotionEffect)1