Skip to content

Commit

Permalink
反作弊常规修复
Browse files Browse the repository at this point in the history
修复FlightA的激流误判、受伤误判、跳跃提升误判。
迁移反作弊相关指令到/trac。

已知误判:(FlightA)鞘翅。
  • Loading branch information
xia-mc committed Mar 27, 2024
1 parent f4113d0 commit 47363d0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 17 deletions.
5 changes: 1 addition & 4 deletions src/main/java/top/infsky/timerecorder/TimeRecorder.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package top.infsky.timerecorder;

import cn.evole.mods.mcbot.api.McBotEvents;
import cn.evole.mods.mcbot.init.callbacks.IEvents;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;
import top.infsky.timerecorder.command.ICmdEvent;
import top.infsky.timerecorder.config.ICmdEvent;
import top.infsky.timerecorder.config.ModConfig;
import top.infsky.timerecorder.data.StatsData;
import top.infsky.timerecorder.data.StatsDump;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;

public class TimeRecorder implements ModInitializer {
@Override
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/top/infsky/timerecorder/anticheat/Check.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package top.infsky.timerecorder.anticheat;

import lombok.Getter;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -43,11 +45,14 @@ public final void flag(String extraMsg) {
public final void setback(@NotNull Vec3 position) {
if (!CONFIG().isAllowSetback()) return;
player.teleportTo(position.x(), position.y(), position.z());
player.connection.resetPosition();
}

public abstract void _onTick();
public void _onTick() {}

public abstract void _onTeleport();
public void _onTeleport() {}

public void _onPacketReceive(Packet<ServerGamePacketListener> packet) {}

public void update() {
if (player == null) return;
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/top/infsky/timerecorder/anticheat/CheckManager.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package top.infsky.timerecorder.anticheat;

import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import top.infsky.timerecorder.anticheat.checks.BlinkA;
import top.infsky.timerecorder.anticheat.checks.FlightA;
import top.infsky.timerecorder.data.PlayerData;

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

public class CheckManager {
public final PlayerData playerData;
public List<Check> checks = new ArrayList<>();
public double violations = 0;
public CheckManager(List<Check> checks) {
public CheckManager(List<Check> checks, PlayerData playerData) {
this.playerData = playerData;
this.checks.addAll(checks);
}

@Contract("_ -> new")
public static @NotNull CheckManager create(PlayerData playerData) {
final CheckManager checkManager = new CheckManager(List.of(
new FlightA(playerData)
));
new FlightA(playerData),
new BlinkA(playerData)
), playerData);
checkManager.onTeleport();
return checkManager;
}

public void update() {
assert playerData.player != null;
if (playerData.player.isSpectator()) return;
for (Check check : checks) {
check.update();
}
Expand All @@ -35,4 +43,10 @@ public void onTeleport() {
check._onTeleport();
}
}

public void onPacketReceive(Packet<ServerGamePacketListener> packet) {
for (Check check : checks) {
check._onPacketReceive(packet);
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/top/infsky/timerecorder/anticheat/checks/BlinkA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package top.infsky.timerecorder.anticheat.checks;

import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.network.protocol.game.ServerboundKeepAlivePacket;
import org.jetbrains.annotations.NotNull;
import top.infsky.timerecorder.anticheat.Check;
import top.infsky.timerecorder.data.PlayerData;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class BlinkA extends Check {
public Queue<ServerboundKeepAlivePacket> keepAlivePackets = new LinkedBlockingQueue<>(10);

public BlinkA(@NotNull PlayerData playerData) {
super("BlinkA", playerData);
}

@Override
public void _onPacketReceive(Packet<ServerGamePacketListener> packet) {
if (packet instanceof ServerboundKeepAlivePacket) {
try {
keepAlivePackets.add((ServerboundKeepAlivePacket) packet);
} catch (IllegalStateException ignored) {
keepAlivePackets.poll();
keepAlivePackets.add((ServerboundKeepAlivePacket) packet);
}
}
}

@Override
public void _onTick() {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package top.infsky.timerecorder.anticheat.checks;

import net.minecraft.world.item.TridentItem;
import net.minecraft.world.phys.Vec3;
import top.infsky.timerecorder.anticheat.Check;
import top.infsky.timerecorder.data.PlayerData;
Expand All @@ -18,6 +19,7 @@ public FlightA(PlayerData player) {

@Override
public void _onTick() {
if (player.getUseItem().getItem() instanceof TridentItem) return;

if (disableTick > 0) {
disableTick--;
Expand All @@ -36,9 +38,17 @@ public void _onTick() {
setbackPos = lastInWaterPos;
}

// fix hurt
if (player.hurtTime > 0) {
jumpTick = 6;
setbackPos = currentPos;
}


if (!player.onGround() && jumpTick > 0
&& currentPos.y() - lastOnGroundPos.y() < 1.25219 + CONFIG().getThreshold()) {
&& currentPos.y() - lastOnGroundPos.y() < 1.25219 * (1 + player.getJumpBoostPower()) + CONFIG().getThreshold()
&& currentPos.distanceTo(lastPos) < player.getSpeed() + CONFIG().getThreshold()
) {
jumpTick--;
} else if (!player.isInWater() && waterTick > 0
// && (lastPos.y() - lastPos2.y() + CONFIG().getThreshold()) > (player.position().y() - lastPos.y()) // 警惕出水弱检测
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.infsky.timerecorder.command;
package top.infsky.timerecorder.anticheat.command;

import com.mojang.brigadier.context.CommandContext;
import net.minecraft.ChatFormatting;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package top.infsky.timerecorder.anticheat.command;

import com.mojang.brigadier.context.CommandContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;

public class HelpCommand {
private static final String opHelpMsg = """
§r§b§lTimeRecorder Anti-Cheat§r
§r/trac help §f- §7显示此帮助信息§r
§r/trac alert §f- §7显示警报信息§r
""";

private static final String memberHelpMsg = """
§r§b§lTimeRecorder Anti-Cheat§r
§r/tr help §f- §7显示此帮助信息§r
""";

public static int execute(@NotNull CommandContext<CommandSourceStack> context) {
context.getSource().sendSystemMessage(Component.literal(
context.getSource().hasPermission(2) ? opHelpMsg : memberHelpMsg
));

return 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package top.infsky.timerecorder.command;
package top.infsky.timerecorder.config;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.commands.CommandSourceStack;
import org.jetbrains.annotations.NotNull;
import top.infsky.timerecorder.anticheat.command.*;
import top.infsky.timerecorder.command.*;

import static net.minecraft.commands.Commands.*;

public class ICmdEvent {
public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispatcher) {
// TimeRecorder本体
dispatcher.register(literal("tr") // 没参数默认显示帮助信息
.executes(HelpCommand::execute)
.executes(top.infsky.timerecorder.command.HelpCommand::execute)
.then(literal("help") // 显示帮助信息
.executes(HelpCommand::execute))
.executes(top.infsky.timerecorder.command.HelpCommand::execute))
.then(literal("reload") // 热重载
.requires(source -> source.hasPermission(2))
.executes(ReloadCommand::execute))
.then(literal("dump")
.requires(source -> source.hasPermission(2))
.then(literal("save") // 保存迄今的统计数据
.executes(DumpCommand.Save::execute))
.executes(DumpCommand.Save::execute))
.then(literal("load") // 从文件还原统计数据
.executes(DumpCommand.Load::execute)
.then(argument("filename", StringArgumentType.string())
Expand All @@ -37,8 +40,13 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa
.executes(RecallCommand::execute)
.then(literal("confirm") // 确认撤回(由/tr recall触发)
.then(argument("message_id", IntegerArgumentType.integer())
.executes(RecallCommand.Confirm::execute))))
.then(literal("alert") // 启用警报
.executes(RecallCommand.Confirm::execute))))
);

// TimeRecorder Anti-Cheat 模块
dispatcher.register(literal("trac") // 没参数默认显示帮助信息
.executes(top.infsky.timerecorder.anticheat.command.HelpCommand::execute)
.then(literal("alert") // 显示警报
.requires(source -> source.hasPermission(2))
.executes(AlertCommand::execute))
);
Expand Down

0 comments on commit 47363d0

Please sign in to comment.