Skip to content

Commit

Permalink
Add item DataComponent implements (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Jul 28, 2024
1 parent 966f50e commit 090493c
Show file tree
Hide file tree
Showing 26 changed files with 860 additions and 256 deletions.
74 changes: 37 additions & 37 deletions bot/basic/cookie.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
package basic

import (
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
)

func (p *Player) handleCookieRequestPacket(packet pk.Packet) error {
var key pk.Identifier
err := packet.Scan(&key)
if err != nil {
return Error{err}
}
cookieContent := p.c.Cookies[string(key)]
err = p.c.Conn.WritePacket(pk.Marshal(
packetid.ServerboundCookieResponse,
key, pk.OptionEncoder[pk.ByteArray]{
Has: cookieContent != nil,
Val: pk.ByteArray(cookieContent),
},
))
if err != nil {
return Error{err}
}
return nil
}

func (p *Player) handleStoreCookiePacket(packet pk.Packet) error {
var key pk.Identifier
var payload pk.ByteArray
err := packet.Scan(&key, &payload)
if err != nil {
return Error{err}
}
p.c.Cookies[string(key)] = []byte(payload)
return nil
}
package basic

import (
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
)

func (p *Player) handleCookieRequestPacket(packet pk.Packet) error {
var key pk.Identifier
err := packet.Scan(&key)
if err != nil {
return Error{err}
}
cookieContent := p.c.Cookies[string(key)]
err = p.c.Conn.WritePacket(pk.Marshal(
packetid.ServerboundCookieResponse,
key, pk.OptionEncoder[pk.ByteArray]{
Has: cookieContent != nil,
Val: pk.ByteArray(cookieContent),
},
))
if err != nil {
return Error{err}
}
return nil
}

func (p *Player) handleStoreCookiePacket(packet pk.Packet) error {
var key pk.Identifier
var payload pk.ByteArray
err := packet.Scan(&key, &payload)
if err != nil {
return Error{err}
}
p.c.Cookies[string(key)] = []byte(payload)
return nil
}
74 changes: 37 additions & 37 deletions bot/basic/tags.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
package basic

import (
"bytes"
"errors"

pk "github.com/Tnze/go-mc/net/packet"
)

func (p *Player) handleUpdateTags(packet pk.Packet) error {
r := bytes.NewReader(packet.Data)

var length pk.VarInt
_, err := length.ReadFrom(r)
if err != nil {
return Error{err}
}

var registryID pk.Identifier
for i := 0; i < int(length); i++ {
_, err = registryID.ReadFrom(r)
if err != nil {
return Error{err}
}

registry := p.c.Registries.Registry(string(registryID))
if registry == nil {
return Error{errors.New("unknown registry: " + string(registryID))}
}

_, err = registry.ReadTagsFrom(r)
if err != nil {
return Error{err}
}
}
return nil
}
package basic

import (
"bytes"
"errors"

pk "github.com/Tnze/go-mc/net/packet"
)

func (p *Player) handleUpdateTags(packet pk.Packet) error {
r := bytes.NewReader(packet.Data)

var length pk.VarInt
_, err := length.ReadFrom(r)
if err != nil {
return Error{err}
}

var registryID pk.Identifier
for i := 0; i < int(length); i++ {
_, err = registryID.ReadFrom(r)
if err != nil {
return Error{err}
}

registry := p.c.Registries.Registry(string(registryID))
if registry == nil {
return Error{errors.New("unknown registry: " + string(registryID))}
}

_, err = registry.ReadTagsFrom(r)
if err != nil {
return Error{err}
}
}
return nil
}
34 changes: 17 additions & 17 deletions data/registryid/bootstrap/builtinregistries.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package bootstrap

import (
"github.com/Tnze/go-mc/data/registryid"
"github.com/Tnze/go-mc/level/block"
"github.com/Tnze/go-mc/registry"
)

func RegisterBlocks(reg *registry.Registry[block.Block]) {
reg.Clear()
for i, key := range registryid.Block {
id, val := reg.Put(key, block.FromID[key])
if int32(i) != id || val == nil || *val == nil {
panic("register blocks failed")
}
}
}
package bootstrap

import (
"github.com/Tnze/go-mc/data/registryid"
"github.com/Tnze/go-mc/level/block"
"github.com/Tnze/go-mc/registry"
)

func RegisterBlocks(reg *registry.Registry[block.Block]) {
reg.Clear()
for i, key := range registryid.Block {
id, val := reg.Put(key, block.FromID[key])
if int32(i) != id || val == nil || *val == nil {
panic("register blocks failed")
}
}
}
22 changes: 22 additions & 0 deletions level/component/attributemodifiers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package component

import "io"

var _ DataComponent = (*AttributeModifiers)(nil)

type AttributeModifiers struct{}

// ID implements DataComponent.
func (AttributeModifiers) ID() string {
return "minecraft:attribute_modifiers"
}

// ReadFrom implements DataComponent.
func (a *AttributeModifiers) ReadFrom(r io.Reader) (n int64, err error) {
panic("unimplemented")
}

// WriteTo implements DataComponent.
func (a *AttributeModifiers) WriteTo(w io.Writer) (n int64, err error) {
panic("unimplemented")
}
22 changes: 22 additions & 0 deletions level/component/canbreak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package component

import "io"

var _ DataComponent = (*CanBreak)(nil)

type CanBreak struct{}

// ID implements DataComponent.
func (CanBreak) ID() string {
return "minecraft:can_break"
}

// ReadFrom implements DataComponent.
func (c *CanBreak) ReadFrom(r io.Reader) (n int64, err error) {
panic("unimplemented")
}

// WriteTo implements DataComponent.
func (c *CanBreak) WriteTo(w io.Writer) (n int64, err error) {
panic("unimplemented")
}
22 changes: 22 additions & 0 deletions level/component/canplaceon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package component

import "io"

var _ DataComponent = (*CanPlaceOn)(nil)

type CanPlaceOn struct{}

// ID implements DataComponent.
func (CanPlaceOn) ID() string {
return "minecraft:can_place_on"
}

// ReadFrom implements DataComponent.
func (c *CanPlaceOn) ReadFrom(r io.Reader) (n int64, err error) {
panic("unimplemented")
}

// WriteTo implements DataComponent.
func (c *CanPlaceOn) WriteTo(w io.Writer) (n int64, err error) {
panic("unimplemented")
}
90 changes: 90 additions & 0 deletions level/component/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package component

import pk "github.com/Tnze/go-mc/net/packet"

type DataComponent interface {
pk.Field
ID() string
}

func NewComponent(id int32) DataComponent {
switch id {
case 0:
return new(CustomData)
case 1:
return new(MaxStackSize)
case 2:
return new(MaxDamage)
case 3:
return new(Damage)
case 4:
return new(Unbreakable)
case 5:
return new(CustomName)
case 6:
return new(ItemName)
case 7:
return new(Lore)
case 8:
return new(Rarity)
case 9:
return new(Enchantments)
case 10:
return new(CanPlaceOn)
case 11:
return new(CanBreak)
case 12:
return new(AttributeModifiers)
case 13:
return new(CustomModelData)
case 14:
return new(HideAdditionalTooptip)
case 15:
return new(HideTooptip)
case 16:
return new(RepairCost)
case 17:
return new(CreativeSlotLock)
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:
case 41:
case 42:
case 43:
return new(Recipes)
case 44:
case 45:
case 46:
case 47:
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
}
return nil
}
10 changes: 10 additions & 0 deletions level/component/components_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package component_test

// func TestNewComponent(t *testing.T) {
// for i, want := range registryid.DataComponentType {
// comp := component.NewComponent(int32(i))
// if got := comp.ID(); got != want {
// t.Errorf("DataComponent type mismatched: %s != %s", got, want)
// }
// }
// }
24 changes: 24 additions & 0 deletions level/component/creativeslotlock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package component

import (
"io"
)

var _ DataComponent = (*CreativeSlotLock)(nil)

type CreativeSlotLock struct{}

// ID implements DataComponent.
func (c *CreativeSlotLock) ID() string {
return "minecraft:creative_slot_lock"
}

// ReadFrom implements DataComponent.
func (c *CreativeSlotLock) ReadFrom(r io.Reader) (n int64, err error) {
return 0, nil
}

// WriteTo implements DataComponent.
func (c *CreativeSlotLock) WriteTo(w io.Writer) (n int64, err error) {
return 0, nil
}
Loading

0 comments on commit 090493c

Please sign in to comment.