A
A
Aviloo2022-01-07 21:00:38
Java
Aviloo, 2022-01-07 21:00:38

How to save HashMap to file?

Hello, forum users. Please tell me how I could store the values ​​from the HashMap . I just can't deal with it. Thanks in advance!

package rStats.pack.mainlab;
 
 
 
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
 
import net.md_5.bungee.api.ChatColor;
 
public class PlayerExp implements Listener {
    private final JavaPlugin plugin;
    public PlayerExp(JavaPlugin plugin) {
        this.plugin = plugin;
    }                                              // это значение , хочу сохранить 
    private static final ConcurrentHashMap<Player, Integer> playersData = new ConcurrentHashMap<>();
 
    public static int addValue(Player p, int value) {
        playersData.put(p, playersData.getOrDefault(p, 0) + value); 
        return value;
    }
    public static int getValue(Player p) {
        return playersData.getOrDefault(p, 0);
    }
    @EventHandler
    public void BlockBreak(BlockBreakEvent e) {
        Player p = e.getPlayer();
        int plusexp = 10;
        addValue(p, plusexp);
        p.sendMessage("Вам начислено 10 очков");
        int intvalue = getValue(p);
        if(intvalue % 50 == 0) {
            org.bukkit.inventory.ItemStack item1 = new org.bukkit.inventory.ItemStack(org.bukkit.Material.DIAMOND);
            p.getInventory().addItem(item1);
            p.sendMessage("Вы заработали "+ intvalue + " очков опыта! Вам полагается приз.");
            String uuid = p.getUniqueId().toString();
            String playerName = p.getName().toString();
            String path = String.join(".", "users", playerName, uuid,"value");
            plugin.getConfig().set(path, intvalue);
            plugin.saveConfig();
        
        }
        if (intvalue % 50 != 0) {
            return;
        }
    }
    @EventHandler
    public void join(PlayerJoinEvent e) {
        Player p = e.getPlayer();
        int intvalue =  getValue(p);
        p.sendMessage(ChatColor.YELLOW + "Твой уровень равен: " + intvalue ); 
    }
    
 
        
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-01-07
@xez

Serialization and deserialization in Java
You can also serialize to json - you get a string, and save it to a file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question