implementing storing chest data into Manager.Screens in bot/screen (#279)
This commit is contained in:
39
bot/screen/chest.go
Normal file
39
bot/screen/chest.go
Normal file
@ -0,0 +1,39 @@
|
||||
package screen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/data/inventory"
|
||||
)
|
||||
|
||||
type Chest struct {
|
||||
Type inventory.InventoryID
|
||||
Title chat.Message
|
||||
Slots []Slot
|
||||
Rows int
|
||||
}
|
||||
|
||||
func (c *Chest) onSetSlot(i int, slot Slot) error {
|
||||
if i < 0 || i >= len(c.Slots) {
|
||||
return errors.New("slot index out of bounds")
|
||||
}
|
||||
c.Slots[i] = slot
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Chest) onClose() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Chest) Container() []Slot {
|
||||
return c.Slots[0 : c.Rows*9]
|
||||
}
|
||||
|
||||
func (c *Chest) Main() []Slot {
|
||||
return c.Slots[c.Rows*9 : c.Rows*9+27]
|
||||
}
|
||||
|
||||
func (c *Chest) Hotbar() []Slot {
|
||||
return c.Slots[c.Rows*9+27 : (c.Rows+4)*9]
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
package screen
|
||||
|
||||
import "github.com/Tnze/go-mc/chat"
|
||||
|
||||
type EventsListener struct {
|
||||
Open func(id int) error
|
||||
Open func(id int, container_type int32, title chat.Message) error
|
||||
SetSlot func(id, index int) error
|
||||
Close func(id int) error
|
||||
}
|
||||
|
@ -81,11 +81,23 @@ func (m *Manager) onOpenScreen(p pk.Packet) error {
|
||||
if err := p.Scan(&ContainerID, &Type, &Title); err != nil {
|
||||
return Error{err}
|
||||
}
|
||||
//if c, ok := m.Screens[byte(ContainerID)]; ok {
|
||||
// TODO: Create the specified container
|
||||
//}
|
||||
if _, ok := m.Screens[int(ContainerID)]; !ok {
|
||||
TypeInt32 := int32(Type)
|
||||
if TypeInt32 < 6 {
|
||||
Rows := TypeInt32 + 1
|
||||
chest := Chest{
|
||||
Type: TypeInt32,
|
||||
Slots: make([]Slot, 9*Rows),
|
||||
Rows: int(Rows),
|
||||
Title: Title,
|
||||
}
|
||||
m.Screens[int(ContainerID)] = &chest
|
||||
}
|
||||
} else {
|
||||
return errors.New("container id already exists in screens")
|
||||
}
|
||||
if m.events.Open != nil {
|
||||
if err := m.events.Open(int(ContainerID)); err != nil {
|
||||
if err := m.events.Open(int(ContainerID), int32(Type), Title); err != nil {
|
||||
return Error{err}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user