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