diff --git a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java index 5cc7909..a297812 100644 --- a/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java +++ b/src/main/java/com/sakurarealm/playerrisk/PlayerRisk.java @@ -1,7 +1,9 @@ package com.sakurarealm.playerrisk; +import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.event.PlayerEnterTownEvent; import com.palmergames.bukkit.towny.event.PlayerLeaveTownEvent; +import com.palmergames.bukkit.towny.object.Town; import com.sakurarealm.playerrisk.api.PlayerRiskManager; import com.sakurarealm.playerrisk.api.PlayerRiskSettings; import org.bukkit.Bukkit; @@ -12,17 +14,18 @@ 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.PlayerLoginEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.plugin.java.JavaPlugin; import java.util.*; public final class PlayerRisk extends JavaPlugin implements Listener { - PlayerRiskManager playerRiskManager = null; + private PlayerRiskManager playerRiskManager = null; //假设的方法,您需要根据实际情况来实现它 private String getCurrentBiome(Player player) { @@ -88,13 +91,30 @@ public final class PlayerRisk extends JavaPlugin implements Listener { * @param event */ @EventHandler - public void onPlayerLogin(PlayerJoinEvent event){ + public void onPlayerJoinGame(PlayerJoinEvent event){ Player player = event.getPlayer(); String name = player.getName(); getLogger().info(ChatColor.AQUA+name+"进入了游戏"); - playerRiskManager.playerLoginBoolean(name); + if(!playerRiskManager.playerLoginBoolean(name)){ + Location location = player.getLocation(); + if (!this.playerStayTown(location)){ + //为空,则在野外上线,但是map集合中又没有记录,重新开始计算 + playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player)); + } + } } + @EventHandler + public void playerRespawn(PlayerRespawnEvent event){ + Player player = event.getPlayer(); + Location respawnLocation = event.getRespawnLocation(); + if(!this.playerStayTown(respawnLocation)){ + //重生在野外 + playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player)); + } + } + + /** * TODO: 当玩家进入城镇时, 危险度不再增加. * @@ -143,6 +163,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener { } + // TODO: Add any other event handlers needed to manage player risk. /** @@ -162,4 +183,19 @@ public final class PlayerRisk extends JavaPlugin implements Listener { } } + + /** + * 用于判断玩家是否在城镇 + * @param location + * @return + */ + public boolean playerStayTown(Location location) { + Town town = TownyAPI.getInstance().getTown(location); + if (town == null){ + //为空则不再城镇 + return false; + }else { + return true; + } + } } diff --git a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java index 904695c..bb813eb 100644 --- a/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java +++ b/src/main/java/com/sakurarealm/playerrisk/api/PlayerRiskManager.java @@ -87,7 +87,7 @@ public class PlayerRiskManager { playerBiomeRisk.get(playerName).put(biome,1); 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) { playerBiomeRisk.get(playerName).put(biome, playerBiomeRisk.get(playerName).get(biome) + 1); }else { logger.info(ChatColor.GOLD+playerName+"的"+biome+"的危险度为:"+playerBiomeRisk.get(playerName).get(biome)); @@ -175,10 +175,10 @@ public class PlayerRiskManager { } /** - * 判断玩家上下时是否在野外 + * 判断玩家上下时是否在野外,如果玩家记录在map集合中,则true * @param name */ - public void playerLoginBoolean(String name) { + public boolean playerLoginBoolean(String name) { int sumRiskLevel = 0; //不为空则玩家在野外下线 @@ -203,6 +203,9 @@ public class PlayerRiskManager { //最后删除 OffOnlinePlayerBiomeRisk 中保存的集合 OffOnlinePlayerBiomeRisk.remove(name); + + return true; } + return false; } } diff --git a/src/main/java/com/sakurarealm/playerrisk/hook/PlaceHolderAPIHook.java b/src/main/java/com/sakurarealm/playerrisk/hook/PlaceHolderAPIHook.java index bc55824..1627724 100644 --- a/src/main/java/com/sakurarealm/playerrisk/hook/PlaceHolderAPIHook.java +++ b/src/main/java/com/sakurarealm/playerrisk/hook/PlaceHolderAPIHook.java @@ -35,6 +35,6 @@ public class PlaceHolderAPIHook extends PlaceholderExpansion { String level = String.valueOf(playerRiskLevel); return level; } - return null; + return "0"; } }