crwdns121607:0crwdne121607:0

crwdns121609:0crwdne121609:0

  • crwdns121611:0crwdne121611:0

  • crwdns121613:0:javadoc:crwdnd121613:0:javadoc:crwdne121613:0

  • crwdns121615:0:javadoc:crwdne121615:0

  • crwdns121617:0:javadoc:crwdnd121617:0:javadoc:crwdnd121617:0:javadoc:crwdnd121617:0:javadoc:crwdne121617:0

  • crwdns121619:0:doc:crwdne121619:0

  • crwdns121621:0:doc:crwdnd121621:0:javadoc:crwdnd121621:0:doc:crwdnd121621:0:doc:crwdne121621:0

Note

crwdns121623:0crwdne121623:0

crwdns121625:0crwdne121625:0

crwdns121627:0crwdne121627:0

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

    [...]
}

crwdns121629:0crwdne121629:0

crwdns121631:0crwdne121631:0

crwdns121633:0:javadoc:crwdne121633: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());
    }
}

crwdns121635:0:javadoc:crwdnd121635:0:javadoc:crwdne121635:0

Note

crwdns121637:0crwdne121637:0

crwdns121639:0crwdne121639:0

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

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

crwdns121641:0crwdne121641: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

crwdns121643:0crwdne121643:0

Tip

crwdns121645:0:javadoc:crwdne121645:0

crwdns121647:0crwdne121647:0

crwdns121649:0:javadoc:crwdnd121649:0:javadoc:crwdne121649: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;

    [...]
}

crwdns121651:0crwdne121651:0

Tip

crwdns121653:0crwdne121653:0

crwdns121655:0:javadoc:crwdne121655:0

Note

crwdns121657:0crwdne121657:0

crwdns121659:0crwdne121659:0

crwdns121661:0crwdne121661: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();

    [...]
}

crwdns121663:0crwdne121663:0

crwdns121665:0crwdne121665: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();
}

crwdns121667:0crwdne121667:0

  • crwdns121669:0crwdne121669:0

  • crwdns121671:0crwdne121671:0

  • crwdns121673:0crwdne121673:0

  • crwdns121675:0crwdne121675:0

  • crwdns121677:0crwdne121677:0

Note

crwdns121679:0crwdne121679:0

crwdns121681:0crwdne121681:0

crwdns121683:0crwdne121683: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);
}

crwdns121685:0crwdne121685:0

crwdns121687:0:javadoc:crwdnd121687:0:javadoc:crwdne121687:0

crwdns121689:0:doc:crwdne121689: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

crwdns121691:0crwdne121691:0