Search in sources :

Example 1 with AbstractFlagTracker

use of com.denizenscript.denizencore.flags.AbstractFlagTracker in project Denizen-For-Bukkit by DenizenScript.

the class EntityFlags method getPropertyString.

@Override
public String getPropertyString() {
    AbstractFlagTracker tracker = entity.getFlagTracker();
    if (!(tracker instanceof DataPersistenceFlagTracker)) {
        return null;
    }
    Collection<String> flagNames = tracker.listAllFlags();
    if (flagNames.isEmpty()) {
        return null;
    }
    MapTag flags = new MapTag();
    for (String name : flagNames) {
        flags.putObject(name, ((DataPersistenceFlagTracker) tracker).getRootMap(name));
    }
    return flags.toString();
}
Also used : DataPersistenceFlagTracker(com.denizenscript.denizen.utilities.flags.DataPersistenceFlagTracker) AbstractFlagTracker(com.denizenscript.denizencore.flags.AbstractFlagTracker) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 2 with AbstractFlagTracker

use of com.denizenscript.denizencore.flags.AbstractFlagTracker 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 AbstractFlagTracker

use of com.denizenscript.denizencore.flags.AbstractFlagTracker in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptContainer method getItemFrom.

public ItemTag getItemFrom(TagContext context) {
    if (isProcessing) {
        Debug.echoError("Item script contains (or chains to) a reference to itself. Cannot process.");
        return null;
    }
    if (context == null) {
        context = new BukkitTagContext(null, null, new ScriptTag(this));
    } else {
        context = new BukkitTagContext((BukkitTagContext) context);
        context.script = new ScriptTag(this);
    }
    // Try to use this script to make an item.
    ItemTag stack;
    isProcessing = true;
    try {
        if (!contains("material", String.class)) {
            Debug.echoError("Item script '" + getName() + "' does not contain a material. Script cannot function.");
            return null;
        }
        // Check validity of material
        String material = TagManager.tag(getString("material"), context);
        if (material.startsWith("m@")) {
            material = material.substring(2);
        }
        stack = ItemTag.valueOf(material, this);
        // Make sure we're working with a valid base ItemStack
        if (stack == null) {
            Debug.echoError("Item script '" + getName() + "' contains an invalid or incorrect material '" + material + "' (did you spell the material name wrong?). Script cannot function.");
            return null;
        }
        // Handle listed mechanisms
        if (contains("mechanisms", Map.class)) {
            YamlConfiguration mechs = getConfigurationSection("mechanisms");
            for (StringHolder key : mechs.getKeys(false)) {
                ObjectTag obj = CoreUtilities.objectToTagForm(mechs.get(key.low), context, true, true);
                stack.safeAdjust(new Mechanism(key.low, obj, context));
            }
        }
        // Set Display Name
        if (contains("display name", String.class)) {
            String displayName = TagManager.tag(getString("display name"), context);
            NMSHandler.getItemHelper().setDisplayName(stack, displayName);
        }
        // Set if the object is bound to the player
        if (contains("bound", String.class)) {
            Deprecations.boundWarning.warn(context);
        }
        // Set Lore
        if (contains("lore", List.class)) {
            List<String> lore = NMSHandler.getItemHelper().getLore(stack);
            if (lore == null) {
                lore = new ArrayList<>();
            }
            for (String line : getStringList("lore")) {
                line = TagManager.tag(line, context);
                lore.add(line);
            }
            CoreUtilities.fixNewLinesToListSeparation(lore);
            NMSHandler.getItemHelper().setLore(stack, lore);
        }
        // Set Durability
        if (contains("durability", String.class)) {
            short durability = Short.valueOf(getString("durability"));
            stack.setDurability(durability);
        }
        // Set Enchantments
        if (contains("enchantments", List.class)) {
            for (String enchantment : getStringList("enchantments")) {
                enchantment = TagManager.tag(enchantment, context);
                try {
                    // Build enchantment context
                    int level = 1;
                    int colon = enchantment.lastIndexOf(':');
                    if (colon == -1) {
                        Debug.echoError("Item script '" + getName() + "' has enchantment '" + enchantment + "' without a level.");
                    } else {
                        level = Integer.valueOf(enchantment.substring(colon + 1).replace(" ", ""));
                        enchantment = enchantment.substring(0, colon).replace(" ", "");
                    }
                    // Add enchantment
                    EnchantmentTag ench = EnchantmentTag.valueOf(enchantment, context);
                    if (ench == null) {
                        Debug.echoError("Item script '" + getName() + "' specifies enchantment '" + enchantment + "' which is invalid.");
                        continue;
                    }
                    if (stack.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                        EnchantmentStorageMeta meta = (EnchantmentStorageMeta) stack.getItemMeta();
                        meta.addStoredEnchant(ench.enchantment, level, true);
                        stack.setItemMeta(meta);
                    } else {
                        stack.getItemStack().addUnsafeEnchantment(ench.enchantment, level);
                        stack.resetCache();
                    }
                } catch (Exception ex) {
                    Debug.echoError("While constructing item script '" + getName() + "', encountered error while applying enchantment '" + enchantment + "':");
                    Debug.echoError(ex);
                }
            }
        }
        // Set Color
        if (contains("color", String.class)) {
            Deprecations.itemScriptColor.warn(context);
            String color = TagManager.tag(getString("color"), context);
            LeatherColorer.colorArmor(stack, color);
        }
        // Set Book
        if (contains("book", String.class)) {
            BookScriptContainer book = ScriptRegistry.getScriptContainer(TagManager.tag(getString("book"), context).replace("s@", ""));
            stack = book.writeBookTo(stack, context);
        }
        if (contains("flags", Map.class)) {
            YamlConfiguration flagSection = getConfigurationSection("flags");
            AbstractFlagTracker tracker = stack.getFlagTracker();
            for (StringHolder key : flagSection.getKeys(false)) {
                tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
            }
            stack.reapplyTracker(tracker);
        }
        stack.setItemScript(this);
    } catch (Exception e) {
        Debug.echoError("Woah! An exception has been called with this item script!");
        Debug.echoError(e);
        stack = null;
    } finally {
        isProcessing = false;
    }
    return stack;
}
Also used : YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) Mechanism(com.denizenscript.denizencore.objects.Mechanism) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) AbstractFlagTracker(com.denizenscript.denizencore.flags.AbstractFlagTracker) EnchantmentTag(com.denizenscript.denizen.objects.EnchantmentTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 4 with AbstractFlagTracker

use of com.denizenscript.denizencore.flags.AbstractFlagTracker in project Denizen-For-Bukkit by DenizenScript.

the class EntityFlags method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("flag_map") && mechanism.requireObject(MapTag.class)) {
        MapTagFlagTracker flags = new MapTagFlagTracker(mechanism.valueAsType(MapTag.class));
        AbstractFlagTracker tracker = entity.getFlagTracker();
        if (!(tracker instanceof DataPersistenceFlagTracker)) {
            return;
        }
        for (String flagName : flags.map.keys()) {
            ((DataPersistenceFlagTracker) tracker).setRootMap(flagName, flags.getRootMap(flagName));
        }
        entity.reapplyTracker(tracker);
    }
}
Also used : DataPersistenceFlagTracker(com.denizenscript.denizen.utilities.flags.DataPersistenceFlagTracker) AbstractFlagTracker(com.denizenscript.denizencore.flags.AbstractFlagTracker) MapTagFlagTracker(com.denizenscript.denizencore.flags.MapTagFlagTracker) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Aggregations

AbstractFlagTracker (com.denizenscript.denizencore.flags.AbstractFlagTracker)4 DataPersistenceFlagTracker (com.denizenscript.denizen.utilities.flags.DataPersistenceFlagTracker)2 MapTag (com.denizenscript.denizencore.objects.core.MapTag)2 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 EnchantmentTag (com.denizenscript.denizen.objects.EnchantmentTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)1 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)1 MapTagFlagTracker (com.denizenscript.denizencore.flags.MapTagFlagTracker)1 Mechanism (com.denizenscript.denizencore.objects.Mechanism)1 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 TimeTag (com.denizenscript.denizencore.objects.core.TimeTag)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 UUID (java.util.UUID)1 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)1