Add a value to an int in the config.yml file

Discussion in 'Plugin Development' started by pipo3090, Sep 21, 2016.

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

    pipo3090

    Hey guys!
    I'm working on a plugin that does the following: You break a block, and get points from that block.
    When I try to add 2 points to the player's points (in config.yml) when he breaks coal ore, it doesn't add it.

    Code:
    Code:
    package com.tfox.bwp.core.events.block;
    
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    
    import com.tfox.bwp.core.Core;
    
    public class MineForPoints implements Listener {
       
        public Core plugin;
       
        public MineForPoints(Core plugin){
           
            this.plugin = plugin;
           
        }
       
        @EventHandler
        public void OnBlockBreak(BlockBreakEvent e){
           
            Player player = e.getPlayer();
           
            if(player.getGameMode() == GameMode.SURVIVAL){
               
                if(e.getBlock().getType() == Material.COAL_ORE){
                   
                    plugin.getConfig().set("points." + player.getName(), (plugin.getConfig().getInt("points." + player.getName()) + 2));
                   
                    plugin.saveConfig();
                    plugin.reloadConfig();
                   
                }
               
            }
           
        }
       
    }
    I know there's nothing wrong with my main class and my command class (which shows the points).

    If anyone can help, feel free to reply ;)

    Kind regards, Tom
     
  2. Offline

    ArsenArsen

    @pipo3090
    Do you ever register it?
    No need to save and reload.
    Show us the other classes too.
    EDIT: Also use UUIDs please ;)
     
  3. Offline

    pipo3090

    @ArsenArsen I've registered it like this in the main class:
    Code:
    getConfig().options().copyDefaults(true);
            saveConfig();
    this is in onEnable() btw.
    I've also got a player join event, that checks if the player joins for the 1st time, if it does, he gets added to the list:\
    Code:
    plugin.getConfig().set("points." + player.getName(), 0);
               
                plugin.saveConfig();
               
                plugin.reloadConfig();
    why do I have to use uuid's? just a question ;)
     
  4. Offline

    ArsenArsen

    @pipo3090
    Use saveDefaultConfig() and UUIDs are good because names can be changed.
    And I was wondering how do you show it.
    Check the config file just in case
     
  5. Offline

    pipo3090

    @ArsenArsen I've changed every "player.getName()" to "player.getUniqueId()", and changed "saveConfig" to "saveDefaultConfig", but now it doesn't even paste the "points:" in my config file(which represents "points.").
     
  6. Offline

    RenditionsRule

    @pipo3090
    Only put the saveDefaultConfig into your onEnable. Also, if you haven't already, register the listener class in your onEnable too.
     
  7. @pipo3090 You are looking for player.getUniqueId().toString()
     
Thread Status:
Not open for further replies.

Share This Page