Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 54 additions & 22 deletions anticheat/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package entity

import (
"log/slog"
"maps"

"github.com/ethaniccc/float32-cube/cube"
"github.com/go-gl/mathgl/mgl32"
Expand All @@ -15,6 +16,7 @@ const (

type Entity struct {
RuntimeId uint64
UniqueId int64

Metadata map[uint32]any
Type string
Expand All @@ -39,43 +41,64 @@ type Entity struct {
Height float32
Scale float32

NetworkOffset float32

IsPlayer bool

historySize int
log **slog.Logger
}

// Config ...
type Config struct {
RuntimeID uint64
UniqueID int64

Type string
Metadata map[uint32]any

NetworkPosition mgl32.Vec3
HistorySize int
IsPlayer bool

Width, Height, Scale float32

Log **slog.Logger
}

// New creates and returns a new Entity instance.
func New(
runtimeId uint64,
entType string,
metadata map[uint32]any,
pos mgl32.Vec3,
historySize int,
isPlayer bool,
width, height, scale float32,
log **slog.Logger,
) *Entity {
func New(c Config) *Entity {
metadata := maps.Clone(c.Metadata)
if metadata == nil {
metadata = make(map[uint32]any)
}
offset := networkOffset(c.Type, metadata)
pos := c.NetworkPosition
pos[1] -= offset

e := &Entity{
RuntimeId: runtimeId,
RuntimeId: c.RuntimeID,
UniqueId: c.UniqueID,

Type: entType,
Type: c.Type,
Metadata: metadata,

Position: pos,
PrevPosition: pos,
RecvPosition: pos,

Width: width,
Height: height,
Scale: scale,
Width: c.Width,
Height: c.Height,
Scale: c.Scale,

NetworkOffset: offset,

PositionHistory: utils.NewCircularQueue(historySize, func() (hp HistoricalPosition) { return }),
PositionHistory: utils.NewCircularQueue(c.HistorySize, func() (hp HistoricalPosition) { return }),

IsPlayer: isPlayer,
IsPlayer: c.IsPlayer,

log: log,
historySize: historySize,
log: c.Log,
historySize: c.HistorySize,
}
/* e.InterpolationTicks = EntityMobInterpolationTicks
if isPlayer {
Expand All @@ -86,16 +109,16 @@ func New(
}

// ReceivePosition updates the position of the entity, and adds the previous position to its position history.
func (e *Entity) ReceivePosition(hp HistoricalPosition) {
func (e *Entity) ReceivePosition(pos mgl32.Vec3, teleport bool) {
e.PrevRecvPosition = e.RecvPosition
e.RecvPosition = hp.Position
e.RecvPosition = pos

e.InterpolationTicks = EntityMobInterpolationTicks
if e.IsPlayer {
e.InterpolationTicks = EntityPlayerInterpolationTicks
}

if hp.Teleport {
if teleport {
e.TicksSinceTeleport = 0
e.InterpolationTicks = 1
}
Expand All @@ -120,6 +143,15 @@ func (e *Entity) UpdateVelocity(vel mgl32.Vec3) {
e.RecvVelocity = vel
}

// UpdateMetadata ...
func (e *Entity) UpdateMetadata(metadata map[uint32]any) {
if e.Metadata == nil {
e.Metadata = make(map[uint32]any, len(metadata))
}
maps.Copy(e.Metadata, metadata)
e.NetworkOffset = networkOffset(e.Type, e.Metadata)
}

// Box returns the entity's bounding box.
func (e *Entity) Box(pos mgl32.Vec3) cube.BBox {
w := (e.Width * e.Scale) / 2
Expand Down
8 changes: 8 additions & 0 deletions anticheat/entity/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ const (
DataKeyOwnerID = 5
DataKeyTargetID = 6
DataKeyFireworkMetadata = 16
DataKeyPlayerFlags = 26
DataKeyScale = 38
DataKeyBoundingBoxWidth = 53
DataKeyBoundingBoxHeight = 54
DataKeyFlagsTwo = 92
)

// DataPlayerFlag ...
const (
DataPlayerFlagSleep = 1
DataPlayerFlagDead = 2
)

const (
Expand Down
25 changes: 25 additions & 0 deletions anticheat/entity/network_offset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package entity

import (
"github.com/oomph-ac/oomph/anticheat/game"
"github.com/oomph-ac/oomph/anticheat/utils"
)

// networkOffset ...
func networkOffset(entityType string, metadata map[uint32]any) float32 {
switch entityType {
case TypePlayer:
if utils.HasMetadataFlag[byte](metadata, DataKeyPlayerFlags, DataPlayerFlagSleep) || utils.HasMetadataFlag[int64](metadata, DataKeyFlagsTwo, DataFlagSleeping) {
return game.SleepingPlayerHeightOffset
}
return game.DefaultPlayerHeightOffset
case TypeItem, TypeFallingBlock, TypeMinecart, TypeChestMinecart, TypeCommandBlockMinecart, TypeHopperMinecart, TypeTNTMinecart:
return game.ItemAndMinecartNetworkOffset
case TypeBoat, TypeChestBoat:
return game.BoatNetworkOffset
case TypeTNT:
return game.PrimedTNTNetworkOffset
default:
return 0
}
}
12 changes: 12 additions & 0 deletions anticheat/entity/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ package entity

const (
TypeFireworksRocket = "minecraft:fireworks_rocket"

TypeBoat = "minecraft:boat"
TypeChestBoat = "minecraft:chest_boat"
TypeChestMinecart = "minecraft:chest_minecart"
TypeCommandBlockMinecart = "minecraft:command_block_minecart"
TypeFallingBlock = "minecraft:falling_block"
TypeHopperMinecart = "minecraft:hopper_minecart"
TypeItem = "minecraft:item"
TypeMinecart = "minecraft:minecart"
TypePlayer = "minecraft:player"
TypeTNT = "minecraft:tnt"
TypeTNTMinecart = "minecraft:tnt_minecart"
)
7 changes: 6 additions & 1 deletion anticheat/game/movement.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ const (
MaxSneakImpulse = float32(0.3)
MaxNormalizedImpulse = float32(0.70710678118) // 1/sqrt(2)

DefaultPlayerHeightOffset = float32(1.62)
DefaultPlayerHeightOffset = float32(1.62001)
SneakingPlayerHeightOffset = float32(1.27)
SleepingPlayerHeightOffset = float32(0.2)

ItemAndMinecartNetworkOffset = float32(0.5)
BoatNetworkOffset = float32(0.375)
PrimedTNTNetworkOffset = float32(0.49)

JumpDelayTicks = 10
GlideBoostTicks = 20
Expand Down
55 changes: 46 additions & 9 deletions anticheat/player/component/acknowledgement/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@ import (
"github.com/go-gl/mathgl/mgl32"
"github.com/oomph-ac/oomph/anticheat/entity"
"github.com/oomph-ac/oomph/anticheat/player"
"github.com/sandertv/gophertunnel/minecraft/protocol"
)

type EntitySize struct {
mEntity *entity.Entity
width float32
height float32
scale float32
type EntityData struct {
mEntity *entity.Entity
metadata map[uint32]any
width float32
height float32
scale float32
}

func NewEntitySizeACK(e *entity.Entity, width, height, scale float32) *EntitySize {
return &EntitySize{
mEntity: e,
func NewEntityDataACK(e *entity.Entity, metadata map[uint32]any, width, height, scale float32) *EntityData {
return &EntityData{
mEntity: e,
metadata: metadata,

width: width,
height: height,
scale: scale,
}
}

func (ack *EntitySize) Run() {
func (ack *EntityData) Run() {
ack.mEntity.UpdateMetadata(ack.metadata)
ack.mEntity.Width = ack.width
ack.mEntity.Height = ack.height
ack.mEntity.Scale = ack.scale
Expand Down Expand Up @@ -56,3 +60,36 @@ func (ack *EntityPosition) Run() {
)
ack.mPlayer = nil
}

type EntityDeltaPosition struct {
mPlayer *player.Player

positionX protocol.Optional[float32]
positionY protocol.Optional[float32]
positionZ protocol.Optional[float32]
runtimeID uint64
teleport bool
}

func NewEntityDeltaPositionACK(p *player.Player, posX, posY, posZ protocol.Optional[float32], rid uint64, teleport bool) *EntityDeltaPosition {
return &EntityDeltaPosition{
mPlayer: p,
positionX: posX,
positionY: posY,
positionZ: posZ,
runtimeID: rid,
teleport: teleport,
}
}

func (ack *EntityDeltaPosition) Run() {
ack.mPlayer.ClientEntityTracker().MoveEntityDelta(
ack.runtimeID,
0,
ack.positionX,
ack.positionY,
ack.positionZ,
ack.teleport,
)
ack.mPlayer = nil
}
3 changes: 3 additions & 0 deletions anticheat/player/component/combat.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ func (c *AuthoritativeCombatComponent) checkForMispredictedEntity() bool {
rewTick := c.mPlayer.ClientTick - 1
attackPos := c.endAttackPos
for rid, e := range c.entityTracker().All() {
if e.Type == entity.TypeItem {
continue
}
rewind, ok := e.Rewind(rewTick)
if !ok {
continue
Expand Down
Loading
Loading