/
menu.go
116 lines (103 loc) · 3.65 KB
/
menu.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package form
import (
"encoding/json"
"fmt"
"go/ast"
"reflect"
)
// Menu represents a menu form. These menus are made up of a title and a body, with a number of buttons which
// come below the body. These buttons may also have buttons on the side of them.
type Menu struct {
title, body string
submittable MenuSubmittable
buttons []Button
}
// Button represents a button added to a Menu form. The button has text on it and an optional image, which
// may be either retrieved from a website or the local assets of the game.
type Button struct {
// Text holds the text displayed on the button. It may use Minecraft formatting codes and may have
// newlines.
Text string
// Image holds a path to an image for the button. The Image may either be an URL pointing to an image,
// such as 'https://someimagewebsite.com/someimage.png', or a path pointing to a local asset, such as
// 'textures/blocks/grass_carried'.
Image string
}
// NewMenu creates a new Menu form using the MenuSubmittable passed to handle the output of the form. The
// title passed is formatted following the rules of fmt.Sprintln.
func NewMenu(submittable MenuSubmittable, title ...interface{}) Menu {
t := reflect.TypeOf(submittable)
if t.Kind() != reflect.Struct {
panic("submittable must be struct")
}
m := Menu{title: format(title), submittable: submittable}
m.verify()
return m
}
// WithBody creates a copy of the Menu form and changes its body to the body passed, after which the new Menu
// form is returned. The text is formatted following the rules of fmt.Sprintln.
func (m Menu) WithBody(body ...interface{}) Menu {
m.body = format(body)
return m
}
// WithButtons creates a copy of the Menu form and appends the buttons passed to the existing buttons, after
// which the new Menu form is returned.
func (m Menu) WithButtons(buttons ...Button) Menu {
m.buttons = append(m.buttons, buttons...)
return m
}
// Title returns the formatted title passed to the menu upon construction using NewMenu().
func (m Menu) Title() string {
return m.title
}
// Body returns the formatted text in the body passed to the menu using WithBody().
func (m Menu) Body() string {
return m.body
}
// Buttons returns a list of all buttons of the MenuSubmittable. It parses them from the fields using
// reflection and returns them.
func (m Menu) Buttons() []Button {
v := reflect.ValueOf(m.submittable)
t := reflect.TypeOf(m.submittable)
buttons := m.buttons
for i := 0; i < v.NumField(); i++ {
fieldT := t.Field(i)
fieldV := v.Field(i)
if !ast.IsExported(fieldT.Name) {
continue
}
// Each exported field is guaranteed to be of type Button.
buttons = append(buttons, fieldV.Interface().(Button))
}
return buttons
}
// SubmitJSON submits a JSON value to the menu, containing the index of the button clicked.
func (m Menu) SubmitJSON(b []byte, submitter Submitter) error {
var index uint
err := json.Unmarshal(b, &index)
if err != nil {
return fmt.Errorf("cannot parse button index as int: %w", err)
}
buttons := m.Buttons()
if index >= uint(len(buttons)) {
return fmt.Errorf("button index points to inexistent button: %v (only %v buttons present)", index, len(buttons))
}
m.submittable.Submit(submitter, buttons[index])
return nil
}
// verify verifies if the form is valid, checking all fields are of the type Button. It panics if the form is
// not valid.
func (m Menu) verify() {
v := reflect.ValueOf(m.submittable)
t := reflect.TypeOf(m.submittable)
for i := 0; i < v.NumField(); i++ {
fieldT := t.Field(i)
if !ast.IsExported(fieldT.Name) {
continue
}
if _, ok := v.Field(i).Interface().(Button); !ok {
panic("all exported fields must be of the type form.Button")
}
}
}
func (m Menu) __() {}