mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2025-01-15 03:51:20 +01:00
Quick adaptions/cleanups for packet level checking.
This commit is contained in:
parent
cdf79c854f
commit
bb8b8fc1f2
@ -26,6 +26,7 @@ import fr.neatmonster.nocheatplus.utilities.ActionFrequency;
|
||||
*/
|
||||
public class MoveFrequency extends PacketAdapter implements Listener, JoinLeaveListener {
|
||||
|
||||
// TODO: Configuration.
|
||||
// TODO: Optimized options (receive only, other?).
|
||||
// TODO: Async version ?
|
||||
|
||||
@ -69,8 +70,8 @@ public class MoveFrequency extends PacketAdapter implements Listener, JoinLeaveL
|
||||
removeName(player.getName());
|
||||
}
|
||||
|
||||
private ActionFrequency getFreq(String name) {
|
||||
ActionFrequency freq = this.freqMap.get(name);
|
||||
private ActionFrequency getFreq(final String name) {
|
||||
final ActionFrequency freq = this.freqMap.get(name);
|
||||
if (freq == null) {
|
||||
return addName(name);
|
||||
} else {
|
||||
@ -79,9 +80,9 @@ public class MoveFrequency extends PacketAdapter implements Listener, JoinLeaveL
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPacketReceiving(PacketEvent event) {
|
||||
public void onPacketReceiving(final PacketEvent event) {
|
||||
// TODO: Add several (at least has look + has pos individually, maybe none/onground)
|
||||
ActionFrequency freq = getFreq(event.getPlayer().getName());
|
||||
final ActionFrequency freq = getFreq(event.getPlayer().getName());
|
||||
freq.add(System.currentTimeMillis(), 1f);
|
||||
if (freq.score(1f) > 300) {
|
||||
event.setCancelled(true);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package fr.neatmonster.nocheatplus.net.protocollib;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@ -25,18 +26,23 @@ public class ProtocolLibComponent implements DisableListener{
|
||||
// Register with ProtocolLib
|
||||
final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
|
||||
LogUtil.logInfo("[NoCheatPlus] ProtocolLib seems to be available.");
|
||||
// Classes having a constructor with Plugin as argument.
|
||||
List<Class<? extends PacketAdapter>> adapterClasses = Arrays.asList(
|
||||
MoveFrequency.class,
|
||||
WeatherDistance.class
|
||||
);
|
||||
for (Class<? extends PacketAdapter> clazz : adapterClasses) {
|
||||
try {
|
||||
PacketAdapter adapter = new MoveFrequency(plugin);
|
||||
PacketAdapter weatherAdapter = new WeatherDistance(plugin);
|
||||
|
||||
// Construct a new instance using reflection.
|
||||
PacketAdapter adapter = clazz.getDeclaredConstructor(Plugin.class).newInstance(plugin);
|
||||
protocolManager.addPacketListener(adapter);
|
||||
protocolManager.addPacketListener(weatherAdapter);
|
||||
registeredPacketAdapters.add(adapter);
|
||||
} catch (Throwable t) {
|
||||
LogUtil.logWarning("[NoCheatPlus] Could not register some packet-level hook.");
|
||||
LogUtil.logWarning("[NoCheatPlus] Could not register packet level hook: " + clazz.getSimpleName());
|
||||
LogUtil.logWarning(t); // TODO: Maybe temporary.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
@ -1,37 +1,43 @@
|
||||
package fr.neatmonster.nocheatplus.net.protocollib;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.events.PacketAdapter;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.events.PacketEvent;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import com.comphenix.protocol.reflect.StructureModifier;
|
||||
|
||||
import fr.neatmonster.nocheatplus.utilities.TrigUtil;
|
||||
|
||||
public class WeatherDistance extends PacketAdapter {
|
||||
|
||||
/** Maximum distance for thunder effects (squared). */
|
||||
private static final double distSq = 512.0 * 512.0; // TODO: Maybe configurable.
|
||||
|
||||
public WeatherDistance(Plugin plugin) {
|
||||
super(plugin, PacketType.Play.Server.NAMED_SOUND_EFFECT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPacketSending(PacketEvent event) {
|
||||
PacketContainer packetContainer = event.getPacket();
|
||||
Player player = event.getPlayer();
|
||||
public void onPacketSending(final PacketEvent event) {
|
||||
final PacketContainer packetContainer = event.getPacket();
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
String soundEffect = packetContainer.getStrings().read(0);
|
||||
|
||||
if (!soundEffect.equals("ambient.weather.thunder"))
|
||||
// Compare sound effect name.
|
||||
if (!packetContainer.getStrings().read(0).equals("ambient.weather.thunder")) {
|
||||
return;
|
||||
}
|
||||
|
||||
double locX = packetContainer.getIntegers().read(0) / 8;
|
||||
double locY = packetContainer.getIntegers().read(1) / 8;
|
||||
double locZ = packetContainer.getIntegers().read(2) / 8;
|
||||
final Location loc = player.getLocation(); // TODO: Use getLocation(useLoc) [synced if async].
|
||||
|
||||
Location weatherLocation = new Location(player.getWorld(), locX, locY, locZ);
|
||||
|
||||
if (player.getLocation().distance(weatherLocation) > 512.0F) {
|
||||
// Compare distance of player to the weather location.
|
||||
final StructureModifier<Integer> ints = packetContainer.getIntegers();
|
||||
if (TrigUtil.distanceSquared(ints.read(0) / 8, ints.read(1) / 8, ints.read(2) / 8, loc.getX(), loc.getY(), loc.getZ()) > distSq) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user