Solved help, my config is unable to update and i dont know what did i do wrong

Discussion in 'Plugin Development' started by yuanjv, Feb 20, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    yuanjv

    I am a noob who started to learn Bukkit.Plugin 3 days ago...

    Code:
    package me.yuanjv.worldsender;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    
    import java.util.*;
    
    public class Recorder implements Listener {
        @EventHandler
        public void changeWorld(PlayerChangedWorldEvent e) {
            FileConfiguration configuration= WorldSender.plugin.getConfig();
            String p = e.getPlayer().getName();
            String w = e.getFrom().getName();
            String[] arr = w.split("_", 0);
            String mainW=arr[0];
            List<String> l;
            String old = null;
            if (configuration.contains(p)){
                l = configuration.getStringList(p);
                for (String name : l) {
                    if (name.equals(mainW) || name.equals(mainW + "_nether") || name.equals(mainW + "_the_end")) {
                        old = name;
                    }
                }
    
                configuration.getStringList(p).remove(old);
                configuration.getStringList(p).add(w);
            }else{
                List<String> newW= new ArrayList<>();
                newW.add(w);
                configuration.addDefault(p,newW);
    
            }
            WorldSender.getPlugin().saveConfig();
        }
    
    }
    I planed to code this plugin for my Multiverse based server for easy world navigation.
    but my only shows this:

    playerName:
    - worldName

    this is my main:
    ........................................................
    Code:
    package me.yuanjv.worldsender;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class WorldSender extends JavaPlugin {
        public static WorldSender plugin;
        @Override
        public void onEnable() {
            plugin=this;
            getConfig().options().copyDefaults(true);
            getServer().getPluginManager().registerEvents(new Recorder(),this);
            getCommand("worldsend").setExecutor(new Command());
            getCommand("delaytp").setExecutor(new tp());
        }
        public static WorldSender getPlugin(){
            return plugin;
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 20, 2022
  2. Offline

    KarimAKL

    You cannot modify #getStringList directly. Instead, you should modify one list, then set the list in the config to the modified version of that list.
    Code:Java
    1. List<String> list = configuration.getStringList("path");
    2.  
    3. list.remove("old");
    4. list.add("new");
    5.  
    6. configuration.set("path", list);


    Also, use #set instead of #addDefault when modifying the section.
     
    yuanjv likes this.
  3. Offline

    yuanjv

    Thank you so much that works.

    if (configuration.contains(p)){
    l = configuration.getStringList(p);
    for (String name : l) {
    if (name.equals(mainW) || name.equals(mainW + "_nether") || name.equals(mainW + "_the_end")) {
    old = name;
    }
    }
    List<String> list = configuration.getStringList(p);

    list.remove(old);
    list.add(w);
    configuration.set(p,list);
    }else{
    List<String> newW= new ArrayList<>();
    newW.add(w);
    configuration.addDefault(p,newW);

    }
    WorldSender.getPlugin().saveConfig();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 21, 2022
Thread Status:
Not open for further replies.

Share This Page