Skip to content

Commit

Permalink
Fix for entities that players can hit through blocking reach/attack r…
Browse files Browse the repository at this point in the history
…ay trace
  • Loading branch information
Axionize committed Nov 16, 2024
1 parent 205e169 commit 2496510
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ac.grim.grimac.utils.data.packetentity;

import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;

import java.util.UUID;

public class PacketEntityArmorStand extends PacketEntity {

public static final int MARKER_FLAG = 16;

boolean isMarker;

public PacketEntityArmorStand(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z, int extraData) {
super(player, uuid, type, x, y, z);
isMarker = (extraData & MARKER_FLAG) != 0;
}

@Override
public boolean canHit() {
return !isMarker;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ac.grim.grimac.utils.data.packetentity;

import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;

import java.util.UUID;

public class PacketEntityArrow extends PacketEntity {

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

@Override
public boolean canHit() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public PacketEntityHook(GrimPlayer player, UUID uuid, EntityType type, double x,
super(player, uuid, type, x, y, z);
this.owner = owner;
}

@Override
public boolean canHit() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public boolean isBoat() {
return isBoat;
}

// Mojang makes this default to true and overrides it for everything where it isn't
// That's too much work for us to replicate...
// This is temporary hack and technically wrong
/* By Default every entity in the game cannot be hit by player crosshair. This is overwritten as follows:
Most Boats, Minecart's, TNT, Falling Blocks, and LivingEntities can only be hit if they're not removed
Every single BlockAttachedEntity can be hit (Leashes and other decorations)
End Crystals and IntersecetionEntities can be hit
Ender Dragon entity itself cannot be hit but its parts can be
ArmorStands can only be hit if they're not removed AND they're not markers.
Of all Projectiles, only redirectable ones (Fireballs - not blaze fireballs, Wind Charge, and Breeze Wind charges) can be hit
Persistent Projectiles can only be hit if they're not on the ground and redirectable
*/
// TLDR If we want to get 90% of the way there everything can be hit except for fishing rod bobbers, arrows, and marker armor stands
public boolean canHit() { return true; }

public boolean isPushable() {
// Players can only push living entities
// Minecarts and boats are the only non-living that can push
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ public void addEntity(int entityID, UUID uuid, EntityType entityType, Vector3d p
packetEntity = new PacketEntityHook(player, uuid, entityType, position.getX(), position.getY(), position.getZ(), data);
} else if (EntityTypes.ENDER_DRAGON.equals(entityType)) {
packetEntity = new PacketEntityEnderDragon(player, uuid, entityID, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.isTypeInstanceOf(entityType, EntityTypes.ABSTRACT_ARROW)) {
packetEntity = new PacketEntityArrow(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.ARMOR_STAND.equals(entityType)) {
packetEntity = new PacketEntityArmorStand(player, uuid, entityType, position.getX(), position.getY(), position.getZ(), data);
} else {
packetEntity = new PacketEntity(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import ac.grim.grimac.utils.data.HitData;
import ac.grim.grimac.utils.data.Pair;
import ac.grim.grimac.utils.data.packetentity.PacketEntity;
import ac.grim.grimac.utils.data.packetentity.TypedPacketEntity;
import ac.grim.grimac.utils.math.GrimMath;
import com.github.retrooper.packetevents.protocol.attribute.Attributes;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
Expand All @@ -28,6 +29,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class BlockRayTrace {

Expand Down Expand Up @@ -292,7 +294,7 @@ public static HitData getNearestHitResult(GrimPlayer player, PacketEntity target
PacketEntity closestEntity = null;

// Check entities
for (PacketEntity entity : player.compensatedEntities.entityMap.values()) {
for (PacketEntity entity : player.compensatedEntities.entityMap.values().stream().filter(TypedPacketEntity::canHit).collect(Collectors.toList())) {
SimpleCollisionBox box = null;
// 1.7 and 1.8 players get a bit of extra hitbox (this is why you should use 1.8 on cross version servers)
// Yes, this is vanilla and not uncertainty. All reach checks have this or they are wrong.
Expand Down

0 comments on commit 2496510

Please sign in to comment.