Skip to content

Commit

Permalink
dragonfly: Remove redundant pointer type usage with atomic.Pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
DaPigGuy committed Sep 9, 2024
1 parent bf99d0e commit 3fb5f57
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
6 changes: 3 additions & 3 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Player struct {
skin atomic.Pointer[skin.Skin]
// s holds the session of the player. This field should not be used directly, but instead,
// Player.session() should be called.
s atomic.Pointer[*session.Session]
s atomic.Pointer[session.Session]
// h holds the current Handler of the player. It may be changed at any time by calling the Handle method.
h atomic.Pointer[Handler]

Expand Down Expand Up @@ -155,7 +155,7 @@ func New(name string, skin skin.Skin, pos mgl64.Vec3) *Player {
// you can leave the data as nil to use default data.
func NewWithSession(name, xuid string, uuid uuid.UUID, skin skin.Skin, s *session.Session, pos mgl64.Vec3, data *Data) *Player {
p := New(name, skin, pos)
p.s.Store(&s)
p.s.Store(s)
p.skin.Store(&skin)
p.uuid, p.xuid = uuid, xuid
p.inv, p.offHand, p.enderChest, p.armour, p.heldSlot = s.HandleInventories()
Expand Down Expand Up @@ -2939,7 +2939,7 @@ func (p *Player) Data() Data {
// is returned.
func (p *Player) session() *session.Session {
if s := p.s.Load(); s != nil {
return *s
return s
}
return session.Nop
}
Expand Down
2 changes: 1 addition & 1 deletion server/session/handler_item_stack_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (h *ItemStackRequestHandler) handleSwap(a *protocol.SwapStackRequestAction,
// smelting. If it does, it will drop the rewards at the player's location.
func (h *ItemStackRequestHandler) collectRewards(s *Session, inv *inventory.Inventory, slot int) {
w := s.c.World()
if inv == *s.openedWindow.Load() && s.containerOpened.Load() && slot == inv.Size()-1 {
if inv == s.openedWindow.Load() && s.containerOpened.Load() && slot == inv.Size()-1 {
if f, ok := w.Block(*s.openedPos.Load()).(smelter); ok {
for _, o := range entity.NewExperienceOrbs(entity.EyePosition(s.c), f.ResetExperience()) {
o.SetVelocity(mgl64.Vec3{(rand.Float64()*0.2 - 0.1) * 2, rand.Float64() * 0.4, (rand.Float64()*0.2 - 0.1) * 2})
Expand Down
2 changes: 1 addition & 1 deletion server/session/handler_player_auth_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (h PlayerAuthInputHandler) handleMovement(pk *packet.PlayerAuthInput, s *Se
}

if expected := s.teleportPos.Load(); expected != nil {
if newPos.Sub(**expected).Len() > 1 {
if newPos.Sub(*expected).Len() > 1 {
// The player has moved before it received the teleport packet. Ignore this movement entirely and
// wait for the client to sync itself back to the server. Once we get a movement that is close
// enough to the teleport position, we'll allow the player to move around again.
Expand Down
6 changes: 3 additions & 3 deletions server/session/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,12 @@ func (s *Session) invByID(id int32) (*inventory.Inventory, bool) {
return s.armour.Inventory(), true
case protocol.ContainerLevelEntity:
if s.containerOpened.Load() {
return *s.openedWindow.Load(), true
return s.openedWindow.Load(), true
}
case protocol.ContainerBarrel:
if s.containerOpened.Load() {
if _, barrel := s.c.World().Block(*s.openedPos.Load()).(block.Barrel); barrel {
return *s.openedWindow.Load(), true
return s.openedWindow.Load(), true
}
}
case protocol.ContainerBeaconPayment:
Expand Down Expand Up @@ -300,7 +300,7 @@ func (s *Session) invByID(id int32) (*inventory.Inventory, bool) {
protocol.ContainerBlastFurnaceIngredient, protocol.ContainerSmokerIngredient:
if s.containerOpened.Load() {
if _, ok := s.c.World().Block(*s.openedPos.Load()).(smelter); ok {
return *s.openedWindow.Load(), true
return s.openedWindow.Load(), true
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions server/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Session struct {
chunkLoader *world.Loader
chunkRadius, maxChunkRadius int32

teleportPos atomic.Pointer[*mgl64.Vec3]
teleportPos atomic.Pointer[mgl64.Vec3]

entityMutex sync.RWMutex
// currentEntityRuntimeID holds the runtime ID assigned to the last entity. It is incremented for every
Expand All @@ -68,7 +68,7 @@ type Session struct {
inTransaction, containerOpened atomic.Bool
openedWindowID atomic.Uint32
openedContainerID atomic.Uint32
openedWindow atomic.Pointer[*inventory.Inventory]
openedWindow atomic.Pointer[inventory.Inventory]
openedPos atomic.Pointer[cube.Pos]
swingingArm atomic.Bool
changingDimension atomic.Bool
Expand Down Expand Up @@ -167,7 +167,7 @@ func New(conn Conn, maxChunkRadius int, log Logger, joinMessage, quitMessage str
quitMessage: quitMessage,
}
inv := inventory.New(1, nil)
s.openedWindow.Store(&inv)
s.openedWindow.Store(inv)

var scoreboardName string
var scoreboardLines []string
Expand Down
11 changes: 5 additions & 6 deletions server/session/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ func (s *Session) ViewEntityTeleport(e world.Entity, position mgl64.Vec3) {
yaw, pitch := e.Rotation().Elem()
if id == selfEntityRuntimeID {
s.chunkLoader.Move(position)
pos := &position
s.teleportPos.Store(&pos)
s.teleportPos.Store(&position)
}

s.writePacket(&packet.SetActorMotion{EntityRuntimeID: id})
Expand Down Expand Up @@ -974,7 +973,7 @@ func (s *Session) OpenBlockContainer(pos cube.Pos) {
nextID := s.nextWindowID()
s.containerOpened.Store(true)
inv := inventory.New(1, nil)
s.openedWindow.Store(&inv)
s.openedWindow.Store(inv)
s.openedPos.Store(&pos)

var containerType byte
Expand All @@ -999,7 +998,7 @@ func (s *Session) OpenBlockContainer(pos cube.Pos) {
b.AddViewer(w, pos)

inv := s.c.EnderChestInventory()
s.openedWindow.Store(&inv)
s.openedWindow.Store(inv)

defer s.sendInv(inv, uint32(nextID))
}
Expand All @@ -1020,7 +1019,7 @@ func (s *Session) openNormalContainer(b block.Container, pos cube.Pos) {
nextID := s.nextWindowID()
s.containerOpened.Store(true)
inv := b.Inventory(s.c.World(), pos)
s.openedWindow.Store(&inv)
s.openedWindow.Store(inv)
s.openedPos.Store(&pos)

var containerType byte
Expand Down Expand Up @@ -1162,7 +1161,7 @@ func (s *Session) closeWindow() {
}
s.openedContainerID.Store(0)
inv := inventory.New(1, nil)
s.openedWindow.Store(&inv)
s.openedWindow.Store(inv)
s.writePacket(&packet.ContainerClose{WindowID: byte(s.openedWindowID.Load())})
}

Expand Down

0 comments on commit 3fb5f57

Please sign in to comment.