func (inv *Inventory) MarshalNbt(tag *nbt.Compound) (err error) { occupiedSlots := 0 for i := range inv.slots { if inv.slots[i].Count > 0 { occupiedSlots++ } } itemList := &nbt.List{nbt.TagCompound, make([]nbt.ITag, 0, occupiedSlots)} for i := range inv.slots { slot := &inv.slots[i] if slot.Count > 0 { slotTag := nbt.NewCompound() slotTag.Set("Slot", &nbt.Byte{int8(i)}) if err = slot.MarshalNbt(slotTag); err != nil { return } itemList.Value = append(itemList.Value, slotTag) } } tag.Set("Items", itemList) return nil }
func (w *PlayerInventory) MarshalNbt(tag *nbt.Compound) (err os.Error) { slots := make([]nbt.ITag, 0, 0) // Add the holding inventory for i := 0; i < int(w.holding.NumSlots()); i++ { slot := w.holding.Slot(SlotId(i)) if !slot.IsEmpty() { slotTag := nbt.NewCompound() slotTag.Set("Slot", &nbt.Byte{int8(i)}) if err = slot.MarshalNbt(slotTag); err != nil { return } slots = append(slots, slotTag) } } // Add the main inventory for i := 0; i < int(w.main.NumSlots()); i++ { slot := w.main.Slot(SlotId(i)) if !slot.IsEmpty() { slotTag := nbt.NewCompound() slotTag.Set("Slot", &nbt.Byte{int8(i + playerInvHoldingNum)}) if err = slot.MarshalNbt(slotTag); err != nil { return } slots = append(slots, slotTag) } } // Add the armor inventory for i := 0; i < int(w.armor.NumSlots()); i++ { slot := w.armor.Slot(SlotId(i)) if !slot.IsEmpty() { slotTag := nbt.NewCompound() slotTag.Set("Slot", &nbt.Byte{int8(i + 100)}) if err = slot.MarshalNbt(slotTag); err != nil { return } slots = append(slots, slotTag) } } tag.Set("Inventory", &nbt.List{nbt.TagCompound, slots}) return nil }
func (w *nbtChunkWriter) SetTileEntities(tileEntities map[BlockIndex]gamerules.ITileEntity) { tileEntitiesNbt := make([]nbt.ITag, 0, len(tileEntities)) for _, entity := range tileEntities { tag := nbt.NewCompound() if err := entity.MarshalNbt(tag); err != nil { log.Printf("%T.MarshalNbt failed: %v", entity, err) continue } tileEntitiesNbt = append(tileEntitiesNbt, tag) } w.chunkTag.Lookup("Level/TileEntities").(*nbt.List).Value = tileEntitiesNbt }
func (w *nbtChunkWriter) SetEntities(entities map[EntityId]gamerules.INonPlayerEntity) { entitiesNbt := make([]nbt.ITag, 0, len(entities)) for _, entity := range entities { tag := nbt.NewCompound() if err := entity.MarshalNbt(tag); err != nil { log.Printf("%T.MarshalNbt failed: %v", entity, err) continue } entitiesNbt = append(entitiesNbt, tag) } w.chunkTag.Lookup("Level/Entities").(*nbt.List).Value = entitiesNbt }
// A player has disconnected from the server func (game *Game) onPlayerDisconnect(entityId EntityId) { oldPlayer := game.players[entityId] delete(game.players, entityId) delete(game.playerNames, oldPlayer.Name()) game.entityManager.RemoveEntityById(entityId) playerData := nbt.NewCompound() if err := oldPlayer.MarshalNbt(playerData); err != nil { log.Printf("Failed to marshal player data: %v", err) return } if err := game.worldStore.WritePlayerData(oldPlayer.Name(), playerData); err != nil { log.Printf("Failed when writing player data: %v", err) } }