Search in sources :

Example 16 with StringHolder

use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.

the class MapScriptContainer method applyTo.

// <--[language]
// @name Map Script Containers
// @group Script Container System
// @description
// Map scripts allow you define custom in-game map items, for usage with the map command.
// 
// The following is the format for the container.
// 
// <code>
// # The name of the map script is used by the map command.
// Map_Script_Name:
// 
// type: map
// 
// # Whether to display the original map below the custom values. Defaults to true.
// # | Some map scripts should have this key!
// original: true/false
// 
// # Whether to constantly update things. Defaults to true.
// # | Some map scripts should have this key!
// auto update: true
// 
// # Whether this map script renders uniquely per-player. Defaults to true.
// # | Some map scripts should have this key!
// contextual: true
// 
// # Lists all contained objects.
// # | Most map scripts should have this key!
// objects:
// 
// # The first object...
// 1:
// # Specify the object type
// # Type can be IMAGE, TEXT, CURSOR, or DOT.
// type: image
// # Specify an HTTP url or file path within Denizen/images/ for the image. Supports animated .gif!
// image: my_image.png
// # Optionally add width/height numbers.
// width: 128
// height: 128
// 
// 2:
// type: text
// # Specify any text to display. Color codes not permitted (unless you know how to format CraftMapCanvas byte-ID color codes).
// text: Hello <player.name>
// # Specify the color of the text as any valid ColorTag.
// color: red
// # Specify a tag to show or hide custom content! Valid for all objects.
// # Note that all inputs other than 'type' for all objects support tags that will be dynamically reparsed per-player each time the map updates.
// visible: <player.name.contains[bob].not>
// 
// 3:
// type: cursor
// # Specify a cursor type
// cursor: red_marker
// # Optionally, specify a cursor direction. '180' seems to display as up-right usually.
// direction: 180
// # Supported on all objects: x/y positions, and whether to use worldly or map coordinates.
// x: 5
// # If 'world_coordinates' is set to 'true', the 'y' value corresponds to the 'z' value of a location.
// y: 5
// # If true: uses world coordinates. If false: uses map local coordinates. (Defaults to false).
// world_coordinates: false
// # If true: when the object goes past the edge, will stay in view at the corner. If false: disappears past the edge (defaults to false).
// show_past_edge: false
// 
// 4:
// type: dot
// # Specify the radius of the dot.
// radius: 1
// # Specify the color of the dot as any valid ColorTag.
// color: red
// 
// </code>
// 
// A list of cursor types is available through <@link tag server.map_cursor_types>.
// 
// -->
public void applyTo(MapView mapView) {
    boolean contextual = getString("contextual", "true").equalsIgnoreCase("true");
    DenizenMapRenderer renderer = new DenizenMapRenderer(mapView.getRenderers(), getString("auto update", "true").equalsIgnoreCase("true"), contextual);
    if (contains("original", String.class)) {
        renderer.displayOriginal = getString("original").equalsIgnoreCase("true");
    }
    if (contains("objects", Map.class)) {
        YamlConfiguration objectsSection = getConfigurationSection("objects");
        List<StringHolder> objectKeys1 = new ArrayList<>(objectsSection.getKeys(false));
        List<String> objectKeys = new ArrayList<>(objectKeys1.size());
        for (StringHolder sh : objectKeys1) {
            objectKeys.add(sh.str);
        }
        objectKeys.sort(new NaturalOrderComparator());
        for (String objectKey : objectKeys) {
            YamlConfiguration objectSection = objectsSection.getConfigurationSection(objectKey);
            if (!objectSection.contains("type")) {
                Debug.echoError("Map script '" + getName() + "' has an object without a specified type!");
                return;
            }
            String type = CoreUtilities.toLowerCase(objectSection.getString("type"));
            String x = objectSection.getString("x", "0");
            String y = objectSection.getString("y", "0");
            String visible = objectSection.getString("visible", "true");
            MapObject added = null;
            switch(type) {
                case "image":
                    if (!objectSection.contains("image")) {
                        Debug.echoError("Map script '" + getName() + "'s image '" + objectKey + "' has no specified image location!");
                        return;
                    }
                    String image = objectSection.getString("image");
                    int width = Integer.parseInt(objectSection.getString("width", "0"));
                    int height = Integer.parseInt(objectSection.getString("height", "0"));
                    added = new MapImage(renderer, x, y, visible, shouldDebug(), image, width, height);
                    break;
                case "text":
                    if (!objectSection.contains("text")) {
                        Debug.echoError("Map script '" + getName() + "'s text object '" + objectKey + "' has no specified text!");
                        return;
                    }
                    String text = objectSection.getString("text");
                    added = new MapText(x, y, visible, shouldDebug(), text, objectSection.getString("color", "black"));
                    break;
                case "cursor":
                    if (!objectSection.contains("cursor")) {
                        Debug.echoError("Map script '" + getName() + "'s cursor '" + objectKey + "' has no specified cursor type!");
                        return;
                    }
                    String cursor = objectSection.getString("cursor");
                    if (cursor == null) {
                        Debug.echoError("Map script '" + getName() + "'s cursor '" + objectKey + "' is missing a cursor type!");
                        return;
                    }
                    added = new MapCursor(x, y, visible, shouldDebug(), objectSection.getString("direction", "0"), cursor);
                    break;
                case "dot":
                    added = new MapDot(x, y, visible, shouldDebug(), objectSection.getString("radius", "1"), objectSection.getString("color", "black"));
                    break;
                default:
                    Debug.echoError("Weird map data!");
                    break;
            }
            if (added != null) {
                renderer.addObject(added);
                if (objectSection.contains("world_coordinates") && objectSection.getString("world_coordinates", "false").equalsIgnoreCase("true")) {
                    added.worldCoordinates = true;
                }
                added.showPastEdge = CoreUtilities.equalsIgnoreCase(objectSection.getString("show_past_edge", "false"), "true");
            }
        }
    }
    DenizenMapManager.setMap(mapView, renderer);
}
Also used : ArrayList(java.util.ArrayList) YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) NaturalOrderComparator(com.denizenscript.denizencore.utilities.NaturalOrderComparator) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder)

