9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-29 03:49:07 +00:00

1.3.0-beta-4

This commit is contained in:
Xiao-MoMi
2023-03-09 17:32:46 +08:00
parent 6d9a89e50f
commit bb811513b1
56 changed files with 946 additions and 386 deletions

View File

@@ -0,0 +1,116 @@
package net.momirealms.customfishing.data;
import net.momirealms.customfishing.CustomFishing;
import net.momirealms.customfishing.fishing.loot.Loot;
import net.momirealms.customfishing.manager.LootManager;
import net.momirealms.customfishing.object.action.Action;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class PlayerStatisticsData {
private final ConcurrentHashMap<String, Integer> amountMap;
private final LootManager lootManager;
public PlayerStatisticsData() {
this.amountMap = new ConcurrentHashMap<>();
this.lootManager = CustomFishing.getInstance().getLootManager();
}
public PlayerStatisticsData(ConfigurationSection section) {
this.lootManager = CustomFishing.getInstance().getLootManager();
this.amountMap = new ConcurrentHashMap<>();
for (String key : section.getKeys(false)) {
amountMap.put(key, section.getInt(key));
}
}
public PlayerStatisticsData(String longText) {
this.lootManager = CustomFishing.getInstance().getLootManager();
this.amountMap = (ConcurrentHashMap<String, Integer>) Arrays.stream(longText.split(";"))
.map(element -> element.split(":"))
.filter(pair -> pair.length == 2)
.collect(Collectors.toConcurrentMap(pair -> pair[0], pair -> Integer.parseInt(pair[1])));
}
public String getLongText() {
StringJoiner joiner = new StringJoiner(";");
for (Map.Entry<String, Integer> entry : amountMap.entrySet()) {
joiner.add(entry.getKey() + ":" + entry.getValue());
}
return joiner.toString();
}
public void addFishAmount(Loot loot, int amount, UUID uuid) {
Integer previous = amountMap.get(loot.getKey());
if (previous == null) previous = 0;
int after = previous + amount;
amountMap.put(loot.getKey(), after);
Player player = Bukkit.getPlayer(uuid);
if (player == null) return;
HashMap<Integer, Action[]> actionMap = loot.getSuccessTimesActions();
if (actionMap != null) {
for (Map.Entry<Integer, Action[]> entry : actionMap.entrySet()) {
if (entry.getKey() > previous && entry.getKey() <= after) {
for (Action action : entry.getValue()) {
action.doOn(player, null);
}
}
}
}
}
public int getFishAmount(String key) {
Integer amount = amountMap.get(key);
return amount == null ? 0 : amount;
}
public boolean hasFished(String key) {
return amountMap.containsKey(key);
}
/**
* Get a category's unlock progress
* @param category category name
* @return percent
*/
public double getCategoryUnlockProgress(String category) {
List<String> categories = lootManager.getCategories(category);
if (categories == null) return -1d;
double total = categories.size();
double unlocked = 0;
for (String value : categories) {
if (hasFished(value)) {
unlocked++;
}
}
return (unlocked / total) * 100d;
}
public int getCategoryTotalFishAmount(String category) {
List<String> categories = lootManager.getCategories(category);
if (categories == null) return -1;
int total = 0;
for (String value : categories) {
total += getFishAmount(value);
}
return total;
}
public void reset() {
amountMap.clear();
}
public ConcurrentHashMap<String, Integer> getAmountMap() {
return amountMap;
}
public void setData(String key, int value) {
amountMap.put(key, value);
}
}