本地测试完毕,日志没删掉,可以在远程服务器里测试看看有没有什么bug,另外有需要的话可以把下线储存的玩家危险度存到数据库中。

This commit is contained in:
yuyu 2024-03-17 14:40:27 +08:00
parent 76f5c3f9a5
commit 5e42968702
3 changed files with 129 additions and 18 deletions

View File

@ -3,7 +3,7 @@ plugins {
}
group = 'com.sakurarealm'
version = '1.0'
version = '1.1'
repositories {
mavenCentral()

View File

@ -4,15 +4,17 @@ import com.palmergames.bukkit.towny.event.PlayerEnterTownEvent;
import com.palmergames.bukkit.towny.event.PlayerLeaveTownEvent;
import com.sakurarealm.playerrisk.api.PlayerRiskManager;
import com.sakurarealm.playerrisk.api.PlayerRiskSettings;
import org.apache.commons.lang.NotImplementedException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
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.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
@ -29,9 +31,12 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
}
/**
* 返回getPlayerRiskManager用于调用危险度
*/
public PlayerRiskManager getPlayerRiskManager() {
if (playerRiskManager == null) {
throw new NullPointerException("PlayerRisk plugin is not initialized.");
throw new NullPointerException(ChatColor.RED + "PlayerRisk plugin is not initialized.");
}
return playerRiskManager;
}
@ -51,26 +56,45 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
//注册监听器
Bukkit.getPluginManager().registerEvents(this,this);
getLogger().info(ChatColor.RED+"PlayerRisks插件开始运行");
getServer().getScheduler().scheduleSyncRepeatingTask(this, this::addRiskToOutTownPlayers,
0, playerRiskManager.settings.getIncreaseInterval()*20);
}
@Override
public void onDisable() {
// Plugin shutdown logic
getLogger().info(ChatColor.GREEN+"PlayerRisk插件关闭");
}
/**
* 玩家退出游戏后从map集合中移出
* @param event
*/
@EventHandler
public void playerOffOnline(PlayerQuitEvent event){
Player player = event.getPlayer();
playerRiskManager.removePlayerLevel(player.getName());
String name = player.getName();
playerRiskManager.offOnlinePlayer(name);
getLogger().info(ChatColor.GREEN+player.getName()+"退出了游戏");
}
/**
* 当玩家登录时,判断玩家是否在野外群落中
* @param event
*/
@EventHandler
public void onPlayerLogin(PlayerJoinEvent event){
Player player = event.getPlayer();
String name = player.getName();
getLogger().info(ChatColor.AQUA+name+"进入了游戏");
playerRiskManager.playerLoginBoolean(name);
}
/**
* TODO: 当玩家进入城镇时, 危险度不再增加.
*
@ -80,6 +104,9 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
public void onPlayerEnterTown(PlayerEnterTownEvent event) {
//当玩家进入城镇后移出map集合并且清零
Player player = event.getPlayer();
getLogger().info(ChatColor.AQUA+"玩家"+player.getName()+"进入了城镇 ");
playerRiskManager.removePlayerLevel(player.getName());
}
@ -91,9 +118,10 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
*/
@EventHandler
public void onPlayerLeaveTown(PlayerLeaveTownEvent event) {
//当玩家离开城镇后,把玩家姓名放入mao
//当玩家离开城镇后,把玩家姓名放入map
Player player = event.getPlayer();
getLogger().info(ChatColor.GREEN+player.getName()+"离开了城镇开始计算危险度");
playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player));
@ -108,9 +136,13 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity();
getLogger().info(ChatColor.GREEN+player.getName()+"死亡了,停止计算危险度");
playerRiskManager.removePlayerLevel(player.getName());
}
// TODO: Add any other event handlers needed to manage player risk.
/**
@ -120,14 +152,14 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
//获取所有玩家的生物群系的危险度
Map<String, Map<String, Integer>> playerBiomeRisk = playerRiskManager.getPlayerBiomeRisk();
for (String name : playerBiomeRisk.keySet()){
Map<String, Integer> biomeRisk = playerBiomeRisk.get(name);
for (String biome : biomeRisk.keySet()){
Integer level = biomeRisk.get(biome);
if (level < playerRiskManager.settings.getLevelMax()/5){
//如果小于10则增加
playerRiskManager.setPlayerRiskLevel(name,biome,level+1);
}
}
//通过玩家姓名获取所在的biome
Player player = Bukkit.getPlayer(name);
//当前玩家所在的生物群系
String biome = getCurrentBiome(player);
playerRiskManager.setPlayerRiskLevel(name,biome);
}
}
}

View File

@ -3,11 +3,13 @@ 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点
@ -18,10 +20,15 @@ import java.util.UUID;
*/
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;
@ -37,6 +44,7 @@ public class PlayerRiskManager {
this.playerRiskLevels = new HashMap<>();
this.playerBiomeRisk = new HashMap<>();
this.OffOnlinePlayerBiomeRisk = new HashMap<>();
//注入settings
this.settings = settings;
@ -72,14 +80,24 @@ public class PlayerRiskManager {
* 修改玩家某个biome的危险度
* @param playerName
* @param biome
* @param level
*/
public void setPlayerRiskLevel(String playerName,String biome, int level) {
//修改单个群系的危险度
playerBiomeRisk.get(playerName).put(biome,level);
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));
}
/**
@ -87,10 +105,35 @@ public class PlayerRiskManager {
* @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
@ -101,6 +144,7 @@ public class PlayerRiskManager {
Map<String,Integer> biomeRisk = new HashMap<>();
biomeRisk.put(biome,0);//从0开始计算
playerBiomeRisk.put(playerName,biomeRisk);
playerRiskLevels.put(playerName,0);
}
@ -121,9 +165,44 @@ public class PlayerRiskManager {
}
//放入总值
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);
}
}
}