Search in sources :

Example 21 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class EngageCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    if (!Utilities.entryHasNPC(scriptEntry)) {
        throw new InvalidArgumentsRuntimeException("This command requires a linked NPC!");
    }
    DurationTag duration = scriptEntry.getObjectTag("duration");
    boolean linkedPlayer = scriptEntry.argAsBoolean("player");
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), npc, duration, db("player", linkedPlayer));
    }
    if (duration.getSecondsAsInt() > 0) {
        setEngaged(npc.getCitizen(), linkedPlayer ? Utilities.getEntryPlayer(scriptEntry) : null, duration.getSecondsAsInt());
    } else {
        setEngaged(npc.getCitizen(), linkedPlayer ? Utilities.getEntryPlayer(scriptEntry) : null, true);
    }
}
Also used : InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) NPCTag(com.denizenscript.denizen.objects.NPCTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 22 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class FishCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag location = scriptEntry.getObjectTag("location");
    ElementTag catchtype = scriptEntry.getElement("catch");
    ElementTag stop = scriptEntry.getElement("stop");
    ElementTag percent = scriptEntry.getElement("percent");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), location, catchtype, percent, stop);
    }
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    FishingTrait trait = npc.getFishingTrait();
    if (stop.asBoolean()) {
        trait.stopFishing();
        return;
    }
    npc.getEquipmentTrait().set(0, new ItemStack(Material.FISHING_ROD));
    trait.setCatchPercent(percent.asInt());
    trait.setCatchType(FishingHelper.CatchType.valueOf(catchtype.asString().toUpperCase()));
    trait.startFishing(location);
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) FishingTrait(com.denizenscript.denizen.npc.traits.FishingTrait) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemStack(org.bukkit.inventory.ItemStack)

