Skip to content

Commit 037ac4b

Browse files
committed
Updated all documentation to reflect changes in v0.10.
1 parent e75e248 commit 037ac4b

File tree

4 files changed

+217
-64
lines changed

4 files changed

+217
-64
lines changed

dev/Commands.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package commands
2222

2323
import (
2424
"github.com/df-mc/dragonfly/server/cmd"
25+
"github.com/df-mc/dragonfly/server/world"
2526
)
2627

2728
// ExampleCommand contains our command parameters.
@@ -36,7 +37,7 @@ type ExampleCommand struct {
3637
}
3738

3839
// Run will be called when the player runs the command. In this case, we will print the number back to the player
39-
func (c ExampleCommand) Run(source cmd.Source, output *cmd.Output) {
40+
func (c ExampleCommand) Run(source cmd.Source, output *cmd.Output, tx *world.Tx) {
4041
msg, ok := c.OptionalMessage.Load()
4142
output.Printf("%d %s (optional arg set? %v)", c.Number, msg, ok)
4243
}
@@ -66,17 +67,20 @@ commands:
6667
```go
6768
package pet
6869

69-
import "github.com/df-mc/dragonfly/server/cmd"
70+
import (
71+
"github.com/df-mc/dragonfly/server/cmd"
72+
"github.com/df-mc/dragonfly/server/world"
73+
)
7074

7175
type CreateCommand struct {
7276
Name cmd.Varargs `cmd:"name"`
7377
}
7478

75-
func (CreateCommand) Run(source cmd.Source, output *cmd.Output) {}
79+
func (CreateCommand) Run(source cmd.Source, output *cmd.Output, tx *world.Tx) {}
7680

7781
type DeleteCommand struct {}
7882

79-
func (DeleteCommand) Run(source cmd.Source, output *cmd.Output) {}
83+
func (DeleteCommand) Run(source cmd.Source, output *cmd.Output, tx *world.Tx) {}
8084
```
8185

8286
Now that we have both of these `cmd.Runnable` implementations, we will add a field at the start of these
@@ -86,22 +90,25 @@ added here to make the parser use a different sub command name.
8690
```go
8791
package pet
8892

89-
import "github.com/df-mc/dragonfly/server/cmd"
93+
import (
94+
"github.com/df-mc/dragonfly/server/cmd"
95+
"github.com/df-mc/dragonfly/server/world"
96+
)
9097

9198
type CreateCommand struct {
9299
// This sub command will be accessible as /pet create <name>.
93100
Create cmd.SubCommand `cmd:"create"`
94101
Name cmd.Varargs `cmd:"name"`
95102
}
96103

97-
func (CreateCommand) Run(source cmd.Source, output *cmd.Output) {}
104+
func (CreateCommand) Run(source cmd.Source, output *cmd.Output, tx *world.Tx) {}
98105

99106
type DeleteCommand struct {
100107
// This subcommand will be accessible as /pet Delete.
101108
Delete cmd.SubCommand
102109
}
103110

104-
func (DeleteCommand) Run(source cmd.Source, output *cmd.Output) {}
111+
func (DeleteCommand) Run(source cmd.Source, output *cmd.Output, tx *world.Tx) {}
105112
```
106113

107114
Having created complete `cmd.Runnable` implementations for both of our subcommands, we can now run `cmd.New` and register the command:

dev/Event-Handlers.md

+20-23
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ The first step is to create a new struct type, which we will initially
2323
put at the bottom of `main.go`:
2424
```go
2525
type MyHandler struct {
26-
p *player.Player
26+
// Any additional data.
2727
}
2828
```
29-
`MyHandler` has a `p` field. This will later hold the player that this
30-
handler will listen to. You can also add other fields that you want to
31-
use for the duration of a player's session. It is generally recommended
32-
to have a `New...` function for your handler to initialise it, so we will
33-
create that:
29+
It is generally recommended to have a `New...` function for your handler to
30+
initialise it, so we will create that:
3431
```go
35-
func NewMyHandler(p *player.Player) *MyHandler {
36-
return &MyHandler{p: p}
32+
func NewMyHandler() *MyHandler {
33+
return &MyHandler{}
3734
}
3835
```
3936
As you can see, we return a `*MyHandler` (pointer) as opposed to `MyHandler`.
@@ -45,7 +42,7 @@ will embed the `player.NopHandler` type:
4542
```go
4643
type MyHandler struct {
4744
player.NopHandler
48-
p *player.Player
45+
// Any additional data.
4946
}
5047
```
5148
`player.NopHandler` is the default `player.Handler` assigned to a player.
@@ -55,9 +52,10 @@ methods if we do not want to. We can now look at [`player.Handler`'s methods]((h
5552
and see which event we want to handle in `MyHandler`. For this example, we
5653
will cancel a player sending a chat message and print it to the terminal:
5754
```go
58-
func (m *MyHandler) HandleChat(ctx *event.Context, message *string) {
55+
func (m *MyHandler) HandleChat(ctx *player.Context, message *string) {
56+
p := ctx.Val()
5957
ctx.Cancel()
60-
fmt.Println("Player", m.p.Name(), "tried to send:", *message)
58+
fmt.Println("Player", p.Name(), "tried to send:", *message)
6159
}
6260
```
6361
The `*event.Context` may be cancelled using `ctx.Cancel()`. This will stop
@@ -69,31 +67,31 @@ Combining this code:
6967
```go
7068
type MyHandler struct {
7169
player.NopHandler
72-
p *player.Player
7370
}
7471

75-
func NewMyHandler(p *player.Player) *MyHandler {
76-
return &MyHandler{p: p}
72+
func NewMyHandler() *MyHandler {
73+
return &MyHandler{}
7774
}
7875

79-
func (m *MyHandler) HandleChat(ctx *event.Context, message *string) {
76+
func (m *MyHandler) HandleChat(ctx *player.Context, message *string) {
77+
p := ctx.Val()
8078
ctx.Cancel()
81-
fmt.Println("Player", m.p.Name(), "tried to send:", *message)
79+
fmt.Println("Player", p.Name(), "tried to send:", *message)
8280
}
8381
```
8482

8583
Now that our `player.Handler` implementation is ready, we will want to
8684
attach it to the player when it joins. Moving back to the `main` function,
8785
we change:
8886
```go
89-
for srv.Accept(nil) {
87+
for p := range srv.Accept() {
88+
_ = p
9089
}
9190
```
9291
to:
9392
```go
94-
for srv.Accept(func(p *player.Player) {
95-
p.Handle(NewMyHandler(p))
96-
}) {
93+
for p := range srv.Accept() {
94+
p.Handle(NewMyHandler())
9795
}
9896
```
9997
Players joining the server will now immediately get `MyHandler` assigned as
@@ -131,14 +129,13 @@ to the handler, like so:
131129
```go
132130
type MyHandler struct {
133131
player.NopHandler
134-
p *player.Player
135132

136133
deaths int
137134
kills int
138135
}
139136
```
140-
By implementing the `HandleDeath(damage.Source)` method, you can detect
141-
when the player quits and, for example, store the data to disk. The
137+
By implementing the `HandleDeath(*player.Player, damage.Source)` method, you can detect
138+
when the player dies and, for example, store the data to disk. The
142139
handler of a player can be retrieved by calling `p.Handler()`, and asserting
143140
it to the correct type like this:
144141
```go

dev/Getting-Started.md

+18-34
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ go run main.go
2222

2323
Dragonfly should now start and display log messages that look like this:
2424
```
25-
INFO[0000] Loading server...
26-
DEBU[0000] Loading world... dimension=overworld
27-
INFO[0000] Loaded world "World". dimension=overworld
28-
DEBU[0000] Loading world... dimension=nether
29-
INFO[0000] Loaded world "World". dimension=nether
30-
DEBU[0000] Loading world... dimension=end
31-
INFO[0000] Loaded world "World". dimension=end
32-
INFO[0000] Starting Dragonfly for Minecraft v1.19.10...
33-
INFO[0000] Server running on [::]:19132.
25+
2024/11/23 12:57:23 INFO Opened dimension. dimension=overworld name=World
26+
2024/11/23 12:57:23 INFO Opened dimension. dimension=nether name=World
27+
2024/11/23 12:57:23 INFO Opened dimension. dimension=end name=World
28+
2024/11/23 12:57:23 INFO Starting Dragonfly server... mc-version=1.21.40 go-version="go1.23.3" commit=2242b772bcc4f65d4f8e12cfab8cf70c2d78cb1a
29+
2024/11/23 12:57:23 INFO Server running. addr=[::]:19132
3430
```
3531

3632
Your server is now running. By default, Dragonfly will run on port 19132.
@@ -66,31 +62,24 @@ are unique to files that have `package main`. This function is called when
6662
the compiled executable is run.
6763
```go
6864
func main() {
69-
log := logrus.New()
70-
log.Formatter = &logrus.TextFormatter{ForceColors: true}
71-
log.Level = logrus.DebugLevel
65+
chat.Global.Subscribe(chat.StdoutSubscriber{})
7266
```
73-
The first thing done here is the creation of a logger using the `sirupsen/logrus`
74-
library, with colours enabled and the log level set to debug. Removal of
75-
the `log.Level = logrus.DebugLevel` line will hide debug messages from the
76-
log. For production servers, this is recommended to reduce log noise.
77-
78-
In the line after that, a `chat.StdoutSubscriber{}` is created.
79-
```go
80-
chat.Global.Subscribe(chat.StdoutSubscriber{})
81-
```
82-
This is a standard implementation of the `chat.Subscriber` interface that
83-
prints chat messages to the console when subscribed to a chat.
67+
The first thing done here is the creation of a
68+
`chat.StdoutSubscriber{}`. This is a standard implementation of the
69+
`chat.Subscriber` interface that prints chat messages to the console when
70+
subscribed to a chat.
8471
8572
After that, the `config.toml` file is read. This calls the `readConfig`
8673
function declared further down in the `main.go` file, which uses the
8774
`pelletier/go-toml` library to read a `server.Config`:
8875
```go
89-
config, err := readConfig()
76+
config, err := readConfig(slog.Default())
9077
if err != nil {
9178
log.Fatalln(err)
9279
}
9380
```
81+
Additionally, a logger is passed that will be used by the server to log
82+
information.
9483
9584
The `server.Config` read from the `config.toml` and logger are then used
9685
to create a new server:
@@ -105,19 +94,14 @@ The last lines of this `main` function are used to start listening and accept
10594
players joining the server:
10695
```go
10796
srv.Listen()
108-
for srv.Accept(nil) {
97+
for p := range srv.Accept() {
98+
_ = p
10999
}
110100
```
111101
This snippet is generally the first place you will want to edit when you
112-
start working on your own server. `srv.Accept()` takes a function that
113-
is called when a player joins.
114-
Passing a non-nil function:
115-
```go
116-
for srv.Accept(func(p *player.Player) {
117-
// Do stuff with p, like attaching a handler.
118-
}) {
119-
}
120-
```
102+
start working on your own server. `srv.Accept()` returns players
103+
as soon as they join the server.
104+
121105
You are now able to listen for a player joining and run code when they
122106
do. Further developer resources will go into depth on how to attach a
123107
handler to a `*player.Player` to listen for events.

0 commit comments

Comments
 (0)