Example 17 with StringHolder

use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.

the class ItemEnchantments method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("remove_enchantments")) {
        if (mechanism.hasValue()) {
            List<EnchantmentTag> toRemove = mechanism.valueAsType(ListTag.class).filter(EnchantmentTag.class, mechanism.context);
            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                for (EnchantmentTag ench : toRemove) {
                    meta.removeStoredEnchant(ench.enchantment);
                }
                item.setItemMeta(meta);
            } else {
                for (EnchantmentTag ench : toRemove) {
                    item.getItemStack().removeEnchantment(ench.enchantment);
                }
                item.resetCache();
            }
        } else {
            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                for (Enchantment ench : meta.getStoredEnchants().keySet()) {
                    meta.removeStoredEnchant(ench);
                }
                item.setItemMeta(meta);
            } else {
                for (Enchantment ench : item.getItemStack().getEnchantments().keySet()) {
                    item.getItemStack().removeEnchantment(ench);
                }
                item.resetCache();
            }
        }
    }
    // -->
    if (mechanism.matches("enchantments")) {
        String val = mechanism.getValue().asString();
        if (val.startsWith("map@") || val.startsWith("[") || (val.contains("=") && !val.contains(","))) {
            MapTag map = mechanism.valueAsType(MapTag.class);
            for (Map.Entry<StringHolder, ObjectTag> enchantments : map.map.entrySet()) {
                Enchantment ench = EnchantmentTag.valueOf(enchantments.getKey().low, mechanism.context).enchantment;
                int level = enchantments.getValue().asElement().asInt();
                if (ench != null) {
                    if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                        EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                        meta.addStoredEnchant(ench, level, true);
                        item.setItemMeta(meta);
                    } else {
                        item.getItemStack().addUnsafeEnchantment(ench, level);
                        item.resetCache();
                    }
                } else {
                    mechanism.echoError("Unknown enchantment '" + enchantments.getKey().str + "'");
                }
            }
        } else {
            for (String enchant : mechanism.valueAsType(ListTag.class)) {
                if (!enchant.contains(",")) {
                    mechanism.echoError("Invalid enchantment format, use name,level|...");
                } else {
                    String[] data = enchant.split(",", 2);
                    try {
                        Enchantment ench = EnchantmentTag.valueOf(data[0], mechanism.context).enchantment;
                        if (ench != null) {
                            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                                meta.addStoredEnchant(ench, Integer.valueOf(data[1]), true);
                                item.setItemMeta(meta);
                            } else {
                                item.getItemStack().addUnsafeEnchantment(ench, Integer.valueOf(data[1]));
                                item.resetCache();
                            }
                        } else {
                            mechanism.echoError("Unknown enchantment '" + data[0] + "'");
                        }
                    } catch (NullPointerException e) {
                        mechanism.echoError("Unknown enchantment '" + data[0] + "'");
                    } catch (NumberFormatException ex) {
                        mechanism.echoError("Cannot apply enchantment '" + data[0] + "': '" + data[1] + "' is not a valid integer!");
                        if (Debug.verbose) {
                            Debug.echoError(ex);
                        }
                    }
                }
            }
        }
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EnchantmentTag(com.denizenscript.denizen.objects.EnchantmentTag) Enchantment(org.bukkit.enchantments.Enchantment)

Example 18 with StringHolder

use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.

the class LegacySavesUpdater method applyFlags.

public static void applyFlags(String object, AbstractFlagTracker tracker, YamlConfiguration section) {
    try {
        if (section == null || section.getKeys(false).isEmpty()) {
            return;
        }
        for (StringHolder flagName : section.getKeys(false)) {
            if (flagName.low.endsWith("-expiration")) {
                continue;
            }
            TimeTag expireAt = null;
            if (section.contains(flagName + "-expiration")) {
                long expireTime = Long.parseLong(section.getString(flagName + "-expiration"));
                expireAt = new TimeTag(expireTime);
            }
            Object value = section.get(flagName.str);
            ObjectTag setAs = CoreUtilities.objectToTagForm(value, CoreUtilities.errorButNoDebugContext);
            tracker.setFlag(flagName.low, setAs, expireAt);
        }
    } catch (Throwable ex) {
        Debug.echoError("Error while updating legacy flags for " + object);
        Debug.echoError(ex);
    }
}
Also used : StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag)

Aggregations

StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)18 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)11 MapTag (com.denizenscript.denizencore.objects.core.MapTag)8 ListTag (com.denizenscript.denizencore.objects.core.ListTag)7 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)6 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)6 ItemTag (com.denizenscript.denizen.objects.ItemTag)4 Map (java.util.Map)4 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)3 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)3 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 EnchantmentTag (com.denizenscript.denizen.objects.EnchantmentTag)2 NPCTag (com.denizenscript.denizen.objects.NPCTag)2 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)2 AbstractFlagTracker (com.denizenscript.denizencore.flags.AbstractFlagTracker)2 Mechanism (com.denizenscript.denizencore.objects.Mechanism)2 TimeTag (com.denizenscript.denizencore.objects.core.TimeTag)2 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)2 TagContext (com.denizenscript.denizencore.tags.TagContext)2