Example 23 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class PushableCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NPCTag denizenNPC = Utilities.getEntryNPC(scriptEntry);
    if (denizenNPC == null) {
        Debug.echoError("No valid NPC attached to this queue!");
        return;
    }
    PushableTrait trait = denizenNPC.getPushableTrait();
    ElementTag state = scriptEntry.getElement("state");
    DurationTag delay = scriptEntry.getObjectTag("delay");
    ElementTag returnable = scriptEntry.getElement("return");
    if (state == null && delay == null && returnable == null) {
        state = new ElementTag("TOGGLE");
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), denizenNPC, state, delay, returnable);
    }
    if (delay != null) {
        trait.setDelay(delay.getSecondsAsInt());
    }
    if (returnable != null) {
        trait.setReturnable(returnable.asBoolean());
    }
    if (state != null) {
        switch(Toggle.valueOf(state.asString().toUpperCase())) {
            case TRUE:
            case ON:
                trait.setPushable(true);
                break;
            case FALSE:
            case OFF:
                trait.setPushable(false);
                break;
            case TOGGLE:
                trait.setPushable(!trait.isPushable());
                break;
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) PushableTrait(com.denizenscript.denizen.npc.traits.PushableTrait)

Example 24 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class LegacySavesUpdater method updateLegacySaves.

public static void updateLegacySaves() {
    Debug.log("==== UPDATING LEGACY SAVES TO NEW FLAG ENGINE ====");
    File savesFile = new File(Denizen.getInstance().getDataFolder(), "saves.yml");
    if (!savesFile.exists()) {
        Debug.echoError("Legacy update went weird: file doesn't exist?");
        return;
    }
    YamlConfiguration saveSection;
    try {
        FileInputStream fis = new FileInputStream(savesFile);
        String saveData = ScriptHelper.convertStreamToString(fis, false);
        fis.close();
        if (saveData.trim().length() == 0) {
            Debug.log("Nothing to update.");
            savesFile.delete();
            return;
        }
        saveSection = YamlConfiguration.load(saveData);
        if (saveSection == null) {
            Debug.echoError("Something went very wrong: legacy saves file failed to load!");
            return;
        }
    } catch (Throwable ex) {
        Debug.echoError(ex);
        return;
    }
    if (!savesFile.renameTo(new File(Denizen.getInstance().getDataFolder(), "saves.yml.bak"))) {
        Debug.echoError("Legacy saves file failed to rename!");
    }
    if (saveSection.contains("Global")) {
        Debug.log("==== Update global data ====");
        YamlConfiguration globalSection = saveSection.getConfigurationSection("Global");
        if (globalSection.contains("Flags")) {
            applyFlags("Server", DenizenCore.serverFlagMap, globalSection.getConfigurationSection("Flags"));
        }
        if (globalSection.contains("Scripts")) {
            YamlConfiguration scriptsSection = globalSection.getConfigurationSection("Scripts");
            for (StringHolder script : scriptsSection.getKeys(false)) {
                YamlConfiguration scriptSection = scriptsSection.getConfigurationSection(script.str);
                if (scriptSection.contains("Cooldown Time")) {
                    long time = Long.parseLong(scriptSection.getString("Cooldown Time"));
                    TimeTag cooldown = new TimeTag(time);
                    DenizenCore.serverFlagMap.setFlag("__interact_cooldown." + script.low, cooldown, cooldown);
                }
            }
        }
    }
    if (saveSection.contains("Players")) {
        Debug.log("==== Update player data ====");
        YamlConfiguration playerSection = saveSection.getConfigurationSection("Players");
        for (StringHolder plPrefix : playerSection.getKeys(false)) {
            YamlConfiguration subSection = playerSection.getConfigurationSection(plPrefix.str);
            for (StringHolder uuidString : subSection.getKeys(false)) {
                if (uuidString.str.length() != 32) {
                    Debug.echoError("Cannot update data for player with non-ID entry listed: " + uuidString);
                    continue;
                }
                try {
                    UUID id = UUID.fromString(uuidString.str.substring(0, 8) + "-" + uuidString.str.substring(8, 12) + "-" + uuidString.str.substring(12, 16) + "-" + uuidString.str.substring(16, 20) + "-" + uuidString.str.substring(20, 32));
                    PlayerTag player = PlayerTag.valueOf(id.toString(), CoreUtilities.errorButNoDebugContext);
                    if (player == null) {
                        Debug.echoError("Cannot update data for player with id: " + uuidString);
                        continue;
                    }
                    YamlConfiguration actual = subSection.getConfigurationSection(uuidString.str);
                    AbstractFlagTracker tracker = player.getFlagTracker();
                    if (actual.contains("Flags")) {
                        applyFlags(player.identify(), tracker, actual.getConfigurationSection("Flags"));
                    }
                    if (actual.contains("Scripts")) {
                        YamlConfiguration scriptsSection = actual.getConfigurationSection("Scripts");
                        for (StringHolder script : scriptsSection.getKeys(false)) {
                            YamlConfiguration scriptSection = scriptsSection.getConfigurationSection(script.str);
                            if (scriptSection.contains("Current Step")) {
                                tracker.setFlag("__interact_step." + script, new ElementTag(scriptSection.getString("Current Step")), null);
                            }
                            if (scriptSection.contains("Cooldown Time")) {
                                long time = Long.parseLong(scriptSection.getString("Cooldown Time"));
                                TimeTag cooldown = new TimeTag(time);
                                tracker.setFlag("__interact_cooldown." + script, cooldown, cooldown);
                            }
                        }
                    }
                    player.reapplyTracker(tracker);
                } catch (Throwable ex) {
                    Debug.echoError("Error updating flags for player with ID " + uuidString.str);
                    Debug.echoError(ex);
                }
            }
        }
    }
    if (saveSection.contains("NPCs")) {
        final YamlConfiguration npcsSection = saveSection.getConfigurationSection("NPCs");
        new BukkitRunnable() {

            @Override
            public void run() {
                Debug.log("==== Late update NPC data ====");
                for (StringHolder npcId : npcsSection.getKeys(false)) {
                    YamlConfiguration actual = npcsSection.getConfigurationSection(npcId.str);
                    NPCTag npc = NPCTag.valueOf(npcId.str, CoreUtilities.errorButNoDebugContext);
                    if (npc == null) {
                        Debug.echoError("Cannot update data for NPC with id: " + npcId.str);
                        continue;
                    }
                    AbstractFlagTracker tracker = npc.getFlagTracker();
                    if (actual.contains("Flags")) {
                        applyFlags(npc.identify(), tracker, actual.getConfigurationSection("Flags"));
                    }
                    npc.reapplyTracker(tracker);
                    Debug.log("==== Done late-updating NPC data ====");
                }
            }
        }.runTaskLater(Denizen.getInstance(), 3);
    }
    Denizen.getInstance().saveSaves(true);
    Debug.log("==== Done updating legacy saves (except NPCs) ====");
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) FileInputStream(java.io.FileInputStream) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) NPCTag(com.denizenscript.denizen.objects.NPCTag) AbstractFlagTracker(com.denizenscript.denizencore.flags.AbstractFlagTracker) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID) File(java.io.File)

