Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question