first commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package de.winniepat.fallSMPRewards;
|
||||
|
||||
import de.winniepat.fallSMPRewards.command.RewardsCommand;
|
||||
import de.winniepat.fallSMPRewards.gui.RewardsGui;
|
||||
import de.winniepat.fallSMPRewards.listener.QuestListener;
|
||||
import de.winniepat.fallSMPRewards.service.QuestService;
|
||||
import de.winniepat.fallSMPRewards.storage.PlayerDataStore;
|
||||
import java.util.Objects;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class FallSMPRewards extends JavaPlugin {
|
||||
|
||||
private QuestService questService;
|
||||
private PlayerDataStore playerDataStore;
|
||||
private RewardsGui rewardsGui;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
|
||||
questService = new QuestService(this);
|
||||
questService.reload();
|
||||
|
||||
playerDataStore = new PlayerDataStore(this);
|
||||
rewardsGui = new RewardsGui(this, questService, playerDataStore);
|
||||
|
||||
getServer().getPluginManager().registerEvents(new QuestListener(this, questService, playerDataStore, rewardsGui), this);
|
||||
Objects.requireNonNull(getCommand("rewards"), "rewards command missing in plugin.yml").setExecutor(new RewardsCommand(this, rewardsGui, questService));
|
||||
|
||||
getLogger().info("Enabled with " + questService.getQuests().size() + " kill quests.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if (playerDataStore != null) {
|
||||
playerDataStore.flush();
|
||||
}
|
||||
getLogger().info("Disabled");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package de.winniepat.fallSMPRewards.command;
|
||||
|
||||
import de.winniepat.fallSMPRewards.FallSMPRewards;
|
||||
import de.winniepat.fallSMPRewards.gui.RewardsGui;
|
||||
import de.winniepat.fallSMPRewards.service.QuestService;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public final class RewardsCommand implements CommandExecutor {
|
||||
|
||||
private static final String USE_PERMISSION = "fallsmprewards.use";
|
||||
private static final String RELOAD_PERMISSION = "fallsmprewards.reload";
|
||||
|
||||
private final FallSMPRewards plugin;
|
||||
private final RewardsGui rewardsGui;
|
||||
private final QuestService questService;
|
||||
|
||||
public RewardsCommand(FallSMPRewards plugin, RewardsGui rewardsGui, QuestService questService) {
|
||||
this.plugin = plugin;
|
||||
this.rewardsGui = rewardsGui;
|
||||
this.questService = questService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (args.length > 0) {
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
if (!sender.hasPermission(RELOAD_PERMISSION)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to reload this plugin.");
|
||||
return true;
|
||||
}
|
||||
|
||||
plugin.reloadConfig();
|
||||
questService.reload();
|
||||
sender.sendMessage(ChatColor.GREEN + "FallSMP-Rewards config reloaded.");
|
||||
return true;
|
||||
}
|
||||
|
||||
sender.sendMessage(ChatColor.RED + "Unknown subcommand. Use /" + label + " or /" + label + " reload");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage(ChatColor.RED + "Only players can open the rewards GUI. Use /" + label + " reload from console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!sender.hasPermission(USE_PERMISSION)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
rewardsGui.open(player);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package de.winniepat.fallSMPRewards.gui;
|
||||
|
||||
import de.winniepat.fallSMPRewards.FallSMPRewards;
|
||||
import de.winniepat.fallSMPRewards.model.PlayerData;
|
||||
import de.winniepat.fallSMPRewards.model.QuestDefinition;
|
||||
import de.winniepat.fallSMPRewards.service.QuestService;
|
||||
import de.winniepat.fallSMPRewards.storage.PlayerDataStore;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
public final class RewardsGui {
|
||||
|
||||
private final FallSMPRewards plugin;
|
||||
private final QuestService questService;
|
||||
private final PlayerDataStore playerDataStore;
|
||||
|
||||
public RewardsGui(FallSMPRewards plugin, QuestService questService, PlayerDataStore playerDataStore) {
|
||||
this.plugin = plugin;
|
||||
this.questService = questService;
|
||||
this.playerDataStore = playerDataStore;
|
||||
}
|
||||
|
||||
public void open(Player player) {
|
||||
Inventory inventory = Bukkit.createInventory(null, 9, getTitle());
|
||||
PlayerData data = playerDataStore.get(player.getUniqueId());
|
||||
List<QuestDefinition> quests = questService.getQuests();
|
||||
|
||||
for (int slot = 0; slot < Math.min(9, quests.size()); slot++) {
|
||||
QuestDefinition quest = quests.get(slot);
|
||||
inventory.setItem(slot, buildQuestIcon(quest, data));
|
||||
}
|
||||
|
||||
player.openInventory(inventory);
|
||||
player.playSound(player.getLocation(), resolveSound("sounds.open", "block.chest.open"), 1f, 1f);
|
||||
}
|
||||
|
||||
public boolean isRewardsInventoryTitle(String title) {
|
||||
return getTitle().equals(title);
|
||||
}
|
||||
|
||||
public boolean claim(Player player, int slot) {
|
||||
List<QuestDefinition> quests = questService.getQuests();
|
||||
if (slot < 0 || slot >= quests.size() || slot >= 9) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QuestDefinition quest = quests.get(slot);
|
||||
PlayerData data = playerDataStore.get(player.getUniqueId());
|
||||
if (data.hasClaimed(quest.getId()) || data.getTotalKills() < quest.getRequiredKills()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!playerDataStore.claim(player.getUniqueId(), quest.getId())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (ItemStack reward : quest.getRewards()) {
|
||||
player.getInventory().addItem(reward).values().forEach(leftover ->
|
||||
player.getWorld().dropItemNaturally(player.getLocation(), leftover));
|
||||
}
|
||||
|
||||
player.sendMessage(color("&aQuest reward claimed: &f" + quest.getTitle()));
|
||||
player.playSound(player.getLocation(), resolveSound("sounds.claim", "entity.player.levelup"), 1f, 1.1f);
|
||||
open(player);
|
||||
return true;
|
||||
}
|
||||
|
||||
private ItemStack buildQuestIcon(QuestDefinition quest, PlayerData data) {
|
||||
boolean claimed = data.hasClaimed(quest.getId());
|
||||
boolean claimable = !claimed && data.getTotalKills() >= quest.getRequiredKills();
|
||||
|
||||
Material material = claimable ? Material.CHEST_MINECART : Material.MINECART;
|
||||
if (claimed) {
|
||||
material = Material.BARRIER;
|
||||
}
|
||||
|
||||
ItemStack item = new ItemStack(material);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) {
|
||||
return item;
|
||||
}
|
||||
|
||||
meta.setDisplayName(quest.getTitle());
|
||||
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.addAll(quest.getDescription());
|
||||
lore.add("");
|
||||
lore.add(color("&7Progress: &f" + Math.min(data.getTotalKills(), quest.getRequiredKills()) + "&7/&f" + quest.getRequiredKills()));
|
||||
lore.add(color("&7Reward:"));
|
||||
for (ItemStack reward : quest.getRewards()) {
|
||||
lore.add(color("&f- " + reward.getAmount() + "x " + prettify(reward.getType().name())));
|
||||
}
|
||||
lore.add("");
|
||||
|
||||
if (claimed) {
|
||||
lore.add(color("&aAlready claimed"));
|
||||
} else if (claimable) {
|
||||
lore.add(color("&eClick to claim!"));
|
||||
} else {
|
||||
lore.add(color("&cNot claimable yet"));
|
||||
}
|
||||
|
||||
meta.setLore(lore);
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private String prettify(String text) {
|
||||
String lower = text.toLowerCase().replace('_', ' ');
|
||||
String[] parts = lower.split(" ");
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String part : parts) {
|
||||
if (part.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (!builder.isEmpty()) {
|
||||
builder.append(' ');
|
||||
}
|
||||
builder.append(Character.toUpperCase(part.charAt(0))).append(part.substring(1));
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String getTitle() {
|
||||
return color(plugin.getConfig().getString("gui.title", "&6Kill Rewards"));
|
||||
}
|
||||
|
||||
private String resolveSound(String path, String fallback) {
|
||||
String configured = plugin.getConfig().getString(path, fallback);
|
||||
if (configured == null || configured.isBlank()) {
|
||||
return fallback;
|
||||
}
|
||||
String normalized = configured.trim().toLowerCase();
|
||||
// Support old enum-style names like BLOCK_CHEST_OPEN.
|
||||
if (!normalized.contains(".")) {
|
||||
normalized = normalized.replace('_', '.');
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private String color(String input) {
|
||||
return ChatColor.translateAlternateColorCodes('&', input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package de.winniepat.fallSMPRewards.listener;
|
||||
|
||||
import de.winniepat.fallSMPRewards.FallSMPRewards;
|
||||
import de.winniepat.fallSMPRewards.gui.RewardsGui;
|
||||
import de.winniepat.fallSMPRewards.model.PlayerData;
|
||||
import de.winniepat.fallSMPRewards.model.QuestDefinition;
|
||||
import de.winniepat.fallSMPRewards.service.QuestService;
|
||||
import de.winniepat.fallSMPRewards.storage.PlayerDataStore;
|
||||
import de.winniepat.fallSMPRewards.storage.PlayerDataStore.KillAddResult;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
|
||||
public final class QuestListener implements Listener {
|
||||
|
||||
private final FallSMPRewards plugin;
|
||||
private final QuestService questService;
|
||||
private final PlayerDataStore playerDataStore;
|
||||
private final RewardsGui rewardsGui;
|
||||
|
||||
public QuestListener(FallSMPRewards plugin, QuestService questService, PlayerDataStore playerDataStore, RewardsGui rewardsGui) {
|
||||
this.plugin = plugin;
|
||||
this.questService = questService;
|
||||
this.playerDataStore = playerDataStore;
|
||||
this.rewardsGui = rewardsGui;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDeath(PlayerDeathEvent event) {
|
||||
Player victim = event.getEntity();
|
||||
Player killer = victim.getKiller();
|
||||
if (killer == null || killer.getUniqueId().equals(victim.getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
long cooldownMillis = getCooldownMillis();
|
||||
KillAddResult killResult = playerDataStore.addKillWithCooldown(killer.getUniqueId(), victim.getUniqueId(), cooldownMillis);
|
||||
if (!killResult.counted()) {
|
||||
killer.sendMessage(color("&cThis kill does not count for rewards. Kill this player again in &f" + formatDuration(killResult.remainingCooldownMillis()) + "&c."));
|
||||
killer.playSound(killer.getLocation(), resolveSound("sounds.denied", "block.note_block.bass"), 1f, 1f);
|
||||
return;
|
||||
}
|
||||
|
||||
int totalKills = killResult.totalKills();
|
||||
PlayerData data = playerDataStore.get(killer.getUniqueId());
|
||||
boolean unlocked = false;
|
||||
for (QuestDefinition quest : questService.getQuests()) {
|
||||
if (!data.hasClaimed(quest.getId()) && totalKills >= quest.getRequiredKills() && totalKills - 1 < quest.getRequiredKills()) {
|
||||
killer.sendMessage(color("&6Quest ready to claim: &f" + quest.getTitle()));
|
||||
unlocked = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (unlocked) {
|
||||
killer.playSound(killer.getLocation(), resolveSound("sounds.unlocked", "ui.toast.challenge_complete"), 1f, 1f);
|
||||
} else {
|
||||
killer.playSound(killer.getLocation(), resolveSound("sounds.progress", "entity.experience_orb.pickup"), 0.6f, 1.2f);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
if (!rewardsGui.isRewardsInventoryTitle(event.getView().getTitle())) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
if (!(event.getWhoClicked() instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getClickedInventory() == null || event.getRawSlot() < 0 || event.getRawSlot() >= 9) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean claimed = rewardsGui.claim(player, event.getRawSlot());
|
||||
if (!claimed) {
|
||||
player.playSound(player.getLocation(), resolveSound("sounds.denied", "block.note_block.bass"), 1f, 1f);
|
||||
}
|
||||
}
|
||||
|
||||
private long getCooldownMillis() {
|
||||
long cooldownMinutes = Math.max(1L, plugin.getConfig().getLong("kill-count-cooldown-minutes", 10L));
|
||||
return cooldownMinutes * 60_000L;
|
||||
}
|
||||
|
||||
private String formatDuration(long remainingMillis) {
|
||||
long totalSeconds = Math.max(1L, (remainingMillis + 999L) / 1000L);
|
||||
long minutes = totalSeconds / 60L;
|
||||
long seconds = totalSeconds % 60L;
|
||||
if (minutes == 0L) {
|
||||
return seconds + "s";
|
||||
}
|
||||
if (seconds == 0L) {
|
||||
return minutes + "m";
|
||||
}
|
||||
return minutes + "m " + seconds + "s";
|
||||
}
|
||||
|
||||
private String resolveSound(String path, String fallback) {
|
||||
String configured = plugin.getConfig().getString(path, fallback);
|
||||
if (configured == null || configured.isBlank()) {
|
||||
return fallback;
|
||||
}
|
||||
String normalized = configured.trim().toLowerCase();
|
||||
if (!normalized.contains(".")) {
|
||||
normalized = normalized.replace('_', '.');
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private String color(String input) {
|
||||
return ChatColor.translateAlternateColorCodes('&', input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package de.winniepat.fallSMPRewards.model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class PlayerData {
|
||||
|
||||
private UUID uuid;
|
||||
private int totalKills;
|
||||
private Set<String> claimedQuestIds;
|
||||
private Map<String, Long> lastCountedKillByVictim;
|
||||
|
||||
public PlayerData() {
|
||||
// Required for Gson.
|
||||
}
|
||||
|
||||
public PlayerData(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
this.totalKills = 0;
|
||||
this.claimedQuestIds = new HashSet<>();
|
||||
this.lastCountedKillByVictim = new HashMap<>();
|
||||
}
|
||||
|
||||
public void sanitize(UUID expectedUuid) {
|
||||
if (uuid == null) {
|
||||
uuid = expectedUuid;
|
||||
}
|
||||
if (claimedQuestIds == null) {
|
||||
claimedQuestIds = new HashSet<>();
|
||||
}
|
||||
if (lastCountedKillByVictim == null) {
|
||||
lastCountedKillByVictim = new HashMap<>();
|
||||
}
|
||||
if (totalKills < 0) {
|
||||
totalKills = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public int getTotalKills() {
|
||||
return totalKills;
|
||||
}
|
||||
|
||||
public int addKill() {
|
||||
totalKills++;
|
||||
return totalKills;
|
||||
}
|
||||
|
||||
public long getRemainingCooldownMillis(UUID victimUuid, long cooldownMillis, long nowMillis) {
|
||||
Long lastCountedMillis = lastCountedKillByVictim.get(victimUuid.toString());
|
||||
if (lastCountedMillis == null) {
|
||||
return 0L;
|
||||
}
|
||||
long elapsed = nowMillis - lastCountedMillis;
|
||||
if (elapsed >= cooldownMillis) {
|
||||
return 0L;
|
||||
}
|
||||
return cooldownMillis - elapsed;
|
||||
}
|
||||
|
||||
public void setLastCountedKill(UUID victimUuid, long nowMillis) {
|
||||
lastCountedKillByVictim.put(victimUuid.toString(), nowMillis);
|
||||
}
|
||||
|
||||
public boolean hasClaimed(String questId) {
|
||||
return claimedQuestIds.contains(questId);
|
||||
}
|
||||
|
||||
public boolean claim(String questId) {
|
||||
return claimedQuestIds.add(questId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package de.winniepat.fallSMPRewards.model;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public final class QuestDefinition {
|
||||
|
||||
private final String id;
|
||||
private final String title;
|
||||
private final List<String> description;
|
||||
private final int requiredKills;
|
||||
private final List<ItemStack> rewards;
|
||||
|
||||
public QuestDefinition(String id, String title, List<String> description, int requiredKills, List<ItemStack> rewards) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.description = List.copyOf(description);
|
||||
this.requiredKills = requiredKills;
|
||||
this.rewards = rewards.stream().map(ItemStack::clone).toList();
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public List<String> getDescription() {
|
||||
return Collections.unmodifiableList(description);
|
||||
}
|
||||
|
||||
public int getRequiredKills() {
|
||||
return requiredKills;
|
||||
}
|
||||
|
||||
public List<ItemStack> getRewards() {
|
||||
return rewards.stream().map(ItemStack::clone).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package de.winniepat.fallSMPRewards.service;
|
||||
|
||||
import de.winniepat.fallSMPRewards.FallSMPRewards;
|
||||
import de.winniepat.fallSMPRewards.model.QuestDefinition;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public final class QuestService {
|
||||
|
||||
private final FallSMPRewards plugin;
|
||||
private final List<QuestDefinition> quests = new ArrayList<>();
|
||||
|
||||
public QuestService(FallSMPRewards plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
plugin.reloadConfig();
|
||||
quests.clear();
|
||||
|
||||
ConfigurationSection questSection = plugin.getConfig().getConfigurationSection("quests");
|
||||
if (questSection == null) {
|
||||
plugin.getLogger().warning("No quests section found in config.yml");
|
||||
return;
|
||||
}
|
||||
|
||||
for (String questId : questSection.getKeys(false)) {
|
||||
ConfigurationSection section = questSection.getConfigurationSection(questId);
|
||||
if (section == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int requiredKills = Math.max(1, section.getInt("required-kills", 1));
|
||||
String title = color(section.getString("title", "&e" + questId));
|
||||
List<String> description = section.getStringList("description").stream().map(this::color).toList();
|
||||
List<ItemStack> rewards = parseRewards(section.getStringList("rewards"));
|
||||
|
||||
if (rewards.isEmpty()) {
|
||||
plugin.getLogger().warning("Quest " + questId + " has no valid rewards and was skipped.");
|
||||
continue;
|
||||
}
|
||||
|
||||
quests.add(new QuestDefinition(questId, title, description, requiredKills, rewards));
|
||||
}
|
||||
|
||||
if (quests.size() > 9) {
|
||||
plugin.getLogger().warning("More than 9 quests configured. GUI shows only the first 9 quests.");
|
||||
}
|
||||
}
|
||||
|
||||
public List<QuestDefinition> getQuests() {
|
||||
return Collections.unmodifiableList(quests);
|
||||
}
|
||||
|
||||
private List<ItemStack> parseRewards(List<String> rawRewards) {
|
||||
List<ItemStack> rewards = new ArrayList<>();
|
||||
for (String raw : rawRewards) {
|
||||
String[] parts = raw.split(":", 2);
|
||||
Material material = Material.matchMaterial(parts[0].trim().toUpperCase());
|
||||
if (material == null || material.isAir()) {
|
||||
plugin.getLogger().warning("Invalid reward material: " + raw);
|
||||
continue;
|
||||
}
|
||||
|
||||
int amount = 1;
|
||||
if (parts.length > 1) {
|
||||
try {
|
||||
amount = Math.max(1, Integer.parseInt(parts[1].trim()));
|
||||
} catch (NumberFormatException ex) {
|
||||
plugin.getLogger().warning("Invalid reward amount: " + raw);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
rewards.add(new ItemStack(material, amount));
|
||||
}
|
||||
return rewards;
|
||||
}
|
||||
|
||||
private String color(String input) {
|
||||
return ChatColor.translateAlternateColorCodes('&', input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package de.winniepat.fallSMPRewards.storage;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import de.winniepat.fallSMPRewards.FallSMPRewards;
|
||||
import de.winniepat.fallSMPRewards.model.PlayerData;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class PlayerDataStore {
|
||||
|
||||
public record KillAddResult(boolean counted, int totalKills, long remainingCooldownMillis) {
|
||||
}
|
||||
|
||||
private final FallSMPRewards plugin;
|
||||
private final Gson gson;
|
||||
private final Path dataDirectory;
|
||||
private final Map<UUID, PlayerData> cache = new HashMap<>();
|
||||
|
||||
public PlayerDataStore(FallSMPRewards plugin) {
|
||||
this.plugin = plugin;
|
||||
this.gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
this.dataDirectory = plugin.getDataFolder().toPath().resolve("playerdata");
|
||||
|
||||
try {
|
||||
Files.createDirectories(dataDirectory);
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException("Could not create player data directory", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerData get(UUID uuid) {
|
||||
return cache.computeIfAbsent(uuid, this::load);
|
||||
}
|
||||
|
||||
public KillAddResult addKillWithCooldown(UUID killerUuid, UUID victimUuid, long cooldownMillis) {
|
||||
PlayerData data = get(killerUuid);
|
||||
long nowMillis = System.currentTimeMillis();
|
||||
long remainingCooldownMillis = data.getRemainingCooldownMillis(victimUuid, cooldownMillis, nowMillis);
|
||||
if (remainingCooldownMillis > 0L) {
|
||||
return new KillAddResult(false, data.getTotalKills(), remainingCooldownMillis);
|
||||
}
|
||||
|
||||
data.setLastCountedKill(victimUuid, nowMillis);
|
||||
int totalKills = data.addKill();
|
||||
save(killerUuid, data);
|
||||
return new KillAddResult(true, totalKills, 0L);
|
||||
}
|
||||
|
||||
public boolean claim(UUID uuid, String questId) {
|
||||
PlayerData data = get(uuid);
|
||||
boolean claimed = data.claim(questId);
|
||||
if (claimed) {
|
||||
save(uuid, data);
|
||||
}
|
||||
return claimed;
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
for (Map.Entry<UUID, PlayerData> entry : cache.entrySet()) {
|
||||
save(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerData load(UUID uuid) {
|
||||
Path file = getPath(uuid);
|
||||
if (!Files.exists(file)) {
|
||||
return new PlayerData(uuid);
|
||||
}
|
||||
|
||||
try {
|
||||
String json = Files.readString(file);
|
||||
PlayerData data = gson.fromJson(json, PlayerData.class);
|
||||
if (data == null) {
|
||||
return new PlayerData(uuid);
|
||||
}
|
||||
data.sanitize(uuid);
|
||||
return data;
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().warning("Failed to read player data for " + uuid + ": " + ex.getMessage());
|
||||
return new PlayerData(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
private void save(UUID uuid, PlayerData data) {
|
||||
try {
|
||||
Files.writeString(getPath(uuid), gson.toJson(data));
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().warning("Failed to save player data for " + uuid + ": " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private Path getPath(UUID uuid) {
|
||||
return dataDirectory.resolve(uuid + ".json");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user