summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/Handy/handy_functions.lua
blob: c142ffd08f088e7547570aad1f28da7b3839cbf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
--[[
General stuff
]]
-- Returns Handy plugin version number
function GetHandyVersion()
	return HANDY_VERSION
end
-- Checks if handy is in proper version
function CheckForRequiedVersion( inVersion )
	if( inVersion > HANDY_VERSION ) then		return false	end
	return true
end
--[[
MCS-specific _functions and nasty hacks :D
]]
function GetChestHeightCheat( inChest )
	local chestGrid = inChest:GetContents()
	LOGINFO( "This function serves no purpose now! You should consider chest:GetContents():GetHeight() now!" )
	LOGINFO( "Also, you might find Handy's new 'IsChestDouble()' useful for your case" )
	return chestGrid:GetHeight()
end
function IsChestDouble( inChest )
	local chestHeight = inChest:GetContents():GetHeight()
	if( chestHeight == 3 ) then
		return false
	end
	return true
end
-- Those two checks how many items of given inItemID chest and player have, and how much they could fit inside them
function ReadChestForItem( inChest, inItemID )
	return ReadGridForItems( inChest:GetContents(), inItemID )
end
function ReadPlayerForItem( inPlayer, inItemID )
	local inventoryFound, inventoryFree = ReadGridForItems( inPlayer:GetInventory():GetInventoryGrid(), inItemID )
	local hotbarFound, hotbarFree = ReadGridForItems( inPlayer:GetInventory():GetHotbarGrid(), inItemID )
	local itemsFound = inventoryFound + hotbarFound
	local freeSpace = inventoryFree + hotbarFree
	return itemsFound, freeSpace
end
-- Following functions are for chest-related operations
-- BEWARE! Those assume you did checked if chest has items/space in it!
function ReadGridForItems( inGrid, inItemID )
	local itemsFound = 0
	local freeSpace = 0
	local slotsCount = inGrid:GetNumSlots()
	local testerItem = cItem( inItemID )
	local maxStackSize = testerItem:GetMaxStackSize()
	for index = 0, (slotsCount - 1) do
		slotItem = inGrid:GetSlot( index )
		if( slotItem:IsEmpty() ) then
			freeSpace = freeSpace + maxStackSize
		else
			if( slotItem:IsStackableWith( testerItem ) ) then
				itemsFound = itemsFound + slotItem.m_ItemCount
				freeSpace = maxStackSize - slotItem.m_ItemCount
			end
		end
	end
	return itemsFound, freeSpace
end

function TakeItemsFromGrid( inGrid, inItem )
	local slotsCount = inGrid:GetNumSlots()
	local removedItem = cItem( inItem )
	for index = 0, (slotsCount - 1) do
		slotItem = inGrid:GetSlot( index )
		if( slotItem:IsSameType( removedItem ) ) then
			if( slotItem.m_ItemCount <= removedItem.m_ItemCount ) then
				removedItem.m_ItemCount = removedItem.m_ItemCount - slotItem.m_ItemCount
				inGrid:EmptySlot( index )
			else
				removedItem.m_ItemCount = slotItem.m_ItemCount - removedItem.m_ItemCount
				inGrid:SetSlot( index, removedItem )
				removedItem.m_ItemCount = 0
			end
			if( removedItem.m_ItemCount <= 0 ) then		break	end
		end
	end
	return removedItem.m_ItemCount
end
--------------
function TakeItemsFromChest( inChest, inItemID, inAmount )	-- MIGHT BE UNSAFE! CHECK FOR ITEMS FIRST!!
	local chestGrid = inChest:GetContents()
	local removedItem = cItem( inItemID, inAmount )
	TakeItemsFromGrid( chestGrid, removedItem )
end
function PutItemsToChest( inChest, inItemID, inAmount )
	local chestGrid = inChest:GetContents()
	local addedItem = cItem( inItemID, inAmount )
	chestGrid:AddItem( addedItem )
end
-- Similar to chest-related.
function TakeItemsFromPlayer( inPlayer, inItemID, inAmount )	-- MIGHT BE UNSAFE! CHECK FIRST!
	local removedItem = cItem( inItemID, inAmount )
	local inventoryGrid = inPlayer:GetInventory():GetInventoryGrid()
	local hotbarGrid = inPlayer:GetInventory():GetHotbarGrid()
	local itemsLeft = TakeItemsFromGrid( inventoryGrid, removedItem )
	if( itemsLeft > 0 ) then
		removedItem = cItem( inItemID, itemsLeft )
		TakeItemsFromGrid( hotbarGrid, removedItem )
	end
end
function GiveItemsToPlayer( inPlayer, inItemID, inAmount )
	local addedItem = cItem( inItemID, inAmount )
	local inventoryGrid = inPlayer:GetInventory():GetInventoryGrid()
	local hotbarGrid = inPlayer:GetInventory():GetHotbarGrid()
	local itemsAdded = inventoryGrid:AddItem( addedItem )
	if( itemsAdded < inAmount ) then
		addedItem.m_ItemCount = addedItem.m_ItemCount - itemsAdded
		hotbarGrid:AddItem( addedItem )
	end
