209 lines
7.0 KiB
Java
209 lines
7.0 KiB
Java
package com.sakurarealm.playerrisk.api;
|
||
|
||
import com.sakurarealm.playerrisk.PlayerRisk;
|
||
import com.sakurarealm.playerrisk.hook.PlaceHolderAPIHook;
|
||
import org.apache.commons.lang.NotImplementedException;
|
||
import org.bukkit.ChatColor;
|
||
import org.bukkit.OfflinePlayer;
|
||
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
import java.util.UUID;
|
||
import java.util.logging.Logger;
|
||
|
||
/**
|
||
* 危机度区间为0~level_max级,每次离开安全区(城镇),重置危险等级:每待n秒会使危机等级(变量)提升1点,
|
||
* 但开始计算时玩家在的每一个每种生物群系最多使危机等级提升10 (如上限level_max是50的话, 玩家至少进入5个不同的生物群系才能达到上限),
|
||
* 玩家每次死亡重生或回到安全区,会重置当前危机等级。
|
||
* 所有变量见config.yml
|
||
* <a href="https://drawrealm.feishu.cn/wiki/ZqTQwSQNyiZUIWkHCRgcuAcYn3f?chunked=false">...</a>
|
||
*/
|
||
public class PlayerRiskManager {
|
||
|
||
Logger logger = Logger.getLogger("PlayerRisk");
|
||
|
||
private final PlayerRisk plugin;
|
||
|
||
public final PlayerRiskSettings settings;
|
||
|
||
//用来记录在野外下线的玩家的危险度,这部分后续可以改成用数据库记录
|
||
private final Map<String,Map<String,Integer>> OffOnlinePlayerBiomeRisk;
|
||
|
||
//记录玩家的总危险度
|
||
private final Map<String, Integer> playerRiskLevels;
|
||
|
||
//用来记录每个玩家在每个生物群系里面的危险度
|
||
private final Map<String,Map<String,Integer>> playerBiomeRisk;
|
||
|
||
// TODO: Add any data structures needed to manage player risk.
|
||
|
||
public PlayerRiskManager(PlayerRisk plugin, PlayerRiskSettings settings) {
|
||
this.plugin = plugin;
|
||
// Register the event listener.
|
||
// plugin.getServer().getPluginManager().registerEvents(new Listener(), plugin);
|
||
|
||
this.playerRiskLevels = new HashMap<>();
|
||
this.playerBiomeRisk = new HashMap<>();
|
||
this.OffOnlinePlayerBiomeRisk = new HashMap<>();
|
||
|
||
//注入settings
|
||
this.settings = settings;
|
||
new PlaceHolderAPIHook(this).register();
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @param playerName 通过玩家姓名获取玩家总危险度
|
||
* @return
|
||
*/
|
||
public int getPlayerRiskLevel(String playerName) {
|
||
|
||
Integer riskLevel = playerRiskLevels.get(playerName);
|
||
return riskLevel;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @param playerName 玩家姓名,
|
||
* @param biome 生物群系
|
||
* @return 返回该玩家在Biome中的危险度
|
||
*/
|
||
public int getBiomeRiskLevel(String playerName,String biome){
|
||
|
||
Integer level = playerBiomeRisk.get(playerName).get(biome);
|
||
|
||
return level;
|
||
}
|
||
|
||
|
||
/**
|
||
* 修改玩家某个biome的危险度
|
||
* @param playerName
|
||
* @param biome
|
||
*/
|
||
public void setPlayerRiskLevel(String playerName,String biome) {
|
||
|
||
if (playerBiomeRisk.get(playerName).get(biome) == null){
|
||
playerBiomeRisk.get(playerName).put(biome,1);
|
||
logger.info(ChatColor.GOLD+playerName+"进入了新的生物去群系且社危险度为1");
|
||
}else {
|
||
if (playerBiomeRisk.get(playerName).get(biome) <= settings.getLevelMax()/5) {
|
||
playerBiomeRisk.get(playerName).put(biome, playerBiomeRisk.get(playerName).get(biome) + 1);
|
||
}else {
|
||
logger.info(ChatColor.GOLD+playerName+"的"+biome+"的危险度为:"+playerBiomeRisk.get(playerName).get(biome));
|
||
}
|
||
}
|
||
|
||
//修改总群系的危险度
|
||
addAllBiomeRisk(playerName);
|
||
|
||
logger.info(ChatColor.GOLD+playerName+"的"+biome+"的危险度:"+playerBiomeRisk.get(playerName).get(biome));
|
||
}
|
||
|
||
/**
|
||
* 玩家死亡或者进入城镇后,通过玩家姓名将玩家从map里删除
|
||
* @param playerName
|
||
*/
|
||
public void removePlayerLevel(String playerName){
|
||
|
||
playerRiskLevels.remove(playerName);
|
||
playerBiomeRisk.remove(playerName);
|
||
}
|
||
|
||
/**
|
||
* 玩家下线后,通过玩家姓名将玩家判断是删除玩家危险度还是记录
|
||
* @param playerName
|
||
*/
|
||
public void offOnlinePlayer(String playerName){
|
||
|
||
if (playerRiskLevels.get(playerName) != null) {
|
||
|
||
Map<String, Integer> biomeRisks = playerBiomeRisk.get(playerName);
|
||
for (String biome : biomeRisks.keySet()) {
|
||
|
||
logger.info(ChatColor.AQUA+playerName+"保存的"+biome+"群系危险度:"+biomeRisks.get(biome));
|
||
}
|
||
//下线后记录到下线的map中
|
||
OffOnlinePlayerBiomeRisk.put(playerName, biomeRisks);
|
||
|
||
logger.info(ChatColor.GOLD+playerName+"的危险度被记录了");
|
||
}
|
||
|
||
playerRiskLevels.remove(playerName);
|
||
playerBiomeRisk.remove(playerName);
|
||
}
|
||
|
||
|
||
/**
|
||
* 玩家离开城镇,并进入对应的biome后,将玩家放入对应的map中
|
||
* 注意此处,当玩家从1群系到2群系的时候,不能调用此方法修改map
|
||
* @param playerName
|
||
* @param biome
|
||
*/
|
||
public void addPlayerToMap(String playerName,String biome){
|
||
Map<String,Integer> biomeRisk = new HashMap<>();
|
||
biomeRisk.put(biome,0);//从0开始计算
|
||
playerBiomeRisk.put(playerName,biomeRisk);
|
||
|
||
playerRiskLevels.put(playerName,0);
|
||
}
|
||
|
||
|
||
|
||
// TODO Add any other methods needed to manage player risk.
|
||
|
||
/**
|
||
*
|
||
* @param playerName 通过玩家姓名获取修改危险度
|
||
*/
|
||
public void addAllBiomeRisk(String playerName){
|
||
Map<String, Integer> stringIntegerMap = playerBiomeRisk.get(playerName);
|
||
//循环叠加每个群系的危险度
|
||
Integer sumLevel = 0;
|
||
for (String str : stringIntegerMap.keySet()){
|
||
sumLevel += stringIntegerMap.get(str);
|
||
}
|
||
//放入总值
|
||
playerRiskLevels.put(playerName,sumLevel);
|
||
|
||
logger.info(ChatColor.GREEN+playerName+"的总危险度:"+sumLevel);
|
||
|
||
}
|
||
|
||
public Map<String, Map<String, Integer>> getPlayerBiomeRisk() {
|
||
return playerBiomeRisk;
|
||
}
|
||
|
||
/**
|
||
* 判断玩家上下时是否在野外
|
||
* @param name
|
||
*/
|
||
public void playerLoginBoolean(String name) {
|
||
int sumRiskLevel = 0;
|
||
|
||
//不为空则玩家在野外下线
|
||
if (OffOnlinePlayerBiomeRisk.containsKey(name)){
|
||
//获取之前下线前保存的生物群系集合
|
||
Map<String, Integer> biomeRisks = OffOnlinePlayerBiomeRisk.get(name);
|
||
|
||
//把之前保存的生物群系危险度集合放入
|
||
playerBiomeRisk.put(name,biomeRisks);
|
||
|
||
//计算总危险度
|
||
for (String biome : biomeRisks.keySet()){
|
||
logger.info(ChatColor.RED+biome);
|
||
Integer level = biomeRisks.get(biome);
|
||
sumRiskLevel += level;
|
||
logger.info(ChatColor.GREEN+name+"登录后的"+biome+"的危险度"+level);
|
||
}
|
||
|
||
//计入总危险度
|
||
playerRiskLevels.put(name,sumRiskLevel);
|
||
logger.info(ChatColor.AQUA+name+"登录后的总危险度"+sumRiskLevel);
|
||
|
||
//最后删除 OffOnlinePlayerBiomeRisk 中保存的集合
|
||
OffOnlinePlayerBiomeRisk.remove(name);
|
||
}
|
||
}
|
||
}
|