use of com.denizenscript.denizencore.utilities.YamlConfiguration in project Denizen-For-Bukkit by DenizenScript.
the class InventoryScriptContainer method getInventoryFrom.
public InventoryTag getInventoryFrom(TagContext context) {
InventoryTag inventory;
context = (context == null ? CoreUtilities.basicContext : context).clone();
ScriptTag thisScript = new ScriptTag(this);
context.script = thisScript;
context.debug = context.debug && shouldDebug();
try {
InventoryType type = InventoryType.CHEST;
if (contains("inventory", String.class)) {
try {
type = InventoryType.valueOf(getString("inventory").toUpperCase());
} catch (IllegalArgumentException ex) {
Debug.echoError(this, "Invalid inventory type specified. Assuming \"CHEST\" (" + ex.getMessage() + ")");
}
} else {
Debug.echoError(this, "Inventory script '" + getName() + "' does not specify an inventory type. Assuming \"CHEST\".");
}
if (type == InventoryType.PLAYER) {
Debug.echoError(this, "Inventory type 'player' is not valid for inventory scripts - defaulting to 'CHEST'.");
type = InventoryType.CHEST;
}
int size = 0;
if (contains("size", String.class)) {
if (type != InventoryType.CHEST) {
Debug.echoError(this, "You can only set the size of chest inventories!");
} else {
String sizeText = TagManager.tag(getString("size"), context);
if (!ArgumentHelper.matchesInteger(sizeText)) {
Debug.echoError(this, "Invalid (not-a-number) size value.");
} else {
size = Integer.parseInt(sizeText);
}
if (size == 0) {
Debug.echoError(this, "Inventory size can't be 0. Assuming default of inventory type...");
}
if (size % 9 != 0) {
size = (int) Math.ceil(size / 9.0) * 9;
Debug.echoError(this, "Inventory size must be a multiple of 9! Rounding up to " + size + "...");
}
if (size < 0) {
size = size * -1;
Debug.echoError(this, "Inventory size must be a positive number! Inverting to " + size + "...");
}
}
}
if (size == 0) {
if (contains("slots", List.class) && type == InventoryType.CHEST) {
size = getStringList("slots").size() * 9;
} else {
size = type.getDefaultSize();
}
}
String title = contains("title", String.class) ? TagManager.tag(getString("title"), context) : null;
if (type == InventoryType.CHEST) {
inventory = new InventoryTag(size, title != null ? title : "Chest");
} else {
if (title == null) {
inventory = new InventoryTag(type);
} else {
inventory = new InventoryTag(type, title);
}
}
inventory.idType = "script";
inventory.idHolder = thisScript;
boolean[] filledSlots = new boolean[size];
if (contains("slots", List.class)) {
ItemStack[] finalItems = new ItemStack[size];
int itemsAdded = 0;
for (String items : getStringList("slots")) {
items = TagManager.tag(items, context).trim();
if (items.isEmpty()) {
continue;
}
if (!items.startsWith("[") || !items.endsWith("]")) {
Debug.echoError(this, "Invalid slots line: [" + items + "]... Ignoring it");
continue;
}
String[] itemsInLine = items.substring(1, items.length() - 1).split("\\[?\\]?\\s+\\[", -1);
for (String item : itemsInLine) {
if (item.isEmpty()) {
finalItems[itemsAdded++] = new ItemStack(Material.AIR);
continue;
}
filledSlots[itemsAdded] = true;
if (contains("definitions." + item, String.class)) {
ItemTag def = ItemTag.valueOf(TagManager.tag(getString("definitions." + item), context), context);
if (def == null) {
Debug.echoError(this, "Invalid definition '" + item + "'... Ignoring it and assuming 'AIR'");
finalItems[itemsAdded] = new ItemStack(Material.AIR);
} else {
finalItems[itemsAdded] = def.getItemStack();
}
} else {
try {
ItemTag itemTag = ItemTag.valueOf(item, context);
if (itemTag == null) {
finalItems[itemsAdded] = new ItemStack(Material.AIR);
Debug.echoError(this, "Invalid slot item: [" + item + "]... ignoring it and assuming 'AIR'");
} else {
finalItems[itemsAdded] = itemTag.getItemStack();
}
} catch (Exception ex) {
Debug.echoError(this, "Invalid slot item: [" + item + "]...");
Debug.echoError(ex);
}
}
itemsAdded++;
}
}
inventory.setContents(finalItems);
}
if (containsScriptSection("procedural items")) {
List<ScriptEntry> entries = getEntries(context.getScriptEntryData(), "procedural items");
if (!entries.isEmpty()) {
InstantQueue queue = new InstantQueue("INV_SCRIPT_ITEM_PROC");
queue.addEntries(entries);
if (contains("definitions", Map.class)) {
YamlConfiguration section = getConfigurationSection("definitions");
for (StringHolder string : section.getKeys(false)) {
String definition = string.str;
queue.addDefinition(definition, section.getString(definition));
}
}
queue.procedural = true;
queue.start();
if (queue.determinations != null) {
ListTag list = ListTag.getListFor(queue.determinations.getObject(0), context);
if (list != null) {
int x = 0;
for (ItemTag item : list.filter(ItemTag.class, context, true)) {
while (x < filledSlots.length && filledSlots[x]) {
x++;
}
if (x >= filledSlots.length || filledSlots[x]) {
break;
}
inventory.setSlots(x, item.getItemStack());
filledSlots[x] = true;
}
}
}
}
}
} catch (Exception e) {
Debug.echoError(this, "Woah! An exception has been called while building this inventory script!");
Debug.echoError(e);
inventory = null;
}
if (inventory != null) {
InventoryTag.trackTemporaryInventory(inventory);
}
return inventory;
}
use of com.denizenscript.denizencore.utilities.YamlConfiguration 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;
}
use of com.denizenscript.denizencore.utilities.YamlConfiguration in project Denizen-For-Bukkit by DenizenScript.
the class EntityScriptContainer method getEntityFrom.
public EntityTag getEntityFrom(PlayerTag player, NPCTag npc) {
EntityTag entity;
try {
TagContext context = new BukkitTagContext(player, npc, new ScriptTag(this));
if (contains("entity_type", String.class)) {
String entityType = TagManager.tag((getString("entity_type", "")), context);
entity = EntityTag.valueOf(entityType, context);
} else {
throw new Exception("Missing entity_type argument!");
}
if (contains("flags", Map.class)) {
YamlConfiguration flagSection = getConfigurationSection("flags");
MapTagFlagTracker tracker = new MapTagFlagTracker();
for (StringHolder key : flagSection.getKeys(false)) {
tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
}
entity.safeAdjust(new Mechanism("flag_map", tracker.map, context));
}
if (contains("mechanisms", Map.class)) {
YamlConfiguration mechSection = getConfigurationSection("mechanisms");
Set<StringHolder> strings = mechSection.getKeys(false);
for (StringHolder string : strings) {
ObjectTag obj = CoreUtilities.objectToTagForm(mechSection.get(string.low), context, true, true);
entity.safeAdjust(new Mechanism(string.low, obj, context));
}
}
boolean any = false;
Set<StringHolder> strings = getContents().getKeys(false);
for (StringHolder string : strings) {
if (!nonMechanismKeys.contains(string.low)) {
any = true;
ObjectTag obj = CoreUtilities.objectToTagForm(getContents().get(string.low), context, true, true);
entity.safeAdjust(new Mechanism(string.low, obj, context));
}
}
if (any) {
Deprecations.entityMechanismsFormat.warn(this);
}
if (entity == null || entity.isUnique()) {
return null;
}
entity.setEntityScript(getName());
} catch (Exception e) {
Debug.echoError("Woah! An exception has been called with this entity script!");
Debug.echoError(e);
entity = null;
}
return entity;
}
use of com.denizenscript.denizencore.utilities.YamlConfiguration in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptHelper method rebuildRecipes.
public void rebuildRecipes() {
for (ItemScriptContainer container : item_scripts.values()) {
try {
if (container.contains("recipes", Map.class)) {
YamlConfiguration section = container.getConfigurationSection("recipes");
int id = 0;
for (StringHolder key : section.getKeys(false)) {
id++;
YamlConfiguration subSection = section.getConfigurationSection(key.str);
String type = CoreUtilities.toLowerCase(subSection.getString("type"));
String internalId;
if (subSection.contains("recipe_id")) {
internalId = subSection.getString("recipe_id");
recipeIdToItemScript.put("denizen:" + internalId, container);
} else {
internalId = getIdFor(container, type + "_recipe", id);
}
String group = subSection.contains("group") ? subSection.getString("group") : "";
ItemStack item = container.getCleanReference().getItemStack().clone();
if (subSection.contains("output_quantity")) {
item.setAmount(Integer.parseInt(subSection.getString("output_quantity")));
}
switch(type) {
case "shaped":
registerShapedRecipe(container, item, subSection.getStringList("input"), internalId, group);
break;
case "shapeless":
registerShapelessRecipe(container, item, subSection.getString("input"), internalId, group);
break;
case "stonecutting":
registerStonecuttingRecipe(container, item, subSection.getString("input"), internalId, group);
break;
case "furnace":
case "blast":
case "smoker":
case "campfire":
float exp = 0;
int cookTime = 40;
if (subSection.contains("experience")) {
exp = Float.parseFloat(subSection.getString("experience"));
}
if (subSection.contains("cook_time")) {
cookTime = DurationTag.valueOf(subSection.getString("cook_time"), new BukkitTagContext(container)).getTicksAsInt();
}
registerFurnaceRecipe(container, item, subSection.getString("input"), exp, cookTime, type, internalId, group);
break;
case "smithing":
String retain = null;
if (subSection.contains("retain")) {
retain = subSection.getString("retain");
}
registerSmithingRecipe(container, item, subSection.getString("base"), subSection.getString("upgrade"), internalId, retain);
break;
}
}
}
// Old script style
if (container.contains("RECIPE", List.class)) {
Deprecations.oldRecipeScript.warn(container);
registerShapedRecipe(container, container.getCleanReference().getItemStack().clone(), container.getStringList("RECIPE"), getIdFor(container, "old_recipe", 0), "custom");
}
if (container.contains("SHAPELESS_RECIPE", String.class)) {
Deprecations.oldRecipeScript.warn(container);
registerShapelessRecipe(container, container.getCleanReference().getItemStack().clone(), container.getString("SHAPELESS_RECIPE"), getIdFor(container, "old_shapeless", 0), "custom");
}
if (container.contains("FURNACE_RECIPE", String.class)) {
Deprecations.oldRecipeScript.warn(container);
registerFurnaceRecipe(container, container.getCleanReference().getItemStack().clone(), container.getString("FURNACE_RECIPE"), 0, 40, "furnace", getIdFor(container, "old_furnace", 0), "custom");
}
} catch (Exception ex) {
Debug.echoError("Error while rebuilding item script recipes for '" + container.getName() + "'...");
Debug.echoError(ex);
}
}
}
use of com.denizenscript.denizencore.utilities.YamlConfiguration 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);
}
Aggregations