github.com/df-mc/dragonfly@v0.9.13/server/item/written_book.go (about)

     1  package item
     2  
     3  // WrittenBook is the item created after a book and quill is signed. It appears the same as a regular book, but
     4  // without the quill, and has an enchanted-looking glint.
     5  type WrittenBook struct {
     6  	// Title is the title of the book.
     7  	Title string
     8  	// Author is the author of the book.
     9  	Author string
    10  	// Generation is the copy tier of the book. 0 = original, 1 = copy of original,
    11  	// 2 = copy of copy.
    12  	Generation WrittenBookGeneration
    13  	// Pages represents the pages within the book.
    14  	Pages []string
    15  }
    16  
    17  // MaxCount always returns 1.
    18  func (WrittenBook) MaxCount() int {
    19  	return 1
    20  }
    21  
    22  // TotalPages returns the total number of pages in the book.
    23  func (w WrittenBook) TotalPages() int {
    24  	return len(w.Pages)
    25  }
    26  
    27  // Page returns a specific page from the book and true when the page exists. It will otherwise return an empty string
    28  // and false.
    29  func (w WrittenBook) Page(page int) (string, bool) {
    30  	if page < 0 || len(w.Pages) <= page {
    31  		return "", false
    32  	}
    33  	return w.Pages[page], true
    34  }
    35  
    36  // DecodeNBT ...
    37  func (w WrittenBook) DecodeNBT(data map[string]any) any {
    38  	if pages, ok := data["pages"].([]any); ok {
    39  		w.Pages = make([]string, len(pages))
    40  		for i, page := range pages {
    41  			w.Pages[i] = page.(map[string]any)["text"].(string)
    42  		}
    43  	}
    44  	w.Title, _ = data["title"].(string)
    45  	w.Author, _ = data["author"].(string)
    46  	if v, ok := data["generation"].(uint8); ok {
    47  		switch v {
    48  		case 0:
    49  			w.Generation = OriginalGeneration()
    50  		case 1:
    51  			w.Generation = CopyGeneration()
    52  		case 2:
    53  			w.Generation = CopyOfCopyGeneration()
    54  		}
    55  	}
    56  	return w
    57  }
    58  
    59  // EncodeNBT ...
    60  func (w WrittenBook) EncodeNBT() map[string]any {
    61  	pages := make([]any, 0, len(w.Pages))
    62  	for _, page := range w.Pages {
    63  		pages = append(pages, map[string]any{"text": page})
    64  	}
    65  	return map[string]any{
    66  		"pages":      pages,
    67  		"author":     w.Author,
    68  		"title":      w.Title,
    69  		"generation": w.Generation.Uint8(),
    70  	}
    71  }
    72  
    73  // EncodeItem ...
    74  func (WrittenBook) EncodeItem() (name string, meta int16) {
    75  	return "minecraft:written_book", 0
    76  }