feat: 完善维护模式与运行期功能管理
This commit is contained in:
@@ -11,8 +11,12 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class MaintenanceManager {
|
||||
|
||||
@@ -38,6 +42,10 @@ public final class MaintenanceManager {
|
||||
config.addDefault("config-version", CURRENT_CONFIG_VERSION);
|
||||
config.addDefault("enabled", false);
|
||||
config.addDefault("bypass-permission", "essentialsc.maintenance.bypass");
|
||||
config.addDefault("notify.enabled", true);
|
||||
config.addDefault("notify.permission", "essentialsc.maintenance.notify");
|
||||
config.addDefault("whitelist.uuids", List.of());
|
||||
config.addDefault("whitelist.names", List.of());
|
||||
config.addDefault("motd.enabled", true);
|
||||
config.addDefault("motd.lines", List.of("&c服务器维护中", "&7请稍后再试"));
|
||||
config.addDefault("kick-message", List.of("&c服务器正在维护", "&7请稍后再进入"));
|
||||
@@ -65,6 +73,66 @@ public final class MaintenanceManager {
|
||||
return config.getString("bypass-permission", "essentialsc.maintenance.bypass");
|
||||
}
|
||||
|
||||
public boolean isNotifyEnabled() {
|
||||
return config.getBoolean("notify.enabled", true);
|
||||
}
|
||||
|
||||
public String getNotifyPermission() {
|
||||
return config.getString("notify.permission", "essentialsc.maintenance.notify");
|
||||
}
|
||||
|
||||
public boolean canJoin(Player player) {
|
||||
return player != null && (player.hasPermission(getBypassPermission()) || isWhitelisted(player));
|
||||
}
|
||||
|
||||
public boolean isWhitelisted(Player player) {
|
||||
if (player == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return getWhitelistUuids().contains(player.getUniqueId().toString().toLowerCase(Locale.ROOT))
|
||||
|| getWhitelistNames().contains(normalizeName(player.getName()));
|
||||
}
|
||||
|
||||
public boolean addWhitelistEntry(String input) {
|
||||
String normalized = normalizeEntry(input);
|
||||
if (normalized.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
UUID uuid = parseUuid(normalized);
|
||||
if (uuid != null) {
|
||||
return addWhitelistUuid(uuid.toString());
|
||||
}
|
||||
|
||||
return addWhitelistName(input);
|
||||
}
|
||||
|
||||
public boolean removeWhitelistEntry(String input) {
|
||||
String normalized = normalizeEntry(input);
|
||||
if (normalized.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
UUID uuid = parseUuid(normalized);
|
||||
if (uuid != null) {
|
||||
return removeWhitelistUuid(uuid.toString());
|
||||
}
|
||||
|
||||
return removeWhitelistName(input);
|
||||
}
|
||||
|
||||
public List<String> getWhitelistEntries() {
|
||||
List<String> entries = new ArrayList<>();
|
||||
entries.addAll(getWhitelistNames());
|
||||
entries.addAll(getWhitelistUuids());
|
||||
return entries;
|
||||
}
|
||||
|
||||
public int getWhitelistCount() {
|
||||
return getWhitelistEntries().size();
|
||||
}
|
||||
|
||||
public boolean isMotdEnabled() {
|
||||
return config.getBoolean("motd.enabled", true);
|
||||
}
|
||||
@@ -100,7 +168,7 @@ public final class MaintenanceManager {
|
||||
if (bossBar == null || player == null || !player.isOnline()) {
|
||||
return;
|
||||
}
|
||||
if (player.hasPermission(getBypassPermission())) {
|
||||
if (canJoin(player)) {
|
||||
bossBar.removePlayer(player);
|
||||
return;
|
||||
}
|
||||
@@ -154,6 +222,81 @@ public final class MaintenanceManager {
|
||||
return Math.max(0.0D, Math.min(1.0D, progress));
|
||||
}
|
||||
|
||||
private boolean addWhitelistUuid(String uuid) {
|
||||
return updateWhitelist("whitelist.uuids", uuid.toLowerCase(Locale.ROOT), true);
|
||||
}
|
||||
|
||||
private boolean addWhitelistName(String name) {
|
||||
return updateWhitelist("whitelist.names", normalizeName(name), true);
|
||||
}
|
||||
|
||||
private boolean removeWhitelistUuid(String uuid) {
|
||||
return updateWhitelist("whitelist.uuids", uuid.toLowerCase(Locale.ROOT), false);
|
||||
}
|
||||
|
||||
private boolean removeWhitelistName(String name) {
|
||||
return updateWhitelist("whitelist.names", normalizeName(name), false);
|
||||
}
|
||||
|
||||
private boolean updateWhitelist(String path, String value, boolean add) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Set<String> entries = path.endsWith(".names") ? getWhitelistNames() : getWhitelistUuids();
|
||||
boolean changed;
|
||||
if (add) {
|
||||
changed = entries.add(value);
|
||||
} else {
|
||||
changed = entries.remove(value);
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
config.set(path, new ArrayList<>(entries));
|
||||
save();
|
||||
refreshBossBar();
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
private Set<String> getWhitelistUuids() {
|
||||
Set<String> entries = new LinkedHashSet<>();
|
||||
for (String value : config.getStringList("whitelist.uuids")) {
|
||||
String normalized = normalizeEntry(value).toLowerCase(Locale.ROOT);
|
||||
if (!normalized.isEmpty()) {
|
||||
entries.add(normalized);
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
private Set<String> getWhitelistNames() {
|
||||
Set<String> entries = new LinkedHashSet<>();
|
||||
for (String value : config.getStringList("whitelist.names")) {
|
||||
String normalized = normalizeName(value);
|
||||
if (!normalized.isEmpty()) {
|
||||
entries.add(normalized);
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
private UUID parseUuid(String input) {
|
||||
try {
|
||||
return UUID.fromString(input);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeEntry(String input) {
|
||||
return input == null ? "" : input.trim();
|
||||
}
|
||||
|
||||
private String normalizeName(String input) {
|
||||
return normalizeEntry(input).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private String colorizeLines(List<String> lines) {
|
||||
if (lines == null || lines.isEmpty()) {
|
||||
return "";
|
||||
|
||||
Reference in New Issue
Block a user