private static void _removeFromValueList(String keyName, String item) { Key key = root.getSubKey(keyName); if (key == null) { key = root.createSubKey(keyName); } String oldValue = key.value; String[] oldValueList = _getValueList(keyName); fireChanging(keyName, _isList(keyName), oldValue, null, oldValueList, null); if (key.value != null) { key.value = null; } if (key.valueList == null) { key.valueList = new java.util.ArrayList(); } else { if (item != null) { key.valueList.remove(item); } } fireChanged(keyName, _isList(keyName), oldValue, null, oldValueList, _getValueList(keyName)); }
private static void _setValueList(String keyName, String[] values) { Key key = root.getSubKey(keyName); if (key == null) { key = root.createSubKey(keyName); } String oldValue = key.value; String[] oldValueList = _getValueList(keyName); fireChanging(keyName, _isList(keyName), oldValue, null, oldValueList, values); if (key.value != null) { key.value = null; } if (key.valueList == null) { key.valueList = new java.util.ArrayList((values == null) ? 0 : values.length); } else { key.valueList.clear(); } if (values != null) { for (int i = 0; i < values.length; i++) { key.valueList.add(values[i]); } } fireChanged(keyName, _isList(keyName), oldValue, null, oldValueList, values); }
private static void _setValue(String keyName, String value) { Key key = root.getSubKey(keyName); if (key == null) { key = root.createSubKey(keyName); } String oldValue = key.value; String[] oldValueList = _getValueList(keyName); fireChanging(keyName, _isList(keyName), oldValue, value, oldValueList, null); if (key.valueList != null) { key.valueList = null; } key.value = value; fireChanged(keyName, _isList(keyName), oldValue, value, oldValueList, null); }
private static Key loadKey(org.w3c.dom.Element keyElement) { Key key = new Key(); String visibility = keyElement.getAttribute("visibility").trim(); if (visibility.equals("open")) { key.visibility = VIS_OPEN; } else if (visibility.equals("advanced")) { key.visibility = VIS_ADVANCED; } else if (visibility.equals("hidden")) { key.visibility = VIS_HIDDEN; } java.util.HashMap map = parseSingleNode(keyElement); org.w3c.dom.Element nameElement = (org.w3c.dom.Element) map.get("name"); if (nameElement != null) { org.w3c.dom.Text textNode = (org.w3c.dom.Text) parseSingleNode(nameElement).get("text"); if (textNode != null) { key.name = textNode.getData().trim(); } } org.w3c.dom.Element valueElement = (org.w3c.dom.Element) map.get("value"); if (valueElement != null) { org.w3c.dom.Element listElement = (org.w3c.dom.Element) parseSingleNode(valueElement).get("list"); if (listElement != null) { key.valueList = new java.util.ArrayList(); java.util.ArrayList items = (java.util.ArrayList) parseSingleNode(listElement).get("items"); if (items != null) { for (java.util.Iterator iter = items.iterator(); iter.hasNext(); ) { org.w3c.dom.Element itemElement = (org.w3c.dom.Element) iter.next(); if (itemElement != null) { org.w3c.dom.Text textNode = (org.w3c.dom.Text) parseSingleNode(itemElement).get("text"); if (textNode != null) { key.valueList.add(textNode.getData().trim()); } } } } } else { org.w3c.dom.Text textNode = (org.w3c.dom.Text) parseSingleNode(valueElement).get("text"); if (textNode != null) { key.value = textNode.getData().trim(); } } } java.util.ArrayList keys = (java.util.ArrayList) map.get("keys"); if (keys != null) { for (java.util.Iterator iter = keys.iterator(); iter.hasNext(); ) { org.w3c.dom.Element subKeyElement = (org.w3c.dom.Element) iter.next(); if (subKeyElement != null) { if (key.subKeys == null) { key.subKeys = new java.util.HashMap(); } Key subKey = loadKey(subKeyElement); if ((subKey != null) && (subKey.name != null)) { key.subKeys.put(subKey.name, subKey); } } } } return key; }
@SuppressWarnings("unchecked") private static void parse(SwornPlugin plugin, Class<?> clazz, Object object) { FileConfiguration config = plugin.getConfig(); for (Field field : clazz.getDeclaredFields()) { if (!field.isAccessible()) field.setAccessible(true); Key key = field.getAnnotation(Key.class); if (key != null) { String path = key.value(); try { Object value = config.get(path); if (value != null) { ValueOptions options = field.getAnnotation(ValueOptions.class); if (options != null) { for (ValueOption option : options.value()) { switch (option) { case FORMAT: value = FormatUtil.format(value.toString()); break; case LIST_LOWER_CASE: List<String> list = new ArrayList<>(); for (String line : (List<String>) value) list.add(line.toLowerCase()); value = list; break; case LOWER_CASE: value = value.toString().toLowerCase(); break; case MINUTE_TO_MILLIS: value = TimeUnit.MINUTES.toMillis(NumberUtil.toLong(value)); break; case PARSE_ITEM: value = ItemUtil.readItem(value.toString(), plugin); break; case PARSE_ITEMS: value = ItemUtil.readItems((List<String>) value, plugin); break; case SECOND_TO_MILLIS: value = TimeUnit.SECONDS.toMillis(NumberUtil.toLong(value)); break; } } for (Class<?> custom : options.custom()) { Method convert = custom.getMethod("convert", Object.class); if (convert.isAccessible()) { value = convert.invoke(null, value); } } } field.set(object, value); } } catch (Throwable ex) { plugin .getLogHandler() .log(Level.SEVERE, Util.getUsefulStack(ex, "loading value from {0}", path)); } } } }