2024-08-03 11:22:27 +08:00

296 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
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.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
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 {
private PlayerRiskManager playerRiskManager = null;
private boolean boolLogger = false;
//假设的方法,您需要根据实际情况来实现它
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(this::onCommand);
//注册监听器
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插件关闭");
}
/**
* 指令重载
* @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 (s.equalsIgnoreCase("playerrisk") || s.equalsIgnoreCase("pr")){
if (strings.length == 0){
if (!commandSender.isOp()){
//不是op不能执行此命令
return false;
}
boolLogger = !boolLogger;
playerRiskManager.setBoolLogger(boolLogger);
if (boolLogger){
commandSender.sendMessage(ChatColor.GREEN+"危险度插件日志开启");
}else {
commandSender.sendMessage(ChatColor.GREEN+"危险度插件日志关闭");
}
return false;
}else if (strings[0].equalsIgnoreCase("reload")){
if (!commandSender.isOp()){
//不是op不能执行此命令
return false;
}
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 if (strings.length == 2 && strings[0].equalsIgnoreCase("query")) {
//查询玩家的危险度
Player player = Bukkit.getPlayer(strings[1]);
if (player == null){
commandSender.sendMessage(ChatColor.RED+"玩家"+strings[1]+"不存在");
}
if (player != null) {
playerRiskManager.questRiskLevel(player);
return true;
}
return false;
} else if (strings[0].equalsIgnoreCase("clear")){
if (!commandSender.isOp()){
//不是op不能执行此命令
return false;
}
if (strings.length == 2){
//长度为2清空某位玩家的危险度
Player player = Bukkit.getPlayer(strings[1]);
if (player == null){
commandSender.sendMessage(ChatColor.RED+strings[1]+"玩家不存在!");
return false;
}
playerRiskManager.removePlayerLevel(strings[1]);
}
}else if (strings.length == 3 && strings[0].equalsIgnoreCase("set")){
if (!commandSender.isOp()){
//不是op不能执行此命令
return false;
}
Player player = Bukkit.getPlayer(strings[1]);
if (player == null){
commandSender.sendMessage(ChatColor.RED+strings[1]+"玩家不存在!");
}
playerRiskManager.setRisk2Player(strings[1], Integer.parseInt(strings[2]));
}
}else {
//指令错误
commandSender.sendMessage(ChatColor.RED+"请输入正确的指令!");
return false;
}
return false;
}
/**
* 玩家退出游戏后从map集合中移出
*/
@EventHandler
public void playerOffOnline(PlayerQuitEvent event){
Player player = event.getPlayer();
String name = player.getName();
playerRiskManager.offOnlinePlayer(name);
if (boolLogger) getLogger().info(ChatColor.GREEN+player.getName()+"退出了游戏");
}
/**
* 当玩家登录时,判断玩家是否在野外群落中
* @param event
*/
@EventHandler
public void onPlayerJoinGame(PlayerJoinEvent event){
Player player = event.getPlayer();
String name = player.getName();
if (boolLogger)getLogger().info(ChatColor.AQUA+name+"进入了游戏");
if(!playerRiskManager.playerLoginBoolean(name)){
Location location = player.getLocation();
if (!this.playerStayTown(location)){
//为空则在野外上线但是map集合中又没有记录重新开始计算
playerRiskManager.addPlayerToHashMap(player.getName(),getCurrentBiome(player));
}
}
}
@EventHandler
public void playerRespawn(PlayerRespawnEvent event){
Player player = event.getPlayer();
Location respawnLocation = event.getRespawnLocation();
if(!this.playerStayTown(respawnLocation)){
//重生在野外
playerRiskManager.addPlayerToHashMap(player.getName(),getCurrentBiome(player));
}
}
/**
* TODO: 当玩家进入城镇时, 危险度不再增加.
*
* @param event The PlayerEnterTownEvent.
*/
@EventHandler
public void onPlayerEnterTown(PlayerEnterTownEvent event) {
//当玩家进入城镇后移出map集合并且清零
Player player = event.getPlayer();
if (boolLogger) getLogger().info(ChatColor.AQUA+"玩家"+player.getName()+"进入了城镇 ");
playerRiskManager.removePlayerLevel(player.getName());
player.sendMessage(ChatColor.GREEN+"进入城镇,危险度归零!");
}
/**
* TODO: 当玩家离开城镇时, 危险度开始增加.
*
* @param event The PlayerLeaveTownEvent.
*/
@EventHandler
public void onPlayerLeaveTown(PlayerLeaveTownEvent event) {
//当玩家离开城镇后,把玩家姓名放入map中
Player player = event.getPlayer();
playerRiskManager.addPlayerToHashMap(player.getName(),getCurrentBiome(player));
player.sendMessage(ChatColor.RED+"离开城镇,开始计算危险度!");
}
/**
* TODO: 当玩家死亡时, 危险度重置.
*
* @param event The PlayerDeathEvent.
*/
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity();
if (boolLogger) 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(){
//获取所有玩家的生物群系的危险度
HashMap<String, HashMap<String, Integer>> playerBiomeRisk = playerRiskManager.getPlayerBiomeRisk();
for (String name : playerBiomeRisk.keySet()){
//通过玩家姓名获取所在的biome
Player player = Bukkit.getPlayer(name);
//当前玩家所在的生物群系
String biome = getCurrentBiome(player);
playerRiskManager.setPlayerRiskLevel(name,biome);
}
}
/**
* 用于判断玩家是否在城镇
* @param location
* @return
*/
public boolean playerStayTown(Location location) {
Town town = TownyAPI.getInstance().getTown(location);
if (town == null){
//为空则不再城镇
return false;
}else {
return true;
}
}
}