How do I get this code underneath this?

Discussion in 'Plugin Development' started by AdamDev, Jul 5, 2017.

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

    AdamDev

    For the past couple of day's I've been trying to make a plugin for my server but I'm stuck and can't get down to the lower part. It may not seem like a lot of code, using a testing file atm.
    I've tried the best I can to do this but can't seem to get it.

    Here's my code:
    Code:
    package me.hype.testing;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String l, String[] a) {
            Player p = (Player) sender;
            String newTemp = a[1] + "1";
            String newNewTemp = a[1] + "2";
    
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to execute this command!");
            }
         // START
            if (cmd.getName().equalsIgnoreCase("addsection")) {
                if (a.length == 0) {
                    p.sendMessage(ChatColor.RED + "Error: Missing arguments.");
                    p.sendMessage(ChatColor.RED + "Usage: /addsection section (name).");
                    return false;
                } else if (a.length != 0) {
                    if (a[0].equalsIgnoreCase("section")) {
                        if (a[1] != null) {
                            String temp = a[1];
                            if(getConfig().getString("lobbys." + temp) != null) {
                                getConfig().getConfigurationSection("lobbys").contains(a[1]);
                                getConfig().set("lobbys." + newTemp + ".name", temp);
                                getConfig().set("lobbys." + newTemp + ".world", p.getLocation().getWorld().getName());
                                saveConfig();
                                p.sendMessage(ChatColor.GREEN + "There was already a section with this name so we went ahead and added a number to it!");
    
                            }
                            if (getConfig().getString("lobbys." + newTemp) != null){
                                getConfig().getConfigurationSection("lobbys").contains(a[1]);
                                getConfig().set("lobbys." + newNewTemp + ".name", temp);
                                getConfig().set("lobbys." + newNewTemp + ".world", p.getLocation().getWorld().getName());
                                saveConfig();
                                p.sendMessage(ChatColor.GREEN + "There was already a section with this name so we went ahead and added 1 to it!");
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8&oThis is the last time that we are putting a one in front, please use a new name!")); 
                            }
    // END
                            getConfig().createSection("lobbys");
                            getConfig().set("lobbys." + temp + ".name", temp);
                            getConfig().set("lobbys." + temp + ".world", p.getLocation().getWorld().getName());
                            saveConfig();
                            p.sendMessage(ChatColor.GREEN + "Created Section.");
                            return true;
                            }
                        }
                    }
                }
            return false;
        }
     
    }
    



    The code between the comments is what I'm trying to do. When you do /addsection section (name of what you want it to be) it will return saying that it created it. When you do the command again, it says that it will 'add a number to it'. Let's for say you named it 'bag'. When you do the command with everything the same, it will return 'added a number to it' and update it to the config file. When you try to do it again it gives the same as the first part of adding the number.

    If you want me to show you anything that is in the console, or a certain line I wouldn't mind.

    Thanks

    ~AdamDev
     
  2. Offline

    MrGeneralQ

    This may sound a bit weird, but you don't even have to create the section. It will do it for you. Just put the data in there with 'set' like you did. Only when you are trying to read from the config make sure to check if the actual data exists. In any scenario, you should check if the data exists before actually getting it.
     
  3. Offline

    AdamDev

    @MrGeneralQ
    So would I use something like getconfig().getSting() then?

    So would I have to use getConfig().getString() then?
    (Ignore other post didn't click reply :p)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.

    EDIT: Can I use something with getconfigurationsection()? Or something else?
     
    Last edited: Jul 9, 2017
Thread Status:
Not open for further replies.

Share This Page