2013-07-29 07:13:03 -04:00
|
|
|
--[[
|
|
|
|
General stuff
|
|
|
|
]]
|
|
|
|
-- Returns Handy plugin version number
|
|
|
|
function GetHandyVersion()
|
|
|
|
return HANDY_VERSION
|
|
|
|
end
|
|
|
|
-- Checks if handy is in proper version
|
2014-06-12 10:01:24 -04:00
|
|
|
function CheckForRequiredVersion( inVersion )
|
2014-01-02 12:25:26 -05:00
|
|
|
if( inVersion > HANDY_VERSION ) then return false end
|
2013-07-29 07:13:03 -04:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
--[[
|
|
|
|
MCS-specific _functions and nasty hacks :D
|
|
|
|
]]
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
return true
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
-- 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 )
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
-- Following functions are for chest-related operations
|
2013-07-29 07:13:03 -04:00
|
|
|
-- BEWARE! Those assume you did checked if chest has items/space in it!
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
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 )
|
2013-07-29 07:13:03 -04:00
|
|
|
else
|
2014-01-02 12:25:26 -05:00
|
|
|
removedItem.m_ItemCount = slotItem.m_ItemCount - removedItem.m_ItemCount
|
|
|
|
inGrid:SetSlot( index, removedItem )
|
|
|
|
removedItem.m_ItemCount = 0
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
if( removedItem.m_ItemCount <= 0 ) then break end
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
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 )
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
|
|
|
-- Similar to chest-related.
|
2014-01-02 12:25:26 -05:00
|
|
|
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 )
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
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 )
|
2013-07-29 07:13:03 -04:00
|
|
|
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:
|
2014-01-02 12:25:26 -05:00
|
|
|
-- 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 )
|
2013-07-29 07:13:03 -04:00
|
|
|
-- Feel free to ignore the difference, or to add those to items.txt
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2014-01-02 12:25:26 -05:00
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
-- 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()
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
cRoot:Get():FindAndDoWithPlayer( inPlayerName, SetProcessingPlayername )
|
2013-07-29 07:13:03 -04:00
|
|
|
return _result
|
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
function GetPlayerByName( inPlayerName )
|
2013-07-29 07:13:03 -04:00
|
|
|
local _player
|
2014-01-02 12:25:26 -05:00
|
|
|
local PlayerSetter = function( Player )
|
2013-07-29 07:13:03 -04:00
|
|
|
_player = Player
|
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
cRoot:Get():FindAndDoWithPlayer( inPlayerName, PlayerSetter )
|
2013-07-29 07:13:03 -04:00
|
|
|
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
|
2014-01-02 12:25:26 -05:00
|
|
|
function round( inX )
|
|
|
|
if( inX%2 ~= 0.5 ) then
|
|
|
|
return math.floor( inX + 0.5 )
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
return inX - 0.5
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
|
|
|
--[[
|
|
|
|
Functions I use for filework and stringswork
|
|
|
|
]]
|
2014-01-02 12:25:26 -05:00
|
|
|
function PluralString( inValue, inSingularString, inPluralString )
|
|
|
|
local _value_string = tostring( inValue )
|
|
|
|
if( _value_string[#_value_string] == "1" ) then
|
|
|
|
return inSingularString
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
return inPluralString
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
2014-01-02 12:25:26 -05:00
|
|
|
function PluralItemName( inItemID, inAmount ) -- BEWARE! TEMPORAL SOLUTION THERE! :D
|
|
|
|
local _value_string = tostring( inValue )
|
2013-07-29 07:13:03 -04:00
|
|
|
local _name = ""
|
2014-01-02 12:25:26 -05:00
|
|
|
if( _value_string[#_value_string] == "1" ) then
|
2013-07-29 07:13:03 -04:00
|
|
|
-- singular names
|
2014-01-02 12:25:26 -05:00
|
|
|
_name = ItemTypeToString( inItemID )
|
2013-07-29 07:13:03 -04:00
|
|
|
else
|
|
|
|
-- plural names
|
2014-01-02 12:25:26 -05:00
|
|
|
_name = ItemTypeToString( inItemID ).."s"
|
2013-07-29 07:13:03 -04:00
|
|
|
end
|
|
|
|
return _name
|
|
|
|
end
|
|
|
|
-- for filewriting purposes. 0 = false, 1 = true
|
2014-01-02 12:25:26 -05:00
|
|
|
function StringToBool( inValue )
|
|
|
|
if( inValue == "1" ) then return true end
|
2013-07-29 07:13:03 -04:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- same, but reversal
|
2014-01-02 12:25:26 -05:00
|
|
|
function BoolToString( inValue )
|
|
|
|
if( inValue == true ) then return 1 end
|
2013-07-29 07:13:03 -04:00
|
|
|
return 0
|
2014-06-12 10:01:24 -04:00
|
|
|
end
|