Git Product home page Git Product logo

Comments (13)

A248 avatar A248 commented on August 21, 2024 1

Yes. You can even do this without using a value serializer:

public interface Config {

   static Map<Integer, Level> defaultLevels() {
        Map<Integer, Level> levelsMap = new HashMap<>();
        levelsMap.put(1, Level.of(5, 10, 1, 10, 0));
        levelsMap.put(2, Level.of(7, 12, 2, 11, 10));
        levelsMap.put(3, Level.of(9, 14, 3, 12, 20));
        levelsMap.put(4, Level.of(12, 16, 4, 13, 30));
        levelsMap.put(5, Level.of(15, 18, 5, 14, 40));
        return levelsMap;
    }

    @ConfDefault.DefaultObject("defaultLevels")
    Map<Integer, @SubSection Level> levels();


}
public interface Level {

	int minSpawnDelay();

	int maxSpawnDelay();

	int spawnCount();

	int spawnRange();

	int price();

	static Level of(int minSpawnDelay, int maxSpawnDelay, int spawnCount, int spawnRange, int price) {
		return new Level() {

			@Override
			public int minSpawnDelay() { return minSpawnDelay; }
			@Override
			public int maxSpawnDelay() { return maxSpawnDelay; }
			@Override
			public int spawnCount() { return spawnCount; }
			@Override
			public int spawnRange() { return spawnRange; }
			@Override
			public int price() { return price; }
		};
	}

}

from dazzleconf.

A248 avatar A248 commented on August 21, 2024 1

Note that you need @SubSection here:

    @ConfDefault.DefaultObject("defaultLevels")
    Map<Integer, @SubSection Level> levels();

from dazzleconf.

A248 avatar A248 commented on August 21, 2024 1

Thanks. This is a bug with the snakeyaml extension. The core should support non-string keys.

Also, I tested that this bug is specific to CommentMode.alternateWriter as well as CommentMode.fullComments. For now, to avoid it, you can use CommentMode.headerOnly, which lacks comment support except for the header.

If you need comments and can't wait until the next release, let me know and I will publish a SNAPSHOT release with the bug fix.

from dazzleconf.

A248 avatar A248 commented on August 21, 2024

You use ValueSerialiser so you can use a custom type in your configuration. Let's say you had the following:

public class CustomType {

	private final String value;

	public CustomType(String value) {
		this.value = value;
	}

	public String stringValue() {
		return value;
	}
}
  1. First implement the ValueSerialiser:
public class MyCustomSerializer implements ValueSerialiser<CustomType> {
	@Override
	public Class<CustomType> getTargetClass() {
		return CustomType.class;
	}

	@Override
	public CustomType deserialise(FlexibleType flexibleType) throws BadValueException {
		// Convert from a String -> CustomType
		String value = flexibleType.getString();
		return new CustomType(value);
	}

	@Override
	public Object serialise(CustomType value, Decomposer decomposer) {
		// Convert from a CustomType -> String
		return value.stringValue();
	}
}
  1. Then add the serializer to the ConfigurationOptions:
ConfigurationOptions options = new ConfigurationOptions.Builder()
				.addSerialiser(new MyCustomSerializer())
				.build();

Use the ConfigurationOptions when you create your config factory (GsonConfigurationFactory, SnakeYamlConfigurationFactory, etc.)

from dazzleconf.

arthurr0 avatar arthurr0 commented on August 21, 2024

How i can use this for object with more variables?

from dazzleconf.

A248 avatar A248 commented on August 21, 2024

I'm not exactly sure what you mean by that. If you need to use this type in your configuration, you can very easily:

public interface Config {

  @DefaultString("default value")
  CustomType something();

  // You can even use it in a List, Set, or Collection
  @DefaultStrings({"value1", "value2"})
  List<CustomType> listOfThings();

}

from dazzleconf.

arthurr0 avatar arthurr0 commented on August 21, 2024

I need put this to config:

   static Map<Integer, Level> defaultLevels() {
        Map<Integer, Level> levelsMap = new HashMap<>();
        levelsMap.put(1, new Level(5, 10, 1, 10, 0));
        levelsMap.put(2, new Level(7, 12, 2, 11, 10));
        levelsMap.put(3, new Level(9, 14, 3, 12, 20));
        levelsMap.put(4, new Level(12, 16, 4, 13, 30));
        levelsMap.put(5, new Level(15, 18, 5, 14, 40));
        return levelsMap;
    }

    @ConfDefault.DefaultObject("defaultLevels")
    Map<Integer, Level> levels();

