profile = node.getNode("debug").getBoolean(profile); traceUnflushedSessions = node.getNode("debugging", "trace-unflushed-sessions").getBoolean(traceUnflushedSessions); wandItem = node.getNode("wand-item").getString(wandItem); try { wandItem = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(wandItem)).getId(); defaultChangeLimit = Math.max(-1, node.getNode("limits", "max-blocks-changed", "default").getInt(defaultChangeLimit)); maxChangeLimit = Math.max(-1, node.getNode("limits", "max-blocks-changed", "maximum").getInt(maxChangeLimit)); defaultMaxPolygonalPoints = Math.max(-1, node.getNode("limits", "max-polygonal-points", "default").getInt(defaultMaxPolygonalPoints)); maxPolygonalPoints = Math.max(-1, node.getNode("limits", "max-polygonal-points", "maximum").getInt(maxPolygonalPoints)); maxRadius = Math.max(-1, node.getNode("limits", "max-radius").getInt(maxRadius)); maxBrushRadius = node.getNode("limits", "max-brush-radius").getInt(maxBrushRadius); maxSuperPickaxeSize = Math.max(1, node.getNode("limits", "max-super-pickaxe-size").getInt(maxSuperPickaxeSize)); butcherDefaultRadius = Math.max(-1, node.getNode("limits", "butcher-radius", "default").getInt(butcherDefaultRadius)); butcherMaxRadius = Math.max(-1, node.getNode("limits", "butcher-radius", "maximum").getInt(butcherMaxRadius)); disallowedBlocks = new HashSet<>(node.getNode("limits", "disallowed-blocks").getList(TypeToken.of(String.class))); } catch (ObjectMappingException e) { logger.warn("Error loading WorldEdit configuration", e); allowedDataCycleBlocks = new HashSet<>(node.getNode("limits", "allowed-data-cycle-blocks").getList(TypeToken.of(String.class))); } catch (ObjectMappingException e) { logger.warn("Error loading WorldEdit configuration", e); registerHelp = node.getNode("register-help").getBoolean(true); logCommands = node.getNode("logging", "log-commands").getBoolean(logCommands);
@Override public void load() { super.load(); creativeEnable = node.getNode("use-in-creative").getBoolean(false); cheatMode = node.getNode("cheat-mode").getBoolean(false); try { config.save(node); } catch (IOException e) { logger.warn("Error loading WorldEdit configuration", e); } }
public static void createDefaults(CommentedConfigurationNode config) { for (Field field : VoxelSniperConfiguration.class.getDeclaredFields()) { if (field.isAnnotationPresent(ConfigValue.class)) { ConfigValue anno = field.getAnnotation(ConfigValue.class); CommentedConfigurationNode node = config.getNode((Object[]) anno.name().split("\\.")); node.setComment(anno.comment()); try { node.setValue(field.get(null)); VoxelSniper.getLogger().debug("Created " + anno.name() + " = " + node.getValue()); } catch (IllegalArgumentException | IllegalAccessException e) { VoxelSniper.getLogger().error("Failed to extract default value for configuration field " + field.toGenericString()); e.printStackTrace(); } } } }
/** * Loads the configuration */ private void loadConfiguration() { configurationFile = getConfigFile(); configurationLoader = HoconConfigurationLoader.builder().setFile(configurationFile).build(); try { if (!configurationFile.exists()) { configurationFile.createNewFile(); config = configurationLoader.load(); config.setComment("This contains settings for MCStats: http://mcstats.org"); config.getNode("mcstats.guid").setValue(UUID.randomUUID().toString()); config.getNode("mcstats.opt-out").setValue(false); config.getNode("mcstats.debug").setValue(false); configurationLoader.save(config); } else { config = configurationLoader.load(); } guid = config.getNode("mcstats.guid").getString(); debug = config.getNode("mcstats.debug").getBoolean(); } catch (final IOException e) { e.printStackTrace(); } }
private void readConfigValue(ConfigValue value, CommentedConfigurationNode node) { if (!value.origin().comments().isEmpty()) { node.setComment(CRLF_MATCH.matcher(Joiner.on('\n').join(value.origin().comments())).replaceAll("")); } switch (value.valueType()) { case OBJECT: if (((ConfigObject) value).isEmpty()) { node.setValue(ImmutableMap.of()); } else { for (Map.Entry<String, ConfigValue> ent : ((ConfigObject) value).entrySet()) { readConfigValue(ent.getValue(), node.getNode(ent.getKey())); } } break; case LIST: List<ConfigValue> values = (ConfigList) value; for (int i = 0; i < values.size(); ++i) { readConfigValue(values.get(i), node.getNode(i)); } break; case NULL: return; default: node.setValue(value.unwrapped()); } }
@Test public void testCommentsMerged() { CommentedConfigurationNode source = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode target = SimpleCommentedConfigurationNode.root(); source.getNode("no-value").setValue("a").setComment("yeah"); source.getNode("existing-value-no-comment").setValue("orig").setComment("maybe"); source.getNode("existing-value").setValue("a").setComment("yeah"); source.getNode("no-parent", "child").setValue("x").setComment("always"); target.getNode("existing-value-no-comment").setValue("new"); target.getNode("existing-value").setValue("b").setComment("nope"); target.mergeValuesFrom(source); assertEquals("yeah", target.getNode("no-value").getComment().orElse(null)); assertEquals("maybe", target.getNode("existing-value-no-comment").getComment().orElse(null)); assertEquals("new", target.getNode("existing-value-no-comment").getString()); assertEquals("nope", target.getNode("existing-value").getComment().orElse(null)); assertEquals("always", target.getNode("no-parent", "child").getComment().orElse(null)); } }
@SuppressWarnings("rawtypes") public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException { try { Object fieldVal = this.field.get(instance); if (fieldVal == null) { node.setValue(null); } else { TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } serial.serialize(this.fieldType, fieldVal, node); } if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) { CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node); if (!commentNode.getComment().isPresent()) { commentNode.setComment(this.comment); } } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to serialize field " + field.getName(), e); } } }
private <T> void setValueAndComment(ConfigurationNode node, TypeToken<T> type, T value, String comment) throws ObjectMappingException { node.setValue(type, value); if (node instanceof CommentedConfigurationNode) { ((CommentedConfigurationNode) node).setComment(comment); } } }
@Test public void testNestedObjectWithComments() throws ObjectMappingException { CommentedConfigurationNode node = SimpleCommentedConfigurationNode.root(); final ObjectMapper<ParentObject>.BoundInstance mapper = ObjectMapper.forObject(new ParentObject()); mapper.populate(node); assertEquals("Comment on parent", node.getNode("inner").getComment().get()); assertTrue(node.getNode("inner").hasMapChildren()); assertEquals("Default value", node.getNode("inner", "test").getString()); assertEquals("Something", node.getNode("inner", "test").getComment().get()); }
assertEquals(1, node.getNode("list").getChildrenList().size()); assertEquals("Changed value", node.getNode("inner").getNode("test").getString()); assertEquals("Changed value", node.getNode("list").getChildrenList().get(0).getNode("test").getString()); assertEquals("Something", node.getNode("inner").getNode("test").getComment().get()); assertEquals("Something", node.getNode("list").getChildrenList().get(0).getNode("test").getComment().get()); assertEquals(ChildObject.class.getName(), node.getNode("inner").getNode("__class__").getString()); assertEquals(ChildObject.class.getName(), node.getNode("list").getChildrenList().get(0).getNode("__class__").getString());
@Test public void testCommentsApplied() throws ObjectMappingException { CommentedConfigurationNode node = SimpleCommentedConfigurationNode.root(); ObjectMapper<CommentedObject>.BoundInstance mapper = ObjectMapper.forClass(CommentedObject.class).bindToNew(); CommentedObject obj = mapper.populate(node); obj.color = "fuchsia"; obj.politician = "All of them"; mapper.serialize(node); assertEquals("You look nice today", node.getNode("commented-key").getComment().orElse(null)); assertEquals("fuchsia", node.getNode("commented-key").getString()); assertFalse(node.getNode("no-comment").getComment().isPresent()); }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.conf"); final Path saveTest = tempDir.resolve("text1.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(saveTest, UTF_8)).build(); CommentedConfigurationNode node = loader.load(); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); CommentedConfigurationNode testNode = node.getNode("test"); assertEquals(" Test node", testNode.getComment().orElse(null)); assertEquals("dog park", node.getNode("other", "location").getValue()); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/roundtrip-test.conf"), UTF_8), Files .readAllLines(saveTest, UTF_8)); }
/** * Saves configuration */ public void save() { try { mapper.serialize(Objects.requireNonNull(baseNode).getNode(baseNodeName)); loader.save(baseNode); } catch (ObjectMappingException | IOException e) { throw new RuntimeException(e); } }
@Override public Map<String, String> getStringMap(String path) { Map<String, String> map = new HashMap<>(); Set<? extends Map.Entry<Object, ? extends CommentedConfigurationNode>> childrens = path.equals("") ? file.getChildrenMap().entrySet(): file.getNode(path).getChildrenMap().entrySet(); for (Map.Entry<Object, ? extends CommentedConfigurationNode> entry : childrens) { map.put(entry.getKey().toString(), entry.getValue().getString()); } return map; }
@Override public SimpleCommentedConfigurationNode setValue(Object value) { if (value instanceof CommentedConfigurationNode && ((CommentedConfigurationNode) value).getComment().isPresent()) { setComment(((CommentedConfigurationNode) value).getComment().get()); } return (SimpleCommentedConfigurationNode)super.setValue(value); }
private Object parseConfiguration(CommentedConfigurationNode config) { if (config.hasListChildren()) { List<Object> cfg = new ArrayList<>(); for (CommentedConfigurationNode node : config.getChildrenList()) { cfg.add(parseConfiguration(node)); } return cfg; } else if (config.hasMapChildren()) { Map<String, Object> cfg = new HashMap<>(); for (CommentedConfigurationNode node : config.getChildrenMap().values()) { cfg.put(node.getKey().toString(), parseConfiguration(node)); } return cfg; } return config.getValue(); } }