Skip to content

Commit

Permalink
Merge pull request #1600 from SamB440/feat/entity-pushing-2
Browse files Browse the repository at this point in the history
Limit entity push abuse
  • Loading branch information
SamB440 authored Aug 11, 2024
2 parents 2ff0bcd + 018197c commit 4359567
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.github.retrooper.packetevents.protocol.potion.PotionType;
import com.github.retrooper.packetevents.util.Vector3d;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerFlying;
Expand All @@ -26,9 +27,12 @@

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

public class PacketEntityReplication extends Check implements PacketCheck {

private boolean hasSentPreWavePacket = true;

// Let's imagine the player is on a boat.
// The player breaks this boat
// If we were to despawn the boat without an extra transaction, then the boat would disappear before
Expand Down Expand Up @@ -85,15 +89,15 @@ public void onPacketSend(PacketSendEvent event) {
}
if (event.getPacketType() == PacketType.Play.Server.SPAWN_LIVING_ENTITY) {
WrapperPlayServerSpawnLivingEntity packetOutEntity = new WrapperPlayServerSpawnLivingEntity(event);
addEntity(packetOutEntity.getEntityId(), packetOutEntity.getEntityType(), packetOutEntity.getPosition(), packetOutEntity.getYaw(), packetOutEntity.getPitch(), packetOutEntity.getEntityMetadata(), 0);
addEntity(packetOutEntity.getEntityId(), packetOutEntity.getEntityUUID(), packetOutEntity.getEntityType(), packetOutEntity.getPosition(), packetOutEntity.getYaw(), packetOutEntity.getPitch(), packetOutEntity.getEntityMetadata(), 0);
}
if (event.getPacketType() == PacketType.Play.Server.SPAWN_ENTITY) {
WrapperPlayServerSpawnEntity packetOutEntity = new WrapperPlayServerSpawnEntity(event);
addEntity(packetOutEntity.getEntityId(), packetOutEntity.getEntityType(), packetOutEntity.getPosition(), packetOutEntity.getYaw(), packetOutEntity.getPitch(), null, packetOutEntity.getData());
addEntity(packetOutEntity.getEntityId(), packetOutEntity.getUUID().orElse(null), packetOutEntity.getEntityType(), packetOutEntity.getPosition(), packetOutEntity.getYaw(), packetOutEntity.getPitch(), null, packetOutEntity.getData());
}
if (event.getPacketType() == PacketType.Play.Server.SPAWN_PLAYER) {
WrapperPlayServerSpawnPlayer packetOutEntity = new WrapperPlayServerSpawnPlayer(event);
addEntity(packetOutEntity.getEntityId(), EntityTypes.PLAYER, packetOutEntity.getPosition(), packetOutEntity.getYaw(), packetOutEntity.getPitch(), packetOutEntity.getEntityMetadata(), 0);
addEntity(packetOutEntity.getEntityId(), packetOutEntity.getUUID(), EntityTypes.PLAYER, packetOutEntity.getPosition(), packetOutEntity.getYaw(), packetOutEntity.getPitch(), packetOutEntity.getEntityMetadata(), 0);
}

if (event.getPacketType() == PacketType.Play.Server.ENTITY_RELATIVE_MOVE) {
Expand All @@ -119,6 +123,34 @@ public void onPacketSend(PacketSendEvent event) {
player.latencyUtils.addRealTimeTask(player.lastTransactionSent.get(), () -> player.compensatedEntities.updateEntityMetadata(entityMetadata.getEntityId(), entityMetadata.getEntityMetadata()));
}

// 1.19.3+
if (event.getPacketType() == PacketType.Play.Server.PLAYER_INFO_UPDATE) {
WrapperPlayServerPlayerInfoUpdate info = new WrapperPlayServerPlayerInfoUpdate(event);
player.latencyUtils.addRealTimeTask(player.lastTransactionSent.get(), () -> {
for (WrapperPlayServerPlayerInfoUpdate.PlayerInfo entry : info.getEntries()) {
final UserProfile gameProfile = entry.getGameProfile();
final UUID uuid = gameProfile.getUUID();
player.compensatedEntities.profiles.put(uuid, gameProfile);
}
});
} else if (event.getPacketType() == PacketType.Play.Server.PLAYER_INFO_REMOVE) {
WrapperPlayServerPlayerInfoRemove remove = new WrapperPlayServerPlayerInfoRemove(event);
player.latencyUtils.addRealTimeTask(player.lastTransactionSent.get(), () -> remove.getProfileIds().forEach(player.compensatedEntities.profiles::remove));
} else if (event.getPacketType() == PacketType.Play.Server.PLAYER_INFO) {
WrapperPlayServerPlayerInfo info = new WrapperPlayServerPlayerInfo(event);
player.latencyUtils.addRealTimeTask(player.lastTransactionSent.get(), () -> {
if (info.getAction() == WrapperPlayServerPlayerInfo.Action.ADD_PLAYER) {
for (WrapperPlayServerPlayerInfo.PlayerData entry : info.getPlayerDataList()) {
final UserProfile gameProfile = entry.getUserProfile();
final UUID uuid = gameProfile.getUUID();
player.compensatedEntities.profiles.put(uuid, gameProfile);
}
} else if (info.getAction() == WrapperPlayServerPlayerInfo.Action.REMOVE_PLAYER) {
info.getPlayerDataList().forEach(profile -> player.compensatedEntities.profiles.remove(profile.getUserProfile().getUUID()));
}
});
}

if (event.getPacketType() == PacketType.Play.Server.ENTITY_EFFECT) {
WrapperPlayServerEntityEffect effect = new WrapperPlayServerEntityEffect(event);

Expand Down Expand Up @@ -428,24 +460,26 @@ private void handleMoveEntity(PacketSendEvent event, int entityId, double deltaX
xRotEntity.packetYaw = yaw;
xRotEntity.steps = entity.isBoat() ? 10 : 3;
}

entity.onFirstTransaction(isRelative, hasPos, deltaX, deltaY, deltaZ, player);
});

player.latencyUtils.addRealTimeTask(lastTrans + 1, () -> {
PacketEntity entity = player.compensatedEntities.getEntity(entityId);
if (entity == null) return;
entity.onSecondTransaction();
});
}

