diff --git a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java index a297812..b103840 100644 --- a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java +++ b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java @@ -9,12 +9,13 @@ import com.sakurarealm.playerrisk.api.PlayerRiskSettings; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; 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.EntityDamageByEntityEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -55,7 +56,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { public void onEnable() { // Plugin startup logic load(); - getCommand("playerrisk").setExecutor(new CommandHandler(this)); + getCommand("playerrisk").setExecutor(this::onCommand); //注册监听器 Bukkit.getPluginManager().registerEvents(this,this); @@ -72,6 +73,51 @@ public final class PlayerRisk extends JavaPlugin implements Listener { getLogger().info(ChatColor.GREEN+"PlayerRisk插件关闭"); } + + /** + * 指令重载 + * @param commandSender Source of the command + * @param command Command which was executed + * @param s Alias of the command which was used + * @param strings Passed command arguments + * @return + */ + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings){ + if (!commandSender.isOp()){ + //不是op不能执行此命令 + return false; + } + if (s.equalsIgnoreCase("playerrisk")){ + if (strings.length == 0){ + commandSender.sendMessage(ChatColor.RED+"请输入reload重载插件!"); + return false; + }else { + String reload = strings[0]; + if (reload.equalsIgnoreCase("reload")){ + //重新加载插件 + //先取消定时任务 + Bukkit.getScheduler().cancelTasks(this); + + //重新加载文件 + + playerRiskManager.setSettings(new PlayerRiskSettings(this)); + + //重新注册定时任务 + getServer().getScheduler().scheduleSyncRepeatingTask(this, this::addRiskToOutTownPlayers, + 0, playerRiskManager.settings.getIncreaseInterval()*20); + + commandSender.sendMessage(ChatColor.GREEN+"危险度插件重载成功"); + return true; + } + } + }else { + //指令错误 + commandSender.sendMessage(ChatColor.RED+"请输入正确的指令!"); + return false; + } + return false; + } + /** * 玩家退出游戏后从map集合中移出 */ @@ -128,6 +174,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { getLogger().info(ChatColor.AQUA+"玩家"+player.getName()+"进入了城镇 "); playerRiskManager.removePlayerLevel(player.getName()); + player.sendMessage(ChatColor.GREEN+"进入城镇,危险度归零!"); } @@ -141,10 +188,10 @@ public final class PlayerRisk extends JavaPlugin implements Listener { //当玩家离开城镇后,把玩家姓名放入map中 Player player = event.getPlayer(); - getLogger().info(ChatColor.GREEN+player.getName()+"离开了城镇开始计算危险度"); +// getLogger().info(ChatColor.GREEN+player.getName()+"离开了城镇开始计算危险度"); playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player)); - + player.sendMessage(ChatColor.RED+"离开城镇,开始计算危险度!"); } diff --git a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java index bb813eb..92c5487 100644 --- a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java +++ b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java @@ -24,7 +24,7 @@ public class PlayerRiskManager { private final PlayerRisk plugin; - public final PlayerRiskSettings settings; + public PlayerRiskSettings settings; //用来记录在野外下线的玩家的危险度,这部分后续可以改成用数据库记录 private final Map> OffOnlinePlayerBiomeRisk; @@ -59,6 +59,9 @@ public class PlayerRiskManager { public int getPlayerRiskLevel(String playerName) { Integer riskLevel = playerRiskLevels.get(playerName); + if (riskLevel == null){ + return 0; + } return riskLevel; } @@ -208,4 +211,13 @@ public class PlayerRiskManager { } return false; } + + + public void setSettings(PlayerRiskSettings settings) { + //清空危险度信息,重载文件 + OffOnlinePlayerBiomeRisk.clear(); + playerRiskLevels.clear(); + playerBiomeRisk.clear(); + this.settings = settings; + } } diff --git a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskSettings.java b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskSettings.java index a9d15d3..a9c8dfd 100644 --- a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskSettings.java +++ b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskSettings.java @@ -1,6 +1,11 @@ package com.sakurarealm.playerrisk.api; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; + +import java.io.File; public class PlayerRiskSettings { @@ -12,6 +17,17 @@ public class PlayerRiskSettings { increaseInterval = config.getInt("increase-interval"); } + + public PlayerRiskSettings(Plugin plugin){ + //保存配置文件 + plugin.saveResource("config.yml",false); + FileConfiguration config; + File file = new File(plugin.getDataFolder(), "config.yml"); + config = YamlConfiguration.loadConfiguration(file); + this.increaseInterval = config.getInt("settings.increase-interval"); + this.levelMax = config.getInt("settings.level-max"); + } + public int getIncreaseInterval() { return increaseInterval; } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e630bfb..e6f9172 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -13,5 +13,4 @@ permissions: commands: playerrisk: description: PlayerRisk command - usage: /playerrisk - aliases: [ pr, prisk ] \ No newline at end of file + usage: /playerrisk \ No newline at end of file