-
Notifications
You must be signed in to change notification settings - Fork 143
/
json.go
118 lines (111 loc) · 3.6 KB
/
json.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package playerdb
import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"github.com/google/uuid"
"time"
)
func (p *Provider) fromJson(d jsonData, lookupWorld func(world.Dimension) *world.World) player.Data {
dim, _ := world.DimensionByID(int(d.Dimension))
mode, _ := world.GameModeByID(int(d.GameMode))
data := player.Data{
UUID: uuid.MustParse(d.UUID),
Username: d.Username,
Position: d.Position,
Velocity: d.Velocity,
Yaw: d.Yaw,
Pitch: d.Pitch,
Health: d.Health,
MaxHealth: d.MaxHealth,
Hunger: d.Hunger,
FoodTick: d.FoodTick,
ExhaustionLevel: d.ExhaustionLevel,
SaturationLevel: d.SaturationLevel,
AbsorptionLevel: d.AbsorptionLevel,
Experience: d.Experience,
AirSupply: d.AirSupply,
MaxAirSupply: d.MaxAirSupply,
EnchantmentSeed: d.EnchantmentSeed,
GameMode: mode,
Effects: dataToEffects(d.Effects),
FireTicks: d.FireTicks,
FallDistance: d.FallDistance,
Inventory: dataToInv(d.Inventory),
EnderChestInventory: make([]item.Stack, 27),
World: lookupWorld(dim),
}
decodeItems(d.EnderChestInventory, data.EnderChestInventory)
return data
}
func (p *Provider) toJson(d player.Data) jsonData {
dim, _ := world.DimensionID(d.World.Dimension())
mode, _ := world.GameModeID(d.GameMode)
return jsonData{
UUID: d.UUID.String(),
Username: d.Username,
Position: d.Position,
Velocity: d.Velocity,
Yaw: d.Yaw,
Pitch: d.Pitch,
Health: d.Health,
MaxHealth: d.MaxHealth,
Hunger: d.Hunger,
FoodTick: d.FoodTick,
ExhaustionLevel: d.ExhaustionLevel,
SaturationLevel: d.SaturationLevel,
AbsorptionLevel: d.AbsorptionLevel,
Experience: d.Experience,
AirSupply: d.AirSupply,
MaxAirSupply: d.MaxAirSupply,
EnchantmentSeed: d.EnchantmentSeed,
GameMode: uint8(mode),
Effects: effectsToData(d.Effects),
FireTicks: d.FireTicks,
FallDistance: d.FallDistance,
Inventory: invToData(d.Inventory),
EnderChestInventory: encodeItems(d.EnderChestInventory),
Dimension: uint8(dim),
}
}
type jsonData struct {
UUID string
Username string
Position, Velocity mgl64.Vec3
Yaw, Pitch float64
Health, MaxHealth float64
Hunger int
FoodTick int
ExhaustionLevel, SaturationLevel float64
AbsorptionLevel float64
EnchantmentSeed int64
Experience int
AirSupply, MaxAirSupply int64
GameMode uint8
Inventory jsonInventoryData
EnderChestInventory []jsonSlot
Effects []jsonEffect
FireTicks int64
FallDistance float64
Dimension uint8
}
type jsonInventoryData struct {
Items []jsonSlot
Boots []byte
Leggings []byte
Chestplate []byte
Helmet []byte
OffHand []byte
MainHandSlot uint32
}
type jsonSlot struct {
Item []byte
Slot int
}
type jsonEffect struct {
ID int
Level int
Duration time.Duration
Ambient bool
}