2017-09-02 03:45:06 -04:00
|
|
|
|
2012-09-23 13:19:34 -04:00
|
|
|
// Window.h
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
// Interfaces to the cWindow class representing a UI window for a specific block
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
#include "../FunctionRef.h"
|
2013-05-30 04:40:13 -04:00
|
|
|
#include "../ItemGrid.h"
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cPlayer;
|
|
|
|
class cWindowOwner;
|
|
|
|
class cClientHandle;
|
2015-09-24 04:48:33 -04:00
|
|
|
class cBrewingstandEntity;
|
2012-09-20 09:25:54 -04:00
|
|
|
class cChestEntity;
|
2013-12-06 19:18:58 -05:00
|
|
|
class cEnderChestEntity;
|
2012-09-20 09:25:54 -04:00
|
|
|
class cFurnaceEntity;
|
2013-06-13 03:36:43 -04:00
|
|
|
class cHopperEntity;
|
2014-09-12 18:18:02 -04:00
|
|
|
class cMinecartWithChest;
|
2014-07-30 15:59:35 -04:00
|
|
|
class cBeaconEntity;
|
2012-09-20 09:25:54 -04:00
|
|
|
class cSlotArea;
|
2014-05-03 17:42:26 -04:00
|
|
|
class cSlotAreaAnvil;
|
2012-09-20 09:25:54 -04:00
|
|
|
class cWorld;
|
|
|
|
|
|
|
|
typedef std::list<cPlayer *> cPlayerList;
|
|
|
|
typedef std::vector<cSlotArea *> cSlotAreas;
|
2017-09-11 17:20:49 -04:00
|
|
|
using cPlayerListCallback = cFunctionRef<bool(cPlayer &)>;
|
|
|
|
using cClientHandleCallback = cFunctionRef<bool(cClientHandle &)>;
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
// tolua_begin
|
|
|
|
|
2014-07-17 16:50:58 -04:00
|
|
|
/**
|
2012-09-20 09:25:54 -04:00
|
|
|
Represents a UI window.
|
|
|
|
|
|
|
|
Each window has a list of players that are currently using it
|
|
|
|
When there's no player using a window, it is destroyed.
|
2014-07-17 16:50:58 -04:00
|
|
|
A window consists of several areas of slots with similar functionality - for example the crafting grid area, or
|
2012-09-20 09:25:54 -04:00
|
|
|
the inventory area. Each area knows what its slots are (GetSlot() function) and can handle mouse clicks.
|
|
|
|
The window acts only as a top-level container for those areas, redirecting the click events to the correct areas.
|
2013-05-30 04:40:13 -04:00
|
|
|
Inventory painting, introduced in 1.5, is handled by the window, too
|
2012-09-20 09:25:54 -04:00
|
|
|
*/
|
|
|
|
class cWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum WindowType
|
|
|
|
{
|
2013-10-28 08:30:24 -04:00
|
|
|
wtInventory = -1, // This value is never actually sent to a client
|
|
|
|
wtChest = 0,
|
|
|
|
wtWorkbench = 1,
|
|
|
|
wtFurnace = 2,
|
|
|
|
wtDropSpenser = 3, // Dropper or Dispenser
|
|
|
|
wtEnchantment = 4,
|
|
|
|
wtBrewery = 5,
|
|
|
|
wtNPCTrade = 6,
|
|
|
|
wtBeacon = 7,
|
|
|
|
wtAnvil = 8,
|
|
|
|
wtHopper = 9,
|
2014-09-11 17:17:27 -04:00
|
|
|
wtDropper = 10,
|
2013-11-08 15:32:14 -05:00
|
|
|
wtAnimalChest = 11,
|
2012-09-20 09:25:54 -04:00
|
|
|
};
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
// tolua_end
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
static const int c_NumInventorySlots = 36;
|
|
|
|
|
|
|
|
cWindow(WindowType a_WindowType, const AString & a_WindowTitle);
|
|
|
|
virtual ~cWindow();
|
|
|
|
|
2013-05-30 15:34:09 -04:00
|
|
|
char GetWindowID(void) const { return m_WindowID; } // tolua_export
|
2013-05-30 04:40:13 -04:00
|
|
|
int GetWindowType(void) const { return m_WindowType; } // tolua_export
|
2016-07-06 06:39:56 -04:00
|
|
|
|
|
|
|
/** Returns the textual representation of the window's type, such as "minecraft:chest". */
|
2014-09-11 17:17:27 -04:00
|
|
|
const AString GetWindowTypeName(void) const; // tolua_export
|
2012-09-20 09:25:54 -04:00
|
|
|
|
2013-04-04 07:47:31 -04:00
|
|
|
cWindowOwner * GetOwner(void) { return m_Owner; }
|
2014-07-21 09:19:48 -04:00
|
|
|
void SetOwner( cWindowOwner * a_Owner) { m_Owner = a_Owner; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns the total number of slots */
|
2012-09-20 09:25:54 -04:00
|
|
|
int GetNumSlots(void) const;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns the number of slots, excluding the player's inventory (used for network protocols) */
|
2013-11-08 15:32:14 -05:00
|
|
|
int GetNumNonInventorySlots(void) const { return GetNumSlots() - c_NumInventorySlots; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
// tolua_begin
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns the item at the specified slot for the specified player. Returns nullptr if invalid SlotNum requested */
|
2013-05-30 04:40:13 -04:00
|
|
|
const cItem * GetSlot(cPlayer & a_Player, int a_SlotNum) const;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Sets the item to the specified slot for the specified player */
|
2013-05-30 04:40:13 -04:00
|
|
|
void SetSlot(cPlayer & a_Player, int a_SlotNum, const cItem & a_Item);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns true if the specified slot is in the Player Main Inventory slotarea */
|
2013-05-30 15:34:09 -04:00
|
|
|
bool IsSlotInPlayerMainInventory(int a_SlotNum) const;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns true if the specified slot is in the Player Hotbar slotarea */
|
2013-05-30 15:34:09 -04:00
|
|
|
bool IsSlotInPlayerHotbar(int a_SlotNum) const;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns true if the specified slot is in the Player Main Inventory or Hotbar slotareas. Note that returns false for Armor. */
|
2013-05-30 15:34:09 -04:00
|
|
|
bool IsSlotInPlayerInventory(int a_SlotNum) const;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
// tolua_end
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Fills a_Slots with the slots read from m_SlotAreas[], for the specified player */
|
2012-09-20 09:25:54 -04:00
|
|
|
void GetSlots(cPlayer & a_Player, cItems & a_Slots) const;
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Handles a click event from a player */
|
2017-08-17 10:27:43 -04:00
|
|
|
virtual void Clicked(
|
2014-07-17 16:50:58 -04:00
|
|
|
cPlayer & a_Player, int a_WindowID,
|
2013-05-08 05:45:07 -04:00
|
|
|
short a_SlotNum, eClickAction a_ClickAction,
|
2012-09-20 09:25:54 -04:00
|
|
|
const cItem & a_ClickedItem
|
|
|
|
);
|
|
|
|
|
2014-07-06 18:50:22 -04:00
|
|
|
virtual void OpenedByPlayer(cPlayer & a_Player);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Called when a player closes this window; notifies all slot areas. Returns true if close accepted */
|
2013-06-02 17:59:25 -04:00
|
|
|
virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse);
|
2012-09-20 09:25:54 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Sends the specified slot's contents to all clients of this window; the slot is specified as local in an area */
|
2013-06-13 03:36:43 -04:00
|
|
|
void BroadcastSlot(cSlotArea * a_Area, int a_LocalSlotNum);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Sends the contents of the whole window to the specified client */
|
2012-09-20 09:25:54 -04:00
|
|
|
void SendWholeWindow(cClientHandle & a_Client);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Sends the contents of the whole window to all clients of this window. */
|
2012-09-20 09:25:54 -04:00
|
|
|
void BroadcastWholeWindow(void);
|
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
// tolua_begin
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
const AString & GetWindowTitle() const { return m_WindowTitle; }
|
2014-07-21 09:19:48 -04:00
|
|
|
void SetWindowTitle(const AString & a_WindowTitle) { m_WindowTitle = a_WindowTitle; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-07-06 06:39:56 -04:00
|
|
|
/** Updates a numerical property associated with the window. Typically used for furnace progressbars.
|
|
|
|
Sends the UpdateWindowProperty packet to all clients of the window */
|
2014-10-03 16:32:41 -04:00
|
|
|
virtual void SetProperty(short a_Property, short a_Value);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-07-06 06:39:56 -04:00
|
|
|
/** Updates a numerical property associated with the window. Typically used for furnace progressbars.
|
|
|
|
Sends the UpdateWindowProperty packet only to the specified player */
|
2014-10-03 16:32:41 -04:00
|
|
|
virtual void SetProperty(short a_Property, short a_Value, cPlayer & a_Player);
|
2014-04-12 08:58:46 -04:00
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
// tolua_end
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
void OwnerDestroyed(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Calls the callback safely for each player that has this window open; returns true if all players have been enumerated */
|
2017-09-11 17:20:49 -04:00
|
|
|
bool ForEachPlayer(cPlayerListCallback a_Callback);
|
2012-09-20 09:25:54 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Calls the callback safely for each client that has this window open; returns true if all clients have been enumerated */
|
2017-09-11 17:20:49 -04:00
|
|
|
bool ForEachClient(cClientHandleCallback a_Callback);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-09-20 16:10:46 -04:00
|
|
|
/** Called on shift-clicking to distribute the stack into other areas; Modifies a_ItemStack as it is distributed!
|
|
|
|
if a_ShouldApply is true, the changes are written into the slots;
|
2014-12-17 13:14:01 -05:00
|
|
|
if a_ShouldApply is false, only a_ItemStack is modified to reflect the number of fits (for fit-testing purposes) */
|
|
|
|
virtual void DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) = 0;
|
2014-12-13 12:49:11 -05:00
|
|
|
|
2014-12-17 13:14:01 -05:00
|
|
|
/** Called from DistributeStack() to distribute the stack into a_AreasInOrder; Modifies a_ItemStack as it is distributed!
|
2015-02-06 15:54:47 -05:00
|
|
|
If a_ShouldApply is true, the changes are written into the slots;
|
|
|
|
if a_ShouldApply is false, only a_ItemStack is modified to reflect the number of fits (for fit-testing purposes)
|
2014-12-17 13:14:01 -05:00
|
|
|
If a_BackFill is true, the areas will be filled from the back (right side). (Example: Empty Hotbar -> Item get in slot 8, not slot 0) */
|
|
|
|
void DistributeStackToAreas(cItem & a_ItemStack, cPlayer & a_Player, cSlotAreas & a_AreasInOrder, bool a_ShouldApply, bool a_BackFill);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2018-01-15 06:35:27 -05:00
|
|
|
/** Called on DblClicking to collect all stackable items from all areas into hand.
|
2014-12-13 12:49:11 -05:00
|
|
|
The items are accumulated in a_Dragging and removed from the SlotAreas immediately.
|
|
|
|
If a_CollectFullStacks is false, slots with full stacks in the area are skipped while collecting.
|
|
|
|
Returns true if full stack has been collected, false if there's space remaining to fill. */
|
2013-11-10 12:42:46 -05:00
|
|
|
bool CollectItemsToHand(cItem & a_Dragging, cSlotArea & a_Area, cPlayer & a_Player, bool a_CollectFullStacks);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Used by cSlotAreas to send individual slots to clients, a_RelativeSlotNum is the slot number relative to a_SlotArea */
|
2012-09-21 16:50:34 -04:00
|
|
|
void SendSlot(cPlayer & a_Player, cSlotArea * a_SlotArea, int a_RelativeSlotNum);
|
2014-04-12 08:58:46 -04:00
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
protected:
|
|
|
|
cSlotAreas m_SlotAreas;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
char m_WindowID;
|
|
|
|
int m_WindowType;
|
|
|
|
AString m_WindowTitle;
|
|
|
|
|
|
|
|
cCriticalSection m_CS;
|
|
|
|
cPlayerList m_OpenedBy;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-01-08 00:01:06 -05:00
|
|
|
bool m_IsDestroyed;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
cWindowOwner * m_Owner;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-04-21 17:29:50 -04:00
|
|
|
static Byte m_WindowIDCounter;
|
2012-09-20 09:25:54 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Sets the internal flag as "destroyed"; notifies the owner that the window is destroying */
|
2013-05-30 15:34:09 -04:00
|
|
|
virtual void Destroy(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
/** Returns the correct slot area for the specified window-global SlotNum
|
|
|
|
Also returns the area-local SlotNum corresponding to the GlobalSlotNum
|
2015-07-31 10:49:10 -04:00
|
|
|
If the global SlotNum is out of range, returns nullptr */
|
2013-05-30 04:40:13 -04:00
|
|
|
cSlotArea * GetSlotArea(int a_GlobalSlotNum, int & a_LocalSlotNum);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
/** Returns the correct slot area for the specified window-global SlotNum
|
|
|
|
Also returns the area-local SlotNum corresponding to the GlobalSlotNum
|
2014-10-20 16:55:07 -04:00
|
|
|
If the global SlotNum is out of range, returns nullptr.
|
2015-07-31 10:49:10 -04:00
|
|
|
Const version. */
|
2013-05-30 04:40:13 -04:00
|
|
|
const cSlotArea * GetSlotArea(int a_GlobalSlotNum, int & a_LocalSlotNum) const;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Prepares the internal structures for inventory painting from the specified player */
|
2013-05-30 04:40:13 -04:00
|
|
|
void OnPaintBegin(cPlayer & a_Player);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Adds the slot to the internal structures for inventory painting by the specified player */
|
2013-05-30 04:40:13 -04:00
|
|
|
void OnPaintProgress(cPlayer & a_Player, int a_SlotNum);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Processes the entire action stored in the internal structures for inventory painting; distributes as many items as possible */
|
2013-05-30 04:40:13 -04:00
|
|
|
void OnLeftPaintEnd(cPlayer & a_Player);
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Processes the entire action stored in the internal structures for inventory painting; distributes one item into each slot */
|
2013-05-30 04:40:13 -04:00
|
|
|
void OnRightPaintEnd(cPlayer & a_Player);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-07-13 09:43:48 -04:00
|
|
|
/** Processes the entire action stored in the internal structures for inventory painting; distributes a full stack into each slot */
|
|
|
|
void OnMiddlePaintEnd(cPlayer & a_Player);
|
|
|
|
|
|
|
|
/** Distributes a_NumToEachSlot items into the slots specified in a_SlotNums; returns the total number of items distributed.
|
|
|
|
@param a_LimitItems if false, no checks are performed on a_Item.m_ItemCount. */
|
|
|
|
int DistributeItemToSlots(cPlayer & a_Player, const cItem & a_Item, int a_NumToEachSlot, const cSlotNums & a_SlotNums, bool a_LimitItems = true);
|
2013-05-30 15:34:09 -04:00
|
|
|
} ; // tolua_export
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|