crwdns18789:0crwdne18789:0

crwdns42938:0crwdne42938:0

  • crwdns42940:0crwdne42940:0
  • crwdns42942:0:javadoc:crwdnd42942:0:javadoc:crwdne42942:0
  • crwdns42944:0:javadoc:crwdne42944:0
  • crwdns42946:0:javadoc:crwdnd42946:0:javadoc:crwdnd42946:0:javadoc:crwdnd42946:0:javadoc:crwdne42946:0
  • crwdns42948:0:doc:crwdne42948:0
  • crwdns42950:0:doc:crwdnd42950:0:javadoc:crwdnd42950:0:doc:crwdnd42950:0:doc:crwdne42950:0

Note

crwdns42952:0crwdne42952:0

crwdns42954:0crwdne42954:0

crwdns18791:0crwdne18791:0

public class DiamondCounter {
    private UUID playerUUID;
    private int diamonds;

    [...]
}

crwdns18792:0crwdne18792:0

crwdns42956:0crwdne42956:0

crwdns18794:0:javadoc:crwdne18794:0

import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;

public class DiamondCounterSerializer implements TypeSerializer<DiamondCounter> {

    @Override
    public DiamondCounter deserialize(TypeToken<?> type, ConfigurationNode value)
      throws ObjectMappingException {
        UUID player = value.getNode("player").getValue(TypeToken.of(UUID.class));
        int diamonds = value.getNode("diamonds").getInt();
        return new DiamondCounter(player, diamonds);
    }

    @Override
    public void serialize(TypeToken<?> type, DiamondCounter obj, ConfigurationNode value)
      throws ObjectMappingException {
        value.getNode("player").setValue(obj.getPlayerUUID());
        value.getNode("diamonds").setValue(obj.getDiamonds());
    }
}

crwdns18795:0:javadoc:crwdnd18795:0:javadoc:crwdne18795:0

Note

crwdns42958:0crwdne42958:0

crwdns18796:0crwdne18796:0

import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;

TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(DiamondCounter.class), new DiamondCounterSerializer());

crwdns18797:0crwdne18797:0

import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializerCollection;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;

TypeSerializerCollection serializers = TypeSerializers.getDefaultSerializers().newChild();
serializers.registerType(TypeToken.of(DiamondCounter.class), new DiamondCounterSerializer());
ConfigurationOptions options = ConfigurationOptions.defaults().setSerializers(serializers);
ConfigurationNode rootNode = someConfigurationLoader.load(options);

Warning

crwdns18798:0crwdne18798:0

Tip

crwdns42960:0:javadoc:crwdne42960:0

crwdns18799:0crwdne18799:0

crwdns18800:0:javadoc:crwdnd18800:0:javadoc:crwdne18800:0

import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;

@ConfigSerializable
public class DiamondCounter {

    @Setting(value="player", comment="Player UUID")
    private UUID playerUUID;
    @Setting(comment="Number of diamonds mined")
    private int diamonds;

    [...]
}

crwdns18801:0crwdne18801:0

Tip

crwdns18802:0crwdne18802:0

crwdns18803:0:javadoc:crwdne18803:0

Note

crwdns42962:0crwdne42962:0

crwdns42964:0crwdne42964:0

crwdns42966:0crwdne42966:0

@ConfigSerializable
public class DiamondCounter {

    @Setting(value="player", comment="Player UUID")
    private UUID playerUUID;

    @Setting(comment="Number of diamonds mined")
    private int diamonds = 0;

    @Setting(comment="The time the player found a diamond last.")
    private LocalDateTime diamonds = LocalDateTime.now();

    [...]
}

crwdns42968:0crwdne42968:0

crwdns42970:0crwdne42970:0

try {
    this.config = this.configManager.load().<Configuration>getValue(Configuration.TYPE, Configuration::generateDefault);
} catch (ObjectMappingException | IOException e) {
    this.logger.error("Failed to load the config - Using a default", e);
    this.config = Configuration.generateErrorDefault();
}

crwdns42972:0crwdne42972:0

  • crwdns42974:0crwdne42974:0
  • crwdns42976:0crwdne42976:0
  • crwdns42978:0crwdne42978:0
  • crwdns42980:0crwdne42980:0
  • crwdns42982:0crwdne42982:0

Note

crwdns42984:0crwdne42984:0

crwdns42986:0crwdne42986:0

crwdns42988:0crwdne42988:0

try {
    this.configManager.save(this.configManager.createEmptyNode().setValue(Configuration.TYPE, this.config));
} catch (IOException | ObjectMappingException e) {
    this.logger.error("Failed to save the config", e);
}

crwdns18804:0crwdne18804:0

crwdns18805:0:javadoc:crwdnd18805:0:javadoc:crwdne18805:0

crwdns18806:0:doc:crwdne18806:0

import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import com.google.inject.Inject;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.GuiceObjectMapperFactory;

@Plugin(name="IStoleThisFromZml", id="shamelesslystolen", version="0.8.15", description = "Stolen")
public class StolenCodeExample {

    @Inject private GuiceObjectMapperFactory factory;
    @Inject private ConfigurationLoader<CommentedConfigurationNode> loader;

    @Listener
    public void enable(GamePreInitializationEvent event) throws IOException, ObjectMappingException {
        CommentedConfigurationNode node =
          loader.load(ConfigurationOptions.defaults().setObjectMapperFactory(factory));
        DiamondCounter myDiamonds = node.getValue(TypeToken.of(DiamondCounter.class));
    }
}

Note

crwdns18807:0crwdne18807:0