重新修改了主框架,区分了每个生物群系的危险度,但是因为没有具体的方法和生物群系的id或者名称,所以用a代替了

This commit is contained in:
yuyu 2024-03-15 15:07:19 +08:00
parent 6523f94298
commit 67757b58b0
3 changed files with 95 additions and 46 deletions

View File

@ -19,8 +19,6 @@ import java.util.*;
public final class PlayerRisk extends JavaPlugin implements Listener {
//用来记录在城镇之外的玩家
Map<String,Integer> playsOutTown = new Hashtable<>();
PlayerRiskManager playerRiskManager = null;
public PlayerRiskManager getPlayerRiskManager() {
@ -45,7 +43,8 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
//注册监听器
Bukkit.getPluginManager().registerEvents(this,this);
getServer().getScheduler().scheduleSyncRepeatingTask(this, this::addRiskToOutTownPlayers, 0, 1200);
getServer().getScheduler().scheduleSyncRepeatingTask(this, this::addRiskToOutTownPlayers,
0, playerRiskManager.settings.getIncreaseInterval()*20);
}
@Override
@ -63,7 +62,7 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
public void onPlayerEnterTown(PlayerEnterTownEvent event) {
//当玩家进入城镇后移出map集合并且清零
Player player = event.getPlayer();
playsOutTown.remove(player.getName());
playerRiskManager.removePlayerLevel(player.getName());
throw new NotImplementedException("Not implemented yet.");
}
@ -74,9 +73,12 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
*/
@EventHandler
public void onPlayerLeaveTown(PlayerLeaveTownEvent event) {
//当玩家离开城镇后,移出map集合并且清零
//当玩家离开城镇后,把玩家姓名放入mao中
Player player = event.getPlayer();
playsOutTown.put(player.getName(),0);
//此处应该有一个方法用来获取生物群系的姓名或者id,在方法中默认只有一个群系,用字符a代替
playerRiskManager.addPlayerToMap(player.getName(),"a");
throw new NotImplementedException("Not implemented yet.");
}
@ -89,21 +91,25 @@ public final class PlayerRisk extends JavaPlugin implements Listener {
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity();
playsOutTown.remove(player.getName());
playerRiskManager.removePlayerLevel(player.getName());
throw new NotImplementedException("Not implemented yet.");
}
// TODO: Add any other event handlers needed to manage player risk.
//注册主任务,一直循环给增加在城镇之外的玩家的危险度
/**
* 此方法会每过60s执行一次,用于增加危险度
*/
public void addRiskToOutTownPlayers(){
for (String name : playsOutTown.keySet()){
//获取玩家当前的危险度
int playerRiskLevel = playerRiskManager.getPlayerRiskLevel(name);
//日志报告测试用
getLogger().info(ChatColor.GREEN+name+"危险度为:"+playerRiskLevel);
//注意此处是把所有生物群系的危险度放在一起计算了,
if (playerRiskLevel < playerRiskManager.settings.getLevelMax()){
playerRiskManager.setPlayerRiskLevel(name,playerRiskLevel + 1);
//获取所有玩家的生物群系的危险度
Map<String, Map<String, Integer>> playerBiomeRisk = playerRiskManager.getPlayerBiomeRisk();
for (String name : playerBiomeRisk.keySet()){
Map<String, Integer> 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);
}
}
}
}

View File

