crwdns141063:0crwdne141063:0
Warning
crwdns141065:0crwdne141065:0
crwdns141067:0crwdne141067:0
crwdns141069:0crwdne141069:0
crwdns141071:0:javadoc:crwdnd141071:0:javadoc:crwdne141071:0
crwdns141073:0:javadoc:crwdne141073:0
crwdns141075:0:javadoc:crwdnd141075:0:javadoc:crwdnd141075:0:javadoc:crwdnd141075:0:javadoc:crwdne141075:0
crwdns141077:0crwdne141077:0
crwdns141079:0:doc:crwdnd141079:0:doc:crwdne141079:0
Note
crwdns141081:0crwdne141081:0
crwdns141083:0crwdne141083:0
crwdns141085:0crwdne141085:0
public class DiamondCounter {
private UUID playerUUID;
private int diamonds;
[...]
}
crwdns141087:0crwdne141087:0
crwdns141089:0crwdne141089:0
crwdns141091:0:javadoc:crwdne141091:0
import com.google.common.reflect.TypeToken;
import org.spongepowered.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.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());
}
}
crwdns141093:0:javadoc:crwdnd141093:0:javadoc:crwdne141093:0
Note
crwdns141095:0crwdne141095:0
crwdns141097:0crwdne141097:0
import org.spongepowered.configurate.objectmapping.serialize.TypeSerializers;
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(DiamondCounter.class), new DiamondCounterSerializer());
crwdns141099:0crwdne141099:0
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.objectmapping.serialize.TypeSerializerCollection;
import org.spongepowered.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
crwdns141101:0crwdne141101:0
Tip
crwdns141103:0:javadoc:crwdne141103:0
crwdns141105:0crwdne141105:0
crwdns141107:0:javadoc:crwdnd141107:0:javadoc:crwdne141107:0
import org.spongepowered.configurate.objectmapping.Setting;
import org.spongepowered.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;
[...]
}
crwdns141109:0crwdne141109:0
Tip
crwdns141111:0crwdne141111:0
crwdns141113:0:javadoc:crwdne141113:0
Note
crwdns141115:0crwdne141115:0
crwdns141117:0crwdne141117:0
crwdns141119:0crwdne141119: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();
[...]
}
crwdns141121:0crwdne141121:0
crwdns141123:0crwdne141123: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();
}
crwdns141125:0crwdne141125:0
crwdns141127:0crwdne141127:0
crwdns141129:0crwdne141129:0
crwdns141131:0crwdne141131:0
crwdns141133:0crwdne141133:0
crwdns141135:0crwdne141135:0
Note
crwdns141137:0crwdne141137:0
crwdns141139:0crwdne141139:0
crwdns141141:0crwdne141141: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);
}
crwdns141143:0crwdne141143:0
crwdns141145:0:javadoc:crwdnd141145:0:javadoc:crwdne141145:0
crwdns141147:0:doc:crwdne141147: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 org.spongepowered.configurate.commented.CommentedConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.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
crwdns141149:0crwdne141149:0