From: Kahrl Date: Sat, 21 Jan 2012 20:21:41 +0000 (+0100) Subject: Inventory menu (with dragging) improved. Crafting is now handled via a IACTION_CRAFT... X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=1efdc36b22532807d21a0beac94524e3eacfe7bc;p=zefram%2Fminetest%2Fminetest_engine.git Inventory menu (with dragging) improved. Crafting is now handled via a IACTION_CRAFT inventory action. --- diff --git a/src/game.cpp b/src/game.cpp index 055320a8..47a0e6af 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1310,7 +1310,7 @@ void the_game( "list", inventoryloc, "craft", v2s32(3, 0), v2s32(3, 3))); draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", inventoryloc, "craftresult", + "list", inventoryloc, "craftpreview", v2s32(7, 1), v2s32(1, 1))); menu->setDrawSpec(draw_spec); diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp index 6e46bd49..a4b16b25 100644 --- a/src/guiInventoryMenu.cpp +++ b/src/guiInventoryMenu.cpp @@ -133,6 +133,8 @@ GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env, m_gamedef(gamedef) { m_selected_item = NULL; + m_selected_amount = 0; + m_selected_dragging = false; m_tooltip_element = NULL; } @@ -317,28 +319,17 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase) } } - if(phase == 1 && !item.empty()) + if(phase == 1) { - // Draw item at the normal position if - // - the item is not being dragged or - // /*- the item is in the crafting result slot*/ - if(!selected /*|| s.listname == "craftresult"*/) + // Draw item stack + if(selected) { - drawItemStack(driver, font, item, - rect, &AbsoluteClippingRect, m_gamedef); + item.takeItem(m_selected_amount); } - } - - if(phase ==2 && !item.empty()) - { - // Draw dragged item - if(selected) + if(!item.empty()) { - v2s32 offset = m_pointer - rect.getCenter(); - rect.UpperLeftCorner += offset; - rect.LowerRightCorner += offset; drawItemStack(driver, font, item, - rect, NULL, m_gamedef); + rect, &AbsoluteClippingRect, m_gamedef); } // Draw tooltip @@ -359,12 +350,38 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase) core::dimension2d(tooltip_width, tooltip_height))); } } - } } +void GUIInventoryMenu::drawSelectedItem() +{ + if(!m_selected_item) + return; + + video::IVideoDriver* driver = Environment->getVideoDriver(); + + // Get font + gui::IGUIFont *font = NULL; + gui::IGUISkin* skin = Environment->getSkin(); + if (skin) + font = skin->getFont(); + + Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc); + assert(inv); + InventoryList *list = inv->getList(m_selected_item->listname); + assert(list); + ItemStack stack = list->getItem(m_selected_item->i); + stack.count = m_selected_amount; + + core::rect imgrect(0,0,imgsize.X,imgsize.Y); + core::rect rect = imgrect + (m_pointer - imgrect.getCenter()); + drawItemStack(driver, font, stack, rect, NULL, m_gamedef); +} + void GUIInventoryMenu::drawMenu() { + updateSelectedItem(); + gui::IGUISkin* skin = Environment->getSkin(); if (!skin) return; @@ -378,22 +395,93 @@ void GUIInventoryMenu::drawMenu() /* Draw items Phase 0: Item slot rectangles - Phase 1: Item images - Phase 2: Dragged item image; tooltip + Phase 1: Item images; prepare tooltip */ - for(int phase=0; phase<=2; phase++) + for(int phase=0; phase<=1; phase++) for(u32 i=0; iisValid()) + { + Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc); + if(inv) + { + InventoryList *list = inv->getList(m_selected_item->listname); + if(list && (u32) m_selected_item->i < list->getSize()) + { + ItemStack stack = list->getItem(m_selected_item->i); + if(m_selected_amount > stack.count) + m_selected_amount = stack.count; + if(!stack.empty()) + selection_valid = true; + } + } + } + if(!selection_valid) + { + delete m_selected_item; + m_selected_item = NULL; + m_selected_amount = 0; + m_selected_dragging = false; + } + } + + // If craftresult is nonempty and nothing else is selected, select it now. + if(!m_selected_item) + { + for(u32 i=0; igetInventory(s.inventoryloc); + InventoryList *list = inv->getList("craftresult"); + if(list && list->getSize() >= 1 && !list->getItem(0).empty()) + { + m_selected_item = new ItemSpec; + m_selected_item->inventoryloc = s.inventoryloc; + m_selected_item->listname = "craftresult"; + m_selected_item->i = 0; + m_selected_amount = 0; + m_selected_dragging = false; + break; + } + } + } + } + + // If craftresult is selected, keep the whole stack selected + if(m_selected_item && m_selected_item->listname == "craftresult") + { + Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc); + assert(inv); + InventoryList *list = inv->getList(m_selected_item->listname); + assert(list); + m_selected_amount = list->getItem(m_selected_item->i).count; + } +} + bool GUIInventoryMenu::OnEvent(const SEvent& event) { if(event.EventType==EET_KEY_INPUT_EVENT) @@ -418,133 +506,242 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event) // Mouse event other than movement v2s32 p(event.MouseInput.X, event.MouseInput.Y); + m_pointer = p; + + // Get selected item and hovered/clicked item (s) + + updateSelectedItem(); ItemSpec s = getItemAtPos(p); Inventory *inv_selected = NULL; Inventory *inv_s = NULL; + if(m_selected_item) { - assert(m_selected_item->isValid()); inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc); assert(inv_selected); + assert(inv_selected->getList(m_selected_item->listname) != NULL); } + + u32 s_count = 0; + if(s.isValid()) { inv_s = m_invmgr->getInventory(s.inventoryloc); assert(inv_s); + + InventoryList *list = inv_s->getList(s.listname); + if(list != NULL && (u32) s.i < list->getSize()) + s_count = list->getItem(s.i).count; + else + s.i = -1; // make it invalid again } - bool different_item = m_selected_item - && ((inv_selected != inv_s) - || (m_selected_item->listname != s.listname) - || (m_selected_item->i != s.i)); - int amount = -1; + // buttons: 0 = left, 1 = right, 2 = middle + // up/down: 0 = down (press), 1 = up (release), 2 = unknown event + int button = 0; + int updown = 2; if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) - amount = 0; + { button = 0; updown = 0; } else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) - amount = 1; + { button = 1; updown = 0; } else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN) - amount = 10; - else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && different_item) - amount = 0; - //else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP && different_item) - // amount = 1; - //else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP && different_item) - // amount = 10; - - if(amount >= 0) + { button = 2; updown = 0; } + else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) + { button = 0; updown = 1; } + else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) + { button = 1; updown = 1; } + else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP) + { button = 2; updown = 1; } + + // Set this number to a positive value to generate a move action + // from m_selected_item to s. + u32 move_amount = 0; + + // Set this number to a positive value to generate a drop action + // from m_selected_item. + u32 drop_amount = 0; + + // Set this number to a positive value to generate a craft action at s. + u32 craft_amount = 0; + + if(updown == 0) { - // Indicates whether source slot should be deselected - bool remove_selection = false; + // Some mouse button has been pressed - //infostream<<"Mouse action at p=("<getList(m_selected_item->listname); - InventoryList *list_to = - inv_to->getList(s.listname); - if(list_from == NULL) - infostream<<"from list doesn't exist"<getItem(m_selected_item->i).empty()) - { - infostream<<"Handing IACTION_MOVE to manager"<count = amount; - a->from_inv = m_selected_item->inventoryloc; - a->from_list = m_selected_item->listname; - a->from_i = m_selected_item->i; - a->to_inv = s.inventoryloc; - a->to_list = s.listname; - a->to_i = s.i; - m_invmgr->inventoryAction(a); - - if(amount == 0 || list_from->getItem(m_selected_item->i).count<=amount) - remove_selection = true; - } - } - else + // Craft preview has been clicked: craft + craft_amount = (button == 2 ? 10 : 1); + } + else if(m_selected_item == NULL) + { + if(s_count != 0) { - /* - Select if nonempty - */ - assert(inv_s); - InventoryList *list = inv_s->getList(s.listname); - if(list && !list->getItem(s.i).empty()) - { - m_selected_item = new ItemSpec(s); - } + // Non-empty stack has been clicked: select it + m_selected_item = new ItemSpec(s); + + if(button == 1) // right + m_selected_amount = (s_count + 1) / 2; + else if(button == 2) // middle + m_selected_amount = MYMIN(s_count, 10); + else // left + m_selected_amount = s_count; + + m_selected_dragging = true; } } - else if(m_selected_item) + else // m_selected_item != NULL { - // If moved outside the menu, drop. - // (Otherwise abort inventory action.) - if(getAbsoluteClippingRect().isPointInside(m_pointer)) + assert(m_selected_amount >= 1); + + if(s.isValid()) { - // Inside menu - remove_selection = true; + // Clicked another slot: move + if(button == 1) // right + move_amount = 1; + else if(button == 2) // middle + move_amount = MYMIN(m_selected_amount, 10); + else // left + move_amount = m_selected_amount; + } + else if(getAbsoluteClippingRect().isPointInside(m_pointer)) + { + // Clicked somewhere else: deselect + m_selected_amount = 0; } else { - // Outside of menu - Inventory *inv_from = inv_selected; - assert(inv_from); - InventoryList *list_from = - inv_from->getList(m_selected_item->listname); - if(list_from == NULL) - infostream<<"from list doesn't exist"<getItem(m_selected_item->i).empty()) - { - infostream<<"Handing IACTION_DROP to manager"<count = amount; - a->from_inv = m_selected_item->inventoryloc; - a->from_list = m_selected_item->listname; - a->from_i = m_selected_item->i; - m_invmgr->inventoryAction(a); - if(amount == 0 || list_from->getItem(m_selected_item->i).count<=amount) - remove_selection = true; - } + // Clicked outside of the window: drop + if(button == 1) // right + drop_amount = 1; + else if(button == 2) // middle + drop_amount = MYMIN(m_selected_amount, 10); + else // left + drop_amount = m_selected_amount; + } + } + } + else if(updown == 1) + { + // Some mouse button has been released + + //infostream<<"Mouse button "<listname != s.listname) || + (m_selected_item->i != s.i)) + { + // Dragged to different slot: move all selected + move_amount = m_selected_amount; } } + else if(m_selected_item != NULL && m_selected_dragging && + !(getAbsoluteClippingRect().isPointInside(m_pointer))) + { + // Dragged outside of window: drop all selected + drop_amount = m_selected_amount; + } - if(remove_selection) + m_selected_dragging = false; + } + + // Possibly send inventory action to server + if(move_amount > 0) + { + // Send IACTION_MOVE + + assert(m_selected_item && m_selected_item->isValid()); + assert(s.isValid()); + + assert(inv_selected && inv_s); + InventoryList *list_from = inv_selected->getList(m_selected_item->listname); + InventoryList *list_to = inv_s->getList(s.listname); + assert(list_from && list_to); + ItemStack stack_from = list_from->getItem(m_selected_item->i); + ItemStack stack_to = list_to->getItem(s.i); + + // Check how many items can be moved + move_amount = stack_from.count = MYMIN(move_amount, stack_from.count); + ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef()); + if(leftover.count == stack_from.count) + { + // Swap the stacks + } + else if(leftover.empty()) + { + // Item fits + } + else { - delete m_selected_item; - m_selected_item = NULL; + // Item only fits partially + move_amount -= leftover.count; } + assert(move_amount > 0 && move_amount <= m_selected_amount); + m_selected_amount -= move_amount; + + infostream<<"Handing IACTION_MOVE to manager"<count = move_amount; + a->from_inv = m_selected_item->inventoryloc; + a->from_list = m_selected_item->listname; + a->from_i = m_selected_item->i; + a->to_inv = s.inventoryloc; + a->to_list = s.listname; + a->to_i = s.i; + m_invmgr->inventoryAction(a); + } + else if(drop_amount > 0) + { + // Send IACTION_DROP + + assert(m_selected_item && m_selected_item->isValid()); + assert(inv_selected); + InventoryList *list_from = inv_selected->getList(m_selected_item->listname); + assert(list_from); + ItemStack stack_from = list_from->getItem(m_selected_item->i); + + // Check how many items can be dropped + drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count); + assert(drop_amount > 0 && drop_amount <= m_selected_amount); + m_selected_amount -= drop_amount; + + infostream<<"Handing IACTION_DROP to manager"<count = drop_amount; + a->from_inv = m_selected_item->inventoryloc; + a->from_list = m_selected_item->listname; + a->from_i = m_selected_item->i; + m_invmgr->inventoryAction(a); + } + else if(craft_amount > 0) + { + // Send IACTION_CRAFT + + assert(s.isValid()); + assert(inv_s); + + infostream<<"Handing IACTION_CRAFT to manager"<count = craft_amount; + a->craft_inv = s.inventoryloc; + m_invmgr->inventoryAction(a); + } + + // If m_selected_amount has been decreased to zero, deselect + if(m_selected_amount == 0) + { + delete m_selected_item; + m_selected_item = NULL; + m_selected_dragging = false; } } if(event.EventType==EET_GUI_EVENT) diff --git a/src/guiInventoryMenu.h b/src/guiInventoryMenu.h index 903a8b13..ed8df88f 100644 --- a/src/guiInventoryMenu.h +++ b/src/guiInventoryMenu.h @@ -137,7 +137,9 @@ public: ItemSpec getItemAtPos(v2s32 p) const; void drawList(const ListDrawSpec &s, int phase); + void drawSelectedItem(); void drawMenu(); + void updateSelectedItem(); bool OnEvent(const SEvent& event); @@ -160,6 +162,9 @@ protected: core::array m_draw_spec; ItemSpec *m_selected_item; + u32 m_selected_amount; + bool m_selected_dragging; + v2s32 m_pointer; gui::IGUIStaticText *m_tooltip_element; }; diff --git a/src/inventorymanager.cpp b/src/inventorymanager.cpp index eeb293cb..b42a8067 100644 --- a/src/inventorymanager.cpp +++ b/src/inventorymanager.cpp @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "main.h" // for g_settings #include "settings.h" #include "utility.h" +#include "craftdef.h" /* InventoryLocation @@ -124,6 +125,10 @@ InventoryAction * InventoryAction::deSerialize(std::istream &is) { a = new IDropAction(is); } + else if(type == "Craft") + { + a = new ICraftAction(is); + } return a; } @@ -152,7 +157,7 @@ IMoveAction::IMoveAction(std::istream &is) to_i = stoi(ts); } -void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player) +void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef) { Inventory *inv_from = mgr->getInventory(from_inv); Inventory *inv_to = mgr->getInventory(to_inv); @@ -273,7 +278,7 @@ IDropAction::IDropAction(std::istream &is) from_i = stoi(ts); } -void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player) +void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef) { Inventory *inv_from = mgr->getInventory(from_inv); @@ -332,3 +337,120 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player) <<" i="<getInventory(craft_inv); + + if(!inv_craft){ + infostream<<"ICraftAction::apply(): FAIL: inventory not found: " + <<"craft_inv=\""<getList("craft"); + InventoryList *list_craftresult = inv_craft->getList("craftresult"); + + /* + If a list doesn't exist or the source item doesn't exist + */ + if(!list_craft){ + infostream<<"ICraftAction::apply(): FAIL: craft list not found: " + <<"craft_inv=\""<getSize() < 1){ + infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: " + <<"craft_inv=\""<itemFits(0, crafted)) + { + // Decrement input and add crafting output + getCraftingResult(inv_craft, crafted, true, gamedef); + list_craftresult->addItem(0, crafted); + mgr->setInventoryModified(craft_inv); + + actionstream<getDescription() + <<" crafts " + < 1) + count_remaining--; + + // Get next crafting result + found = getCraftingResult(inv_craft, crafted, false, gamedef); + } + + infostream<<"ICraftAction::apply(): crafted " + <<" craft_inv=\""<getList("craft"); + if(!clist || clist->getSize() != 9) + return false; + + // Mangle crafting grid to an another format + CraftInput ci; + ci.method = CRAFT_METHOD_NORMAL; + ci.width = 3; + for(u16 i=0; i<9; i++) + ci.items.push_back(clist->getItem(i)); + + // Find out what is crafted and add it to result item slot + CraftOutput co; + bool found = gamedef->getCraftDefManager()->getCraftResult( + ci, co, decrementInput, gamedef); + if(found) + result.deSerialize(co.item, gamedef->getItemDefManager()); + + if(found && decrementInput) + { + // CraftInput has been changed, apply changes in clist + for(u16 i=0; i<9; i++) + { + clist->changeItem(i, ci.items[i]); + } + } + + return found; +} + diff --git a/src/inventorymanager.h b/src/inventorymanager.h index f6ae4feb..890d0516 100644 --- a/src/inventorymanager.h +++ b/src/inventorymanager.h @@ -92,6 +92,7 @@ public: #define IACTION_MOVE 0 #define IACTION_DROP 1 +#define IACTION_CRAFT 2 struct InventoryAction { @@ -99,7 +100,8 @@ struct InventoryAction virtual u16 getType() const = 0; virtual void serialize(std::ostream &os) const = 0; - virtual void apply(InventoryManager *mgr, ServerActiveObject *player) = 0; + virtual void apply(InventoryManager *mgr, ServerActiveObject *player, + IGameDef *gamedef) = 0; }; struct IMoveAction : public InventoryAction @@ -139,7 +141,7 @@ struct IMoveAction : public InventoryAction os<idef()), inventory_backup(NULL), - craftresult_is_preview(true), hp(20), peer_id(PEER_ID_INEXISTENT), // protected @@ -64,6 +63,7 @@ void Player::resetInventory() inventory.clear(); inventory.addList("main", PLAYER_INVENTORY_SIZE); inventory.addList("craft", 9); + inventory.addList("craftpreview", 1); inventory.addList("craftresult", 1); } @@ -119,7 +119,6 @@ void Player::serialize(std::ostream &os) args.setFloat("pitch", m_pitch); args.setFloat("yaw", m_yaw); args.setV3F("position", m_position); - args.setBool("craftresult_is_preview", craftresult_is_preview); args.setS32("hp", hp); args.writeLines(os); @@ -157,11 +156,10 @@ void Player::deSerialize(std::istream &is) setPitch(args.getFloat("pitch")); setYaw(args.getFloat("yaw")); setPosition(args.getV3F("position")); + bool craftresult_is_preview = true; try{ craftresult_is_preview = args.getBool("craftresult_is_preview"); - }catch(SettingNotFoundException &e){ - craftresult_is_preview = true; - } + }catch(SettingNotFoundException &e){} try{ hp = args.getS32("hp"); }catch(SettingNotFoundException &e){ @@ -169,6 +167,18 @@ void Player::deSerialize(std::istream &is) } inventory.deSerialize(is); + + if(inventory.getList("craftpreview") == NULL) + { + // Convert players without craftpreview + inventory.addList("craftpreview", 1); + + if(craftresult_is_preview) + { + // Clear craftresult + inventory.getList("craftresult")->changeItem(0, ItemStack()); + } + } } #ifndef SERVER diff --git a/src/player.h b/src/player.h index 43f6ee5e..085a4a15 100644 --- a/src/player.h +++ b/src/player.h @@ -155,8 +155,6 @@ public: // Actual inventory is backed up here when creative mode is used Inventory *inventory_backup; - bool craftresult_is_preview; - u16 hp; u16 peer_id; diff --git a/src/server.cpp b/src/server.cpp index 9f3db34d..17755188 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2377,13 +2377,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) */ if(a->getType() == IACTION_MOVE) { - InventoryList *rlist = player->inventory.getList("craftresult"); - assert(rlist); - InventoryList *clist = player->inventory.getList("craft"); - assert(clist); - InventoryList *mlist = player->inventory.getList("main"); - assert(mlist); - IMoveAction *ma = (IMoveAction*)a; ma->from_inv.applyCurrentPlayer(player->getName()); @@ -2398,98 +2391,36 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) (ma->to_inv.name == player->getName()); /* - Disable moving items into craftresult from elsewhere + Disable moving items out of craftpreview */ - if(to_inv_is_current_player - && ma->to_list == "craftresult" - && (!from_inv_is_current_player - || ma->from_list != "craftresult")) + if(ma->from_list == "craftpreview") { infostream<<"Ignoring IMoveAction from " <<(ma->from_inv.dump())<<":"<from_list <<" to "<<(ma->to_inv.dump())<<":"<to_list - <<" because dst is craftresult" - <<" and src isn't craftresult"<from_list<from_list == "craftresult" - && player->craftresult_is_preview - && g_settings->getBool("creative_mode") == false) + if(ma->to_list == "craftpreview" || ma->to_list == "craftresult") { - ItemStack crafting_result; - bool crafting_possible = GetCraftingResult(peer_id, - crafting_result, false); - - /* - If the craftresult is placed on itself, - crafting takes place and result is moved - into main list. - */ - if(crafting_possible - && to_inv_is_current_player - && ma->to_list == "craftresult") - { - if(mlist->roomForItem(crafting_result)) - { - actionstream<getName() - <<" crafts " - <addItem(crafting_result); - rlist->clearItems(); - player->craftresult_is_preview = true; - srp->m_inventory_not_sent = true; - } - - } - /* - Otherwise, if the destination is part of - the same player's inventory, crafting - takes place normally. - */ - else if(crafting_possible - && to_inv_is_current_player) - { - InventoryList *list = player->inventory.getList(ma->to_list); - if(list && list->itemFits(ma->to_i, crafting_result)) - { - actionstream<getName() - <<" crafts " - <addItem(ma->to_i, crafting_result); - rlist->clearItems(); - player->craftresult_is_preview = true; - srp->m_inventory_not_sent = true; - } - } - - // Do not apply the action normally. + infostream<<"Ignoring IMoveAction from " + <<(ma->from_inv.dump())<<":"<from_list + <<" to "<<(ma->to_inv.dump())<<":"<to_list + <<" because dst is "<to_list<getType() == IACTION_CRAFT) + { + ICraftAction *ca = (ICraftAction*)a; + + ca->craft_inv.applyCurrentPlayer(player->getName()); + + //bool craft_inv_is_current_player = + // (ca->craft_inv.type == InventoryLocation::PLAYER) && + // (ca->craft_inv.name == player->getName()); + + // Disallow crafting if not allowed to interact + if((getPlayerPrivs(player) & PRIV_INTERACT) == 0) + { + infostream<<"Cannot craft: " + <<"No interact privilege"<craft_inv); + if(owner_craft != "" && owner_craft != player->getName()) + { + infostream<<"WARNING: "<getName() + <<" tried to access an inventory that" + <<" belongs to "<apply(this, srp); + a->apply(this, srp, this); // Eat the action delete a; } @@ -4049,95 +4016,24 @@ void Server::RespawnPlayer(Player *player) SendPlayerHP(player); } -bool Server::GetCraftingResult(u16 peer_id, ItemStack &result, bool decrementInput) -{ - DSTACK(__FUNCTION_NAME); - - Player* player = m_env->getPlayer(peer_id); - assert(player); - - // Get the crafting InventoryList of the player in which we will operate - InventoryList *clist = player->inventory.getList("craft"); - assert(clist); - - // Mangle crafting grid to an another format - CraftInput ci; - ci.method = CRAFT_METHOD_NORMAL; - ci.width = 3; - for(u16 i=0; i<9; i++) - { - ci.items.push_back(clist->getItem(i)); - } - - // Find out what is crafted and add it to result item slot - CraftOutput co; - bool found = m_craftdef->getCraftResult(ci, co, decrementInput, this); - if(found) - result.deSerialize(co.item, m_itemdef); - - if(decrementInput) - { - // CraftInput has been changed, apply changes in clist - for(u16 i=0; i<9; i++) - { - clist->changeItem(i, ci.items[i]); - } - } - - return found; -} - void Server::UpdateCrafting(u16 peer_id) { DSTACK(__FUNCTION_NAME); Player* player = m_env->getPlayer(peer_id); assert(player); - ServerRemotePlayer *srp = static_cast(player); + // Get a preview for crafting + ItemStack preview; // No crafting in creative mode - if(g_settings->getBool("creative_mode")) - return; - - // Get the InventoryLists of the player in which we will operate - InventoryList *clist = player->inventory.getList("craft"); - assert(clist); - InventoryList *rlist = player->inventory.getList("craftresult"); - assert(rlist); - InventoryList *mlist = player->inventory.getList("main"); - assert(mlist); - - // If the result list is not a preview and is not empty, try to - // throw the item into main list - if(!player->craftresult_is_preview && rlist->getUsedSlots() != 0) - { - // Grab item out of craftresult - ItemStack item = rlist->changeItem(0, ItemStack()); - // Try to put in main - ItemStack leftover = mlist->addItem(item); - // If there are leftovers, put them back to craftresult - rlist->addItem(leftover); - // Inventory was modified - srp->m_inventory_not_sent = true; - } - - // If result list is empty, we will make it preview what would be - // crafted - if(rlist->getUsedSlots() == 0) - player->craftresult_is_preview = true; - - // If it is a preview, find out what is the crafting result - // and put it in - if(player->craftresult_is_preview) - { - // Clear the possible old preview in it - rlist->clearItems(); - - // Put the new preview in - ItemStack crafting_result; - if(GetCraftingResult(peer_id, crafting_result, false)) - rlist->addItem(crafting_result); - } + if(g_settings->getBool("creative_mode") == false) + getCraftingResult(&player->inventory, preview, false, this); + + // Put the new preview in + InventoryList *plist = player->inventory.getList("craftpreview"); + assert(plist); + assert(plist->getSize() >= 1); + plist->changeItem(0, preview); } RemoteClient* Server::getClient(u16 peer_id) diff --git a/src/server.h b/src/server.h index d979cb51..4fdb6006 100644 --- a/src/server.h +++ b/src/server.h @@ -595,7 +595,6 @@ private: void HandlePlayerHP(Player *player, s16 damage); void RespawnPlayer(Player *player); - bool GetCraftingResult(u16 peer_id, ItemStack &result, bool decrementInput); void UpdateCrafting(u16 peer_id); // When called, connection mutex should be locked