Skip to content

Commit

Permalink
Refactor!: Generics and transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard127 committed Sep 10, 2023
1 parent 1ce4ae4 commit 52d2a09
Show file tree
Hide file tree
Showing 39 changed files with 392 additions and 457 deletions.
18 changes: 9 additions & 9 deletions bot/provider/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Edouard127/go-mc/data/entity"
"github.com/Edouard127/go-mc/data/enums"
"github.com/Edouard127/go-mc/data/packetid"
"github.com/Edouard127/go-mc/internal/util"
"github.com/Edouard127/go-mc/level/block"
"github.com/Edouard127/go-mc/maths"
pk "github.com/Edouard127/go-mc/net/packet"
Expand All @@ -29,7 +30,7 @@ type Player struct {
PlayerInfo world.PlayerInfo
WorldInfo world.WorldInfo
EntityPlayer *core.EntityPlayer
Transactions *transactions.Transactions
Transactions *util.Queue[[]*transactions.SlotAction]
Abilities *Abilities

// Player info
Expand Down Expand Up @@ -67,7 +68,7 @@ func NewPlayer(settings basic.Settings, clientWorld *world.World, info data.Auth
EntityPlayer: core.NewEntityPlayer(info.Name, 0, uuid.MustParse(info.UUID), 116, 0, 0, 0, 0, 0),
Controller: &core.Controller{},
Manager: screen.NewManager(),
Transactions: transactions.NewTransactions(),
Transactions: util.NewQueue[[]*transactions.SlotAction](),
Abilities: NewAbilities(),
IsSpawn: false,
}
Expand Down Expand Up @@ -106,13 +107,12 @@ func (pl *Player) Chat(c *Client, msg string) error {
}

func RunTransactions(c *Client, cancel context.CancelFunc) error {
if t := c.Player.Transactions.Next(); t == nil {
return nil
} else {
for _, v := range t.Packets {
if err := c.Conn.WritePacket(*v); err != nil {
return err
}
var next []*transactions.SlotAction
for c.Player.Transactions.Len() > 0 {
next = c.Player.Transactions.Pop()

for i := range next {
c.Conn.WritePacket(pk.Marshal(packetid.SPacketClickWindow, pk.UnsignedByte(c.Player.Manager.CurrentScreen.GetType()), pk.VarInt(c.Player.Manager.StateID), next[i]))
}
}

Expand Down
25 changes: 5 additions & 20 deletions bot/provider/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,17 @@ func SetContainerContent(c *Client, p pk.Packet, cancel context.CancelFunc) erro
state pk.VarInt
data []Slot
item Slot
err error
)

if err := p.Scan(&id, &state, pk.Array(&data), &item); err != nil {
return fmt.Errorf("failed to scan SetContainerContent: %w", err)
}

var container screen.Container
container := screen.Containers[int(id)]

c.Player.Manager.StateID = int32(state)

if id == 0 {
container = c.Player.Manager.Inventory
} else {
container = c.Player.Manager.Screens[int(id)]
}

for i, data := range data {
err := container.SetSlot(i, data)
for i := range data {
err = container.SetSlot(i, &data[i])
if err != nil {
return err
}
Expand Down Expand Up @@ -366,15 +359,7 @@ func SetContainerSlot(c *Client, p pk.Packet, cancel context.CancelFunc) error {

c.Player.Manager.StateID = int32(stateId)

var container screen.Container

if containerId == 0 {
container = c.Player.Manager.Inventory
} else {
container = c.Player.Manager.Screens[int(containerId)]
}

return container.SetSlot(int(slotId), data)
return screen.Containers[int(containerId)].SetSlot(int(slotId), &data)
}

func SetCooldown(c *Client, p pk.Packet, cancel context.CancelFunc) error {
Expand Down
4 changes: 2 additions & 2 deletions bot/screen/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
// Mode 3
const (
MiddleClick Button = iota + 2
_ // Just so my IDE doesn't scream at me because of the comment
_
)

// Mode 4
Expand All @@ -56,5 +56,5 @@ const (
// Mode 6
const (
DoubleClick Button = iota
_ // Just so my IDE doesn't scream at me because of the comment
_
)
77 changes: 30 additions & 47 deletions bot/screen/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,51 @@ package screen
import (
"github.com/Edouard127/go-mc/data/grids"
"github.com/Edouard127/go-mc/data/item"
"github.com/Edouard127/go-mc/data/slots"
)

type Manager struct {
Cursor item.Item
CurrentScreen grids.Container
HeldItem item.Item
CurrentScreen *Container
Screens map[int]Container
Inventory *grids.GenericInventory
// The last state received from the server
StateID int32
StateID int32 // The last state received from the server
}

func NewManager() *Manager {
inventory := new(grids.GenericInventory)
return &Manager{
Screens: fillContainers(inventory),
Inventory: inventory,
}
return &Manager{Inventory: Containers[0].(*grids.GenericInventory)}
}

func (m *Manager) OpenScreen(id int32) {
c := m.Screens[int(id)]
m.CurrentScreen = &c
m.CurrentScreen = Containers[int(id)]
}

func (m *Manager) CloseScreen() {
m.CurrentScreen = nil
m.Cursor = item.Item{}
}

func fillContainers(inventory *grids.GenericInventory) map[int]Container {
return map[int]Container{
1: grids.NewGeneric9x1(inventory),
2: grids.NewGeneric9x2(inventory),
3: grids.NewGeneric9x3(inventory),
4: grids.NewGeneric9x4(inventory),
5: grids.NewGeneric9x5(inventory),
6: grids.NewGeneric9x6(inventory),
7: grids.NewGeneric3x3(inventory),
8: grids.NewAnvil(inventory),
9: grids.NewBeacon(inventory),
10: grids.NewBlastFurnace(inventory),
11: grids.NewBrewingStand(inventory),
12: grids.NewCraftingTable(inventory),
13: grids.NewEnchantmentTable(inventory),
14: grids.NewFurnace(inventory),
15: grids.NewGrindstone(inventory),
16: grids.NewHopper(inventory),
17: grids.InitGenericContainer("nil", 0, 0, inventory), // TODO: This is the only one that is not a container, I don't know why mojang did this.
18: grids.NewLoom(inventory),
19: grids.NewMerchant(inventory),
20: grids.NewShulkerBox(inventory),
21: grids.NewSmithingTable(inventory),
22: grids.NewSmoker(inventory),
23: grids.NewCartographyTable(inventory),
24: grids.NewStonecutter(inventory),
}
}

type Container interface {
GetSlot(int) *slots.Slot
SetSlot(int, slots.Slot) error
OnClose() error
var Containers = map[int]grids.Container{
0: new(grids.GenericInventory),
1: grids.NewGeneric9x1(),
2: grids.NewGeneric9x2(),
3: grids.NewGeneric9x3(),
4: grids.NewGeneric9x4(),
5: grids.NewGeneric9x5(),
6: grids.NewGeneric9x6(),
7: grids.NewGeneric3x3(),
8: grids.NewAnvil(),
9: grids.NewBeacon(),
10: grids.NewBlastFurnace(),
11: grids.NewBrewingStand(),
12: grids.NewCraftingTable(),
13: grids.NewEnchantmentTable(),
14: grids.NewFurnace(),
15: grids.NewGrindstone(),
16: grids.NewHopper(),
17: grids.InitGenericContainer("nil", 0, 0), // TODO: This is the only one that is not a container, I don't know why mojang did this.
18: grids.NewLoom(),
19: grids.NewMerchant(),
20: grids.NewShulkerBox(),
21: grids.NewSmithingTable(),
22: grids.NewSmoker(),
23: grids.NewCartographyTable(),
24: grids.NewStonecutter(),
}
22 changes: 22 additions & 0 deletions bot/screen/screen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package screen

import (
"fmt"
"github.com/Edouard127/go-mc/data/slots"
"github.com/Edouard127/go-mc/nbt"
"testing"
)

func TestNewManager(t *testing.T) {
m := NewManager()
m.Inventory.SetSlot(2, &slots.Slot{
Index: 6969,
ID: 0,
Count: 0,
NBT: nbt.RawMessage{},
})
Containers[2].SetSlot(2, &slots.Slot{Index: 420})
fmt.Println(m.Inventory.GetSlot(2))
m.Inventory.OpenWith(Containers[2])
fmt.Println(m.Inventory.GetSlot(2))
}
9 changes: 0 additions & 9 deletions data/grids/anvil.go

This file was deleted.

9 changes: 0 additions & 9 deletions data/grids/beacon.go

This file was deleted.

9 changes: 0 additions & 9 deletions data/grids/blast_furnace.go

This file was deleted.

9 changes: 0 additions & 9 deletions data/grids/brewing_stand.go

This file was deleted.

9 changes: 0 additions & 9 deletions data/grids/cartography_table.go

This file was deleted.

9 changes: 0 additions & 9 deletions data/grids/crafting_table.go

This file was deleted.

9 changes: 0 additions & 9 deletions data/grids/enchantment_table.go

This file was deleted.

9 changes: 0 additions & 9 deletions data/grids/furnace.go

This file was deleted.

53 changes: 29 additions & 24 deletions data/grids/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,49 @@ import (
"github.com/Edouard127/go-mc/data/slots"
)

type Container interface {
GetSlot(int) *slots.Slot
SetSlot(int, *slots.Slot) error
OnClose() error
GetType() int
GetSize() int
}

type Generic struct {
Name string // Name of the grid.
Type int // Type is the int corresponding to the window type.
Size int
Data []slots.Slot
Inventory *GenericInventory
Name string // Name of the grid.
Type int // Type is the int corresponding to the window type.
Size int
Data []*slots.Slot
}

func InitGenericContainer(name string, id, size int, inventory *GenericInventory) *Generic {
func InitGenericContainer(name string, id, size int) *Generic {
return &Generic{
Name: name,
Type: id,
Size: size,
Data: make([]slots.Slot, size),
Inventory: inventory,
Name: name,
Type: id,
Size: size,
Data: make([]*slots.Slot, size),
}
}

func (g *Generic) OnClose() error { return nil }

func (g *Generic) GetSlot(i int) *slots.Slot {
if i < 0 || i >= len(g.Inventory.Slots) {
if i < 0 || i >= len(g.Data) {
return nil
}

if i < g.Size {
return &g.Data[i]
} else {
return &g.Inventory.Slots[g.Size : g.Size+35][i]
}
return g.Data[i]
}

func (g *Generic) SetSlot(i int, s slots.Slot) error {
if i < g.Size {
g.Data[i] = s
} else {
g.Inventory.Slots[g.Size : g.Size+35][i] = s
}

func (g *Generic) SetSlot(i int, s *slots.Slot) error {
g.Data[i] = s
return nil
}

func (g *Generic) GetType() int {
return g.Type
}

func (g *Generic) GetSize() int {
return g.Size
}
9 changes: 0 additions & 9 deletions data/grids/generic3x3.go

This file was deleted.

Loading

0 comments on commit 52d2a09

Please sign in to comment.