cLuaWindow


Index:
Articles
Classes
Hooks

Quick navigation:
BannerPattern
BossBarColor
BossBarDivisionType
cArrowEntity
cBeaconEntity
cBedEntity
cBlockArea
cBlockEntity
cBlockEntityWithItems
cBlockInfo
cBoat
cBoundingBox
cBrewingstandEntity
cChatColor
cChestEntity
cChunkDesc
cClientHandle
cColor
cCommandBlockEntity
cCompositeChat
cCraftingGrid
cCraftingRecipe
cCryptoHash
cCuboid
cDispenserEntity
cDropperEntity
cDropSpenserEntity
cEnchantments
cEnderCrystal
cEntity
cEntityEffect
cExpBottleEntity
cExpOrb
cFallingBlock
cFile
cFireChargeEntity
cFireworkEntity
cFloater
cFlowerPotEntity
cFurnaceEntity
cGhastFireballEntity
cHangingEntity
cHopperEntity
cIniFile
cInventory
cItem
cItemFrame
cItemGrid
cItems
cJson
cJukeboxEntity
cLeashKnot
cLineBlockTracer
cLuaWindow
cMap
cMapManager
cMobHeadEntity
cMobSpawnerEntity
cMojangAPI
cMonster
cNetwork
cNoteEntity
cObjective
cPainting
cPawn
cPickup
cPlayer
cPlugin
cPluginLua
cPluginManager
cProjectileEntity
cRankManager
cRoot
cScoreboard
cServer
cServerHandle
cSignEntity
cSplashPotionEntity
cStringCompression
cTCPLink
cTeam
cThrownEggEntity
cThrownEnderPearlEntity
cThrownSnowballEntity
cTNTEntity
cUDPEndpoint
cUrlClient
cUrlParser
CustomStatistic
cUUID
cWebAdmin
cWindow
cWitherSkullEntity
cWorld
EffectID
HTTPFormData
HTTPRequest
HTTPTemplateRequest
ItemCategory
lxp
SmokeDirection
sqlite3
StatisticsManager
TakeDamageInfo
tolua
Vector3d
Vector3f
Vector3i
Globals

Contents


cLuaWindow class

This class is used by plugins wishing to display a custom window to the player, unrelated to block entities or entities near the player. The window can be of any type and have any contents that the plugin defines. Callbacks for when the player modifies the window contents and when the player closes the window can be set.

This class inherits from the cWindow class, so all cWindow's functions and constants can be used, in addition to the cLuaWindow-specific functions listed below.

The contents of this window are represented by a cWindow:GetSlot() etc. or cPlayer:GetInventory() to access the player inventory.

When creating a new cLuaWindow object, you need to specify both the window type and the contents' width and height. Note that Cuberite accepts any combination of these, but opening a window for a player may crash their client if the contents' dimensions don't match the client's expectations.

To open the window for a player, call cPlayer:OpenWindow(). Multiple players can open window of the same cLuaWindow object. All players see the same items in the window's contents (like chest, unlike crafting table).


Inheritance

This class inherits from the following parent classes:


Functions

NameParametersReturn valueNotes
() (constructor)
WindowTypeWindowType
ContentsWidthnumber
ContentsHeightnumber
Titlestring
Creates a new object of this class. The window is not shown to any player yet.
GetContents
cItemGrid
Returns the cItemGrid object representing the internal storage in this window
SetOnClicked
OnClickedCallbackfunction
Sets the function that the window will call when it is about to process a click from a player. See below for the signature of the callback function.
SetOnClosing
OnClosingCallbackfunction
Sets the function that the window will call when it is about to be closed by a player
SetOnSlotChanged
OnSlotChangedCallbackfunction
Sets the function that the window will call when a slot is changed by a player

Functions inherited from cWindow

