github.com/df-mc/dragonfly@v0.9.13/server/player/playerdb/json.go (about) 1 package playerdb 2 3 import ( 4 "github.com/df-mc/dragonfly/server/item" 5 "github.com/df-mc/dragonfly/server/player" 6 "github.com/df-mc/dragonfly/server/world" 7 "github.com/go-gl/mathgl/mgl64" 8 "github.com/google/uuid" 9 "time" 10 ) 11 12 func (p *Provider) fromJson(d jsonData, lookupWorld func(world.Dimension) *world.World) player.Data { 13 dim, _ := world.DimensionByID(int(d.Dimension)) 14 mode, _ := world.GameModeByID(int(d.GameMode)) 15 data := player.Data{ 16 UUID: uuid.MustParse(d.UUID), 17 Username: d.Username, 18 Position: d.Position, 19 Velocity: d.Velocity, 20 Yaw: d.Yaw, 21 Pitch: d.Pitch, 22 Health: d.Health, 23 MaxHealth: d.MaxHealth, 24 Hunger: d.Hunger, 25 FoodTick: d.FoodTick, 26 ExhaustionLevel: d.ExhaustionLevel, 27 SaturationLevel: d.SaturationLevel, 28 AbsorptionLevel: d.AbsorptionLevel, 29 Experience: d.Experience, 30 AirSupply: d.AirSupply, 31 MaxAirSupply: d.MaxAirSupply, 32 EnchantmentSeed: d.EnchantmentSeed, 33 GameMode: mode, 34 Effects: dataToEffects(d.Effects), 35 FireTicks: d.FireTicks, 36 FallDistance: d.FallDistance, 37 Inventory: dataToInv(d.Inventory), 38 EnderChestInventory: make([]item.Stack, 27), 39 World: lookupWorld(dim), 40 } 41 decodeItems(d.EnderChestInventory, data.EnderChestInventory) 42 return data 43 } 44 45 func (p *Provider) toJson(d player.Data) jsonData { 46 dim, _ := world.DimensionID(d.World.Dimension()) 47 mode, _ := world.GameModeID(d.GameMode) 48 return jsonData{ 49 UUID: d.UUID.String(), 50 Username: d.Username, 51 Position: d.Position, 52 Velocity: d.Velocity, 53 Yaw: d.Yaw, 54 Pitch: d.Pitch, 55 Health: d.Health, 56 MaxHealth: d.MaxHealth, 57 Hunger: d.Hunger, 58 FoodTick: d.FoodTick, 59 ExhaustionLevel: d.ExhaustionLevel, 60 SaturationLevel: d.SaturationLevel, 61 AbsorptionLevel: d.AbsorptionLevel, 62 Experience: d.Experience, 63 AirSupply: d.AirSupply, 64 MaxAirSupply: d.MaxAirSupply, 65 EnchantmentSeed: d.EnchantmentSeed, 66 GameMode: uint8(mode), 67 Effects: effectsToData(d.Effects), 68 FireTicks: d.FireTicks, 69 FallDistance: d.FallDistance, 70 Inventory: invToData(d.Inventory), 71 EnderChestInventory: encodeItems(d.EnderChestInventory), 72 Dimension: uint8(dim), 73 } 74 } 75 76 type jsonData struct { 77 UUID string 78 Username string 79 Position, Velocity mgl64.Vec3 80 Yaw, Pitch float64 81 Health, MaxHealth float64 82 Hunger int 83 FoodTick int 84 ExhaustionLevel, SaturationLevel float64 85 AbsorptionLevel float64 86 EnchantmentSeed int64 87 Experience int 88 AirSupply, MaxAirSupply int64 89 GameMode uint8 90 Inventory jsonInventoryData 91 EnderChestInventory []jsonSlot 92 Effects []jsonEffect 93 FireTicks int64 94 FallDistance float64 95 Dimension uint8 96 } 97 98 type jsonInventoryData struct { 99 Items []jsonSlot 100 Boots []byte 101 Leggings []byte 102 Chestplate []byte 103 Helmet []byte 104 OffHand []byte 105 MainHandSlot uint32 106 } 107 108 type jsonSlot struct { 109 Item []byte 110 Slot int 111 } 112 113 type jsonEffect struct { 114 ID int 115 Level int 116 Duration time.Duration 117 Ambient bool 118 }