166 lines
5.2 KiB
Java
166 lines
5.2 KiB
Java
package com.sakurarealm.playerrisk;
|
||
|
||
import com.palmergames.bukkit.towny.event.PlayerEnterTownEvent;
|
||
import com.palmergames.bukkit.towny.event.PlayerLeaveTownEvent;
|
||
import com.sakurarealm.playerrisk.api.PlayerRiskManager;
|
||
import com.sakurarealm.playerrisk.api.PlayerRiskSettings;
|
||
import org.bukkit.Bukkit;
|
||
import org.bukkit.ChatColor;
|
||
import org.bukkit.Location;
|
||
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.PlayerDeathEvent;
|
||
import org.bukkit.event.player.PlayerJoinEvent;
|
||
import org.bukkit.event.player.PlayerLoginEvent;
|
||
import org.bukkit.event.player.PlayerQuitEvent;
|
||
import org.bukkit.plugin.java.JavaPlugin;
|
||
|
||
import java.util.*;
|
||
|
||
public final class PlayerRisk extends JavaPlugin implements Listener {
|
||
|
||
PlayerRiskManager playerRiskManager = null;
|
||
|
||
//假设的方法,您需要根据实际情况来实现它
|
||
private String getCurrentBiome(Player player) {
|
||
//实现获取玩家当前所在生物群系的逻辑
|
||
return player.getLocation().getBlock().getBiome().name();
|
||
}
|
||
|
||
|
||
/**
|
||
* 返回getPlayerRiskManager用于调用危险度
|
||
*/
|
||
public PlayerRiskManager getPlayerRiskManager() {
|
||
if (playerRiskManager == null) {
|
||
throw new NullPointerException(ChatColor.RED + "PlayerRisk plugin is not initialized.");
|
||
}
|
||
return playerRiskManager;
|
||
}
|
||
|
||
public void load() {
|
||
saveDefaultConfig();
|
||
FileConfiguration config = getConfig();
|
||
ConfigurationSection settingsSection = config.getConfigurationSection("settings");
|
||
playerRiskManager = new PlayerRiskManager(this, new PlayerRiskSettings(settingsSection));
|
||
}
|
||
|
||
@Override
|
||
public void onEnable() {
|
||
// Plugin startup logic
|
||
load();
|
||
getCommand("playerrisk").setExecutor(new CommandHandler(this));
|
||
//注册监听器
|
||
Bukkit.getPluginManager().registerEvents(this,this);
|
||
|
||
getLogger().info(ChatColor.RED+"PlayerRisks插件开始运行");
|
||
|
||
getServer().getScheduler().scheduleSyncRepeatingTask(this, this::addRiskToOutTownPlayers,
|
||
0, playerRiskManager.settings.getIncreaseInterval()*20);
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onDisable() {
|
||
// Plugin shutdown logic
|
||
getLogger().info(ChatColor.GREEN+"PlayerRisk插件关闭");
|
||
}
|
||
|
||
/**
|
||
* 玩家退出游戏后从map集合中移出
|
||
*/
|
||
@EventHandler
|
||
public void playerOffOnline(PlayerQuitEvent event){
|
||
Player player = event.getPlayer();
|
||
|
||
String name = player.getName();
|
||
|
||
playerRiskManager.offOnlinePlayer(name);
|
||
|
||
getLogger().info(ChatColor.GREEN+player.getName()+"退出了游戏");
|
||
}
|
||
|
||
/**
|
||
* 当玩家登录时,判断玩家是否在野外群落中
|
||
* @param event
|
||
*/
|
||
@EventHandler
|
||
public void onPlayerLogin(PlayerJoinEvent event){
|
||
Player player = event.getPlayer();
|
||
String name = player.getName();
|
||
getLogger().info(ChatColor.AQUA+name+"进入了游戏");
|
||
playerRiskManager.playerLoginBoolean(name);
|
||
}
|
||
|
||
/**
|
||
* TODO: 当玩家进入城镇时, 危险度不再增加.
|
||
*
|
||
* @param event The PlayerEnterTownEvent.
|
||
*/
|
||
@EventHandler
|
||
public void onPlayerEnterTown(PlayerEnterTownEvent event) {
|
||
//当玩家进入城镇后,移出map集合并且清零
|
||
Player player = event.getPlayer();
|
||
|
||
getLogger().info(ChatColor.AQUA+"玩家"+player.getName()+"进入了城镇 ");
|
||
|
||
playerRiskManager.removePlayerLevel(player.getName());
|
||
|
||
}
|
||
|
||
/**
|
||
* TODO: 当玩家离开城镇时, 危险度开始增加.
|
||
*
|
||
* @param event The PlayerLeaveTownEvent.
|
||
*/
|
||
@EventHandler
|
||
public void onPlayerLeaveTown(PlayerLeaveTownEvent event) {
|
||
//当玩家离开城镇后,把玩家姓名放入map中
|
||
Player player = event.getPlayer();
|
||
|
||
getLogger().info(ChatColor.GREEN+player.getName()+"离开了城镇开始计算危险度");
|
||
|
||
playerRiskManager.addPlayerToMap(player.getName(),getCurrentBiome(player));
|
||
|
||
|
||
}
|
||
|
||
/**
|
||
* TODO: 当玩家死亡时, 危险度重置.
|
||
*
|
||
* @param event The PlayerDeathEvent.
|
||
*/
|
||
@EventHandler
|
||
public void onPlayerDeath(PlayerDeathEvent event) {
|
||
Player player = event.getEntity();
|
||
|
||
getLogger().info(ChatColor.GREEN+player.getName()+"死亡了,停止计算危险度");
|
||
|
||
playerRiskManager.removePlayerLevel(player.getName());
|
||
|
||
}
|
||
|
||
// TODO: Add any other event handlers needed to manage player risk.
|
||
|
||
/**
|
||
* 此方法会每过60s执行一次,用于增加危险度
|
||
*/
|
||
public void addRiskToOutTownPlayers(){
|
||
//获取所有玩家的生物群系的危险度
|
||
Map<String, Map<String, Integer>> playerBiomeRisk = playerRiskManager.getPlayerBiomeRisk();
|
||
for (String name : playerBiomeRisk.keySet()){
|
||
|
||
//通过玩家姓名获取所在的biome
|
||
Player player = Bukkit.getPlayer(name);
|
||
//当前玩家所在的生物群系
|
||
String biome = getCurrentBiome(player);
|
||
|
||
playerRiskManager.setPlayerRiskLevel(name,biome);
|
||
|
||
}
|
||
}
|
||
}
|