-
Notifications
You must be signed in to change notification settings - Fork 141
/
written_book.go
71 lines (65 loc) · 1.84 KB
/
written_book.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
package item
// WrittenBook is the item created after a book and quill is signed. It appears the same as a regular book, but
// without the quill, and has an enchanted-looking glint.
type WrittenBook struct {
// Title is the title of the book.
Title string
// Author is the author of the book.
Author string
// Generation is the copy tier of the book. 0 = original, 1 = copy of original,
// 2 = copy of copy.
Generation WrittenBookGeneration
// Pages represents the pages within the book.
Pages []string
}
// MaxCount always returns 1.
func (w WrittenBook) MaxCount() int {
return 1
}
// Page returns a specific page from the book and true when the page exists. It will otherwise return an empty string
// and false.
func (w WrittenBook) Page(page int) (string, bool) {
if page < 0 || len(w.Pages) <= page {
return "", false
}
return w.Pages[page], true
}
// DecodeNBT ...
func (w WrittenBook) DecodeNBT(data map[string]any) any {
if pages, ok := data["pages"].([]any); ok {
w.Pages = make([]string, len(pages))
for i, page := range pages {
w.Pages[i] = page.(map[string]any)["text"].(string)
}
}
w.Title, _ = data["title"].(string)
w.Author, _ = data["author"].(string)
if v, ok := data["generation"].(uint8); ok {
switch v {
case 0:
w.Generation = OriginalGeneration()
case 1:
w.Generation = CopyGeneration()
case 2:
w.Generation = CopyOfCopyGeneration()
}
}
return w
}
// EncodeNBT ...
func (w WrittenBook) EncodeNBT() map[string]any {
pages := make([]any, 0, len(w.Pages))
for _, page := range w.Pages {
pages = append(pages, map[string]any{"text": page})
}
return map[string]any{
"pages": pages,
"author": w.Author,
"title": w.Title,
"generation": w.Generation.Uint8(),
}
}
// EncodeItem ...
func (w WrittenBook) EncodeItem() (name string, meta int16) {
return "minecraft:written_book", 0
}