@ -22,8 +22,12 @@ public class PlayerRiskManager {
public final PlayerRiskSettings settings;
//记录玩家的总危险度
private final Map<String, Integer> playerRiskLevels;
//用来记录每个玩家在每个生物群系里面的危险度
private final Map<String,Map<String,Integer>> playerBiomeRisk;
// TODO: Add any data structures needed to manage player risk.
public PlayerRiskManager(PlayerRisk plugin, PlayerRiskSettings settings) {
@ -32,57 +36,94 @@ public class PlayerRiskManager {
// plugin.getServer().getPluginManager().registerEvents(new Listener(), plugin);
this.playerRiskLevels = new HashMap<>();
this.playerBiomeRisk = new HashMap<>();
//注入settings
this.settings = settings;
new PlaceHolderAPIHook(this).register();
}
//给外部插件调用,用于返回总危险度刷怪
/**
*
* @param playerName 通过玩家姓名获取玩家总危险度
* @return
*/
public int getPlayerRiskLevel(String playerName) {
// Get offline player by name.
// OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
Integer riskLevel = playerRiskLevels.get(playerName);
Integer riskLevel = playerRiskLevels.get(playerName);
return riskLevel;
}
public int getPlayerRiskLevel(UUID playerId) {
// Get offline player by UUID.
OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerId);
return getPlayerRiskLevel(player);
}
/**
* TODO: 获取玩家的危险度等级
*
* @param player 玩家
* @return 危险度等级
* @param playerName 玩家姓名
* @param biome 生物群系
* @return 返回该玩家在Biome中的危险度
*/
public int getPlayerRiskLevel(OfflinePlayer player) {
throw new NotImplementedException("Not implemented yet.");
public int getBiomeRiskLevel(String playerName,String biome){
Integer level = playerBiomeRisk.get(playerName).get(biome);
return level;
}
public void setPlayerRiskLevel(String playerName, int level) {
// Get offline player by name.
// OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
// setPlayerRiskLevel(player, level);
//修改map里面的value来修改危险度
playerRiskLevels.put(playerName,level);
}
public void setPlayerRiskLevel(UUID playerId, int level) {
// Get offline player by UUID.
OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerId);
setPlayerRiskLevel(player, level);
/**
* 修改玩家某个biome的危险度
* @param playerName
* @param biome
* @param level
*/
public void setPlayerRiskLevel(String playerName,String biome, int level) {
//修改单个群系的危险度
playerBiomeRisk.get(playerName).put(biome,level);
//修改总群系的危险度
addAllBiomeRisk(playerName);
}
/**
* TODO: 设置玩家的危险度等级
* 玩家死亡或者进入城镇后通过玩家姓名将玩家从map里删除
* @param playerName
*/
public void setPlayerRiskLevel(OfflinePlayer player, int level) {
throw new NotImplementedException("Not implemented yet.");
public void removePlayerLevel(String playerName){
playerRiskLevels.remove(playerName);
playerBiomeRisk.remove(playerName);
}
/**
* 玩家离开城镇,并进入对应的biome后,将玩家放入对应的map中
* 注意此处当玩家从1群系到2群系的时候不能调用此方法修改map
* @param playerName
* @param biome
*/
public void addPlayerToMap(String playerName,String biome){
Map<String,Integer> biomeRisk = new HashMap<>();
biomeRisk.put(biome,0);//从0开始计算
playerBiomeRisk.put(playerName,biomeRisk);
playerRiskLevels.put(playerName,0);
}
// TODO Add any other methods needed to manage player risk.
/**
*
* @param playerName 通过玩家姓名获取修改危险度
*/
public void addAllBiomeRisk(String playerName){
Map<String, Integer> stringIntegerMap = playerBiomeRisk.get(playerName);
//循环叠加每个群系的危险度
Integer sumLevel = 0;
for (String str : stringIntegerMap.keySet()){
sumLevel += stringIntegerMap.get(str);
}
//放入总值
playerRiskLevels.put(playerName,sumLevel);
}
public Map<String, Map<String, Integer>> getPlayerBiomeRisk() {
return playerBiomeRisk;
}
}

View File

@ -31,7 +31,9 @@ public class PlaceHolderAPIHook extends PlaceholderExpansion {
@Override
public String onRequest(OfflinePlayer player, String params) {
if (params.equals("level")) { // prisk_level
return String.valueOf(playerRiskManager.getPlayerRiskLevel(player));
int playerRiskLevel = playerRiskManager.getPlayerRiskLevel(player.getName());
String level = String.valueOf(playerRiskLevel);
return level;
}
return null;
}