-
Notifications
You must be signed in to change notification settings - Fork 145
/
item.go
66 lines (58 loc) · 2.18 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package entity
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/internal/nbtconv"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"time"
)
// NewItem creates a new item entity using the item stack passed. The item
// entity will be positioned at the position passed. If the stack's count
// exceeds its max count, the count of the stack will be changed to the
// maximum.
func NewItem(i item.Stack, pos mgl64.Vec3) *Ent {
return Config{Behaviour: itemConf.New(i)}.New(ItemType{}, pos)
}
// NewItemPickupDelay creates a new item entity containing item stack i. A
// delay may be specified which defines for how long the item stack cannot be
// picked up from the ground.
func NewItemPickupDelay(i item.Stack, pos mgl64.Vec3, delay time.Duration) *Ent {
config := itemConf
config.PickupDelay = delay
return Config{Behaviour: config.New(i)}.New(ItemType{}, pos)
}
var itemConf = ItemBehaviourConfig{
Gravity: 0.04,
Drag: 0.02,
}
// ItemType is a world.EntityType implementation for Item.
type ItemType struct{}
func (ItemType) EncodeEntity() string { return "minecraft:item" }
func (ItemType) NetworkOffset() float64 { return 0.125 }
func (ItemType) BBox(world.Entity) cube.BBox {
return cube.Box(-0.125, 0, -0.125, 0.125, 0.25, 0.125)
}
func (ItemType) DecodeNBT(m map[string]any) world.Entity {
i := nbtconv.MapItem(m, "Item")
if i.Empty() {
return nil
}
n := NewItem(i, nbtconv.Vec3(m, "Pos"))
n.SetVelocity(nbtconv.Vec3(m, "Motion"))
n.age = time.Duration(nbtconv.Int16(m, "Age")) * (time.Second / 20)
n.Behaviour().(*ItemBehaviour).pickupDelay = time.Duration(nbtconv.Int64(m, "PickupDelay")) * (time.Second / 20)
return n
}
func (ItemType) EncodeNBT(e world.Entity) map[string]any {
it := e.(*Ent)
b := it.Behaviour().(*ItemBehaviour)
return map[string]any{
"Health": int16(5),
"Age": int16(it.Age() / (time.Second * 20)),
"PickupDelay": int64(b.pickupDelay / (time.Second * 20)),
"Pos": nbtconv.Vec3ToFloat32Slice(it.Position()),
"Motion": nbtconv.Vec3ToFloat32Slice(it.Velocity()),
"Item": nbtconv.WriteItem(b.Item(), true),
}
}