void cEnderChestEntity::SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid) { for (int i = 0; i < a_Grid.GetNumSlots(); i++) { Json::Value Slot; a_Grid.GetSlot(i).GetJson(Slot); a_Value.append(Slot); } }
void cNBTChunkSerializer::AddItemGrid(const cItemGrid & a_Grid, int a_BeginSlotNum) { int NumSlots = a_Grid.GetNumSlots(); for (int i = 0; i < NumSlots; i++) { const cItem & Item = a_Grid.GetSlot(i); if (Item.IsEmpty()) { continue; } AddItem(Item, i + a_BeginSlotNum); } // for i - chest slots[] }
/// Moves items from the specified ItemGrid into this hopper. Returns true if contents have changed. bool cHopperEntity::MoveItemsFromGrid(cItemGrid & a_Grid) { int NumSlots = a_Grid.GetNumSlots(); // First try adding items of types already in the hopper: for (int i = 0; i < NumSlots; i++) { if (a_Grid.IsSlotEmpty(i)) { continue; } if (MoveItemsFromSlot(a_Grid.GetSlot(i), false)) { a_Grid.ChangeSlotCount(i, -1); return true; } } // No already existing stack can be topped up, try again with allowing new stacks: for (int i = 0; i < NumSlots; i++) { if (a_Grid.IsSlotEmpty(i)) { continue; } if (MoveItemsFromSlot(a_Grid.GetSlot(i), true)) { a_Grid.ChangeSlotCount(i, -1); return true; } } return false; }
void cEnderChestEntity::LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid) { int SlotIdx = 0; for (Json::Value::iterator itr = a_Value.begin(); itr != a_Value.end(); ++itr) { cItem Item; Item.FromJson(*itr); a_Grid.SetSlot(SlotIdx, Item); SlotIdx++; } }
void cWSSAnvil::LoadItemGridFromNBT(cItemGrid & a_ItemGrid, const cParsedNBT & a_NBT, int a_ItemsTagIdx, int a_SlotOffset) { int NumSlots = a_ItemGrid.GetNumSlots(); for (int Child = a_NBT.GetFirstChild(a_ItemsTagIdx); Child != -1; Child = a_NBT.GetNextSibling(Child)) { int SlotTag = a_NBT.FindChildByName(Child, "Slot"); if ((SlotTag < 0) || (a_NBT.GetType(SlotTag) != TAG_Byte)) { continue; } int SlotNum = (int)(a_NBT.GetByte(SlotTag)) - a_SlotOffset; if ((SlotNum < 0) || (SlotNum >= NumSlots)) { // SlotNum outside of the range continue; } cItem Item; if (LoadItemFromNBT(Item, a_NBT, Child)) { a_ItemGrid.SetSlot(SlotNum, Item); } } // for itr - ItemDefs[] }
/// Moves one piece to the specified ItemGrid's slot. Returns true if contents have changed. bool cHopperEntity::MoveItemsToSlot(cItemGrid & a_ItemGrid, int a_DestSlotNum) { if (a_ItemGrid.IsSlotEmpty(a_DestSlotNum)) { // The slot is empty, move the first non-empty slot from our contents: for (int i = 0; i < ContentsWidth * ContentsHeight; i++) { if (!m_Contents.IsSlotEmpty(i)) { a_ItemGrid.SetSlot(a_DestSlotNum, m_Contents.GetSlot(i).CopyOne()); m_Contents.ChangeSlotCount(i, -1); return true; } } return false; } else { // The slot is taken, try to top it up: const cItem & DestSlot = a_ItemGrid.GetSlot(a_DestSlotNum); if (DestSlot.IsFullStack()) { return false; } for (int i = 0; i < ContentsWidth * ContentsHeight; i++) { if (m_Contents.GetSlot(i).IsStackableWith(DestSlot)) { a_ItemGrid.ChangeSlotCount(a_DestSlotNum, 1); m_Contents.ChangeSlotCount(i, -1); return true; } } return false; } }
/// Moves items to the specified ItemGrid. Returns true if contents have changed bool cHopperEntity::MoveItemsToGrid(cItemGrid & a_ItemGrid) { // Iterate through our slots, try to move from each one: for (int i = 0; i < ContentsWidth * ContentsHeight; i++) { const cItem & SrcItem = m_Contents.GetSlot(i); if (SrcItem.IsEmpty()) { continue; } cItem ToAdd = SrcItem.CopyOne(); if (a_ItemGrid.AddItem(ToAdd) > 0) { m_Contents.ChangeSlotCount(i, -1); return true; } } return false; }
cSlotAreaItemGrid::cSlotAreaItemGrid(cItemGrid & a_ItemGrid, cWindow & a_ParentWindow) : super(a_ItemGrid.GetNumSlots(), a_ParentWindow), m_ItemGrid(a_ItemGrid) { m_ItemGrid.AddListener(*this); }