Hi there,
I’m trying to get back a list who is stored into a Hocon config file.
The code looks like this: getConfig().getNode(“MyNode”).getList(TypeToken.of(String.class));
But my IDE returns me an ObjectMappingException.
Can anyone help me?
Hi there,
I’m trying to get back a list who is stored into a Hocon config file.
The code looks like this: getConfig().getNode(“MyNode”).getList(TypeToken.of(String.class));
But my IDE returns me an ObjectMappingException.
Can anyone help me?
Please provide more detail. In a [code] [/code]
block, please paste the stacktrace. Otherwise it’s just guesswork.
[code]@Plugin(id = “net.esperia.RPGChat”, name = “RPGChat”, version = “0.0.1”)
public class RPGChat {
@Inject
@ConfigDir(sharedRoot = false)
private File privateConfigDir;
private ConfigurationLoader<CommentedConfigurationNode> loader;
private ConfigurationNode rootNode;
private List<String> langueadaar = new ArrayList<String>();
private List<String> languehura = new ArrayList<String>();
private List<String> langueqadja = new ArrayList<String>();
@Listener
public void onInitialization(GameInitializationEvent e) throws ObjectMappingException {
File confFile = new File(this.privateConfigDir, "config.yml");
loader = HoconConfigurationLoader.builder().setFile(confFile).build();
CommandSpec hello = CommandSpec.builder()
.description(Text.of("Welcomes the player who performs the command"))
.executor(new HelloCommand())
.build();
Sponge.getGame().getCommandManager().register(this, hello, "hello");
Sponge.getGame().getEventManager().registerListeners(this, new ChatListener());
try {
if (!this.privateConfigDir.isDirectory()) {
this.privateConfigDir.mkdir();
}
if(confFile.isFile()){
confFile.createNewFile();
}
this.rootNode = loader.load();
}catch(IOException ex){
ex.printStackTrace();
}
if(getConfig().getNode("Tests","Langues","Adaars").getValue() != null){
langueadaar = getConfig().getNode("Tests","Langues","Adaars").getList(TypeToken.of(String.class));
}else{
langueadaar.add("JeanMichel");
langueadaar.add("TrucMuche");
}
}
@Listener
public void onDisable(GameStoppingEvent e){
getConfig().getNode("Tests","Langues","Adaars").setValue(langueadaar);
saveConfig();
}
public ConfigurationNode getConfig(){
return rootNode;
}
public void saveConfig(){
try{
loader.save(rootNode);
}catch(IOException e){
e.printStackTrace();
}
}
public List<String> getLangueadaar(){
return langueadaar;
}
public List<String> getLanguehura(){
return languehura;
}
public List<String> getLangueqadja(){
return langueqadja;
}
}[/code]
And i don’t have any stack trace because my IDE (IntelliJ) gives me an error, not a server. The name of the error is “ObjectMappingException”. And if i force compile my plugin, i get a NullPointerException when i use my list…
langueadaar = config.getNode(“Tests”,“Langues”,“Adaars”).getChildrenList().stream().map(ConfigurationNode::getString).collect(Collectors.toList())
Thanks for the help, just a last problem:
When i do ConfigurationNode::getString my IDE says “Method references are not supported at this language level”
Make sure you’re targeting Java 8. In eclipse, you right click on the project -> Properties -> Java Compiler then change it there
It will be similar for Intellij too
I’ve done that, but it’s doesn’t remove the error
Not sure why that won’t work. Make sure you’ve got your IDE using JDK 8.
Of course you can just use a standard for loop
List<? extends CommentedConfigurationNode> nodeList = myNode.getChildrenList();
for (CommentedConfigurationNode node : nodeList) {
String entry = node.getString(); // Do whatever you want with this
}
The key here is getChildrenList()
, it will return a list of all child elements for the config node
Your code is perfectly working! A big thanks to you!
Hi again! I have a new problem with saving the list now… Before it was working but not now, the difference is that my lists are modified in another class. But it’s done correctly and my main class return the good size of the list when debugging.
So, i use this code for saving my lists:
@Listener
public void onDisable(GameStoppingEvent e){
getConfig().getNode("Langues","Adaars").setValue(langueadaar);
getConfig().getNode("Langues","Hura").setValue(languehura);
getConfig().getNode("Langues","Qadja").setValue(langueqadja);
saveConfig();
}
But in my config file, there is no “Langues” node anywhere…
Can you help me?
It could be that the method is not being called because it is not added to the event manager.
Remember, every class that uses @Listener
needs to be registered with the event bus. The main plugin class is automatically added but others are not.
When you construct your other class, make sure to call Sponge.getEventManager().registerListeners(myPlugin, theObject);
where theObject
is the instance of the class that has @Listener
annotations
Sorry! I’ve forgot to precise that this method is in the main
So: The lists and the saving is in the Main class and the editing in other classes with commands
OK never mind then. Can I see the code in the other class? Also, are you sure saveConfig
is defiantly the same as when it was working?
An example of modifying
if(!plugin.getLangueadaar().contains(getRPName(target))){
plugin.getLangueadaar().add(getRPName(target));
p.sendMessage(Text.of(TextColors.GREEN, "Ce joueur peut désormais parler Adaarion"));
}else{
p.sendMessage(Text.of(TextColors.RED,"Ce joueur sait déjà parler cette langue"));
}
The getLangueadaar() method returns the list langueadaar of the main.
The saving method is still the same:
public void saveConfig(){
try{
loader.save(rootNode);
}catch(IOException e){
e.printStackTrace();
}
}
the problem was just that my test server wasn’t stop correctly! It’s now solved! Thanks you!