fix: prevent chat handler errors and corrupt stealth.json crashes
This commit is contained in:
+1
-1
@@ -14,7 +14,7 @@ loom {
|
||||
splitEnvironmentSourceSets()
|
||||
|
||||
mods {
|
||||
"localchat" {
|
||||
"msnexus" {
|
||||
sourceSet sourceSets.main
|
||||
sourceSet sourceSets.client
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx2G
|
||||
minecraft_version=26.1.2
|
||||
yarn_mappings=1.21.11+build.6
|
||||
loader_version=0.19.2
|
||||
mod_version=1.0
|
||||
mod_version=1.1
|
||||
maven_group=org.example1
|
||||
archives_base_name=msnexus
|
||||
fabric_version=0.155.2+26.1.2
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.examp.john;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -23,8 +24,18 @@ public class AntiAbuse {
|
||||
private static final int REPEAT_THRESHOLD = 5;
|
||||
private static final double DISTANCE_THRESHOLD = 50.0;
|
||||
private static final long TIME_WINDOW_MS = 5 * 60 * 1000;
|
||||
private static final long ALERT_COOLDOWN_MS = 30 * 1000;
|
||||
|
||||
private static final Map<UUID, List<MessageRecord>> history = new HashMap<>();
|
||||
private static final Map<UUID, Long> lastAlertTime = new HashMap<>();
|
||||
|
||||
public static void register() {
|
||||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
||||
UUID uuid = handler.player.getUUID();
|
||||
history.remove(uuid);
|
||||
lastAlertTime.remove(uuid);
|
||||
});
|
||||
}
|
||||
|
||||
public static void recordMessage(ServerPlayer player, String message) {
|
||||
UUID uuid = player.getUUID();
|
||||
@@ -42,6 +53,7 @@ public class AntiAbuse {
|
||||
}
|
||||
|
||||
private static void checkForAbuse(ServerPlayer player, List<MessageRecord> records) {
|
||||
UUID uuid = player.getUUID();
|
||||
Map<String, int[]> counts = new HashMap<>();
|
||||
Map<String, double[]> bounds = new HashMap<>();
|
||||
|
||||
@@ -65,6 +77,12 @@ public class AntiAbuse {
|
||||
double spread = Math.max(b[1] - b[0], b[3] - b[2]);
|
||||
|
||||
if (spread > DISTANCE_THRESHOLD) {
|
||||
long now = System.currentTimeMillis();
|
||||
Long lastAlert = lastAlertTime.get(uuid);
|
||||
if (lastAlert != null && now - lastAlert < ALERT_COOLDOWN_MS) {
|
||||
continue;
|
||||
}
|
||||
lastAlertTime.put(uuid, now);
|
||||
MSNexus.magicModLog.warn(
|
||||
"[ANTI-ABUSE] Гравець {} ймовірно шукає інших: "
|
||||
+ "'{}' ×{} разів, переміщення ~{} блоків",
|
||||
|
||||
@@ -20,11 +20,25 @@ import java.util.List;
|
||||
|
||||
public class ChatHandler {
|
||||
public static void register() {
|
||||
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) -> {
|
||||
try {
|
||||
return handleMessage(message.signedContent(), sender);
|
||||
} catch (Exception e) {
|
||||
MSNexus.magicModLog.error(
|
||||
"[CHAT] Не вдалося обробити повідомлення від {}", sender.getName().getString(), e);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean handleMessage(String text, ServerPlayer sender) {
|
||||
final String prefix = "!";
|
||||
final float LOCAL_CHAT_RADIUS = 100;
|
||||
|
||||
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) -> {
|
||||
String text = message.signedContent();
|
||||
if (text.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ServerLevel serverLevel = sender.level();
|
||||
List<ServerPlayer> players = serverLevel.getServer().getPlayerList().getPlayers();
|
||||
|
||||
@@ -32,6 +46,9 @@ public class ChatHandler {
|
||||
|
||||
if (text.startsWith(prefix)) {
|
||||
String actualMessage = text.substring(1).trim();
|
||||
if (actualMessage.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Component globalMessage = Component.empty()
|
||||
.append(formattedName)
|
||||
.append(Component.literal(": "))
|
||||
@@ -87,7 +104,6 @@ public class ChatHandler {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@ public class MSNexus implements ModInitializer {
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
StealthStorage.load();
|
||||
AntiAbuse.register();
|
||||
ChatHandler.register();
|
||||
StealthCommand.register();
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class CommandLogMixin {
|
||||
CommandLogger.logCommand(player, command);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
org.examp.john.MSNexus.magicModLog.warn("[CMD] Не вдалося залогувати команду: {}", e.getMessage());
|
||||
org.examp.john.MSNexus.magicModLog.warn("[CMD] Не вдалося залогувати команду", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.examp.john.stealth;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import org.examp.john.MSNexus;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -11,6 +11,7 @@ import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -23,7 +24,9 @@ import java.util.UUID;
|
||||
public class StealthStorage {
|
||||
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
private static final Path CONFIG_PATH = Path.of("config", "localchat", "stealth.json");
|
||||
private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir()
|
||||
.resolve("localchat")
|
||||
.resolve("stealth.json");
|
||||
|
||||
private static Map<UUID, Boolean> data = new HashMap<>();
|
||||
|
||||
@@ -35,23 +38,33 @@ public class StealthStorage {
|
||||
return;
|
||||
}
|
||||
try (Reader reader = Files.newBufferedReader(CONFIG_PATH, StandardCharsets.UTF_8)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Boolean> raw = GSON.fromJson(reader, Map.class);
|
||||
Map<String, Object> raw = GSON.fromJson(reader, Map.class);
|
||||
data = new HashMap<>();
|
||||
if (raw != null) {
|
||||
for (Map.Entry<String, Boolean> entry : raw.entrySet()) {
|
||||
data.put(UUID.fromString(entry.getKey()), entry.getValue());
|
||||
for (Map.Entry<String, Object> entry : raw.entrySet()) {
|
||||
try {
|
||||
UUID uuid = UUID.fromString(entry.getKey());
|
||||
if (entry.getValue() instanceof Boolean value) {
|
||||
data.put(uuid, value);
|
||||
} else {
|
||||
MSNexus.magicModLog.warn(
|
||||
"stealth.json: пропущено некоректне значення для {}", entry.getKey());
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
MSNexus.magicModLog.warn(
|
||||
"stealth.json: пропущено некоректний ключ {}", entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException | JsonSyntaxException | IllegalArgumentException e) {
|
||||
}
|
||||
} catch (IOException | RuntimeException e) {
|
||||
MSNexus.magicModLog.warn("Не вдалося прочитати stealth.json: {}", e.getMessage());
|
||||
data = new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isEnabled(UUID uuid) {
|
||||
return data.getOrDefault(uuid, false);
|
||||
return Boolean.TRUE.equals(data.get(uuid));
|
||||
}
|
||||
|
||||
public static boolean toggle(UUID uuid) {
|
||||
@@ -70,13 +83,20 @@ public class StealthStorage {
|
||||
private static void save() {
|
||||
try {
|
||||
Files.createDirectories(CONFIG_PATH.getParent());
|
||||
try (Writer writer = Files.newBufferedWriter(CONFIG_PATH, StandardCharsets.UTF_8)) {
|
||||
Path tmp = CONFIG_PATH.resolveSibling(CONFIG_PATH.getFileName() + ".tmp");
|
||||
try (Writer writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8)) {
|
||||
Map<String, Boolean> raw = new HashMap<>();
|
||||
for (Map.Entry<UUID, Boolean> entry : data.entrySet()) {
|
||||
raw.put(entry.getKey().toString(), entry.getValue());
|
||||
}
|
||||
GSON.toJson(raw, writer);
|
||||
}
|
||||
try {
|
||||
Files.move(tmp, CONFIG_PATH,
|
||||
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
||||
} catch (IOException e) {
|
||||
Files.move(tmp, CONFIG_PATH, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
MSNexus.magicModLog.error("Не вдалося зберегти stealth.json: {}", e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user