end
-- This function returns item max stack for a given itemID. It uses vanilla max stack size, and uses several non-common items notations;
-- Those are:
-- oneonerecord( because aparently 11record wasn't the best idea in lua scripting application )
-- carrotonastick( because it wasn't added to items.txt yet )
-- waitrecord( for same reason )
-- Feel free to ignore the difference, or to add those to items.txt
function GetItemMaxStack( inItemID )
	local testerItem = cItem( inItemID )
	LOGINFO( "This function serves no real purpose now, maybe consider using cItem:GetMaxStackSize()?" )
	return testerItem:GetMaxStackSize()
end
function ItemIsArmor( inItemID, inCheckForHorseArmor )
	inCheckForHorseArmor = inCheckForHorseArmor or false
	if( inItemID == E_ITEM_LEATHER_CAP )		then	return true		end
	if( inItemID == E_ITEM_LEATHER_TUNIC )		then	return true		end
	if( inItemID == E_ITEM_LEATHER_PANTS )		then	return true		end
	if( inItemID == E_ITEM_LEATHER_BOOTS )		then	return true		end
	
	if( inItemID == E_ITEM_CHAIN_HELMET )		then	return true		end
	if( inItemID == E_ITEM_CHAIN_CHESTPLATE )	then	return true		end
	if( inItemID == E_ITEM_CHAIN_LEGGINGS )		then	return true		end
	if( inItemID == E_ITEM_CHAIN_BOOTS )		then	return true		end
	
	if( inItemID == E_ITEM_IRON_HELMET )		then	return true		end
	if( inItemID == E_ITEM_IRON_CHESTPLATE )	then	return true		end
	if( inItemID == E_ITEM_IRON_LEGGINGS )		then	return true		end
	if( inItemID == E_ITEM_IRON_BOOTS )			then	return true		end
	
	if( inItemID == E_ITEM_DIAMOND_HELMET )		then	return true		end
	if( inItemID == E_ITEM_DIAMOND_CHESTPLATE )	then	return true		end
	if( inItemID == E_ITEM_DIAMOND_LEGGINGS )	then	return true		end
	if( inItemID == E_ITEM_DIAMOND_BOOTS )		then	return true		end
	
	if( inItemID == E_ITEM_GOLD_HELMET )		then	return true		end
	if( inItemID == E_ITEM_GOLD_CHESTPLATE )	then	return true		end
	if( inItemID == E_ITEM_GOLD_LEGGINGS )		then	return true		end
	if( inItemID == E_ITEM_GOLD_BOOTS )			then	return true		end
	
	if( inCheckForHorseArmor ) then
		if( inItemID == E_ITEM_IRON_HORSE_ARMOR )		then	return true		end
		if( inItemID == E_ITEM_GOLD_HORSE_ARMOR )		then	return true		end
		if( inItemID == E_ITEM_DIAMOND_HORSE_ARMOR )	then	return true		end
	end
	return false
end
-- Returns full-length playername for a short name( usefull for parsing commands )
function GetExactPlayername( inPlayerName )
	local _result = inPlayerName
	local function SetProcessingPlayername( inPlayer )
		_result = inPlayer:GetName()
	end
	cRoot:Get():FindAndDoWithPlayer( inPlayerName, SetProcessingPlayername )
	return _result
end
function GetPlayerByName( inPlayerName )
	local _player
	local PlayerSetter = function( Player )
		_player = Player
	end
	cRoot:Get():FindAndDoWithPlayer( inPlayerName, PlayerSetter )
	return _player
end
--[[
Not-so-usual math _functions
]]
-- Rounds floating point number. Because lua guys think this function doesn't deserve to be presented in lua's math
function round( inX )
  if( inX%2 ~= 0.5 ) then
    return math.floor( inX + 0.5 )
  end
  return inX - 0.5
end
--[[
Functions I use for filework and stringswork
]]
function PluralString( inValue, inSingularString, inPluralString )
	local _value_string = tostring( inValue )
	if( _value_string[#_value_string] == "1" ) then
		return inSingularString
	end
	return inPluralString
end
function PluralItemName( inItemID, inAmount )	-- BEWARE! TEMPORAL SOLUTION THERE! :D
	local _value_string = tostring( inValue )
	local _name = ""
	if( _value_string[#_value_string] == "1" ) then
		-- singular names
		_name = ItemTypeToString( inItemID )
	else
		-- plural names
		_name = ItemTypeToString( inItemID ).."s"
	end
	return _name
end
-- for filewriting purposes. 0 = false, 1 = true
function StringToBool( inValue )
	if( inValue == "1" ) then return true end
	return false
end
-- same, but reversal
function BoolToString( inValue )
	if( inValue == true ) then return 1 end
	return 0
end