Config Reading&Writing

Discussion in 'Plugin Development' started by RenditionsRule, Oct 23, 2014.

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

    RenditionsRule

    Hello Bukkit Community. I am having a problem with my plugin using the Configuration File. I am using it to store location data but it doesn't appear to be saving or reading from it correctly. My code may be sloppy but I am not asking to be judged on that aspect so don't bother.

    Class:
    Code:
    package net.renditions;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
        
        ArrayList<Player> inGame = new ArrayList<Player>();
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        
        
        
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            
            if(!(sender instanceof Player)) {
                sender.sendMessage("§cThe console can not perform Roleplaying Commands!");
                return true;
            }
            
            Player player = (Player) sender;
            
            if(cmd.getName().equalsIgnoreCase("roleplay")) {
                
                if(args.length == 0) {
                    player.sendMessage("§1[§3Roleplay§1] §aTPW_RP! Coded by RenditionsRule!");
                    player.sendMessage("§1[§3Roleplay§1] §aHelp Page:");
                    player.sendMessage("§f/roleplay list §e- §3Lists all Roleplays");
                    player.sendMessage("§f/roleplay join <RpName> §e- §3Joins a Roleplay");
                    player.sendMessage("§f/roleplay leave §e- §3Leave your current Roleplay");
                    player.sendMessage(" ");
                    player.sendMessage("§1[§3Roleplay§1] §aAdmin Commands:");
                    player.sendMessage("§f/roleplay create <RpName> §e- §3Create a new Roleplay");
                    player.sendMessage("§f/roleplay setpoint <RpName> §e- §3Set the Tp Point of a Roleplay");
                    player.sendMessage("§f/roleplay enable <RpName> §e- §3Allows users to enter a Roleplay");
                    player.sendMessage("§f/roleplay delete <RpName> §e- §3Completely resets a Roleplay's Data");
                    return true;
                }
                
                if(args[0].equalsIgnoreCase("create") || args[0].equalsIgnoreCase("setpoint") || args[0].equalsIgnoreCase("enable") || args[0].equalsIgnoreCase("delete")) {
                    
                    if(!player.hasPermission("roleplaying.admin")) {
                        player.sendMessage("§1[§3Roleplay§1] §cYou do not have permission!");
                        return true;
                    } else {
                        
                        if(args[0].equalsIgnoreCase("create")) {
                            if(!(args.length == 2)) {
                                player.sendMessage("§1[§3Roleplay§1] §cUsage: /roleplay create <RpName>");
                                return true;
                            } else {
                                if(getConfig().contains("Roleplaying.arenas." + args[1].toLowerCase() + ".enabled")) {
                                    player.sendMessage("§1[§3Roleplay§1] §cThe roleplay known as \"" + args[1] + "\" already exists!");
                                    return true;
                                } else {
                                    getConfig().set("Roleplaying.arenas." + args[1].toLowerCase() + ".enabled", false);
                                    getConfig().set("Roleplaying.listArenas", getConfig().getStringList("Roleplaying.listArenas").add(args[1].toLowerCase()));
                                    player.sendMessage("§1[§3Roleplay§1] §aRoleplay Created! Now use /roleplay setpoint <RpName> to make a teleportation point!");
                                    saveConfig();
                                    return true;
                                }
                            }
                        }
                        if(args[0].equalsIgnoreCase("setpoint")) {
                            if(!(args.length == 2)) {
                                player.sendMessage("§1[§3Roleplay§1] §cUsage: /roleplay setpoint <RpName>");
                                return true;
                            } else {
                                if(getConfig().contains("Roleplaying.arenas." + args[1].toLowerCase() + "enabled")) {
                                    getConfig().set("Roleplaying.arenas." + args[1].toLowerCase() + "x", player.getLocation().getX());
                                    getConfig().set("Roleplaying.arenas." + args[1].toLowerCase() + "y", player.getLocation().getY());
                                    getConfig().set("Roleplaying.arenas." + args[1].toLowerCase() + "z", player.getLocation().getZ());
                                    saveConfig();
                                    player.sendMessage("§1[§3Roleplay§1] §aThe Teleportation Point for \"" + args[1] + "\" has been set!");
                                    
                                    if(getConfig().getBoolean("Roleplaying.arenas." + args[1].toLowerCase() + ".enabled", false)){
                                        
                                        getConfig().set("Roleplaying.arenas." + args[1].toLowerCase() + ".enabled", true);
                                        saveConfig();
                                        player.sendMessage("§1[§3Roleplay§1] §aYou have enabled the Roleplay, \"" + args[1] + "\"!");
                                        return true;
                                    } else {
                                        return true;
                                    }
                                } else {
                                    player.sendMessage("§1[§3Roleplay§1] §cThe Roleplay known as \"" + args[1] + "\" does not exist!");
                                    return true;
                                }
                            }
                        }
                        if(args[0].equalsIgnoreCase("enable")) {
                            if(!(args.length == 2)) {
                                player.sendMessage("§1[§3Roleplay§1] §cUsage: /roleplay enable <RpName>");
                                return true;
                            } else {
                                if(getConfig().contains("Roleplaying.arenas." + args[1] + ".enabled")) {
                                    if(getConfig().getString("Roleplaying.arenas." + args[1] + ".enabled").equalsIgnoreCase("true")) {
                                        player.sendMessage("§1[§3Roleplay§1] §cThis Roleplay is already enabled!");
                                        return true;
                                    } else {
                                        getConfig().set("Roleplaying.arenas." + args[1] + ".enabled", "true");
                                        saveConfig();
                                        player.sendMessage("§1[§3Roleplay§1] §aYou have enabled the Roleplay, \"" + args[1] + "\"!");
                                        return true;
                                    }
                                } else {
                                    player.sendMessage("§1[§3Roleplay§1] §cThe Roleplay known as \"" + args[1] + "\" does not exist!");
                                    return true;
                                }
                            }
                        }
                        if(args[0].equalsIgnoreCase("delete")) {
                            if(!(args.length == 2)) {
                                player.sendMessage("§1[§3Roleplay§1] §cUsage: /roleplay delete <RpName>");
                                return true;
                            } else {
                                if(getConfig().contains("Roleplaying.arenas." + args[1] + ".enabled")) {
                                    getConfig().set("Roleplaying.arenas." + args[1] + "enabled", "false");
                                    getConfig().set("Roleplaying.arenas." + args[1] + "x", "");
                                    getConfig().set("Roleplaying.arenas." + args[1] + "y", "");
                                    getConfig().set("Roleplaying.arenas." + args[1] + "z", "");
                                    saveConfig();
                                    player.sendMessage("§1[§3Roleplay§1] §aThe Roleplay has been reset for future usage!");
                                    return true;
                                } else {
                                    player.sendMessage("§1[§3Roleplay§1] §cThe Roleplay known as \"" + args[1] + "\" does not exist!");
                                    return true;
                                }
                            }
                        }
                        
                    }
                    
                }
                if(args[0].equalsIgnoreCase("join")) {
                    
                    if(!(args.length == 2)) {
                        player.sendMessage("§1[§3Roleplay§1] §cUsage: /roleplay join <RpName>");
                        return true;
                    }
                    
                    if(getConfig().contains("Roleplaying.arenas." + args[1].toLowerCase() + ".enabled")) {
                        if(getConfig().getBoolean("Roleplaying.arenas." + args[1].toLowerCase() + "enabled", true)){
                            player.teleport(new Location(player.getWorld(), getConfig().getDouble("Roleplaying.arenas." + args[1] + "x"), getConfig().getDouble("Roleplaying.arenas." + args[1] + "y"), getConfig().getDouble("Roleplaying.arenas." + args[1] + "z")));
                            inGame.add(player);
                            player.sendMessage("§1[§3Roleplay§1] §aYou have joined the roleplay, \"" + args[1] + "\"!");
                            return true;
                        } else {
                            player.sendMessage("§1[§3Roleplay§1] §cThis roleplay has not been enabled!");
                            return true;
                        }
                    } else {
                        player.sendMessage("§1[§3Roleplay§1] §cThe arena known as \"" + args[1] + "\" does not exist!");
                        return true;
                    }
                    
                }
                if(args[0].equalsIgnoreCase("leave")) {
                    if(!(args.length == 1)) {
                        player.sendMessage("§1[§3Roleplay§1] §cUsage: /roleplay leave");
                        return true;
                    } else {
                        if(inGame.contains(player)) {
                            player.sendMessage("§1[§3Roleplay§1] §aYou have successfully left the Roleplay!");
                            return true;
                        } else {
                            player.sendMessage("§1[§3Roleplay§1] §cYou are not participating in a Roleplay!");
                            return true;
                        }
                    }
                }
                if(args[0].equalsIgnoreCase("list")) {
                    if(!(args.length == 1)) {
                        player.sendMessage("§1[§3Roleplay§1] §cUsage: /roleplay list");
                        return true;
                    } else {
                        player.sendMessage("§1[§3Roleplay§1] §aHere is a list of Roleplays:");
                        for(String roleplay : getConfig().getStringList("Roleplaying.listArenas")) {
                            player.sendMessage("§3- §f" + roleplay);
                        }
                    }
                }
                
            }
            
            // End Command Sequence
            return true;
        }
    }
    
     
  2. Offline

    ProtoTempus

    RenditionsRule I took a quick look but can not find anything out-of-place. Can you explain precise behavior and post any debug output you [hopefully] tried.
     
  3. Offline

    RenditionsRule

    ProtoTempus;

    No Console Output. Absolutely no errors it just doesn't read, write or display anything.

    I use the code to test if the path exists, and if it does, it wont let me create another roleplay with the same name. But it doesn't recognize the path and lets me make another one anyway. It also lets me join when my code should clearly not let you join until it is enabled.
     
  4. Offline

    ProtoTempus

    Alright, so debugging is something you'll do all the time while programming.
    1. Start with one piece of code that needs to work first.
    2. Output using the Bukkit Logger.
    2a. Is the output what you expected? If it is, look later in your code, something else must be breaking it. If not, look up higher, is it being loaded properly? Is the file being generated correctly?
    3. Double check you're implementing the Bukkit Configuration API correctly.

    In short, print debug output, see what is happening in your code while it is running.
     
  5. Offline

    RenditionsRule

    ProtoTempus

    I debugged every last bit of code. I just can't figure out what the problem is. I've been told by a friend to switch to HashMaps for data saving but I don't prefer them, not to mention I'm not experienced.
     
  6. Offline

    ProtoTempus

    RenditionsRule Great, If you've debugged it could you please post some of the debug output? That'll help me see where you are going wrong.

    Edit: Also your config file will help too.
     
  7. Offline

    RenditionsRule

    ProtoTempus

    I debugged it ages ago and removed all traces of the output for personal reasons. From looking at the code, can you see any problems? If you need to, feel free to use/debug it yourself.

    bump?

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

Share This Page