修复了再生后出生在野外没有记录的bug

This commit is contained in:
yuyu 2024-04-08 18:01:35 +08:00
parent 5e42968702
commit f2adc94e06
3 changed files with 47 additions and 8 deletions

View File

@ -1,7 +1,9 @@
package com.sakurarealm.playerrisk; package com.sakurarealm.playerrisk;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.event.PlayerEnterTownEvent; import com.palmergames.bukkit.towny.event.PlayerEnterTownEvent;
import com.palmergames.bukkit.towny.event.PlayerLeaveTownEvent; 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.PlayerRiskManager;
import com.sakurarealm.playerrisk.api.PlayerRiskSettings; import com.sakurarealm.playerrisk.api.PlayerRiskSettings;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -12,17 +14,18 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.*; import java.util.*;
public final class PlayerRisk extends JavaPlugin implements Listener { public final class PlayerRisk extends JavaPlugin implements Listener {
PlayerRiskManager playerRiskManager = null; private PlayerRiskManager playerRiskManager = null;
//假设的方法您需要根据实际情况来实现它 //假设的方法您需要根据实际情况来实现它
private String getCurrentBiome(Player player) { private String getCurrentBiome(Player player) {
@ -88,13 +91,30 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
* @param event * @param event
*/ */
@EventHandler @EventHandler
public void onPlayerLogin(PlayerJoinEvent event){ public void onPlayerJoinGame(PlayerJoinEvent event){
Player player = event.getPlayer(); Player player = event.getPlayer();
String name = player.getName(); String name = player.getName();
getLogger().info(ChatColor.AQUA+name+"进入了游戏"); 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: 当玩家进入城镇时, 危险度不再增加. * TODO: 当玩家进入城镇时, 危险度不再增加.
* *
@ -143,6 +163,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
} }
// TODO: Add any other event handlers needed to manage player risk. // 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;
}
}
} }

View File

@ -87,7 +87,7 @@ public class PlayerRiskManager {
playerBiomeRisk.get(playerName).put(biome,1); playerBiomeRisk.get(playerName).put(biome,1);
logger.info(ChatColor.GOLD+playerName+"进入了新的生物去群系且社危险度为1"); logger.info(ChatColor.GOLD+playerName+"进入了新的生物去群系且社危险度为1");
}else { }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); playerBiomeRisk.get(playerName).put(biome, playerBiomeRisk.get(playerName).get(biome) + 1);
}else { }else {
logger.info(ChatColor.GOLD+playerName+""+biome+"的危险度为:"+playerBiomeRisk.get(playerName).get(biome)); logger.info(ChatColor.GOLD+playerName+""+biome+"的危险度为:"+playerBiomeRisk.get(playerName).get(biome));
@ -175,10 +175,10 @@ public class PlayerRiskManager {
} }
/** /**
* 判断玩家上下时是否在野外 * 判断玩家上下时是否在野外,如果玩家记录在map集合中则true
* @param name * @param name
*/ */
public void playerLoginBoolean(String name) { public boolean playerLoginBoolean(String name) {
int sumRiskLevel = 0; int sumRiskLevel = 0;
//不为空则玩家在野外下线 //不为空则玩家在野外下线
@ -203,6 +203,9 @@ public class PlayerRiskManager {
//最后删除 OffOnlinePlayerBiomeRisk 中保存的集合 //最后删除 OffOnlinePlayerBiomeRisk 中保存的集合
OffOnlinePlayerBiomeRisk.remove(name); OffOnlinePlayerBiomeRisk.remove(name);
return true;
} }
return false;
} }
} }

View File

@ -35,6 +35,6 @@ public class PlaceHolderAPIHook extends PlaceholderExpansion {
String level = String.valueOf(playerRiskLevel); String level = String.valueOf(playerRiskLevel);
return level; return level;
} }
return null; return "0";
} }
} }