Example 25 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class DamageTrigger method damageTrigger.

// <--[language]
// @name Damage Triggers
// @group NPC Interact Scripts
// @description
// Damage Triggers are triggered when when a player left clicks the NPC.
// Despite the name, these do not actually require the NPC take any damage, only that the player left clicks the NPC.
// 
// In scripts, use <context.damage> to measure how much damage was done to the NPC
// (though note that invincible NPCs don't necessarily take any damage even when this is non-zero).
// 
// These are very basic with no extraneous complexity.
// 
// -->
// <--[action]
// @Actions
// no damage trigger
// 
// @Triggers when the NPC is damaged by a player but no damage trigger fires.
// 
// @Context
// None
// 
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// damage
// 
// @Triggers when the NPC is damaged by a player.
// 
// @Context
// <context.damage> returns how much damage was done.
// 
// @Determine
// "cancelled" to cancel the damage event.
// 
// -->
// <--[action]
// @Actions
// damaged
// 
// @Triggers when the NPC is damaged by an entity.
// 
// @Context
// <context.damage> returns how much damage was done.
// <context.damager> returns the entity that did the damage.
// 
// @Determine
// "cancelled" to cancel the damage event.
// 
// -->
@EventHandler
public void damageTrigger(EntityDamageByEntityEvent event) {
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("damage", new ElementTag(event.getDamage()));
    if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
        NPCTag npc = new NPCTag(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
        if (npc == null) {
            return;
        }
        if (npc.getCitizen() == null) {
            return;
        }
        EntityTag damager = new EntityTag(event.getDamager());
        if (damager.isProjectile() && damager.hasShooter()) {
            damager = damager.getShooter();
        }
        context.put("damager", damager.getDenizenObject());
        ListTag determ = npc.action("damaged", null, context);
        if (determ != null && determ.containsCaseInsensitive("cancelled")) {
            event.setCancelled(true);
            return;
        }
        if (!damager.isPlayer()) {
            return;
        }
        PlayerTag dplayer = damager.getDenizenPlayer();
        if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
            return;
        }
        if (!npc.getTriggerTrait().isEnabled(name)) {
            return;
        }
        TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, dplayer);
        if (!trigger.wasTriggered()) {
            return;
        }
        if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
            event.setCancelled(true);
            return;
        }
        List<InteractScriptContainer> scripts = InteractScriptHelper.getInteractScripts(npc, dplayer, true, ClickTrigger.class);
        boolean any = false;
        if (scripts != null) {
            for (InteractScriptContainer script : scripts) {
                String id = null;
                Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, dplayer);
                if (!idMap.isEmpty()) {
                    for (Map.Entry<String, String> entry : idMap.entrySet()) {
                        String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(dplayer, npc, null, false, new ScriptTag(script)));
                        if (ItemTag.valueOf(entry_value, script).comparesTo(dplayer.getPlayerEntity().getEquipment().getItemInMainHand()) >= 0) {
                            id = entry.getKey();
                        }
                    }
                }
                if (parse(npc, dplayer, script, id, context)) {
                    any = true;
                }
            }
        }
        if (!any) {
            npc.action("no damage trigger", dplayer);
        }
    }
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) HashMap(java.util.HashMap) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Aggregations

NPCTag (com.denizenscript.denizen.objects.NPCTag)72 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)24 EventHandler (org.bukkit.event.EventHandler)22 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)21 Player (org.bukkit.entity.Player)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)12 EntityTag (com.denizenscript.denizen.objects.EntityTag)10 ListTag (com.denizenscript.denizencore.objects.core.ListTag)9 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 HashMap (java.util.HashMap)8 NPC (net.citizensnpcs.api.npc.NPC)8 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)6 Location (org.bukkit.Location)6 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)5 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)5 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Entity (org.bukkit.entity.Entity)5