Config not adding

Discussion in 'Plugin Development' started by MrSamuelB, May 1, 2016.

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

    MrSamuelB

    This is not working. In my config, I have it set to 0. It looks like this: count: 0 lower case c on count. It wont let me add 1 everytime the event happens. Pls Help.

    Code:
    package me.MrSamuelB.NahVoid;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        String RED = ChatColor.RED + "";
        String GREEN = ChatColor.GREEN + "";
        String BLUE = ChatColor.BLUE + "";
        String AQUA = ChatColor.AQUA + "";
        String GOLD = ChatColor.GOLD + "";
        String PINK = ChatColor.LIGHT_PURPLE + "";
        String YELLOW = ChatColor.YELLOW + "";
        String BOLD = ChatColor.BOLD + "";
        String UNDERLINE = ChatColor.UNDERLINE + "";
        String PREFIX = GOLD + "[" + AQUA + "GodGold" + GOLD + "] ";
       
        public void onEnable() {
            System.out.println("NahVoid has been Enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            loadConfig();
        }
       
        public void onDisable() {
            System.out.println("NahVoid has been Disabled!");
        }
       
        private void loadConfig() {
            FileConfiguration cfg = this.getConfig();
            cfg.options().copyDefaults(true);
            saveDefaultConfig();
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
            if (commandLabel.equalsIgnoreCase("loc")) {
                if (p.isOp()) {
                    Location loc = p.getLocation();
                    int x = loc.getBlockX();
                    int y = loc.getBlockY();
                    int z = loc.getBlockZ();
                    float pi = loc.getPitch();
                    float ya = loc.getYaw();
                    p.sendMessage(GOLD+"-- Your Location --");
                    p.sendMessage(RED+"X"+GREEN+": "+x+"");
                    p.sendMessage(RED+"Y"+GREEN+": "+y+"");
                    p.sendMessage(RED+"Z"+GREEN+": "+z+"");
                    p.sendMessage(RED+"Pitch"+GREEN+": "+pi+"");
                    p.sendMessage(RED+"Yaw"+GREEN+": "+ya+"");
                } else {
                    p.sendMessage(RED+"No.");
                }
            } else if (commandLabel.equalsIgnoreCase("count")) {
                int c = getConfig().getInt("count");
                p.sendMessage(c+"");
            }
            return false;
        }
    
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if (p.getWorld().getName().equals("world")) {
                if (p.getLocation().getBlockY()<20) {
                    Location loc = new Location(p.getWorld(), 0.5, 61, 0.5, 0, 0);
                    p.teleport(loc);
                    p.setFallDistance(0F);
                    int c = getConfig().getInt("count");
                    c++;
                }
            }
        }
    }
     
  2. Offline

    pingpong1999

    you never save the config, you are just setting the int which retrieves the config.
     
  3. Offline

    Lordloss

    Code:
    System.out.println("NahVoid has been Disabled!");
    You dont need to log your enable/disable, bukkit does that for you, so you dont need your onDisable at all.

    Code:
    saveDefaultConfig();
    AFAIK you should just do that if you know that the config doesnt exist.

    Code:
    Player p = (Player) sender;
    Never blind cast!

    Code:
     if (commandLabel.equalsIgnoreCase("loc")) {
    you should check the command and not the label.
     
  4. Offline

    I Al Istannen

    @Lordloss
    The Javadoc for saveDefaultConfig():
    So it shouldn't matter if you call it everytime. In fact, from my experience it really doesn't.

    @MrSamuelB
    I would make my class variables private if they are not used anywhere else. Similary, if you writem them in Uppercase you imply they are constants. Add the final keyword to them to ensure that.

    Second, you don't need them. If ChatColor.RED is too long, add this as the import:
    "import static org.bukkit.ChatColor.RED;"
    You can replace the RED with a * if you want to import all and are too lazy to write them. Eclipse should auto correct that though, if you press "CTRL+SHIFT+O", I think. Just make sure you don't do a static import too often, it makes it harder to read.
    If you want to get them as Strings, just append ".toString()" after the color.

    I would return true at the end of the onCommand, as the command was processed correctly. Returning false sends the usage message defined in the plugin.yml.

    Now to your problem:
    Code:
    int c = getConfig().getInt("count");
    c++;
    This means:
    Copy the value of getConfig().getInt("count"); in the variable c.
    Increase the variable c by one. Now c is one higher, but as it is a COPY (primiteves are always a copy), the config won't get updated.
    You need to call getConfig().set("count", c); afterwards to update the config.

    The first poster told you to save the config, but depending on what he meant it might not be a good idea. Writing a file to disk is quite costly time wise and doing this every moveevent will lag the server more than necessary. You will need to save the config in the onDisable() or periodically though. Setting it with set(String, Object) is fast, but won't save it to disk. Use config.save(File) for that. In case of the main config you can also use JavaPlugin.saveConfig() for that.

    I might have skipped something, so please add if you noticed some more things. Do also scream, if I made a mistake :) Always appreciate that.
     
  5. Offline

    MrSamuelB

    @I Al Istannen

    I changed my event handler to this but it is still not working.
    Code:
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if (p.getWorld().getName().equals("world")) {
                if (p.getLocation().getBlockY()<20) {
                    Location loc = new Location(p.getWorld(), 0.5, 61, 0.5, 0, 0);
                    p.teleport(loc);
                    p.setFallDistance(0F);
                    int c = getConfig().getInt("count");
                    getConfig().set("count", c++);
                    saveConfig();
                }
            }
        }
    EDIT: I changed the c++ to c+1 and it seemed to work out. Thanks for the help everyone
     
  6. Offline

    I Al Istannen

    @MrSamuelB
    Yes, ++c should habe worked too. The c++ returns c, evaluates the expression and then adds one to c. The ++c first adds one and then returns the value. General rule of thumb is to only use them in for loops vor if they are the only expression in the line. It gets really confusign otherwise. You solution is fine too, oft course. Just wanted to mention that. You Gould Google " Java i++ vs ++i" vor something Mike this, i am on my phone. Nice it worked :)
     
Thread Status:
Not open for further replies.

Share This Page