@@ -23,17 +23,14 @@ The first step is to create a new struct type, which we will initially
23
23
put at the bottom of ` main.go ` :
24
24
``` go
25
25
type MyHandler struct {
26
- p *player. Player
26
+ // Any additional data.
27
27
}
28
28
```
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:
34
31
``` go
35
- func NewMyHandler (p * player . Player ) *MyHandler {
36
- return &MyHandler{p: p }
32
+ func NewMyHandler () *MyHandler {
33
+ return &MyHandler{}
37
34
}
38
35
```
39
36
As you can see, we return a ` *MyHandler ` (pointer) as opposed to ` MyHandler ` .
@@ -45,7 +42,7 @@ will embed the `player.NopHandler` type:
45
42
``` go
46
43
type MyHandler struct {
47
44
player.NopHandler
48
- p *player. Player
45
+ // Any additional data.
49
46
}
50
47
```
51
48
` 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
55
52
and see which event we want to handle in ` MyHandler ` . For this example, we
56
53
will cancel a player sending a chat message and print it to the terminal:
57
54
``` 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 ()
59
57
ctx.Cancel ()
60
- fmt.Println (" Player" , m. p .Name (), " tried to send:" , *message)
58
+ fmt.Println (" Player" , p.Name (), " tried to send:" , *message)
61
59
}
62
60
```
63
61
The ` *event.Context ` may be cancelled using ` ctx.Cancel() ` . This will stop
@@ -69,31 +67,31 @@ Combining this code:
69
67
``` go
70
68
type MyHandler struct {
71
69
player.NopHandler
72
- p *player.Player
73
70
}
74
71
75
- func NewMyHandler (p * player . Player ) *MyHandler {
76
- return &MyHandler{p: p }
72
+ func NewMyHandler () *MyHandler {
73
+ return &MyHandler{}
77
74
}
78
75
79
- func (m *MyHandler ) HandleChat (ctx *event .Context , message *string ) {
76
+ func (m *MyHandler ) HandleChat (ctx *player .Context , message *string ) {
77
+ p := ctx.Val ()
80
78
ctx.Cancel ()
81
- fmt.Println (" Player" , m. p .Name (), " tried to send:" , *message)
79
+ fmt.Println (" Player" , p.Name (), " tried to send:" , *message)
82
80
}
83
81
```
84
82
85
83
Now that our ` player.Handler ` implementation is ready, we will want to
86
84
attach it to the player when it joins. Moving back to the ` main ` function,
87
85
we change:
88
86
``` go
89
- for srv.Accept (nil ) {
87
+ for p := range srv.Accept () {
88
+ _ = p
90
89
}
91
90
```
92
91
to:
93
92
``` 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 ())
97
95
}
98
96
```
99
97
Players joining the server will now immediately get ` MyHandler ` assigned as
@@ -131,14 +129,13 @@ to the handler, like so:
131
129
``` go
132
130
type MyHandler struct {
133
131
player.NopHandler
134
- p *player.Player
135
132
136
133
deaths int
137
134
kills int
138
135
}
139
136
```
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
142
139
handler of a player can be retrieved by calling ` p.Handler() ` , and asserting
143
140
it to the correct type like this:
144
141
``` go
0 commit comments