From 73ae23840ad127e9375f30f9c217385a9164bb32 Mon Sep 17 00:00:00 2001 From: yuyu <124714592@qq.com> Date: Sat, 3 Aug 2024 11:22:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../sakurarealm/playerrisk/PlayerRisk.java | 79 ++++++++--- .../playerrisk/api/PlayerRiskManager.java | 129 ++++++++++++++---- src/main/resources/plugin.yml | 3 +- 4 files changed, 168 insertions(+), 45 deletions(-) diff --git a/build.gradle b/build.gradle index e56c268..246b48a 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group = 'com.sakurarealm' -version = '1.1' +version = '1.2' repositories { mavenCentral() diff --git a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java index b103840..7b0c034 100644 --- a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java +++ b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java @@ -27,6 +27,7 @@ import java.util.*; public final class PlayerRisk extends JavaPlugin implements Listener { private PlayerRiskManager playerRiskManager = null; + private boolean boolLogger = false; //假设的方法,您需要根据实际情况来实现它 private String getCurrentBiome(Player player) { @@ -83,15 +84,26 @@ public final class PlayerRisk extends JavaPlugin implements Listener { * @return */ public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings){ - if (!commandSender.isOp()){ - //不是op不能执行此命令 - return false; - } - if (s.equalsIgnoreCase("playerrisk")){ + + if (s.equalsIgnoreCase("playerrisk") || s.equalsIgnoreCase("pr")){ if (strings.length == 0){ - commandSender.sendMessage(ChatColor.RED+"请输入reload重载插件!"); + if (!commandSender.isOp()){ + //不是op不能执行此命令 + return false; + } + boolLogger = !boolLogger; + playerRiskManager.setBoolLogger(boolLogger); + if (boolLogger){ + commandSender.sendMessage(ChatColor.GREEN+"危险度插件日志开启"); + }else { + commandSender.sendMessage(ChatColor.GREEN+"危险度插件日志关闭"); + } return false; - }else { + }else if (strings[0].equalsIgnoreCase("reload")){ + if (!commandSender.isOp()){ + //不是op不能执行此命令 + return false; + } String reload = strings[0]; if (reload.equalsIgnoreCase("reload")){ //重新加载插件 @@ -109,6 +121,42 @@ public final class PlayerRisk extends JavaPlugin implements Listener { commandSender.sendMessage(ChatColor.GREEN+"危险度插件重载成功"); return true; } + } else if (strings.length == 2 && strings[0].equalsIgnoreCase("query")) { + //查询玩家的危险度 + Player player = Bukkit.getPlayer(strings[1]); + if (player == null){ + commandSender.sendMessage(ChatColor.RED+"玩家"+strings[1]+"不存在"); + } + if (player != null) { + playerRiskManager.questRiskLevel(player); + return true; + } + return false; + } else if (strings[0].equalsIgnoreCase("clear")){ + if (!commandSender.isOp()){ + //不是op不能执行此命令 + return false; + } + if (strings.length == 2){ + //长度为2,清空某位玩家的危险度 + Player player = Bukkit.getPlayer(strings[1]); + if (player == null){ + commandSender.sendMessage(ChatColor.RED+strings[1]+"玩家不存在!"); + return false; + } + playerRiskManager.removePlayerLevel(strings[1]); + } + }else if (strings.length == 3 && strings[0].equalsIgnoreCase("set")){ + if (!commandSender.isOp()){ + //不是op不能执行此命令 + return false; + } + Player player = Bukkit.getPlayer(strings[1]); + if (player == null){ + commandSender.sendMessage(ChatColor.RED+strings[1]+"玩家不存在!"); + } + + playerRiskManager.setRisk2Player(strings[1], Integer.parseInt(strings[2])); } }else { //指令错误 @@ -129,7 +177,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { playerRiskManager.offOnlinePlayer(name); - getLogger().info(ChatColor.GREEN+player.getName()+"退出了游戏"); + if (boolLogger) getLogger().info(ChatColor.GREEN+player.getName()+"退出了游戏"); } /** @@ -140,12 +188,12 @@ public final class PlayerRisk extends JavaPlugin implements Listener { public void onPlayerJoinGame(PlayerJoinEvent event){ Player player = event.getPlayer(); String name = player.getName(); - getLogger().info(ChatColor.AQUA+name+"进入了游戏"); + if (boolLogger)getLogger().info(ChatColor.AQUA+name+"进入了游戏"); if(!playerRiskManager.playerLoginBoolean(name)){ Location location = player.getLocation(); if (!this.playerStayTown(location)){ //为空,则在野外上线,但是map集合中又没有记录,重新开始计算 - playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player)); + playerRiskManager.addPlayerToHashMap(player.getName(),getCurrentBiome(player)); } } } @@ -156,7 +204,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { Location respawnLocation = event.getRespawnLocation(); if(!this.playerStayTown(respawnLocation)){ //重生在野外 - playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player)); + playerRiskManager.addPlayerToHashMap(player.getName(),getCurrentBiome(player)); } } @@ -171,7 +219,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { //当玩家进入城镇后,移出map集合并且清零 Player player = event.getPlayer(); - getLogger().info(ChatColor.AQUA+"玩家"+player.getName()+"进入了城镇 "); + if (boolLogger) getLogger().info(ChatColor.AQUA+"玩家"+player.getName()+"进入了城镇 "); playerRiskManager.removePlayerLevel(player.getName()); player.sendMessage(ChatColor.GREEN+"进入城镇,危险度归零!"); @@ -188,9 +236,8 @@ public final class PlayerRisk extends JavaPlugin implements Listener { //当玩家离开城镇后,把玩家姓名放入map中 Player player = event.getPlayer(); -// getLogger().info(ChatColor.GREEN+player.getName()+"离开了城镇开始计算危险度"); - playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player)); + playerRiskManager.addPlayerToHashMap(player.getName(),getCurrentBiome(player)); player.sendMessage(ChatColor.RED+"离开城镇,开始计算危险度!"); } @@ -204,7 +251,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { public void onPlayerDeath(PlayerDeathEvent event) { Player player = event.getEntity(); - getLogger().info(ChatColor.GREEN+player.getName()+"死亡了,停止计算危险度"); + if (boolLogger) getLogger().info(ChatColor.GREEN+player.getName()+"死亡了,停止计算危险度"); playerRiskManager.removePlayerLevel(player.getName()); @@ -218,7 +265,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { */ public void addRiskToOutTownPlayers(){ //获取所有玩家的生物群系的危险度 - Map> playerBiomeRisk = playerRiskManager.getPlayerBiomeRisk(); + HashMap> playerBiomeRisk = playerRiskManager.getPlayerBiomeRisk(); for (String name : playerBiomeRisk.keySet()){ //通过玩家姓名获取所在的biome diff --git a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java index 92c5487..5afa2ab 100644 --- a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java +++ b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java @@ -3,11 +3,17 @@ 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.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; +import org.bukkit.block.Biome; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import java.util.HashMap; -import java.util.Map; +import java.util.Random; import java.util.UUID; import java.util.logging.Logger; @@ -20,6 +26,7 @@ import java.util.logging.Logger; */ public class PlayerRiskManager { + private boolean boolLogger = false; Logger logger = Logger.getLogger("PlayerRisk"); private final PlayerRisk plugin; @@ -27,13 +34,13 @@ public class PlayerRiskManager { public PlayerRiskSettings settings; //用来记录在野外下线的玩家的危险度,这部分后续可以改成用数据库记录 - private final Map> OffOnlinePlayerBiomeRisk; + private final HashMap> OffOnlinePlayerBiomeRisk; //记录玩家的总危险度 - private final Map playerRiskLevels; + private final HashMap playerRiskLevels; //用来记录每个玩家在每个生物群系里面的危险度 - private final Map> playerBiomeRisk; + private final HashMap> playerBiomeRisk; // TODO: Add any data structures needed to manage player risk. @@ -88,23 +95,23 @@ public class PlayerRiskManager { if (playerBiomeRisk.get(playerName).get(biome) == null){ playerBiomeRisk.get(playerName).put(biome,1); - logger.info(ChatColor.GOLD+playerName+"进入了新的生物去群系且社危险度为1"); + if (boolLogger) logger.info(ChatColor.GOLD+playerName+"进入了新的生物去群系且社危险度为1"); }else { - if (playerBiomeRisk.get(playerName).get(biome) < settings.getLevelMax()/5) { + if (playerBiomeRisk.get(playerName).get(biome) < settings.getLevelMax()/5 && playerRiskLevels.get(playerName) < settings.getLevelMax()) { playerBiomeRisk.get(playerName).put(biome, playerBiomeRisk.get(playerName).get(biome) + 1); }else { - logger.info(ChatColor.GOLD+playerName+"的"+biome+"的危险度为:"+playerBiomeRisk.get(playerName).get(biome)); + if (boolLogger) logger.info(ChatColor.GOLD+playerName+"的"+biome+"的危险度为:"+playerBiomeRisk.get(playerName).get(biome)); } } //修改总群系的危险度 addAllBiomeRisk(playerName); - logger.info(ChatColor.GOLD+playerName+"的"+biome+"的危险度:"+playerBiomeRisk.get(playerName).get(biome)); + if (boolLogger) logger.info(ChatColor.GOLD+playerName+"的"+biome+"的危险度:"+playerBiomeRisk.get(playerName).get(biome)); } /** - * 玩家死亡或者进入城镇后,通过玩家姓名将玩家从map里删除 + * 玩家死亡或者进入城镇后,通过玩家姓名将玩家从HashMap里删除 * @param playerName */ public void removePlayerLevel(String playerName){ @@ -121,15 +128,15 @@ public class PlayerRiskManager { if (playerRiskLevels.get(playerName) != null) { - Map biomeRisks = playerBiomeRisk.get(playerName); + HashMap biomeRisks = playerBiomeRisk.get(playerName); for (String biome : biomeRisks.keySet()) { - logger.info(ChatColor.AQUA+playerName+"保存的"+biome+"群系危险度:"+biomeRisks.get(biome)); + if (boolLogger) logger.info(ChatColor.AQUA+playerName+"保存的"+biome+"群系危险度:"+biomeRisks.get(biome)); } - //下线后记录到下线的map中 + //下线后记录到下线的HashMap中 OffOnlinePlayerBiomeRisk.put(playerName, biomeRisks); - logger.info(ChatColor.GOLD+playerName+"的危险度被记录了"); + if (boolLogger) logger.info(ChatColor.GOLD+playerName+"的危险度被记录了"); } playerRiskLevels.remove(playerName); @@ -138,13 +145,13 @@ public class PlayerRiskManager { /** - * 玩家离开城镇,并进入对应的biome后,将玩家放入对应的map中 - * 注意此处,当玩家从1群系到2群系的时候,不能调用此方法修改map + * 玩家离开城镇,并进入对应的biome后,将玩家放入对应的HashMap中 + * 注意此处,当玩家从1群系到2群系的时候,不能调用此方法修改HashMap * @param playerName * @param biome */ - public void addPlayerToMap(String playerName,String biome){ - Map biomeRisk = new HashMap<>(); + public void addPlayerToHashMap(String playerName,String biome){ + HashMap biomeRisk = new HashMap<>(); biomeRisk.put(biome,0);//从0开始计算 playerBiomeRisk.put(playerName,biomeRisk); @@ -160,25 +167,25 @@ public class PlayerRiskManager { * @param playerName 通过玩家姓名获取修改危险度 */ public void addAllBiomeRisk(String playerName){ - Map stringIntegerMap = playerBiomeRisk.get(playerName); + HashMap stringIntegerHashMap = playerBiomeRisk.get(playerName); //循环叠加每个群系的危险度 Integer sumLevel = 0; - for (String str : stringIntegerMap.keySet()){ - sumLevel += stringIntegerMap.get(str); + for (String str : stringIntegerHashMap.keySet()){ + sumLevel += stringIntegerHashMap.get(str); } //放入总值 playerRiskLevels.put(playerName,sumLevel); - logger.info(ChatColor.GREEN+playerName+"的总危险度:"+sumLevel); + if (boolLogger) logger.info(ChatColor.GREEN+playerName+"的总危险度:"+sumLevel); } - public Map> getPlayerBiomeRisk() { + public HashMap> getPlayerBiomeRisk() { return playerBiomeRisk; } /** - * 判断玩家上下时是否在野外,如果玩家记录在map集合中,则true + * 判断玩家上下时是否在野外,如果玩家记录在HashMap集合中,则true * @param name */ public boolean playerLoginBoolean(String name) { @@ -187,22 +194,22 @@ public class PlayerRiskManager { //不为空则玩家在野外下线 if (OffOnlinePlayerBiomeRisk.containsKey(name)){ //获取之前下线前保存的生物群系集合 - Map biomeRisks = OffOnlinePlayerBiomeRisk.get(name); + HashMap biomeRisks = OffOnlinePlayerBiomeRisk.get(name); //把之前保存的生物群系危险度集合放入 playerBiomeRisk.put(name,biomeRisks); //计算总危险度 for (String biome : biomeRisks.keySet()){ - logger.info(ChatColor.RED+biome); + if (boolLogger) logger.info(ChatColor.RED+biome); Integer level = biomeRisks.get(biome); sumRiskLevel += level; - logger.info(ChatColor.GREEN+name+"登录后的"+biome+"的危险度"+level); + if (boolLogger) logger.info(ChatColor.GREEN+name+"登录后的"+biome+"的危险度"+level); } //计入总危险度 playerRiskLevels.put(name,sumRiskLevel); - logger.info(ChatColor.AQUA+name+"登录后的总危险度"+sumRiskLevel); + if (boolLogger) logger.info(ChatColor.AQUA+name+"登录后的总危险度"+sumRiskLevel); //最后删除 OffOnlinePlayerBiomeRisk 中保存的集合 OffOnlinePlayerBiomeRisk.remove(name); @@ -220,4 +227,72 @@ public class PlayerRiskManager { playerBiomeRisk.clear(); this.settings = settings; } + + /** + * 会清空玩家的危险度然后随机赋予几个群系危险度 + * @param name + * @param level + */ + public void setRisk2Player(String name,int level){ + if (level > 50){ + level = 50; + } + + HashMap biomeRisk = new HashMap<>(); +// HashMap> biomeRiskLevel = new HashMap<>(); + int copyLevel = level; + Random random = new Random(); + + + + for (Biome biome : Biome.values()){ + int i = random.nextInt(5); + if (i == 1){ + if (copyLevel >= 10){ + copyLevel -= 10; + biomeRisk.put(biome.name(),10); + }else if ( copyLevel > 0){ + biomeRisk.put(biome.name(),copyLevel); + break; + } + } + if (level == 0){ + break; + } + } + + playerRiskLevels.put(name,level); + playerBiomeRisk.put(name,biomeRisk); + + Player player = Bukkit.getPlayer(name); + for (String biome : biomeRisk.keySet()){ + player.sendMessage(ChatColor.GREEN+biome+"群系的危险度被设置为:"+biomeRisk.get(biome)); + } + } + + /** + * 查询玩家的危险度 + */ + public void questRiskLevel(Player player){ + HashMap biomeRisk = playerBiomeRisk.get(player.getName()); + Integer level = playerRiskLevels.get(player.getName()); + player.sendMessage(ChatColor.AQUA+"总危险度为:"+level); + + if (biomeRisk == null){ + player.sendMessage(ChatColor.GOLD+"玩家"+"危险度为:"+0); + return; + } + + for (String biome : biomeRisk.keySet()){ + player.sendMessage(ChatColor.GREEN+biome+"群系的危险度为:"+biomeRisk.get(biome)); + } + } + + public boolean isBoolLogger() { + return boolLogger; + } + + public void setBoolLogger(boolean boolLogger) { + this.boolLogger = boolLogger; + } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e6f9172..35f621f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -13,4 +13,5 @@ permissions: commands: playerrisk: description: PlayerRisk command - usage: /playerrisk \ No newline at end of file + usage: /playerrisk + aliases: [pr]