Skip to content

Commit

Permalink
Refactor!: 1.20.1 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard127 committed Aug 31, 2023
1 parent 65699f0 commit 4b88f1f
Show file tree
Hide file tree
Showing 51 changed files with 619 additions and 530 deletions.
6 changes: 6 additions & 0 deletions bot/core/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ package core

type Controller struct {
Left, Right, Forward, Back float64
LeftImpulse, RightImpulse float64
Jump, Sneak, Sprint, Drop bool
}

func (c *Controller) Reset() {
c.Left, c.Right, c.Forward, c.Back = 0, 0, 0, 0
c.Jump, c.Sneak, c.Sprint, c.Drop = false, false, false, false
}
19 changes: 19 additions & 0 deletions bot/core/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ type UnaliveEntity struct {
UUID uuid.UUID
lastPosition maths.Vec3d
Position maths.Vec3d
EyePosition maths.Vec3d
Rotation maths.Vec2d
Motion maths.Vec3d
BoundingBox maths.AxisAlignedBB[float64]
Width, Height float64
Vehicle Entity
Passengers []Entity
dataManager map[int32]interface{}
}

Expand All @@ -38,6 +41,9 @@ type Entity interface {
SetRotation(yaw, pitch float64)
SetMotion(x, y, z float64)
SetSize(width, height float64)
Equals(other entity.TypeEntity) bool
IsPassenger() bool
GetPassengers() []Entity
}

func (e *UnaliveEntity) GetName() string {
Expand Down Expand Up @@ -100,6 +106,7 @@ func (e *UnaliveEntity) IsPlayer() bool {
func (e *UnaliveEntity) SetPosition(x, y, z float64) {
e.lastPosition = e.Position
e.Position = maths.Vec3d{X: x, Y: y, Z: z}
e.EyePosition = maths.Vec3d{X: x, Y: y + e.Height*0.85, Z: z}
}

func (e *UnaliveEntity) SetRotation(yaw, pitch float64) {
Expand Down Expand Up @@ -140,6 +147,18 @@ func (e *UnaliveEntity) SetSize(width, height float64) {
}
}

func (e *UnaliveEntity) Equals(other entity.TypeEntity) bool {
return e.Type == other
}

func (e *UnaliveEntity) IsPassenger() bool {
return e.Vehicle != nil
}

func (e *UnaliveEntity) IsVehicle() bool {
return len(e.Passengers) > 0
}

func NewEntity(id int32, uuid uuid.UUID, t int32, x, y, z float64, yaw, pitch float64) *UnaliveEntity {
entityType := entity.TypeEntityByID[t]
e := &UnaliveEntity{
Expand Down
6 changes: 3 additions & 3 deletions bot/core/entityliving.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type EntityLiving struct {
Absorption float32
ActiveItem *item.Item
ActiveItemStackUseCount int32
ActivePotionEffects map[int32]*effects.EffectStatus
ActivePotionEffects map[int32]effects.EffectStatus
dead bool
OnGround bool
MoveStrafing float32
Expand Down Expand Up @@ -65,7 +65,7 @@ func (e *EntityLiving) IsPotionActive(effect effects.Effect) bool {
return ok
}

func (e *EntityLiving) GetPotionEffect(effect effects.Effect) *effects.EffectStatus {
func (e *EntityLiving) GetPotionEffect(effect effects.Effect) effects.EffectStatus {
return e.ActivePotionEffects[effect.ID]
}

Expand All @@ -76,6 +76,6 @@ func (e *EntityLiving) IsLivingEntity() bool {
func NewEntityLiving(id int32, uuid uuid.UUID, t int32, x, y, z float64, yaw, pitch float64) *EntityLiving {
return &EntityLiving{
UnaliveEntity: NewEntity(id, uuid, t, x, y, z, yaw, pitch),
ActivePotionEffects: make(map[int32]*effects.EffectStatus),
ActivePotionEffects: make(map[int32]effects.EffectStatus),
}
}
4 changes: 4 additions & 0 deletions bot/core/entityplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (e *EntityPlayer) GetDisplayName() string {
return e.Username // e.Username replaces the generic e.Type.DisplayName (which is "Player")
}

func (e *EntityPlayer) IsPlayer() bool {
return true
}

func NewEntityPlayer(displayName string, id int32, uuid uuid.UUID, t int32, x, y, z float64, yaw, pitch float64) *EntityPlayer {
return &EntityPlayer{
EntityLiving: NewEntityLiving(id, uuid, t, x, y, z, yaw, pitch),
Expand Down
46 changes: 46 additions & 0 deletions bot/provider/abilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package provider

import (
pk "github.com/Edouard127/go-mc/net/packet"
"io"
)

type Abilities struct {
Invulnerable bool
Flying bool
AllowFlying bool
InstantBuild bool
CanBuild bool
FlyingSpeed float64
WalkingSpeed float64
}

func NewAbilities() *Abilities {
return &Abilities{CanBuild: true, FlyingSpeed: 0.05, WalkingSpeed: 0.1}
}

func (a *Abilities) ReadFrom(r io.Reader) (int64, error) {
var flags pk.Byte
var flyingSpeed pk.Float
nn, err := pk.Tuple{flags, flyingSpeed}.ReadFrom(r)
if err != nil {
return nn, err
}

a.FlyingSpeed = float64(flyingSpeed)

a.Invulnerable = flags&0x01 != 0
a.Flying = flags&0x02 != 0
a.AllowFlying = flags&0x04 != 0
a.InstantBuild = flags&0x08 != 0

return nn, nil
}

func (a *Abilities) WriteTo(w io.Writer) (int64, error) {
var flags byte
if a.Flying {
flags |= 0x02
}
return pk.Byte(flags).WriteTo(w)
}
Loading

0 comments on commit 4b88f1f

Please sign in to comment.