Skip to content

Commit

Permalink
generic atomic values and reduced use of mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Nov 29, 2023
1 parent 8dcd425 commit 76951d8
Show file tree
Hide file tree
Showing 28 changed files with 269 additions and 348 deletions.
3 changes: 2 additions & 1 deletion core_commands/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/dynamitemc/dynamite/server/commands"
"github.com/dynamitemc/dynamite/server/lang/placeholder"
"github.com/google/uuid"
)

var ban_cmd = &commands.Command{
Expand All @@ -31,7 +32,7 @@ var ban_cmd = &commands.Command{
if len(ctx.Arguments) > 1 {
reason = server.Lang.Translate("disconnect.banned.reason", placeholder.New(map[string]string{"reason": strings.Join(ctx.Arguments[1:], " ")}, player.PlaceholderContext))
}
server.Ban(player.Name(), player.UUID().String(), strings.Join(ctx.Arguments[1:], " "))
server.Ban(player.Session.Name(), uuid.UUID(player.Session.UUID()).String(), strings.Join(ctx.Arguments[1:], " "))
player.Disconnect(reason)
},
}
4 changes: 2 additions & 2 deletions core_commands/gamemode.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ var gamemode_cmd = &commands.Command{
}
pl = p
}
if int(pl.GameMode()) == gm {
if int(pl.GameMode.Get()) == gm {
return
}
pl.SetGameMode(byte(gm))
ph := placeholder.New(map[string]string{"gamemode": pascalify(ctx.Arguments[0])}, pl.PlaceholderContext)
msg := pl.Server.(*server.Server).Lang.Translate("commands.gamemode.success.other", ph)
if exe, ok := ctx.Executor.(*player.Player); ok && pl.UUID() == exe.UUID() {
if exe, ok := ctx.Executor.(*player.Player); ok && pl.Session.UUID() == exe.Session.UUID() {
msg = pl.Server.(*server.Server).Lang.Translate("commands.gamemode.success.self", ph)
}
ctx.Reply(msg)
Expand Down
2 changes: 1 addition & 1 deletion core_commands/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var kill_cmd = &commands.Command{
}
pl = p
}
name := pl.Name()
name := pl.Session.Name()
pl.Kill(name + " was killed")
ctx.Reply(pl.Server.(*server.Server).Lang.Translate("commands.kill.success.single", pl.PlaceholderContext))
pl.Server.(*server.Server).GlobalMessage(chat.NewMessage(name + " was killed"))
Expand Down
4 changes: 2 additions & 2 deletions core_commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ var list_cmd = &commands.Command{
msg := fmt.Sprintf("There are %d of a max of %d players online: ", l, srv.Config.MaxPlayers)
var index int
srv.Players.Range(func(_ uuid.UUID, p *player.Player) bool {
msg += p.Name()
msg += p.Session.Name()
if len(ctx.Arguments) == 1 && ctx.Arguments[0] == "uuids" {
msg += fmt.Sprintf(" (%s)", p.UUID())
msg += fmt.Sprintf(" (%s)", uuid.UUID(p.Session.UUID()))
}
if index != l-1 {
msg += ", "
Expand Down
2 changes: 1 addition & 1 deletion core_commands/nick.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var nick_cmd = &commands.Command{
}
} else {
p = srv.Players.Find(func(_ uuid.UUID, pl *player.Player) bool {
return pl.Name() == ctx.Arguments[0]
return pl.Session.Name() == ctx.Arguments[0]
})
}
if p == nil {
Expand Down
12 changes: 6 additions & 6 deletions core_commands/tp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ var tp_cmd = &commands.Command{
ep, es := exe.GetPrefixSuffix()
pp, ps := player.GetPrefixSuffix()
ctx.Reply(srv.Lang.Translate("commands.teleport.success.entity.single", placeholder.New(map[string]string{
"player": exe.Name(),
"player": exe.Session.Name(),
"player_prefix": ep,
"player_suffx": es,
"player1": player.Name(),
"player1": player.Session.Name(),
"player1_prefix": pp,
"player1_suffx": ps,
})))
Expand All @@ -54,10 +54,10 @@ var tp_cmd = &commands.Command{
ep, es := player1.GetPrefixSuffix()
pp, ps := player2.GetPrefixSuffix()
ctx.Reply(srv.Lang.Translate("commands.teleport.success.entity.single", placeholder.New(map[string]string{
"player": player1.Name(),
"player": player1.Session.Name(),
"player_prefix": ep,
"player_suffx": es,
"player1": player2.Name(),
"player1": player2.Session.Name(),
"player1_prefix": pp,
"player1_suffx": ps,
})))
Expand Down Expand Up @@ -90,7 +90,7 @@ var tp_cmd = &commands.Command{
prefix, suffix := exe.GetPrefixSuffix()
ctx.Reply(srv.Lang.Translate("commands.teleport.success.location.single", placeholder.New(
map[string]string{
"player": exe.Name(),
"player": exe.Session.Name(),
"player_prefix": prefix,
"player_suffx": suffix,
"x": fmt.Sprint(x),
Expand Down Expand Up @@ -125,7 +125,7 @@ var tp_cmd = &commands.Command{
prefix, suffix := player.GetPrefixSuffix()
ctx.Reply(srv.Lang.Translate("commands.teleport.success.location.single", placeholder.New(
map[string]string{
"player": player.Name(),
"player": player.Session.Name(),
"player_prefix": prefix,
"player_suffx": suffix,
"x": fmt.Sprint(x),
Expand Down
6 changes: 3 additions & 3 deletions server/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func (srv *Server) MakeOperator(p *player.Player) {
srv.mu.Lock()
defer srv.mu.Unlock()
srv.Operators = append(srv.Operators, user{
UUID: p.UUID().String(),
Name: p.Name(),
UUID: uuid.UUID(p.Session.UUID()).String(),
Name: p.Session.Name(),
})
}

Expand All @@ -164,7 +164,7 @@ func (srv *Server) MakeNotOperator(p *player.Player) {
srv.mu.Lock()
defer srv.mu.Unlock()
for i, op := range srv.Operators {
if op.UUID == p.UUID().String() {
if op.UUID == uuid.UUID(p.Session.UUID()).String() {
srv.Operators = slices.Delete(srv.Operators, i, i+1)
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (srv *Server) GlobalMessage(message chat.Message) {

func (srv *Server) OperatorMessage(message chat.Message) {
srv.Players.Range(func(_ uuid.UUID, p *player.Player) bool {
if p.ClientSettings().ChatMode == 2 || !p.Operator() {
if p.ClientSettings().ChatMode == 2 || !p.Operator.Get() {
return true
}
p.SendPacket(&packet.SystemChatMessage{
Expand Down
2 changes: 1 addition & 1 deletion server/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (srv *Server) FindEntity(id int32) interface{} {

func (srv *Server) FindEntityByUUID(id [16]byte) interface{} {
if _, p := srv.Players.Range(func(_ uuid.UUID, p *player.Player) bool {
return p.UUID() != id
return p.Session.UUID() != id
}); p != nil {
return p
}
Expand Down
78 changes: 38 additions & 40 deletions server/entity/pos/pos.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,98 @@
package pos

import (
"fmt"
"math"
"sync/atomic"

"github.com/dynamitemc/dynamite/util/atomic"
)

type EntityPosition struct {
x, y, z,
yaw, pitch atomic.Value
onGround *atomic.Bool
x, y, z *atomic.Value[float64]
yaw, pitch *atomic.Value[float32]
onGround *atomic.Value[bool]
}

func NewEntityPosition() *EntityPosition {
func NewEntityPosition(x, y, z float64, yaw, pitch float32, ong bool) *EntityPosition {
var e EntityPosition
e.SetX(0)
e.SetY(0)
e.SetZ(0)
e.SetYaw(0)
e.SetPitch(0)

e.onGround = &atomic.Bool{}
e.x = atomic.NewValue(x)
e.y = atomic.NewValue(y)
e.z = atomic.NewValue(z)
e.yaw = atomic.NewValue(yaw)
e.pitch = atomic.NewValue(pitch)
e.onGround = atomic.NewValue(ong)
return &e
}

func (pos *EntityPosition) X() float64 {
return pos.x.Load().(float64)
return pos.x.Get()
}

func (pos *EntityPosition) Y() float64 {
return pos.y.Load().(float64)
return pos.y.Get()
}

func (pos *EntityPosition) Z() float64 {
return pos.z.Load().(float64)
return pos.z.Get()
}

func (pos *EntityPosition) Yaw() float32 {
return pos.yaw.Load().(float32)
return pos.yaw.Get()
}

func (pos *EntityPosition) Pitch() float32 {
return pos.pitch.Load().(float32)
return pos.pitch.Get()
}

func (pos *EntityPosition) OnGround() bool {
return pos.onGround.Load()
return pos.onGround.Get()
}

func (pos *EntityPosition) SetX(x float64) {
pos.x.Store(x)
pos.x.Set(x)
}

func (pos *EntityPosition) SetY(y float64) {
pos.y.Store(y)
pos.y.Set(y)
}

func (pos *EntityPosition) SetZ(z float64) {
pos.z.Store(z)
pos.z.Set(z)
}

func (pos *EntityPosition) SetPosition(x, y, z float64) {
pos.x.Store(x)
pos.y.Store(y)
pos.z.Store(z)
pos.x.Set(x)
pos.y.Set(y)
pos.z.Set(z)
}

func (pos *EntityPosition) SetRotation(y, p float32) {
pos.yaw.Store(y)
pos.pitch.Store(p)
pos.yaw.Set(y)
pos.pitch.Set(p)
}

func (pos *EntityPosition) All() (x, y, z float64, yaw, pitch float32, ong bool) {
return pos.X(), pos.Y(), pos.Z(), pos.Yaw(), pos.Pitch(), pos.OnGround()
func (pos *EntityPosition) Position() (x, y, z float64) {
return pos.x.Get(), pos.y.Get(), pos.z.Get()
}

func (pos *EntityPosition) SetAll(x, y, z float64, yaw, pitch float32, ong bool) {
pos.SetX(x)
pos.SetY(y)
pos.SetZ(z)

pos.SetYaw(yaw)
pos.SetPitch(pitch)

pos.SetOnGround(ong)
func (pos *EntityPosition) Rotation() (yaw, pitch float32) {
return pos.yaw.Get(), pos.pitch.Get()
}

func (pos *EntityPosition) SetYaw(y float32) {
pos.yaw.Store(y)
pos.yaw.Set(y)
}

func (pos *EntityPosition) SetPitch(p float32) {
pos.pitch.Store(p)
pos.pitch.Set(p)
}

func (pos *EntityPosition) SetOnGround(ong bool) {
pos.onGround.Store(ong)
pos.onGround.Set(ong)
}

func (pos *EntityPosition) String() string {
return fmt.Sprintf("(%f %f %f | %f%f)", pos.X(), pos.Y(), pos.Z(), pos.Yaw(), pos.Pitch())
}

func DegreesToAngle(degrees float32) byte {
Expand Down
3 changes: 2 additions & 1 deletion server/handler/chat_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"github.com/dynamitemc/dynamite/logger/color"
"github.com/dynamitemc/dynamite/server/commands"
"github.com/dynamitemc/dynamite/server/player"
"github.com/google/uuid"
)

func ChatCommandPacket(state *player.Player, graph *commands.Graph, log *logger.Logger, content string, timestamp, salt int64, sigs []packet.Argument) {
log.Info(color.FromChat(chat.NewMessage(fmt.Sprintf("[%s] Player %s (%s) issued server command /%s", state.IP(), state.Name(), state.UUID(), content))))
log.Info(color.FromChat(chat.NewMessage(fmt.Sprintf("[%s] Player %s (%s) issued server command /%s", state.IP(), state.Session.Name(), uuid.UUID(state.Session.UUID()).String(), content))))
args := strings.Split(content, " ")
cmd := args[0]
var command *commands.Command
Expand Down
2 changes: 1 addition & 1 deletion server/handler/player_abilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func PlayerAbilities(state *player.Player, flags byte) {
state.SetFlying(flags == enum.PlayerAbilityFlying)
state.Flying.Set(flags == enum.PlayerAbilityFlying)
}
4 changes: 2 additions & 2 deletions server/handler/player_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func PlayerAction(state *player.Player, pk *packet.PlayerActionServer) {
switch pk.Status {
case enum.PlayerActionStartedDigging:
if state.GameMode() == enum.GameModeCreative {
if state.GameMode.Get() == enum.GameModeCreative {
state.BreakBlock(int64(pk.Location))
}
state.BroadcastMetadataInArea(&packet.SetEntityMetadata{
Expand All @@ -27,7 +27,7 @@ func PlayerAction(state *player.Player, pk *packet.PlayerActionServer) {
})
case enum.PlayerActionDropItemStack, enum.PlayerActionDropItem:
if s, ok := state.Inventory.HeldItem(); ok {
state.SetPreviousSelectedSlot(s)
state.PreviousSelectedSlot.Set(s)
state.Inventory.DeleteSlot(int8(s.Slot))
}
//controller.DropSlot()
Expand Down
2 changes: 1 addition & 1 deletion server/handler/player_movement.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func PlayerMovement(
state *player.Player,
p packet.Packet,
) {
if state.IsDead() {
if state.IsDead.Get() {
return
}
x, y, z := state.Position.X(), state.Position.Y(), state.Position.Z()
Expand Down
4 changes: 2 additions & 2 deletions server/handler/set_creative_mode_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

func SetCreativeModeSlot(state *player.Player, slot int16, data packet.Slot) {
if state.GameMode() != enum.GameModeCreative {
if state.GameMode.Get() != enum.GameModeCreative {
state.Disconnect(chat.NewMessage("bruh cant use the creative button without creative"))
return
}
s := inventory.NetworkSlotToDataSlot(slot)
if !data.Present {
if s, ok := state.Inventory.Slot(s); ok {
state.SetPreviousSelectedSlot(s)
state.PreviousSelectedSlot.Set(s)
}
//state.ClearItem(s)
} else {
Expand Down
2 changes: 1 addition & 1 deletion server/handler/set_held_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package handler
import "github.com/dynamitemc/dynamite/server/player"

func SetHeldItem(state *player.Player, heldItem int16) {
state.SetSelectedSlot(int32(heldItem))
state.Inventory.SelectedSlot.Set(int32(heldItem))
}
2 changes: 1 addition & 1 deletion server/handler/teleport_to_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TeleportToEntity(state *player.Player, uuid [16]byte) {
if state.GameMode() != enum.GameModeSpectator {
if state.GameMode.Get() != enum.GameModeSpectator {
state.Disconnect(chat.NewMessage("Yo how do you do dat without gamemode spectator?"))
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/handler/use_item_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func UseItemOn(state *player.Player, pk *packet.UseItemOnServer, f func(d *world
// todo check for snow/ flowers etc
//return
}
i, ok := state.Inventory.Slot(int8(state.SelectedSlot()))
i, ok := state.Inventory.Slot(int8(state.Inventory.SelectedSlot.Get()))
if !ok {
return
}
Expand Down
Loading

0 comments on commit 76951d8

Please sign in to comment.