Skip to content

Commit

Permalink
protocol typing
Browse files Browse the repository at this point in the history
knockback (wip)
  • Loading branch information
oq-x committed Nov 5, 2023
1 parent 363eaf1 commit c935422
Show file tree
Hide file tree
Showing 43 changed files with 513 additions and 81 deletions.
2 changes: 1 addition & 1 deletion core_commands/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var ban_cmd = &commands.Command{
Name: "ban",
RequiredPermissions: []string{"server.command.ban"},
Arguments: []commands.Argument{
commands.NewEntityArgument("player", commands.EntityPlayerOnly),
commands.NewEntityArg("player", commands.EntityPlayerOnly),
commands.NewStrArg("reason", commands.GreedyPhrase),
},
Execute: func(ctx commands.CommandContext) {
Expand Down
2 changes: 1 addition & 1 deletion core_commands/deop.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var deop_cmd = &commands.Command{
Name: "deop",
RequiredPermissions: []string{"server.command.deop"},
Arguments: []commands.Argument{
commands.NewEntityArgument("player", commands.EntityPlayerOnly),
commands.NewEntityArg("player", commands.EntityPlayerOnly),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion core_commands/dimension.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var dimension_cmd = &commands.Command{
Name: "dimension",
RequiredPermissions: []string{"server.command.dimension"},
Arguments: []commands.Argument{
commands.NewDimensionArgument("dimension"),
commands.NewDimensionArg("dimension"),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions core_commands/gamemode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var gamemode_cmd = &commands.Command{
Name: "gamemode",
RequiredPermissions: []string{"server.command.gamemode"},
Arguments: []commands.Argument{
commands.NewGamemodeArgument("mode"),
commands.NewEntityArgument("player", commands.EntityPlayerOnly),
commands.NewGamemodeArg("mode"),
commands.NewEntityArg("player", commands.EntityPlayerOnly),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion core_commands/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var kill_cmd = &commands.Command{
Name: "kill",
RequiredPermissions: []string{"server.command.kill"},
Arguments: []commands.Argument{
commands.NewEntityArgument("player", commands.EntityPlayerOnly),
commands.NewEntityArg("player", commands.EntityPlayerOnly),
},
Execute: func(ctx commands.CommandContext) {
var player *server.Session
Expand Down
2 changes: 1 addition & 1 deletion core_commands/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var msg_cmd = &commands.Command{
Name: "msg",
RequiredPermissions: []string{"server.chat"},
Arguments: []commands.Argument{
commands.NewEntityArgument("targets", commands.EntitySingle),
commands.NewEntityArg("targets", commands.EntitySingle),
commands.NewStrArg("message", commands.GreedyPhrase),
},
Execute: func(ctx commands.CommandContext) {
Expand Down
2 changes: 1 addition & 1 deletion core_commands/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var op_cmd = &commands.Command{
Name: "op",
RequiredPermissions: []string{"server.command.op"},
Arguments: []commands.Argument{
commands.NewEntityArgument("player", commands.EntityPlayerOnly),
commands.NewEntityArg("player", commands.EntityPlayerOnly),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions core_commands/summon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var summon_cmd = &commands.Command{
Aliases: []string{},
RequiredPermissions: []string{"server.command.summon"},
Arguments: []commands.Argument{
commands.NewResourceKeyArgument("entity", "minecraft:entity_type"),
commands.NewVector3Argument("location"),
commands.NewResourceKeyArg("entity", "minecraft:entity_type"),
commands.NewVector3Arg("location"),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) < 4 {
Expand Down
12 changes: 7 additions & 5 deletions core_commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ var test_cmd = &commands.Command{
Name: "test",
RequiredPermissions: []string{"server.command.test"},
Arguments: []commands.Argument{
commands.NewVector3Arg("pos1"),
commands.NewStrArg("d", commands.SingleWord),
commands.NewBoolArg("gay"),
commands.NewFloatArg("f"),
commands.NewVector3Argument("pos"),
commands.NewIntArg("f"),
commands.NewVector3Arg("pos"),
},
Execute: func(ctx commands.CommandContext) {
x, y, z, _ := ctx.GetVector3("pos1")
d, _ := ctx.GetString("d")
gay, _ := ctx.GetBool("gay")
f, _ := ctx.GetFloat64("f")
x, y, z, _ := ctx.GetVector3("pos")
f, _ := ctx.GetInt64("f")
x1, y1, z1, _ := ctx.GetVector3("pos")

ctx.Reply(chat.NewMessage(fmt.Sprintf("%s %v %f %f %f %f", d, gay, f, x, y, z)))
ctx.Reply(chat.NewMessage(fmt.Sprintf("%f %f %f %s %v %d %f %f %f", x, y, z, d, gay, f, x1, y1, z1)))
},
}
4 changes: 2 additions & 2 deletions core_commands/tp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var tp_cmd = &commands.Command{
RequiredPermissions: []string{"server.command.op"},
Aliases: []string{"teleport"},
Arguments: []commands.Argument{
commands.NewEntityArgument("targets", commands.EntityPlayerOnly),
commands.NewEntityArgument("destination", commands.EntityPlayerOnly).SetAlternative(commands.NewVector3Argument("location")),
commands.NewEntityArg("targets", commands.EntityPlayerOnly),
commands.NewEntityArg("destination", commands.EntityPlayerOnly).SetAlternative(commands.NewVector3Arg("location")),
},
Execute: func(ctx commands.CommandContext) {
srv := getServer(ctx.Executor)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
)

require (
github.com/aimjel/minecraft v0.0.0-20231104165759-5cfa906f9947
github.com/aimjel/minecraft v0.0.0-20231105145341-243d6972b4d8
github.com/gorilla/websocket v1.5.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/aimjel/minecraft v0.0.0-20231102074502-922c6e0af42d h1:12teyvLFC7LHPD
github.com/aimjel/minecraft v0.0.0-20231102074502-922c6e0af42d/go.mod h1:jb47g4nDw3J8JdA4WxQpDx25NoIZiyNCjFOtM1b3Urk=
github.com/aimjel/minecraft v0.0.0-20231104165759-5cfa906f9947 h1:iDwZHCBcbj3ArSM9MMw8zYIcuM3f31Yifu7ut7V5jg4=
github.com/aimjel/minecraft v0.0.0-20231104165759-5cfa906f9947/go.mod h1:jb47g4nDw3J8JdA4WxQpDx25NoIZiyNCjFOtM1b3Urk=
github.com/aimjel/minecraft v0.0.0-20231105145341-243d6972b4d8 h1:NbzK6skaCpdYxZzUIpH1gp5J4qDk6vMm/x+A87uoMeM=
github.com/aimjel/minecraft v0.0.0-20231105145341-243d6972b4d8/go.mod h1:jb47g4nDw3J8JdA4WxQpDx25NoIZiyNCjFOtM1b3Urk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
6 changes: 6 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func ParseChat(msg chat.Message) string {
str += color.New(attrs...).SprintFunc()(*text.Text)
}

str = strings.ReplaceAll(str, "\n", "\n\r")

return str
}

Expand Down Expand Up @@ -119,6 +121,7 @@ func (logger *Logger) Info(format string, a ...interface{}) {
Time: time,
Message: str,
})
str = strings.ReplaceAll(str, "\n", "\n\r")
if !logger.done {
fmt.Printf("\r%s %s: %s\n", GB(time), BB("INFO "), str)
fmt.Print("\r> ")
Expand All @@ -139,6 +142,7 @@ func (logger *Logger) Debug(format string, a ...interface{}) {
Time: time,
Message: str,
})
str = strings.ReplaceAll(str, "\n", "\n\r")
if !logger.done {
fmt.Printf("\r%s %s: %s\n", GB(time), CB("DEBUG"), str)
fmt.Print("\r> ")
Expand All @@ -156,6 +160,7 @@ func (logger *Logger) Error(format string, a ...interface{}) {
Time: time,
Message: str,
})
str = strings.ReplaceAll(str, "\n", "\n\r")
if !logger.done {
fmt.Fprintf(os.Stderr, "\r%s %s: %s\n", GB(time), RB("ERROR"), str)
fmt.Print("\r> ")
Expand All @@ -173,6 +178,7 @@ func (logger *Logger) Warn(format string, a ...interface{}) {
Time: time,
Message: str,
})
str = strings.ReplaceAll(str, "\n", "\n\r")
if !logger.done {
fmt.Printf("\r%s %s: %s\n", GB(time), YB("WARN "), str)
fmt.Print("\r> ")
Expand Down
6 changes: 1 addition & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ func start(cfg *server.Config) {
var cfg server.Config

func main() {
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
server.OldState, _ = term.MakeRaw(int(os.Stdin.Fd()))

log.Info("Starting Dynamite 1.20.1 server")
if util.HasArg("-prof") {
Expand Down
52 changes: 48 additions & 4 deletions server/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ import (

"github.com/aimjel/minecraft/chat"
"github.com/aimjel/minecraft/protocol/types"
"github.com/dynamitemc/dynamite/server/enum"
"github.com/dynamitemc/dynamite/server/item"
"github.com/dynamitemc/dynamite/server/registry"
"github.com/dynamitemc/dynamite/server/world"

"github.com/aimjel/minecraft/packet"
)

type Vector struct {
X, Y, Z float64
}

func (v *Vector) Normalize() {
length := math.Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z)
if length != 0 {
v.X /= length
v.Y /= length
v.Z /= length
}
}

func (srv *Server) GlobalMessage(message chat.Message) {
srv.mu.RLock()
defer srv.mu.RUnlock()
Expand Down Expand Up @@ -122,19 +136,38 @@ func positionIsValid(x, y, z float64) bool {
!math.IsInf(x, 0) && !math.IsInf(y, 0) && !math.IsInf(z, 0)
}

func (p *Session) Hit(entityId int32) {
func cond[T any](c bool, t T, f T) T {
if c {
return t
} else {
return f
}
}

func (p *Session) Attack(entityId int32) {
e := p.Server.FindEntity(entityId)
x, y, z := p.Player.Position()
soundId := int32(519)
if pl, ok := e.(*Session); ok {
if pl.Player.GameMode() == 1 {
if pl.Player.GameMode() == enum.GameModeCreative {
return
}
x1, y1, z1 := pl.Player.Position()

x2 := x1 - x
z2 := z1 - z

v := Vector{x2, y1, z2}

v.Normalize()

pl.Push(x1+x2, y1, z1+z2)

health := pl.Player.Health()
pl.SetHealth(health - 1)
pl.SendPacket(&packet.DamageEvent{
EntityID: entityId,
SourceTypeID: 31,
SourceTypeID: enum.DamageTypePlayerAttack,
SourceCauseID: p.entityID + 1,
SourceDirectID: p.entityID + 1,
SourcePositionX: &x,
Expand All @@ -147,6 +180,17 @@ func (p *Session) Hit(entityId int32) {
if ok {
soundId = sound.ProtocolID
}

x1, y1, z1 := entity.data.Pos[0], entity.data.Pos[1], entity.data.Pos[2]

x2 := x1 - x
z2 := z1 - z

v := Vector{x2, y1, z2}

v.Normalize()

p.Server.SetEntityPosition(entity.ID, x1+x2, y1, z1+z2)
}

p.Server.mu.RLock()
Expand All @@ -157,7 +201,7 @@ func (p *Session) Hit(entityId int32) {
}
pl.SendPacket(&packet.DamageEvent{
EntityID: entityId,
SourceTypeID: 31,
SourceTypeID: enum.DamageTypePlayerAttack,
SourceCauseID: p.entityID + 1,
SourceDirectID: p.entityID + 1,
SourcePositionX: &x,
Expand Down
3 changes: 2 additions & 1 deletion server/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/aimjel/minecraft/chat"
"github.com/aimjel/minecraft/packet"
"github.com/dynamitemc/dynamite/server/enum"
)

func (p *Session) Chat(pk *packet.ChatMessageServer) {
Expand Down Expand Up @@ -131,7 +132,7 @@ func (p *Session) Whisper(pl *Session, msg string, timestamp, salt int64, sig []
//MessageSignature: sig,
Salt: salt,
Timestamp: timestamp,
ChatType: 3,
ChatType: enum.ChatTypeMsgCommandIncoming,
NetworkName: chat.NewMessage(prefix + p.Name() + suffix),
NetworkTargetName: &tgt,
})
Expand Down
14 changes: 7 additions & 7 deletions server/commands/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewStrArg(name string, properties stringArgType) Argument {
}
}

func NewEntityArgument(name string, properties byte) Argument {
func NewEntityArg(name string, properties byte) Argument {
props := types.CommandProperties{Flags: properties}
return Argument{
Name: name,
Expand All @@ -101,7 +101,7 @@ func NewEntityArgument(name string, properties byte) Argument {
}
}

func NewGamemodeArgument(name string) Argument {
func NewGamemodeArg(name string) Argument {
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -110,7 +110,7 @@ func NewGamemodeArgument(name string) Argument {
}
}

func NewChatComponentArgument(name string) Argument {
func NewChatComponentArg(name string) Argument {
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -119,7 +119,7 @@ func NewChatComponentArgument(name string) Argument {
}
}

func NewDimensionArgument(name string) Argument {
func NewDimensionArg(name string) Argument {
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -128,7 +128,7 @@ func NewDimensionArgument(name string) Argument {
}
}

func NewVector3Argument(name string) Argument {
func NewVector3Arg(name string) Argument {
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -137,7 +137,7 @@ func NewVector3Argument(name string) Argument {
}
}

func NewVector2Argument(name string) Argument {
func NewVector2Arg(name string) Argument {
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -146,7 +146,7 @@ func NewVector2Argument(name string) Argument {
}
}

func NewResourceKeyArgument(name string, registry string) Argument {
func NewResourceKeyArg(name string, registry string) Argument {
return Argument{
Name: name,
Parser: Parser{
Expand Down
Loading

0 comments on commit c935422

Please sign in to comment.