NameParametersReturn valueNotes
GetSlot
PlayercPlayer
SlotNumbernumber
cItem
Returns the item at the specified slot for the specified player. Returns nil and logs to server console on error.
GetWindowID
number
Returns the ID of the window, as used by the network protocol
GetWindowTitle
string
Returns the window title that will be displayed to the player
GetWindowType
number
Returns the type of the window, one of the constants in the table above
GetWindowTypeName
string
Returns the textual representation of the window's type, such as "minecraft:chest".
IsSlotInPlayerHotbar
SlotNumnumber
boolean
Returns true if the specified slot number is in the player hotbar
IsSlotInPlayerInventory
SlotNumnumber
boolean
Returns true if the specified slot number is in the player's main inventory or in the hotbar. Note that this returns false for armor slots!
IsSlotInPlayerMainInventory
SlotNumnumber
boolean
Returns true if the specified slot number is in the player's main inventory
SetProperty
PropertyIDnumber
PropertyValuenumber
PlayercPlayer
Updates a numerical property associated with the window. Typically used for furnace progressbars. Sends the UpdateWindowProperty packet to the specified Player, or to all current clients of the window if Player is not specified.
SetSlot
PlayercPlayer
SlotNumnumber
cItemcItem
Sets the contents of the specified slot for the specified player. Ignored if the slot number is invalid
SetWindowTitle
WindowTitlestring
Sets the window title that will be displayed to the player

Callbacks

The object calls the following functions at the appropriate time:

OnClicked Callback

This callback, settable via the SetOnClicked() function, will be called when the player clicks a slot in the window. The callback can cancel the click.

function OnWindowClicked(a_Window, a_Player, a_SlotNum, a_ClickAction, a_ClickedItem)

The a_Window parameter is the cLuaWindow object representing the window, a_Player is the player who made the click, a_SlotNum is the slot the player clicked, a_ClickAction is the type of click the player made, and a_ClickedItem is the item the player clicked on, if applicable. If the function returns true, the click is cancelled (internally, the server resends the window slots to the player to keep the player in sync).

OnClosing Callback

This callback, settable via the SetOnClosing() function, will be called when the player tries to close the window, or the window is closed for any other reason (such as a player disconnecting).

function OnWindowClosing(a_Window, a_Player, a_CanRefuse)

The a_Window parameter is the cLuaWindow object representing the window, a_Player is the player for whom the window is about to close. a_CanRefuse specifies whether the callback can refuse the closing. If the callback returns true and a_CanRefuse is true, the window is not closed (internally, the server sends a new OpenWindow packet to the client).

OnSlotChanged Callback

This callback, settable via the SetOnSlotChanged() function, will be called whenever the contents of any slot in the window's contents (i. e. NOT in the player inventory!) changes.

function OnWindowSlotChanged(a_Window, a_SlotNum)

The a_Window parameter is the cLuaWindow object representing the window, a_SlotNum is the slot number. There is no reference to a cPlayer, because the slot change needn't originate from the player action. To get or set the slot, you'll need to retrieve a cPlayer object, for example by calling cWorld:DoWithPlayer().

Any returned values are ignored.

Example

This example is taken from the Debuggers plugin, used to test the API functionality. It opens a window and refuse to close it 3 times. It also logs slot changes to the server console and prevents shift-clicking in the window.
-- Callback that refuses to close the window twice, then allows:
local Attempt = 1;
local OnClosing = function(Window, Player, CanRefuse)
	Player:SendMessage("Window closing attempt #" .. Attempt .. "; CanRefuse = " .. tostring(CanRefuse));
	Attempt = Attempt + 1;
	return CanRefuse and (Attempt <= 3);  -- refuse twice, then allow, unless CanRefuse is set to true
end

-- Log the slot changes:
local OnSlotChanged = function(Window, SlotNum)
	LOG("Window \"" .. Window:GetWindowTitle() .. "\" slot " .. SlotNum .. " changed.");
end

-- Prevent shift-clicking:
local OnClicked = function(Window, ClickingPlayer, SlotNum, ClickAction, ClickedItem)
	if ClickAction == caShiftLeftClick then
		return true
	end
end

-- Set window contents:
-- a_Player is a cPlayer object received from the outside of this code fragment
local Window = cLuaWindow(cWindow.wtHopper, 3, 3, "TestWnd");
Window:SetSlot(a_Player, 0, cItem(E_ITEM_DIAMOND, 64));
Window:SetOnClicked(OnClicked);
Window:SetOnClosing(OnClosing);
Window:SetOnSlotChanged(OnSlotChanged);

-- Open the window:
a_Player:OpenWindow(Window);
Generated by APIDump on 2024-10-25 05:01:00, Build ID Unknown, Commit approx: 0325de7dacaf1e2feaea5eaab4791bc23d78f9e7