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