-
Notifications
You must be signed in to change notification settings - Fork 143
/
handler_modal_form_response.go
47 lines (41 loc) · 1.3 KB
/
handler_modal_form_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package session
import (
"bytes"
"fmt"
"github.com/df-mc/atomic"
"github.com/df-mc/dragonfly/server/player/form"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"sync"
)
// ModalFormResponseHandler handles the ModalFormResponse packet.
type ModalFormResponseHandler struct {
mu sync.Mutex
forms map[uint32]form.Form
currentID atomic.Uint32
}
// nullBytes contains the word 'null' converted to a byte slice.
var nullBytes = []byte("null\n")
// Handle ...
func (h *ModalFormResponseHandler) Handle(p packet.Packet, s *Session) error {
pk := p.(*packet.ModalFormResponse)
h.mu.Lock()
f, ok := h.forms[pk.FormID]
delete(h.forms, pk.FormID)
h.mu.Unlock()
if !ok && bytes.Equal(pk.ResponseData, nullBytes) {
// Sometimes the client seems to send a second response with "null" as the response, which would
// cause the player to be kicked by the server. This should patch that.
return nil
}
if bytes.Equal(pk.ResponseData, nullBytes) || len(pk.ResponseData) == 0 {
// The form was cancelled: The cross in the top right corner was clicked.
pk.ResponseData = nil
}
if !ok {
return fmt.Errorf("no form with ID %v currently opened", pk.FormID)
}
if err := f.SubmitJSON(pk.ResponseData, s.c); err != nil {
return fmt.Errorf("error submitting form data: %w", err)
}
return nil
}