Search in sources :

Example 1 with TimeTag

use of com.denizenscript.denizencore.objects.core.TimeTag in project Denizen-For-Bukkit by DenizenScript.

the class BukkitScriptProperties method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <ScriptTag.cooled_down[<player>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the script is currently cooled down for the player. Any global
    // cooldown present on the script will also be taken into account. Not specifying a player will result in
    // using the attached player available in the script entry. Not having a valid player will result in 'null'.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "cooled_down", (attribute, script) -> {
        PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
        if (player != null && player.isValid()) {
            return new ElementTag(CooldownCommand.checkCooldown(player, script.script.getContainer().getName()));
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.cooldown[<player>]>
    // @returns DurationTag
    // @description
    // Returns the time left for the player to cooldown for the script.
    // -->
    PropertyParser.<BukkitScriptProperties, DurationTag>registerTag(DurationTag.class, "cooldown", (attribute, script) -> {
        PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
        return CooldownCommand.getCooldownDuration(player, script.script.getName());
    });
    // <--[tag]
    // @attribute <ScriptTag.step[(<player>)]>
    // @returns ElementTag
    // @description
    // Returns the name of a script step that the player is currently on.
    // Must be an INTERACT script.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "step", (attribute, script) -> {
        PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
        if (player != null && player.isValid()) {
            return new ElementTag(InteractScriptHelper.getCurrentStep(player, script.script.getContainer().getName()));
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.step_expiration[(<player>)]>
    // @returns TimeTag
    // @description
    // Returns the time that an interact script step expires at, if applied by <@link command zap> with a duration limit.
    // -->
    PropertyParser.<BukkitScriptProperties, TimeTag>registerTag(TimeTag.class, "step_expiration", (attribute, script) -> {
        PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
        if (player != null && player.isValid()) {
            return InteractScriptHelper.getStepExpiration(player, script.script.getContainer().getName());
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.default_step>
    // @returns ElementTag
    // @description
    // Returns the name of the default step of an interact script.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerStaticTag(ElementTag.class, "default_step", (attribute, script) -> {
        String step = ((InteractScriptContainer) script.script.getContainer()).getDefaultStepName();
        return new ElementTag(step);
    });
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 2 with TimeTag

use of com.denizenscript.denizencore.objects.core.TimeTag 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 3 with TimeTag

use of com.denizenscript.denizencore.objects.core.TimeTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemFlags method getObjectAttribute.

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // -->
    if (attribute.startsWith("with_flag")) {
        ItemTag item = new ItemTag(this.item.getItemStack().clone());
        FlagCommand.FlagActionProvider provider = new FlagCommand.FlagActionProvider();
        provider.tracker = item.getFlagTracker();
        DataAction action = DataActionHelper.parse(provider, attribute.getParam(), attribute.context);
        // -->
        if (attribute.startsWith("duration", 2)) {
            provider.expiration = new TimeTag(TimeTag.now().millis() + attribute.getContextObject(2).asType(DurationTag.class, attribute.context).getMillis());
            attribute.fulfill(1);
        }
        action.execute(attribute.context);
        item.reapplyTracker(provider.tracker);
        return item.getObjectAttribute(attribute.fulfill(1));
    }
    return null;
}
Also used : FlagCommand(com.denizenscript.denizencore.scripts.commands.core.FlagCommand) DataAction(com.denizenscript.denizencore.utilities.data.DataAction) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 4 with TimeTag

use of com.denizenscript.denizencore.objects.core.TimeTag in project Denizen-For-Bukkit by DenizenScript.

the class BanCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag action = scriptEntry.getElement("action");
    List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    ListTag addresses = scriptEntry.getObjectTag("addresses");
    ElementTag reason = scriptEntry.getElement("reason");
    TimeTag expire = scriptEntry.getObjectTag("expire");
    ElementTag source = scriptEntry.getElement("source");
    Date expiration = expire == null ? null : new Date(expire.millis());
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), action, db("targets", targets), addresses, reason, expire, source);
    }
    Actions banAction = Actions.valueOf(action.toString().toUpperCase());
    switch(banAction) {
        case ADD:
            if (targets != null) {
                for (PlayerTag player : targets) {
                    if (player.isValid()) {
                        Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), reason.toString(), expiration, source.toString());
                        if (player.isOnline()) {
                            player.getPlayerEntity().kickPlayer(reason.toString());
                        }
                    }
                }
            }
            if (addresses != null) {
                for (String address : addresses) {
                    Bukkit.getBanList(BanList.Type.IP).addBan(address, reason.toString(), expiration, source.toString());
                }
            }
            break;
        case REMOVE:
            if (targets != null) {
                for (PlayerTag player : targets) {
                    if (player.isValid()) {
                        if (player.getOfflinePlayer().isBanned()) {
                            Bukkit.getBanList(BanList.Type.NAME).pardon(player.getName());
                        }
                    }
                }
            }
            if (addresses != null) {
                for (String address : addresses) {
                    Bukkit.getBanList(BanList.Type.IP).pardon(address);
                }
            }
            break;
    }
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BanList(org.bukkit.BanList) List(java.util.List) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Date(java.util.Date)

Example 5 with TimeTag

use of com.denizenscript.denizencore.objects.core.TimeTag in project Denizen-For-Bukkit by DenizenScript.

the class ZapCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final ScriptTag script = scriptEntry.getObjectTag("script");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    ElementTag stepElement = scriptEntry.getElement("step");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), Utilities.getEntryPlayer(scriptEntry), script, stepElement != null ? stepElement : db("step", "++ (inc)"), duration);
    }
    String step = stepElement == null ? null : stepElement.asString();
    String currentStep = InteractScriptHelper.getCurrentStep(Utilities.getEntryPlayer(scriptEntry), script.getName());
    // Special-case for backwards compatibility: ability to use ZAP to count up steps.
    if (step == null) {
        // to '1' so it can be incremented next time.
        if (ArgumentHelper.matchesInteger(currentStep)) {
            step = String.valueOf(Integer.parseInt(currentStep) + 1);
        } else {
            step = "1";
        }
    } else if (step.equals("*")) {
        step = ((InteractScriptContainer) script.getContainer()).getDefaultStepName();
    }
    if (step.equalsIgnoreCase(currentStep)) {
        Debug.echoError(scriptEntry, "Zapping to own current step!");
        return;
    }
    TimeTag expiration = null;
    if (duration != null && duration.getSeconds() > 0) {
        expiration = new TimeTag(TimeTag.now().millis() + duration.getMillis());
    }
    Utilities.getEntryPlayer(scriptEntry).getFlagTracker().setFlag("__interact_step." + script.getName(), new ElementTag(step), expiration);
}
Also used : ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Aggregations

TimeTag (com.denizenscript.denizencore.objects.core.TimeTag)7 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)3 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)3 InteractScriptContainer (com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)1 AbstractFlagTracker (com.denizenscript.denizencore.flags.AbstractFlagTracker)1 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 FlagCommand (com.denizenscript.denizencore.scripts.commands.core.FlagCommand)1 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)1 DataAction (com.denizenscript.denizencore.utilities.data.DataAction)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Date (java.util.Date)1 List (java.util.List)1