From 5e42968702e2a81724d30c9632ef3ecd52d75b27 Mon Sep 17 00:00:00 2001 From: yuyu <124714592@qq.com> Date: Sun, 17 Mar 2024 14:40:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E6=B5=8B=E8=AF=95=E5=AE=8C?= =?UTF-8?q?=E6=AF=95,=E6=97=A5=E5=BF=97=E6=B2=A1=E5=88=A0=E6=8E=89?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E5=9C=A8=E8=BF=9C=E7=A8=8B=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E9=87=8C=E6=B5=8B=E8=AF=95=E7=9C=8B=E7=9C=8B?= =?UTF-8?q?=E6=9C=89=E6=B2=A1=E6=9C=89=E4=BB=80=E4=B9=88bug=EF=BC=8C?= =?UTF-8?q?=E5=8F=A6=E5=A4=96=E6=9C=89=E9=9C=80=E8=A6=81=E7=9A=84=E8=AF=9D?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=8A=8A=E4=B8=8B=E7=BA=BF=E5=82=A8=E5=AD=98?= =?UTF-8?q?=E7=9A=84=E7=8E=A9=E5=AE=B6=E5=8D=B1=E9=99=A9=E5=BA=A6=E5=AD=98?= =?UTF-8?q?=E5=88=B0=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../sakurarealm/playerrisk/PlayerRisk.java | 58 ++++++++++--- .../playerrisk/api/PlayerRiskManager.java | 87 ++++++++++++++++++- 3 files changed, 129 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index d359b6b..e56c268 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group = 'com.sakurarealm' -version = '1.0' +version = '1.1' repositories { mavenCentral() diff --git a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java index 451351b..5cc7909 100644 --- a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java +++ b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java @@ -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> playerBiomeRisk = playerRiskManager.getPlayerBiomeRisk(); for (String name : playerBiomeRisk.keySet()){ - Map 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); + } } } diff --git a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java index 378f052..904695c 100644 --- a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java +++ b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java @@ -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> OffOnlinePlayerBiomeRisk; + //记录玩家的总危险度 private final Map 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 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 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> getPlayerBiomeRisk() { return playerBiomeRisk; } + + /** + * 判断玩家上下时是否在野外 + * @param name + */ + public void playerLoginBoolean(String name) { + int sumRiskLevel = 0; + + //不为空则玩家在野外下线 + if (OffOnlinePlayerBiomeRisk.containsKey(name)){ + //获取之前下线前保存的生物群系集合 + Map 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); + } + } }