Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 394
src/main/java/com/kodari/sentinelac/SentinelAC.java
package com.kodari.sentinelac;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.permissions.Permissions;
import net.minecraft.world.entity.player.Abilities;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.storage.LevelResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
public class SentinelAC implements ModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger("sentinelac");
private static final Set<UUID> BANNED = new HashSet<>();
private static final Set<UUID> BYPASSED = new HashSet<>();
private static final Set<UUID> ADMINS = new HashSet<>();
private static final Map<UUID, Long> BAN_EXPIRIES = new HashMap<>();
private static final Map<UUID, Integer> WARNINGS = new HashMap<>();
private static final Map<UUID, Long> WARNING_EXPIRIES = new HashMap<>();
private static final Map<UUID, OreStats> ORE_STATS = new HashMap<>();
private static final long STATS_WINDOW_MILLIS = 10 * 60 * 1000L;
private static final long MAX_DURATION_SECONDS = 31_536_000L;
private static Path dataFile;
private static boolean loaded;
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> dispatcher.register(
Commands.literal("ac")
.requires(SentinelAC::canModerate)
.then(Commands.literal("ban")
.then(Commands.argument("target", EntityArgument.player()).executes(context -> {
ServerPlayer target = EntityArgument.getPlayer(context, "target");
BANNED.add(target.getUUID());
BAN_EXPIRIES.put(target.getUUID(), 0L);
saveData();
target.connection.disconnect(Component.literal("Banned by a moderator."));
return success(context.getSource(), "Banned " + target.getPlainTextName() + ".");
})))
.then(Commands.literal("kick")
.then(Commands.argument("target", EntityArgument.player()).executes(context -> {
ServerPlayer target = EntityArgument.getPlayer(context, "target");
target.connection.disconnect(Component.literal("Kicked by a moderator."));
return success(context.getSource(), "Kicked " + target.getPlainTextName() + ".");
})))
.then(Commands.literal("warn")
.then(Commands.argument("target", EntityArgument.player()).executes(context -> {
ServerPlayer target = EntityArgument.getPlayer(context, "target");
int count = WARNINGS.merge(target.getUUID(), 1, Integer::sum);
saveData();
target.sendSystemMessage(Component.literal("You have been warned by staff. Warning count: " + count + "."));
return success(context.getSource(), "Warned " + target.getPlainTextName() + ".");
})))
.then(Commands.literal("timeban")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.argument("seconds", StringArgumentType.word()).executes(context -> {
long seconds = readDuration(context);
if (seconds < 1) return 0;
ServerPlayer target = EntityArgument.getPlayer(context, "target");
BANNED.add(target.getUUID());
BAN_EXPIRIES.put(target.getUUID(), System.currentTimeMillis() + seconds * 1000L);
saveData();
long minutes = Math.max(1, seconds / 60);
target.connection.disconnect(Component.literal("Temporarily banned for " + seconds + " seconds."));
return success(context.getSource(), "Time-banned " + target.getPlainTextName() + " for " + minutes + " minute(s).");
}))))
.then(Commands.literal("timewarn")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.argument("seconds", StringArgumentType.word()).executes(context -> {
long seconds = readDuration(context);
if (seconds < 1) return 0;
ServerPlayer target = EntityArgument.getPlayer(context, "target");
int count = WARNINGS.merge(target.getUUID(), 1, Integer::sum);
WARNING_EXPIRIES.put(target.getUUID(), System.currentTimeMillis() + seconds * 1000L);
saveData();
target.sendSystemMessage(Component.literal("You have received a staff warning active for " + seconds + " seconds. Warning count: " + count + "."));
return success(context.getSource(), "Time-warned " + target.getPlainTextName() + ".");
}))))
.then(Commands.literal("bypass")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.literal("enable").executes(context -> setAccess(context, false, true)))
.then(Commands.literal("disable").executes(context -> setAccess(context, false, false)))))
.then(Commands.literal("admin")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.literal("enable").executes(context -> setAccess(context, true, true)))
.then(Commands.literal("disable").executes(context -> setAccess(context, true, false)))))
.then(Commands.literal("god")
.then(Commands.argument("target", EntityArgument.player()).executes(context -> {
ServerPlayer target = EntityArgument.getPlayer(context, "target");
Abilities abilities = target.getAbilities();
abilities.invulnerable = !abilities.invulnerable;
target.onUpdateAbilities();
return success(context.getSource(), "God mode " + (abilities.invulnerable ? "enabled for " : "disabled for ") + target.getPlainTextName() + ".");
})))
.then(Commands.literal("fly")
.then(Commands.argument("target", EntityArgument.player()).executes(context -> {
ServerPlayer target = EntityArgument.getPlayer(context, "target");
Abilities abilities = target.getAbilities();
abilities.mayfly = !abilities.mayfly;
if (!abilities.mayfly) abilities.flying = false;
target.onUpdateAbilities();
return success(context.getSource(), "Flight " + (abilities.mayfly ? "enabled for " : "disabled for ") + target.getPlainTextName() + ".");
})))
.then(Commands.literal("teleport")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.argument("destination", EntityArgument.player()).executes(context -> {
ServerPlayer target = EntityArgument.getPlayer(context, "target");
ServerPlayer destination = EntityArgument.getPlayer(context, "destination");
teleport(target, destination);
return success(context.getSource(), "Teleported " + target.getPlainTextName() + " to " + destination.getPlainTextName() + ".");
}))))
.then(Commands.literal("bring")
.then(Commands.argument("target", EntityArgument.player()).executes(context -> {
ServerPlayer destination = context.getSource().getPlayer();
if (destination == null) {
context.getSource().sendFailure(Component.literal("Bring must be used by an in-game moderator."));
return 0;
}
ServerPlayer target = EntityArgument.getPlayer(context, "target");
teleport(target, destination);
return success(context.getSource(), "Brought " + target.getPlainTextName() + ".");
})))
));
ServerPlayConnectionEvents.JOIN.register((listener, sender, server) -> {
ensureLoaded(server);
UUID playerId = listener.player.getUUID();
Long banExpiry = BAN_EXPIRIES.get(playerId);
if (banExpiry != null) {
if (banExpiry == 0L || banExpiry > System.currentTimeMillis()) {
listener.disconnect(Component.literal(banExpiry == 0L ? "You are banned from this server." : "Your temporary ban is still active."));
return;
}
BAN_EXPIRIES.remove(playerId);
BANNED.remove(playerId);
saveData();
}
Long warningExpiry = WARNING_EXPIRIES.get(playerId);
if (warningExpiry != null) {
if (warningExpiry > System.currentTimeMillis()) {
listener.player.sendSystemMessage(Component.literal("You have an active staff warning."));
} else {
WARNING_EXPIRIES.remove(playerId);
saveData();
}
}
});
PlayerBlockBreakEvents.AFTER.register((level, player, pos, state, blockEntity) -> {
if (!(player instanceof ServerPlayer serverPlayer) || !isTrackedOre(state)) return;
if (BYPASSED.contains(serverPlayer.getUUID()) || ADMINS.contains(serverPlayer.getUUID())) return;
long now = System.currentTimeMillis();
OreStats stats = ORE_STATS.computeIfAbsent(serverPlayer.getUUID(), id -> new OreStats(now));
if (now - stats.windowStarted > STATS_WINDOW_MILLIS) {
stats.reset(now);
}
stats.total++;
if (!isExposed(level, pos)) stats.hidden++;
if (!stats.flagged && stats.total >= 8 && stats.hidden >= 6 && stats.hidden / (double) stats.total >= 0.8) {
stats.flagged = true;
LOGGER.warn("Possible x-ray: {} ({}) mined {} tracked ores, {} apparently unexposed, in ten minutes.", serverPlayer.getPlainTextName(), serverPlayer.getUUID(), stats.total, stats.hidden);
}
});
}
private static boolean canModerate(CommandSourceStack source) {
ensureLoaded(source.getServer());
ServerPlayer player = source.getPlayer();
return source.permissions().hasPermission(Permissions.COMMANDS_MODERATOR) || player != null && ADMINS.contains(player.getUUID());
}
private static int success(CommandSourceStack source, String message) {
source.sendSuccess(() -> Component.literal(message), true);
return 1;