The Inv library allows the use of inventory menus, providing tools to create or send fake inventories.
To create an inventory menu using the Inv library, follow these steps:
Handle Player Packets: inv requires you to use the RedirectPlayerPackets on player join which makes it possible for the library to handle incoming player packets, mostly use to handle container closing.
// The 'conf' variable represents the server config.
conf.Listeners = intercept.WrapListeners(conf.Listeners)
srv := conf.New()
intercept.Start(srv)Create Menu Submittable: Your menu requires a menu submittable, here's an example:
type MySubmittable struct{}
func (m MySubmittable) Submit(p *player.Player, it item.Stack) {
fmt.Println("Submitted", it)
}
func (m MySubmittable) Close(p *player.Player) {
fmt.Println("Closed")
}Create Menu: Use the inv.NewMenu to create a new inventory menu.
m := inv.NewMenu(MySubmittable{}, "test", inv.ContainerTypeChest)Populate Menu Slots: Provide the menu with a item.Stack slice:
var stacks = make([]item.Stack, 27)
for i := 0; i < 27; i++ {
stacks[i] = item.NewStack(block.StainedGlass{Colour: item.ColourRed()}, 1)
}
m = m.WithStacks(stacks...)Sending the Menu to a Player To display the inventory menu to a player, use the inv.SendMenu function. Here's an example:
// The 'p' variable represents the targeted player.
inv.SendMenu(p, m)This code sends the inventory menu to the specified player.
Entity inventories support any positive slot count without placing fake blocks. They use an invisible client-side entity and the bundled Inventory UI Resource Pack.
Add the pack to your Dragonfly server config. Entity inventories also require the intercepted listeners shown above.
import "github.com/bedrock-gophers/inv/entityinv"
if err := entityinv.AddToConfig(&conf); err != nil {
log.Fatalln(err)
}
conf.ResourcesRequired = trueCreate and send an arbitrary-sized menu:
type MyEntityMenu struct{}
func (MyEntityMenu) Submit(p *player.Player, stack item.Stack) {
fmt.Println("Submitted", stack)
}
func (MyEntityMenu) Close(p *player.Player) {
fmt.Println("Closed")
}
stacks := make([]item.Stack, 59)
m := entityinv.NewMenu(MyEntityMenu{}, "59-slot Inventory", len(stacks)).WithStacks(stacks...)
entityinv.SendMenu(p, m)Menus above 54 slots get a scrollbar. Call entityinv.UpdateMenu to replace an open menu's contents, and call
entityinv.CloseContainer when a player quits to release its session state.
Form inventories use Bedrock menu forms rendered as a chest by the bundled resource pack. Add the pack to your Dragonfly server config before creating the server:
import "github.com/bedrock-gophers/inv/forminv"
if err := forminv.AddToConfig(&conf); err != nil {
log.Fatalln(err)
}
conf.ResourcesRequired = trueRegister the bundled pack once. It overrides Bedrock server-form UI definitions so menus created by forminv render as chest grids while preserving normal form submission.
Create and send a form inventory menu with the forminv package:
import (
"github.com/bedrock-gophers/inv/forminv"
"github.com/df-mc/dragonfly/server/player"
)
type MyFormMenu struct{}
func (MyFormMenu) Submit(p *player.Player, slot forminv.Slot) {
fmt.Println("Submitted", slot.Button.Text)
}
func (MyFormMenu) Close(p *player.Player) {
fmt.Println("Closed")
}
m := forminv.NewMenu(MyFormMenu{}, "test", forminv.LargeChest).WithSlots(
forminv.NewSlot(4, "Nodebuff", "textures/items/diamond_sword", "nodebuff"),
forminv.NewSlot(22, "Settings", "textures/ui/icon_setting", "settings"),
)
forminv.SendMenu(p, m)Use NewSlot or slot.At(index) to place a slot at a specific index. Unpositioned slots fill the first open slot, and empty slots are rendered as placeholders. Use WithSlots when you want to bind a submitted value that is separate from the rendered button.