Skip to content

Commit

Permalink
INFO显示修改 新增一堆开关
Browse files Browse the repository at this point in the history
  • Loading branch information
Noctiro committed Aug 2, 2022
1 parent b273d93 commit 1f94f7f
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 20 deletions.
26 changes: 21 additions & 5 deletions src/main/java/com/obcbo/aliwaka/Aliwaka.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import java.util.Objects;
import java.util.logging.Logger;

import static com.obcbo.aliwaka.file.Config.crEnable;
import static com.obcbo.aliwaka.file.Config.guardEnable;

public final class Aliwaka extends JavaPlugin {
public static final Logger logger = Logger.getLogger("Aliwaka");

@Override
public void onEnable() {
Metrics metrics = new Metrics(this, 15979);// bstats统计
new Metrics(this, 15979);// bstats统计
logger.info("MAIN > 开始加载");
saveDefaultConfig();
saveResource("message.yml", false);// false为不覆盖 true为每次调用都覆盖
Expand All @@ -33,8 +36,17 @@ public void onEnable() {
}
logger.info("COMMAND > 命令注册完毕");

Bukkit.getScheduler().runTask(this, PointsChecker::start);
Bukkit.getScheduler().runTask(this, Guard::start);
Bukkit.getScheduler().runTask(this, () -> {
if (crEnable) {
PointsChecker.start();
}
});
//Bukkit.getScheduler().runTask(this, Guard::start);
Bukkit.getScheduler().runTask(this, () -> {
if (guardEnable) {
Guard.start();
}
});
logger.info("TASK > 任务开始加载");
logger.info("MAIN > 成功启用插件");
}
Expand All @@ -51,8 +63,12 @@ public static void reload() {
Guard.stop();
Config.load();
Message.load();
PointsChecker.start();
Guard.start();
if (crEnable) {
PointsChecker.start();
}
if (guardEnable) {
Guard.start();
}
logger.info("重载完成");
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/obcbo/aliwaka/command/function.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;

import static com.obcbo.aliwaka.file.Config.crEnable;
import static com.obcbo.aliwaka.file.Message.*;
import static com.obcbo.aliwaka.task.AntiCR.CountChunk.playerPoints;

Expand Down Expand Up @@ -37,6 +38,11 @@ public static boolean core(CommandSender sender, String[] args) {
} else if ("stop".equals(args[2])) {
PointsChecker.stop();
} else if ("list".equals(args[2])) {
if (!crEnable) {// 功能关闭时执行
sender.sendMessage(Message.prefix + functionDisable);
return true;
}

sender.sendMessage(Message.prefix + crOutput);

List<Map.Entry<String, Integer>> list = new ArrayList<>(playerPoints.entrySet()); //转换为list
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/obcbo/aliwaka/command/info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.obcbo.aliwaka.command;

import com.obcbo.aliwaka.file.Message;
import com.obcbo.aliwaka.task.AntiCR.PointsChecker;
import com.obcbo.aliwaka.task.Guard;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
Expand All @@ -16,13 +17,16 @@ public class info {
public static boolean core(CommandSender sender) {
sender.sendMessage(Message.prefix + "开始输出服务器信息");

sender.sendMessage("§6服务器版本§7:§f " + Bukkit.getVersion() + String.format(" §6在线玩家§7:§f §f%d/%d", Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers()));
sender.sendMessage("§6危险值§7:§f " + Guard.getWarn());
sender.sendMessage("§6服务器版本§7:§f " + Bukkit.getVersion() +
String.format(" §6在线玩家§7:§f §f%d/%d", Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers()));
sender.sendMessage("§6危险值§7:§f " + Guard.getWarn() +
" §f(§6Guard§7:§f " + (Guard.guard.isAlive() ? "§a运行中" : "§c已关闭") +
" §6AntiCR§7:§f " + (PointsChecker.PointsChecker.isAlive() ? "§a运行中" : "§c已关闭") + "§f)");

long max = Runtime.getRuntime().maxMemory();
long use = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
sender.sendMessage("§6内存§7:§f " + String.format("%.2f%%", use / (double) max * 100) + " (" + unitByte(max) + "§7-§f" + unitByte(use) + "§7=§f" + unitByte(max - use) + " 分配§7:§f" + unitByte(Runtime.getRuntime().totalMemory()) + ")");

DecimalFormat df = new DecimalFormat("#.00");// 保留小数点后两位
List<String> tps = new ArrayList<>(4);// tps值有4个
for (double single : Bukkit.getTPS()) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/obcbo/aliwaka/file/Config.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.obcbo.aliwaka.file;

import com.obcbo.aliwaka.Aliwaka;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static com.obcbo.aliwaka.Aliwaka.logger;

public class Config {
static final Plugin config = JavaPlugin.getProvidingPlugin(Aliwaka.class);
public static int CD;
public static boolean crEnable;
public static int crCheckInterval;
public static int crSpeedInterval;
public static float crSpeedNormalWalk;
Expand All @@ -32,10 +31,14 @@ public class Config {
public static int crSecondCondition;
public static int crSecondReduce;

public static boolean guardEnable;
public static int guardCheckInterval;
public static boolean memEnable;
public static int memPercentage;
public static boolean tpsEnable;
public static int tpsWarnThreshold;
public static int tpsDangerThreshold;
public static boolean msptEnable;
public static int msptDangerThreshold;

public static @NotNull FileConfiguration getConfig() {
Expand All @@ -48,6 +51,7 @@ public static void load() {
}

CD = getConfig().getInt("CD", 10000);
crEnable = getConfig().getBoolean("AntiCR.enable", true);
crCheckInterval = getConfig().getInt("AntiCR.check-interval", 10000);
crListenCommand = getConfig().getStringList("AntiCR.listen-command.text");
crCommandImplement = getConfig().getInt("AntiCR.listen-command.implement", 200);
Expand All @@ -63,10 +67,14 @@ public static void load() {
crSecondCondition = getConfig().getInt("AntiCR.num.second-condition", 10);
crSecondReduce = getConfig().getInt("AntiCR.num.second-reduce", 10);

guardEnable = getConfig().getBoolean("Guard.enable", true);
guardCheckInterval = getConfig().getInt("Guard.check-interval", 10000);
memEnable = getConfig().getBoolean("Guard.mem-check.enable", true);
memPercentage = getConfig().getInt("Guard.mem-check.percentage", 90);
tpsEnable = getConfig().getBoolean("Guard.tps-check.enable", true);
tpsWarnThreshold = getConfig().getInt("Guard.tps-check.warn-threshold", 18);
tpsDangerThreshold = getConfig().getInt("Guard.tps-check.danger-threshold", 3);
msptEnable = getConfig().getBoolean("Guard.mspt-check.enable", true);
msptDangerThreshold = getConfig().getInt("Guard.mspt-check.danger-threshold", 80);
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/obcbo/aliwaka/file/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Message {
public static String CD;
public static String notComplete;
public static String wrongCommand;
public static String functionDisable;
public static String gcStart;
public static String gcEnd;
public static String crOutput;
Expand All @@ -36,7 +37,7 @@ public class Message {
public static String shellError;

public static void load() {
if (!(message.getInt("version") == 1)) {
if (!(message.getInt("version") == 2)) {
logger.warning("消息文件message.yml是旧版");
}
prefix = get("prefix", "&8[&6Ali&ewaka&8]&r ");
Expand All @@ -48,6 +49,7 @@ public static void load() {
CD = get("CD", "冷却中");
notComplete = get("not-complete", "你似乎还没有输入完命令");
wrongCommand = get("wrong-command", "错误的命令");
functionDisable = get("function-disable", "此功能处于关闭状态");
gcStart = get("GC.start", "服务器开始强制回收内存,可能会有短暂卡顿");
gcEnd = get("GC.end", "服务器内存回收完成 耗时%time%ms");
crLimit = get("AntiCR.limit", "暂时限制速度");
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/obcbo/aliwaka/task/AntiCR/CountChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class CountChunk implements Listener {

@EventHandler
public void playerChunkLoadEvent(PlayerChunkLoadEvent event) {
if (!crEnable) return;// 获取功能是否开启
add(event.getPlayer().getName());
}

@EventHandler
public void playerCommandPreprocessEvent(PlayerCommandPreprocessEvent event) {
if (!crEnable) return;
for (String n : crListenCommand) {
if (event.getMessage().startsWith("/" + n)) {
int set = playerPoints.get(event.getPlayer().getName()) >= crCommandImplement ?
Expand All @@ -34,6 +36,7 @@ public void playerCommandPreprocessEvent(PlayerCommandPreprocessEvent event) {

@EventHandler
public void playerJoinEvent(PlayerJoinEvent event) {
if (!crEnable) return;
// 防止玩家退出无法恢复到正常速度
if (!playerPoints.containsKey(event.getPlayer().getName())) {
event.getPlayer().setWalkSpeed(crSpeedNormalWalk);
Expand All @@ -43,11 +46,13 @@ public void playerJoinEvent(PlayerJoinEvent event) {

@EventHandler
public void playerQuitEvent(PlayerQuitEvent event) {
if (!crEnable) return;
// 玩家退出时删除记录
playerPoints.remove(event.getPlayer().getName());
}

private void add(String player) {
if (!crEnable) return;
if (playerPoints.containsKey(player)) {
int now = playerPoints.get(player) + 1;
playerPoints.put(player, now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class PointsChecker implements Runnable {
private static boolean on = true;
static final Set<Player> controlList = new HashSet<>();
private static final Thread PointsChecker = new Thread(new PointsChecker(), "Aliwaka-PointsChecker");
public static final Thread PointsChecker = new Thread(new PointsChecker(), "Aliwaka-PointsChecker");

public static void start() {
if (PointsChecker.isAlive()) {
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/obcbo/aliwaka/task/Guard.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static com.obcbo.aliwaka.file.Message.gcStart;

public class Guard implements Runnable {
private static final Thread guard = new Thread(new Guard(), "Aliwaka-Guard");
public static final Thread guard = new Thread(new Guard(), "Aliwaka-Guard");
private static boolean on = true;
private static int warn = 0;// 危险值

Expand Down Expand Up @@ -47,9 +47,15 @@ public void run() {
}

private void core() {
memcheck();
tpscheck();
msptcheck();
if (memEnable) {
memcheck();
}
if (tpsEnable) {
tpscheck();
}
if (msptEnable) {
msptcheck();
}
try {
Thread.sleep(guardCheckInterval);
} catch (InterruptedException e) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# 配置文件版本 非必要不要更改
# 配置文件版本 非必要不要更改
version: 3
# 普通玩家命令CD时间 目前仅用于GC指令 单位ms
CD: 10000
# 跑图检测
AntiCR:
# 功能开关 true为开
enable: true
# 检测间隔 单位ms
check-interval: 10000
# 命令开头为设置值 就减少设置值
Expand Down Expand Up @@ -33,6 +35,7 @@ AntiCR:
implement: 1500
# 执行后减少到
after-implement: 1000
# 后面这四个数值 设置为0代表禁用
# 第一次判断 大于等于数值时
first-condition: 100
# 第一次判断返回为true就减少设置数值
Expand All @@ -43,19 +46,23 @@ AntiCR:
second-reduce: 10
# 卡顿检测
Guard:
enable: true
# 检测间隔 单位ms
check-interval: 10000
# 内存检测
mem-check:
enable: true
# 占用百分比 当达到设置值及以上时 执行GC
percentage: 90
# TPS检测
tps-check:
enable: true
# 执行值 当TPS低于此值时 开始计算危险值
warn-threshold: 18
# 危险值 低于设置值时 立刻执行GC和区块回收
danger-threshold: 3
# MSPT检测
mspt-check:
enable: true
# MSPT数值 当达到设置值及以上时 执行GC
danger-threshold: 80
6 changes: 4 additions & 2 deletions src/main/resources/message.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 配置文件版本 非必要不要更改
version: 1
# 配置文件版本 非必要不要更改
version: 2
# 消息前缀
prefix: "&8[&6Ali&ewaka&8]&r "
# 显示颜色
Expand All @@ -25,6 +25,8 @@ CD: "冷却中"
not-complete: "你似乎还没有输入完命令"
# 错误的命令
wrong-command: "错误的命令"
# 关闭的功能
function-disable: "此功能处于关闭状态"
# 垃圾回收
GC:
start: "服务器开始强制回收内存,可能会有短暂卡顿"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '${version}'
main: com.obcbo.aliwaka.Aliwaka
api-version: 1.16
authors: [ ObcbO ]
description: Mik automatic management robot
description: Automatic management plugin
website: https://github.com/Encinet/Aliwaka
#此为非必须依赖
#softdepend:
Expand Down

0 comments on commit 1f94f7f

Please sign in to comment.