引入危险警告服务

This commit is contained in:
2024-12-27 00:43:03 +08:00
parent 970c2c5cfd
commit c5015bf656
4 changed files with 85 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package ling.coordinateRecorder;
import ling.coordinateRecorder.Commands.ZbCommand;
import ling.coordinateRecorder.Listener.*;
import ling.coordinateRecorder.Service.DangerWarningService;
import ling.coordinateRecorder.Service.ItemShowService;
import ling.coordinateRecorder.Service.ServiceManager;
import ling.coordinateRecorder.Service.TransmitService;
@@ -19,6 +20,7 @@ public final class CoordinateRecorder extends JavaPlugin {
private static Plugin authMePlugin;
private static TransmitService transmitService;
private static ItemShowService itemShowService;
private static DangerWarningService dangerWarningService;
private static void start() throws SQLException {
database.installPlugin();
@@ -54,6 +56,8 @@ public final class CoordinateRecorder extends JavaPlugin {
command.setTabCompleter(zb);
transmitService = (TransmitService) ServiceManager.getCurrent().startService(TransmitService.class, 4L);
itemShowService = (ItemShowService) ServiceManager.getCurrent().startService(ItemShowService.class, 200L);
dangerWarningService =
(DangerWarningService) ServiceManager.getCurrent().startService(DangerWarningService.class, 10L);
getLogger().info("加载完毕");
} catch (SQLException e) {
throw new RuntimeException("插件初始化失败", e);
@@ -70,6 +74,10 @@ public final class CoordinateRecorder extends JavaPlugin {
return transmitService;
}
public static DangerWarningService getDangerWarningService() {
return dangerWarningService;
}
@Override
public void onDisable() {
// Plugin shutdown logic

View File

@@ -2,6 +2,7 @@ package ling.coordinateRecorder.Listener;
import ling.coordinateRecorder.Config;
import ling.coordinateRecorder.CoordinateRecorder;
import ling.coordinateRecorder.data.Entity.TraverseLoginPlayerInterface;
import ling.coordinateRecorder.data.PlayerData;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@@ -106,4 +107,10 @@ public class PlayerMap {
if (data != null)
data.updateLocal();
}
public void traversePlayer(TraverseLoginPlayerInterface on) {
for (Map.Entry<UUID, PlayerData> entry : this.playerList.entrySet()) {
on.onLoginPlayer(entry.getValue());
}
}
}

View File

@@ -0,0 +1,69 @@
package ling.coordinateRecorder.Service;
import ling.coordinateRecorder.Listener.PlayerMap;
import ling.coordinateRecorder.data.Entity.TraverseLoginPlayerInterface;
import ling.coordinateRecorder.data.PlayerData;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import org.bukkit.Location;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
/// 危险警告服务
public class DangerWarningService implements Service, TraverseLoginPlayerInterface {
private static final BaseComponent WARNING_MESSAGE;
static {
WARNING_MESSAGE = new TextComponent();
BaseComponent warning = new TextComponent("[危险警告]");
warning.setColor(ChatColor.YELLOW);
warning.setBold(true);
warning.setUnderlined(true);
TranslatableComponent name = new TranslatableComponent(
"entity.minecraft." + EntityType.CREEPER.name().toLowerCase());
name.setBold(true);
WARNING_MESSAGE.addExtra(warning);
WARNING_MESSAGE.addExtra(" ");
WARNING_MESSAGE.addExtra(name);
WARNING_MESSAGE.addExtra(" ");
WARNING_MESSAGE.addExtra("正在你的背后!");
}
@Override
public void start() {
PlayerMap.getCurrent().traversePlayer(this);
}
@Override
public void onLoginPlayer(PlayerData data) {
Player player = data.getPlayer();
Location location = player.getLocation();
Vector direction = location.getDirection();
//玩家背后
Location backLocation = location.clone().add(direction.multiply(-11));
backLocation.setY(location.getY());
// 搜索背后 20 格范围内的实体
double detectionRadius = 10.0;
for (Entity entity : player.getWorld().getNearbyEntities(backLocation, detectionRadius, detectionRadius,
detectionRadius)) {
if (entity instanceof Creeper) {
BaseComponent message = new TextComponent(WARNING_MESSAGE);
BaseComponent distance =
new TextComponent(String.valueOf((int) location.distance(entity.getLocation())));
distance.setColor(ChatColor.YELLOW);
message.addExtra("距离你");
message.addExtra(distance);
message.addExtra("M");
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, message);
break;
}
}
}
}