public void addEntity(int entityID, EntityType type, Vector3d position, float xRot, float yRot, List<EntityData> entityMetadata, int extraData) {
public void addEntity(int entityID, UUID uuid, EntityType type, Vector3d position, float xRot, float yRot, List<EntityData> entityMetadata, int extraData) {
if (despawnedEntitiesThisTransaction.contains(entityID)) {
player.sendTransaction();
}

player.compensatedEntities.serverPositionsMap.put(entityID, new TrackerData(position.getX(), position.getY(), position.getZ(), xRot, yRot, type, player.lastTransactionSent.get()));

player.latencyUtils.addRealTimeTask(player.lastTransactionSent.get(), () -> {
player.compensatedEntities.addEntity(entityID, type, position, xRot, extraData);
player.compensatedEntities.addEntity(entityID, uuid, type, position, xRot, extraData);
if (entityMetadata != null) {
player.compensatedEntities.updateEntityMetadata(entityID, entityMetadata);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ac/grim/grimac/manager/CheckManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import ac.grim.grimac.utils.latency.CompensatedCooldown;
import ac.grim.grimac.utils.latency.CompensatedFireworks;
import ac.grim.grimac.utils.latency.CompensatedInventory;
import ac.grim.grimac.utils.team.TeamHandler;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.google.common.collect.ClassToInstanceMap;
Expand Down Expand Up @@ -65,6 +66,7 @@ public CheckManager(GrimPlayer player) {
.put(PacketPlayerAbilities.class, new PacketPlayerAbilities(player))
.put(PacketWorldBorder.class, new PacketWorldBorder(player))
.put(ActionManager.class, player.actionManager)
.put(TeamHandler.class, new TeamHandler(player))
.put(ClientBrand.class, new ClientBrand(player))
.put(NoFallA.class, new NoFallA(player))
.put(BadPacketsO.class, new BadPacketsO(player))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import ac.grim.grimac.GrimAPI;
import ac.grim.grimac.manager.init.Initable;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.anticheat.LogUtil;
import ac.grim.grimac.utils.lists.HookedListWrapper;
import com.github.retrooper.packetevents.util.reflection.Reflection;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
import org.bukkit.Bukkit;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ac/grim/grimac/player/GrimPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class GrimPlayer implements GrimUser {
public boolean wasSneaking;
public boolean isSprinting;
public boolean lastSprinting;
public String teamName;
// The client updates sprinting attribute at end of each tick
// Don't false if the server update's the player's sprinting status
public boolean lastSprintingForSpeed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
import ac.grim.grimac.utils.data.packetentity.PacketEntityStrider;
import ac.grim.grimac.utils.math.GrimMath;
import ac.grim.grimac.utils.nmsutil.*;
import ac.grim.grimac.utils.team.EntityPredicates;
import ac.grim.grimac.utils.team.TeamHandler;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.world.states.defaulttags.BlockTags;
import com.github.retrooper.packetevents.protocol.world.states.type.StateType;
import com.github.retrooper.packetevents.protocol.world.states.type.StateTypes;
import com.github.retrooper.packetevents.util.Vector3d;
import com.viaversion.viaversion.api.Via;
import io.github.retrooper.packetevents.util.viaversion.ViaVersionUtil;
import org.bukkit.util.Vector;

public class MovementTicker {
Expand All @@ -27,24 +33,35 @@ public MovementTicker(GrimPlayer player) {

public static void handleEntityCollisions(GrimPlayer player) {
// 1.7 and 1.8 do not have player collision
if (player.getClientVersion().isOlderThan(ClientVersion.V_1_9)) return;
if (player.getClientVersion().isOlderThan(ClientVersion.V_1_9)
// Check that ViaVersion disables all collisions on a 1.8 server for 1.9+ clients
|| (PacketEvents.getAPI().getServerManager().getVersion().isOlderThan(ServerVersion.V_1_9)
&& (!ViaVersionUtil.isAvailable() || Via.getConfig().isPreventCollision()))) return;

int possibleCollidingEntities = 0;

// Players in vehicles do not have collisions
if (!player.compensatedEntities.getSelf().inVehicle()) {
// Calculate the offset of the player to colliding other stuff
SimpleCollisionBox playerBox = GetBoundingBox.getBoundingBoxFromPosAndSize(player, player.lastX, player.lastY, player.lastZ, 0.6f, 1.8f);
SimpleCollisionBox expandedPlayerBox = playerBox.copy().expandToAbsoluteCoordinates(player.x, player.y, player.z).expand(1);
playerBox.union(GetBoundingBox.getBoundingBoxFromPosAndSize(player, player.x, player.y, player.z, 0.6f, 1.8f).expand(player.getMovementThreshold()));
playerBox.expand(0.2);

final TeamHandler teamHandler = player.checkManager.getPacketCheck(TeamHandler.class);

for (PacketEntity entity : player.compensatedEntities.entityMap.values()) {
if (!entity.isPushable())
continue;

// 1.9+ player on 1.8- server with ViaVersion prevent-collision disabled.
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9)
&& !EntityPredicates.canBePushedBy(player, entity, teamHandler).test(player)) continue;

SimpleCollisionBox entityBox = entity.getPossibleCollisionBoxes();

if (expandedPlayerBox.isCollided(entityBox))
if (playerBox.isCollided(entityBox)) {
possibleCollidingEntities++;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ public Vector handleStartingVelocityUncertainty(GrimPlayer player, VectorData ve
//
// Be somewhat careful as there is an antikb (for horizontal) that relies on this lenience
Vector uncertainty = new Vector(avgColliding * 0.08, additionVertical, avgColliding * 0.08);

Vector min = new Vector(player.uncertaintyHandler.xNegativeUncertainty - additionHorizontal, -bonusY + player.uncertaintyHandler.yNegativeUncertainty, player.uncertaintyHandler.zNegativeUncertainty - additionHorizontal);
Vector max = new Vector(player.uncertaintyHandler.xPositiveUncertainty + additionHorizontal, bonusY + player.uncertaintyHandler.yPositiveUncertainty, player.uncertaintyHandler.zPositiveUncertainty + additionHorizontal);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public Vector[] corners() {
return vectors;
}

public SimpleCollisionBox union(SimpleCollisionBox other) {
this.minX = Math.min(this.minX, other.minX);
this.minY = Math.min(this.minY, other.minY);
this.minZ = Math.min(this.minZ, other.minZ);
this.maxX = Math.max(this.maxX, other.maxX);
this.maxY = Math.max(this.maxY, other.maxY);
this.maxZ = Math.max(this.maxZ, other.maxZ);
return this;
}

public SimpleCollisionBox expandToAbsoluteCoordinates(double x, double y, double z) {
return expandToCoordinate(x - ((minX + maxX) / 2), y - ((minY + maxY) / 2), z - ((minZ + maxZ) / 2));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ac/grim/grimac/utils/data/VectorData.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ public enum VectorType {
TridentJump,
Trident,
SlimePistonBounce,
Entity_Pushing,
ZeroPointZeroThree,
AttackSlow,
Flip_Sneaking,
Flip_Use_Item
Flip_Use_Item,
EntityPushing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.potion.PotionType;
import com.github.retrooper.packetevents.util.Vector3d;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

// You may not copy this check unless your anticheat is licensed under GPL
public class PacketEntity extends TypedPacketEntity {

public final TrackedPosition trackedServerPosition;

@Getter
private final UUID uuid; // NULL ON VERSIONS BELOW 1.9
public PacketEntity riding;
public List<PacketEntity> passengers = new ArrayList<>(0);
public boolean isDead = false;
Expand All @@ -49,11 +53,13 @@ public class PacketEntity extends TypedPacketEntity {

public PacketEntity(EntityType type) {
super(type);
this.uuid = null;
this.trackedServerPosition = new TrackedPosition();
}

public PacketEntity(GrimPlayer player, EntityType type, double x, double y, double z) {
public PacketEntity(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z) {
super(type);
this.uuid = uuid;
this.trackedServerPosition = new TrackedPosition();
this.trackedServerPosition.setPos(new Vector3d(x, y, z));
if (player.getClientVersion().isOlderThan(ClientVersion.V_1_9)) { // Thanks ViaVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;

import java.util.UUID;

public class PacketEntityCamel extends PacketEntityHorse {

public boolean dashing = false; //TODO: handle camel dashing

public PacketEntityCamel(GrimPlayer player, EntityType type, double x, double y, double z, float xRot) {
super(player, type, x, y, z, xRot);
public PacketEntityCamel(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z, float xRot) {
super(player, uuid, type, x, y, z, xRot);

jumpStrength = 0.42F;
movementSpeedAttribute = 0.09f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;

import java.util.UUID;

public class PacketEntityHook extends PacketEntity{
public int owner;
public int attached = -1;

public PacketEntityHook(GrimPlayer player, EntityType type, double x, double y, double z, int owner) {
super(player, type, x, y, z);
public PacketEntityHook(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z, int owner) {
super(player, uuid, type, x, y, z);
this.owner = owner;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;

import java.util.UUID;

public class PacketEntityHorse extends PacketEntityTrackXRot {
public boolean isRearing = false;
public boolean hasSaddle = false;
public boolean isTame = false;
public double jumpStrength = 0.7;
public float movementSpeedAttribute = 0.225f;

public PacketEntityHorse(GrimPlayer player, EntityType type, double x, double y, double z, float xRot) {
super(player, type, x, y, z, xRot);
public PacketEntityHorse(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z, float xRot) {
super(player, uuid, type, x, y, z, xRot);
this.stepHeight = 1.0f;

if (EntityTypes.isTypeInstanceOf(type, EntityTypes.CHESTED_HORSE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;

import java.util.UUID;

public class PacketEntityRideable extends PacketEntity {

public boolean hasSaddle = false;
Expand All @@ -11,8 +13,8 @@ public class PacketEntityRideable extends PacketEntity {

public float movementSpeedAttribute = 0.1f;

public PacketEntityRideable(GrimPlayer player, EntityType type, double x, double y, double z) {
super(player, type, x, y, z);
public PacketEntityRideable(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z) {
super(player, uuid, type, x, y, z);
this.stepHeight = 1.0f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.world.BlockFace;

import java.util.UUID;

public class PacketEntityShulker extends PacketEntity {
public BlockFace facing = BlockFace.DOWN;

public PacketEntityShulker(GrimPlayer player, EntityType type, double x, double y, double z) {
super(player, type, x, y, z);
public PacketEntityShulker(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z) {
super(player, uuid, type, x, y, z);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;

import java.util.UUID;

public class PacketEntitySizeable extends PacketEntity {
public int size = 4; // To support entity metadata being sent after spawn, assume max size of vanilla slime

public PacketEntitySizeable(GrimPlayer player, EntityType type, double x, double y, double z) {
super(player, type, x, y, z);
public PacketEntitySizeable(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z) {
super(player, uuid, type, x, y, z);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;

import java.util.UUID;

public class PacketEntityStrider extends PacketEntityRideable {
public boolean isShaking = false;

public PacketEntityStrider(GrimPlayer player, EntityType type, double x, double y, double z) {
super(player, type, x, y, z);
public PacketEntityStrider(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z) {
super(player, uuid, type, x, y, z);
}
}
Loading

0 comments on commit 4359567

Please sign in to comment.