[unsolved]What am I doing wrong for the config

Discussion in 'Plugin Development' started by Jogy34, Oct 20, 2011.

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

    Jogy34

    I'm trying to write to the config. After the player joins I want to add their name to a config. What am I doing wrong?

    Main class:
    Code:
    public class PluginMain extends JavaPlugin{
    public static PluginMain plugin;
    
    public static boolean newPlayer = false;
    private static String[] joinedPlayers = new String[500];
    private static boolean addedPlayers = false;
    private static String[] noPlayers = {"NONE","If Players are on they have to rejoin"};
    private final onJoinPlayerListener onJoinplayerListener = new onJoinPlayerListener(this);
    public Logger log = Logger.getLogger("Minecraft");
    @Override
    public void onDisable() {
    this.log.info("plugin disabled");
    }
    private static FileConfiguration config;
    
    @Override
    public void onEnable() {
    config = this.getConfig();
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_LOGIN, this.onJoinplayerListener, Event.Priority.Normal, this);
    for(int i = 0; i < joinedPlayers.length; i++){
    if(joinedPlayers[i] != null){
    addedPlayers = true;
    config.set("Players.Joined Players", Arrays.asList(joinedPlayers));
    }
    }
    if(addedPlayers == false){
    config.set("Players.Joined Players", Arrays.asList(noPlayers));
    }
    saveConfig();
    this.log.info("Plugin Enabled");
                        }
    public static String[] getJoinedPlayers(){
    return joinedPlayers;
    }
    public static void newPlayerAdd(String name){
    int section = -1;
    for(int i = 0; i < joinedPlayers.length; i++){
    if(joinedPlayers[i] == null && section == -1){
    section = i;
    }
    if(section != -1){
    joinedPlayers[section] = name;
    config.set("Players.Joined Players", Arrays.asList(joinedPlayers));
    }else{
    Bukkit.getServer().broadcastMessage("Too many players (over 500) have joined please delete some in the config");
    }
    }
    }
    }
    
    onJoinPlayerListener:
    Code:
    public class onJoinPlayerListener extends PlayerListener{
    public static PluginMain plugin;
    public onJoinPlayerListener(PluginMain instance) {
            plugin = instance;
        }
        private static String[] joinedPlayers = PluginMain.getJoinedPlayers();
        @Override
        public void onPlayerJoin(PlayerJoinEvent event){
        Player player = event.getPlayer();
        String name = player.getName();
        Boolean joined = false;
        int i = 0;
        int section = -1;
        for(i = 0; i < joinedPlayers.length; i++){
        if(joinedPlayers[i].equalsIgnoreCase(name)){
        joined = true;
        }
        if(joined == false){
        for(i = 0; i < joinedPlayers.length; i++){
        if(joinedPlayers[i] == null && section < 0){
        section = i;
        }
        }
        PluginMain.newPlayerAdd(name);
        }
        }
        }
        public static String[] getJoinedPlayer(){
        return joinedPlayers;
        }
        }
    I am not actually making this into a plugin I just want this as a reference for some other plugins that I want to make.

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  2. Offline

    thehutch

    Well no offence but your code is a mess no spaces or indentations its kind of hard to read personally
     
  3. Offline

    Jogy34

    my actual code isn't like that. For whatever reason it copied over from eclipse in that way.
     
  4. Offline

    thehutch

    You could do this:
    Code:
    config.set("playernames." + player);
     
  5. Offline

    Jogy34

    @thehutch there would have to be a value associated with that
     
  6. Offline

    thehutch

    yeah what I type was basically, it would add the player to a list under "Playernames"
     
  7. Offline

    Jogy34

    @thehutch The thing is i want a list of the players names to be the values
     
  8. Offline

    thehutch

    hmm sorry even though I am english I am very bad at it and so have trouble trying to understand what your trying to say. Could you describe your problem in detail or what your plugin is meant to do sorry :(
     
  9. Offline

    Jogy34

    @thehutch Every time a player joins I want their name to be put into a config under 'Joined Players' currently if there is no one in the config one that looks like this is created.
    Code:
    Players:
      Joined Players:
      - NONE
      - If Players are on they have to rejoin
    
    Now say I join I want to send my name to the main class where it then puts it in the config like so
    Code:
    Players:
      Joined Players:
      - Jogy34
    
    It the first config 'NONE' and 'If Players are on they have to rejoin' are stored as strings in an array and then printed out as a list into the config.

    In the second one I would have it so that my name is stored in a separate array and then printed into the config over writing the original list.

    The second part currently isn't working and I'm stumped why.
     
  10. Offline

    thehutch

    So basically you want to add the that list everytime someone joins on the server?
    If so then you would want to either set or addDefault to the config idk because its 1:25 am :(
    Code:
    config.set("Players." + playername);
    
    // or
    
    config.addDefault("Players." + playername)
    
    // also with the default one you might need to
    // do this:
    
    config.options().copyDefault(true);
    
    // idk but try them all out ok
     
  11. Offline

    Jogy34

    that isn't quite what I'm looking for. I want it so that the player names are in a list and that they are put into the config after they log on. The original config making isn't what is wrong. it's adding the players in after the config has been created
     
  12. @Jogy34 : didn't read the whole post and don't want to (as I also have some difficulties with the new config)
    Just to say : try this.
    Code:
    [syntax=java]Your code here[/syntax]
     
  13. Offline

    Sagacious_Zed Bukkit Docs

    getConfig().getList("players").add(player.toName());
    saveConfig()
     
  14. Offline

    Windwaker

    No @Sagacious_Zed

    More like:

    Code:java
    1.  
    2. config.set("Players.Joined Players", Arrays.asList(yourList);
    3.  


    This will produce

    Code:
    Players:
      Joined Players:
      - Jogy34
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    @Walker Crouse
    To prove a point run this. you will see that it writes to a config.yml file properly.

    http://dl.dropbox.com/u/4641522/Bukkit Plugins/SimplyMute-1.0-SNAPSHOT.jar (source)

    Code:
    import java.text.MessageFormat;
    import java.util.ArrayList;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.Event.Type;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class SimplyMute extends JavaPlugin {
    
        public static final String NOT_ALLOWED = "I'm sorry Dave, I'm afraid I can't do that";
    
        public void onDisable() {
            this.getServer().getLogger().info(MessageFormat.format("{0} disabled", this.toString()));
        }
    
        public void onEnable() {
            this.getCommand("mute").setExecutor(new CommandExecutor() {
    
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    if (sender.hasPermission("simplymute.mute")) {
                        if (args.length == 0) {
                            return false;
                        } else {
                            if (getConfig().getList("muted") == null) {
                                getConfig().set("muted", new ArrayList<String>());
                            }
                            getConfig().getList("muted").add(args[0]);
                            saveConfig();
                            return true;
                        }
                    } else {
                        sender.sendMessage(NOT_ALLOWED);
                        return true;
                    }
                }
            });
    
            this.getCommand("unmute").setExecutor(new CommandExecutor() {
    
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    if (sender.hasPermission("simplymute.unmute")) {
                        if (args.length == 0) {
                            return false;
                        } else {
                            if (getConfig().getList("muted") != null) {
                                getConfig().getList("muted").remove(args[0]);
                            }
                            saveConfig();
                            return true;
                        }
                    } else {
                        sender.sendMessage(NOT_ALLOWED);
                        return true;
                    }
                }
            });
    
            this.getServer().getPluginManager().registerEvent(Type.PLAYER_CHAT, new PlayerListener() {
    
                @Override
                public void onPlayerChat(PlayerChatEvent event) {
                    if (getConfig().getList("muted") != null
                            && getConfig().getList("muted").contains(event.getPlayer().getName())) {
                        event.setCancelled(true);
                        event.getPlayer().sendMessage("You have been muted.");
                    }
                }
            }, Priority.Normal, this);
            this.getServer().getLogger().info(MessageFormat.format("{0} enabled", this.toString()));
        }
    }
    Lets put this code under GPLv3
    EDIT: did the compiling, but i can't seem to pack the source with the class files with maven....
     
  16. Offline

    Windwaker

    @Sagacious_Zed
    I'm sorry I didn't mean to sound like I was disagreeing with you, it's simply just not the thing he was looking for and also not the recommended method. (source). It's basically the same thing.
     
  17. Offline

    Sagacious_Zed Bukkit Docs

    @Walker Crouse
    I wrote the article.....

    Besides, why bother creating new list objects, when the one in memory is a working list, and can be readily added to...
    So I think this is exactly what he is looking for. Adding to the list of players in the config.
     
  18. Offline

    thehutch

    hehe @Sagacious_Zed for copying and pasting that mute plugin which you made for me :) obviously I edited it and improved but yh its ok to share to the world :)
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    I really didn't write it for you..... I was doing it to test some parts of the API, specifically the mutability of it. But I will release it as GPLv3 at this point.... So that It can be used as an example.

    BTW, this means your code has to be GPLv3 too....
     
  20. Offline

    thehutch

    Oh I like to use CaptainAwesomes RAKE license :p
     
  21. Offline

    Jogy34

  22. Offline

    Jogy34

    Anyone else have any ideas?
     
  23. Offline

    Sagacious_Zed Bukkit Docs

    @Jogy34
    Try getting rid of every single use of a static variable with a non static one
     
  24. Offline

    Jogy34

    @Sagacious_Zed I don't know whether or not you genuinely think that getting rid of the variables that were static would fix it or if you were just annoyed that I was over using static but, I made every variable that I could non-static (The FileConfiguration config; needed to be static otherwise it would only have worked within onEnable) but I can't test it because my computer is being retarded and not letting me connect to any server, not even my local host server, right now.
     
  25. Offline

    Sagacious_Zed Bukkit Docs

    @Jogy34
    FileConfiguration specifically has to be made non static, since you are having problems with it.... If you have a reference to your plugin's main class you can call getConfig() anytime after your plugin's initialize method has been called.
     
  26. Offline

    Jogy34

    @Sagacious_Zed that could be the problem but again I can't test it:( but ill tell you if it works when I get a chance to

    @Sagacious_Zed i changed the original config to non-static and tried:
    Code:
    FileConfiguration aConfig = plugin.getConfig();
    ...
    aConfig.set("Players.Joined Players", Arrays.asList(joinedPlayers));
    AND
    plugin.getConfig().set("Players.Joined Players", Arrays.asList(joinedPlayers));
    
    neither worked.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  27. Offline

    Sagacious_Zed Bukkit Docs

    @Jogy34 what does your code look like now?

    The only other thing I can think of is the Bukkit and Craftbukkit version that you are using.
     
  28. Offline

    Jogy34

    @Sagacious_Zed This is my current code:

    Main class:
    Code:
    package me.Jogy34.Plugin;
    
    import java.util.Arrays;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Plugin extends JavaPlugin{
    	public static Plugin plugin;
    	private static String[] joinedPlayers = new String[500];
    	private boolean addedPlayers = false;
    	private String[] noPlayers = {"NONE","If Players are on they have to rejoin"};
    	private final onJoinPlayerListener onJoinplayerListener = new onJoinPlayerListener(this);
    	private Logger log = Logger.getLogger("Minecraft");
    	@Override
    	public void onDisable() {
    		this.log.info("Plugin disabled");
    	}
    	private FileConfiguration config;
    
    	@Override
    	public void onEnable() {
    		config = this.getConfig();
    		PluginManager pm = getServer().getPluginManager();
    		pm.registerEvent(Event.Type.PLAYER_LOGIN, this.onJoinplayerListener, Event.Priority.Normal, this);
    		 for(int i = 0; i < joinedPlayers.length; i++){
    			 if(joinedPlayers[i] != null){
    				 addedPlayers = true;
    				 config.set("Players.Joined Players", Arrays.asList(joinedPlayers));
    				 break;
    			 }
    		 }
    		 if(addedPlayers == false){
    			 config.set("Players.Joined Players", Arrays.asList(noPlayers));
    		 }
    		 saveConfig();
    		 this.log.info("Plugin Enabled");
    	                    }
    	public static String[] getJoinedPlayers(){
    		return joinedPlayers;
    	}
    	public static void newPlayerAdd(String name){
    		int section = -1;
    		for(int i = 0; i < joinedPlayers.length; i++){
    			if(joinedPlayers[i] == null && section == -1){
    				section = i;
    			}
    		if(section != -1){
    			FileConfiguration aConfig = plugin.getConfig();
    			joinedPlayers[section] = name;
    			aConfig.set("Players.Joined Players", Arrays.asList(joinedPlayers));
    			plugin.getConfig().set("Players.Joined Players", Arrays.asList(joinedPlayers));
    		}else{
    			Bukkit.getServer().broadcastMessage("Too many players (over 500) have joined please delete some in the config");
    		}
    		}
    	}
    }
    
    onJoinPlayerListener
    Code:
    package me.Jogy34.Plugin;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class onJoinPlayerListener extends PlayerListener{
    	public static Plugin plugin;
        public onJoinPlayerListener(Plugin instance) {
            plugin = instance;
        }
        private static String[] joinedPlayers = Plugin.getJoinedPlayers();
        @Override
        public void onPlayerJoin(PlayerJoinEvent event){
        	Player player = event.getPlayer();
        	String name = player.getName();
        	Boolean joined = false;
        	int i = 0;
        	for(i = 0; i < joinedPlayers.length; i++){
        		if(joinedPlayers[i].equalsIgnoreCase(name)){
        			joined = true;
        		}
        		if(joined == false){
        Plugin.newPlayerAdd(name);
        			}
        		}
        	}
        public static String[] getJoinedPlayer(){
        	return joinedPlayers;
        }
    
    }
    
    
     
  29. Offline

    Jogy34

    Any other ideas?
     
  30. Offline

    Windwaker

    Try this out
     
Thread Status:
Not open for further replies.

Share This Page