from dazzleconf.

A248 avatar A248 commented on August 21, 2024

I see. For that, you can treat a Level as a list of integers and implement your serializer like this:

public class LevelSerializer implements ValueSerialiser<Level> {
	@Override
	public Class<Level> getTargetClass() {
		return Level.class;
	}

	@Override
	public Level deserialise(FlexibleType flexibleType) throws BadValueException {
		List<Integer> values = new ArrayList<>();
		for (FlexibleType element : flexibleType.getList()) {
			values.add(element.getInteger());
		}
		return new Level(values);
	}

	@Override
	public Object serialise(Level value, Decomposer decomposer) {
		return value.values();
	}
}

public class Level {

	private final List<Integer> values;

	public Level(List<Integer> values) {
		this.values = List.copyOf(values);
		if (this.values.size() != 5) {
			throw new IllegalArgumentException("Size of Level values must be 5");
		}
	}

	public List<Integer> values() {
		return values;
	}
}

There are other ways you could do this, like by treating a Level as a configuration section, but I choose this way because it's relatively simple.

from dazzleconf.

arthurr0 avatar arthurr0 commented on August 21, 2024

But you can show my how i can do ths like by treating a level as configuration section? Your solution is not convenient to configuration.

Level:

public class Level {
    int minSpawnDelay;
    int maxSpawnDelay;
    int spawnCount;
    int spawnRage;
    int price;
    }

from dazzleconf.

arthurr0 avatar arthurr0 commented on August 21, 2024

Thank you wery much <3

from dazzleconf.

A248 avatar A248 commented on August 21, 2024

You're welcome. A thanks to you too - I've added this information to the wiki now:
https://github.com/A248/DazzleConf/wiki/Examples-of-custom-types-in-your-configuration

from dazzleconf.

arthurr0 avatar arthurr0 commented on August 21, 2024

Map key need be String :(

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeRemainingMapEntries(CommentedWriter.java:99) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeMap(CommentedWriter.java:93) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.lambda$writeValue$0(CommentedWriter.java:188) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.descendAndDo(CommentedWriter.java:157) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeValue(CommentedWriter.java:188) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeRemainingMapEntries(CommentedWriter.java:112) ~[?:?]        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeMap(CommentedWriter.java:93) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.lambda$writeValue$0(CommentedWriter.java:188) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.descendAndDo(CommentedWriter.java:157) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeValue(CommentedWriter.java:188) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeRemainingMapEntries(CommentedWriter.java:112) ~[?:?]        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeMap(CommentedWriter.java:93) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeData(CommentedWriter.java:84) ~[?:?]
        at space.arim.dazzleconf.ext.snakeyaml.SnakeYamlConfigurationFactory.writeMapToWriter(SnakeYamlConfigurationFactory.java:148) ~[?:?]
        at space.arim.dazzleconf.factory.AbstractConfigurationFactory$ConfigFactoryDelegate.writeMap(AbstractConfigurationFactory.java:153) ~[?:?]
        at space.arim.dazzleconf.factory.HumanReadableConfigurationFactory.bufferedWriteMap(HumanReadableConfigurationFactory.java:125) ~[?:?]
        at space.arim.dazzleconf.factory.HumanReadableConfigurationFactory.writeMap(HumanReadableConfigurationFactory.java:114) ~[?:?]
        at space.arim.dazzleconf.factory.ConfigurationFormatFactory.write(ConfigurationFormatFactory.java:184) ~[?:?]
        at space.arim.dazzleconf.factory.DelegatingConfigurationFactory.write(DelegatingConfigurationFactory.java:72) ~[?:?]
        at space.arim.dazzleconf.factory.AbstractConfigurationFactory.write(AbstractConfigurationFactory.java:42) ~[?:?]        at space.arim.dazzleconf.helper.ConfigurationHelper.reloadConfigData(ConfigurationHelper.java:102) ~[?:?]
        at pl.minecodes.minespawner.configuration.ConfigurationLoader.reloadConfig(ConfigurationLoader.java:35) ~[?:?]
        at pl.minecodes.minespawner.MineSpawner.loadConfiguration(MineSpawner.java:33) ~[?:?]
        at pl.minecodes.minespawner.MineSpawner.onEnable(MineSpawner.java:41) ~[?:?]

from dazzleconf.

A248 avatar A248 commented on August 21, 2024

Released in 1.2.1

from dazzleconf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.