84 lines
3.2 KiB
Java
84 lines
3.2 KiB
Java
package me.konsolas.aac.heuristics.combat;
|
|
|
|
import com.comphenix.protocol.PacketType;
|
|
import com.comphenix.protocol.PacketType.Play.Client;
|
|
import com.comphenix.protocol.ProtocolLibrary;
|
|
import com.comphenix.protocol.events.PacketAdapter;
|
|
import com.comphenix.protocol.events.PacketEvent;
|
|
import com.comphenix.protocol.events.PacketListener;
|
|
import com.comphenix.protocol.wrappers.EnumWrappers.EntityUseAction;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.TimeUnit;
|
|
import me.konsolas.aac.AAC;
|
|
import me.konsolas.aac.heuristics.base.Heuristic;
|
|
import me.konsolas.aac.internal.packetwrapper.WrapperPlayClientUseEntity;
|
|
import net.jodah.expiringmap.ExpirationPolicy;
|
|
import net.jodah.expiringmap.ExpiringMap;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
public class AccuracyHeuristic extends Heuristic {
|
|
private final PacketListener packetListener;
|
|
private final ExpiringMap<UUID, Boolean> recentMovementPackets = ExpiringMap.builder()
|
|
.expiration(8L, TimeUnit.MILLISECONDS)
|
|
.expirationPolicy(ExpirationPolicy.CREATED)
|
|
.build();
|
|
|
|
public AccuracyHeuristic(AAC plugin) {
|
|
super(plugin);
|
|
this.packetListener = new AttackTimingPacketListener(
|
|
this.plugin, Client.FLYING, Client.POSITION, Client.POSITION_LOOK, Client.USE_ENTITY, Client.LOOK
|
|
);
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(this.packetListener);
|
|
}
|
|
|
|
@Override
|
|
public void destroy() {
|
|
super.destroy();
|
|
ProtocolLibrary.getProtocolManager().removePacketListener(this.packetListener);
|
|
}
|
|
|
|
@Override
|
|
public int getProbability(Player player) {
|
|
int probability = (int) (Math.pow(this.getRawScore(player), 0.8) / 2.0);
|
|
return probability >= 100 ? 99 : probability;
|
|
}
|
|
|
|
private final class AttackTimingPacketListener extends PacketAdapter {
|
|
private AttackTimingPacketListener(Plugin plugin, PacketType... types) {
|
|
super(plugin, types);
|
|
}
|
|
|
|
@Override
|
|
public void onPacketReceiving(PacketEvent event) {
|
|
if (event.getPacketType() == Client.USE_ENTITY) {
|
|
if (new WrapperPlayClientUseEntity(event.getPacket()).getAction() == EntityUseAction.ATTACK) {
|
|
if (AccuracyHeuristic.this.recentMovementPackets.containsKey(event.getPlayer().getUniqueId())) {
|
|
AccuracyHeuristic.this.plugin
|
|
.getServer()
|
|
.getScheduler()
|
|
.scheduleSyncDelayedTask(AccuracyHeuristic.this.plugin, new AttackTimingViolationTask(event.getPlayer()), 150L);
|
|
} else {
|
|
AccuracyHeuristic.this.setRawScore(event.getPlayer(), (int) (AccuracyHeuristic.this.getRawScore(event.getPlayer()) * 0.85));
|
|
}
|
|
}
|
|
} else {
|
|
AccuracyHeuristic.this.recentMovementPackets.put(event.getPlayer().getUniqueId(), false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private final class AttackTimingViolationTask implements Runnable {
|
|
private final Player player;
|
|
|
|
private AttackTimingViolationTask(Player player) {
|
|
this.player = player;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
AccuracyHeuristic.this.setRawScore(this.player, AccuracyHeuristic.this.getRawScore(this.player) + 50);
|
|
}
|
|
}
|
|
}
|