diff options
author | Tau <tau.tihon@gmail.com> | 2014-01-02 18:25:26 +0100 |
---|---|---|
committer | Tau <tau.tihon@gmail.com> | 2014-01-02 18:25:26 +0100 |
commit | 0c095faac855806f96d307cd7fa46120034523e9 (patch) | |
tree | 21e7e78f10452584a09a7e9e8277121984f928e4 /MCServer | |
parent | Merge pull request #479 from mc-server/cmake-win (diff) | |
download | cuberite-0c095faac855806f96d307cd7fa46120034523e9.tar cuberite-0c095faac855806f96d307cd7fa46120034523e9.tar.gz cuberite-0c095faac855806f96d307cd7fa46120034523e9.tar.bz2 cuberite-0c095faac855806f96d307cd7fa46120034523e9.tar.lz cuberite-0c095faac855806f96d307cd7fa46120034523e9.tar.xz cuberite-0c095faac855806f96d307cd7fa46120034523e9.tar.zst cuberite-0c095faac855806f96d307cd7fa46120034523e9.zip |
Diffstat (limited to 'MCServer')
-rw-r--r-- | MCServer/Plugins/Handy/handy.lua | 2 | ||||
-rw-r--r-- | MCServer/Plugins/Handy/handy_functions.lua | 427 |
2 files changed, 145 insertions, 284 deletions
diff --git a/MCServer/Plugins/Handy/handy.lua b/MCServer/Plugins/Handy/handy.lua index 6d226ccaf..e4e9d3f5f 100644 --- a/MCServer/Plugins/Handy/handy.lua +++ b/MCServer/Plugins/Handy/handy.lua @@ -1,7 +1,7 @@ -- Global variables PLUGIN = {} -- Reference to own plugin object CHEST_WIDTH = 9 -HANDY_VERSION = 1 +HANDY_VERSION = 2 --[[ Handy is a plugin for other plugins. It contain no commands, no hooks, but functions to ease plugins developers' life. diff --git a/MCServer/Plugins/Handy/handy_functions.lua b/MCServer/Plugins/Handy/handy_functions.lua index a76980c6e..c142ffd08 100644 --- a/MCServer/Plugins/Handy/handy_functions.lua +++ b/MCServer/Plugins/Handy/handy_functions.lua @@ -6,350 +6,211 @@ function GetHandyVersion() return HANDY_VERSION end -- Checks if handy is in proper version -function CheckForRequiedVersion(IN_version) - if (IN_version > HANDY_VERSION) then return false end +function CheckForRequiedVersion( inVersion ) + if( inVersion > HANDY_VERSION ) then return false end return true end --[[ MCS-specific _functions and nasty hacks :D ]] --- There's a "GetChestHeight" function inside source code, but it's not lua-exported -function GetChestHeightCheat(IN_chest) - if (IN_chest:GetSlot(28) == nil) then -- this means we're trying to get double chest slot and FAIL - LOGWARN("HANDY: single chest checked") - return 3 +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 - LOGWARN("HANDY: double chest checked") - return 6 + return true end --- Those two checks how many items of given IN_itemID chest and player have, and how much they could fit inside them -function ReadChestForItem(IN_chest, IN_itemID) - local _items_found = 0 - local _free_space = 0 - -- stalk through chest slots... - local _slot_counter = 0 - local _slot_item - local _item_max_stack = GetItemMaxStack(IN_itemID) - while true do - _slot_item = IN_chest:GetSlot(_slot_counter) - if (_slot_item ~= nil) then - if (_slot_item.m_ItemID == IN_itemID) then - _items_found = _items_found + _slot_item.m_ItemCount - _free_space = _free_space + (_item_max_stack - _slot_item.m_ItemCount) - end - if (_slot_item:IsEmpty() == true) then - _free_space = _free_space + _item_max_stack - end - end - _slot_counter = _slot_counter + 1 - if (_slot_counter == CHEST_WIDTH*GetChestHeightCheat(IN_chest)) then - break - end - end - return _items_found, _free_space +-- 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(IN_player, IN_itemID) - local _items_found = 0 - local _free_space = 0 - -- stalk through IN_player inventory slots... - local _slot_counter = 9 - if (ItemIsArmor(IN_itemID) == true) then _slot_counter = 5 end - local _slot_item - local _item_max_stack = GetItemMaxStack(IN_itemID) - while true do - _slot_item = IN_player:GetInventory():GetSlot(_slot_counter) - if (_slot_item ~= nil) then - if (_slot_item.m_ItemID == IN_itemID) then - _items_found = _items_found + _slot_item.m_ItemCount - _free_space = _free_space + (_item_max_stack - _slot_item.m_ItemCount) - end - if (_slot_item:IsEmpty() == true) then - _free_space = _free_space + _item_max_stack - end - end - _slot_counter = _slot_counter + 1 - if (_slot_counter == 45) then - break - end - end - return _items_found, _free_space +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 (since noone was bothered writing them in MCS code) +-- Following functions are for chest-related operations -- BEWARE! Those assume you did checked if chest has items/space in it! -function TakeItemsFromChest(IN_chest, IN_itemID, IN_ammount) -- UNSAFE! CHECK FOR ITEMS FIRST!! - -- stalk through chest slots... - local _slot_counter = 0 - local _slot_item - local _take_count = IN_ammount - while true do - _slot_item = IN_chest:GetSlot(_slot_counter) - if (_slot_item ~= nil) then - if (_slot_item.m_ItemID == IN_itemID) then - -- assuming player have enought money - if (_take_count > 0) then - if (_take_count > _slot_item.m_ItemCount) then - _take_count = _take_count - _slot_item.m_ItemCount - IN_chest:SetSlot(_slot_counter, cItem()) -- a bit hacky, can't make cItem:Clear() work( - else - local _left_count = _slot_item.m_ItemCount - _take_count - IN_chest:SetSlot(_slot_counter, cItem(_slot_item.m_ItemID, _left_count)) -- a bit hacky - _take_count = 0 - end - end - if (_take_count == 0) then - break - end +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 - _slot_counter = _slot_counter + 1 - if (_slot_counter == CHEST_WIDTH*GetChestHeightCheat(IN_chest)) then - break - end end -end -function PutItemsToChest(IN_chest, IN_itemID, IN_ammount) -- UNSAFE! CHECK FOR SPACE FIRST!! - -- stalk through chest slots... - local _slot_counter = 0 - local _slot_item - local _put_count = IN_ammount - local _max_stack = GetItemMaxStack(IN_itemID) - while true do - _slot_item = IN_chest:GetSlot(_slot_counter) - local _portion = 0 - local _ammount_to_set = 0 - if (_slot_item ~= nil) then - if (_slot_item:IsEmpty() == true) then - _portion = math.min(_max_stack, _put_count) - _ammount_to_set = _portion + 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 - if (_slot_item.m_ItemID == IN_itemID) then - -- choose between how much we need to put and how much free space left - _portion = math.min(_put_count, _max_stack - _slot_item.m_ItemCount) - _ammount_to_set = _slot_item.m_ItemCount + _portion - end + removedItem.m_ItemCount = slotItem.m_ItemCount - removedItem.m_ItemCount + inGrid:SetSlot( index, removedItem ) + removedItem.m_ItemCount = 0 end - end - IN_chest:SetSlot(_slot_counter, cItem(IN_itemID, _ammount_to_set)) -- we add max stack to chest - _put_count = _put_count - _portion - if (_put_count == 0) then break end - _slot_counter = _slot_counter + 1 - if (_slot_counter == CHEST_WIDTH*GetChestHeightCheat(IN_chest)) then - break + 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(IN_player, IN_itemID, IN_ammount) -- UNSAFE! CHECK FIRST! - local _put_count = IN_ammount - local _max_stack = GetItemMaxStack(IN_itemID) - while true do - local _portion = math.min(_max_stack, _put_count) - IN_player:GetInventory():RemoveItem(cItem(IN_itemID, _portion)) - _put_count = _put_count - _portion - if (_put_count == 0) then break end +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(IN_player, IN_itemID, IN_ammount) -- UNSAFE! CHECK FIRST! - local _put_count = IN_ammount - local _max_stack = GetItemMaxStack(IN_itemID) - while true do - local _portion = math.min(_max_stack, _put_count) - IN_player:GetInventory():AddItem(cItem(IN_itemID, _portion)) - _put_count = _put_count - _portion - if (_put_count == 0) then break 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) +-- 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(IN_itemID) - local _result = 64 - -- Tools and swords - if (IN_itemID == woodensword) then _result = 1 end - if (IN_itemID == woodenshovel) then _result = 1 end - if (IN_itemID == woodenpickaxe) then _result = 1 end - if (IN_itemID == woodenaxe) then _result = 1 end - if (IN_itemID == woodenhoe) then _result = 1 end - if (IN_itemID == stonesword) then _result = 1 end - if (IN_itemID == stoneshovel) then _result = 1 end - if (IN_itemID == stonepickaxe) then _result = 1 end - if (IN_itemID == stoneaxe) then _result = 1 end - if (IN_itemID == stonehoe) then _result = 1 end - if (IN_itemID == ironsword) then _result = 1 end - if (IN_itemID == ironshovel) then _result = 1 end - if (IN_itemID == ironpickaxe) then _result = 1 end - if (IN_itemID == ironaxe) then _result = 1 end - if (IN_itemID == ironhoe) then _result = 1 end - if (IN_itemID == diamondsword) then _result = 1 end - if (IN_itemID == diamondshovel) then _result = 1 end - if (IN_itemID == diamondpickaxe) then _result = 1 end - if (IN_itemID == diamondaxe) then _result = 1 end - if (IN_itemID == diamondhoe) then _result = 1 end - if (IN_itemID == goldensword) then _result = 1 end - if (IN_itemID == goldenshovel) then _result = 1 end - if (IN_itemID == goldenpickaxe) then _result = 1 end - if (IN_itemID == goldenaxe) then _result = 1 end - if (IN_itemID == goldenhoe) then _result = 1 end - - if (IN_itemID == flintandsteel) then _result = 1 end - if (IN_itemID == bow) then _result = 1 end - if (IN_itemID == sign) then _result = 16 end - if (IN_itemID == woodendoor) then _result = 1 end - if (IN_itemID == irondoor) then _result = 1 end - if (IN_itemID == cake) then _result = 1 end - if (IN_itemID == cauldron) then _result = 1 end - if (IN_itemID == mushroomstew) then _result = 1 end - if (IN_itemID == painting) then _result = 1 end - if (IN_itemID == bucket) then _result = 16 end - if (IN_itemID == waterbucket) then _result = 1 end - if (IN_itemID == lavabucket) then _result = 1 end - if (IN_itemID == minecart) then _result = 1 end - if (IN_itemID == saddle) then _result = 1 end - if (IN_itemID == snowball) then _result = 16 end - if (IN_itemID == boat) then _result = 1 end - if (IN_itemID == milkbucket) then _result = 1 end - if (IN_itemID == storageminecart) then _result = 1 end - if (IN_itemID == poweredminecart) then _result = 1 end - if (IN_itemID == egg) then _result = 16 end - if (IN_itemID == fishingrod) then _result = 1 end - if (IN_itemID == bed) then _result = 1 end - if (IN_itemID == map) then _result = 1 end - if (IN_itemID == shears) then _result = 1 end - if (IN_itemID == enderpearl) then _result = 16 end - if (IN_itemID == potion) then _result = 1 end - if (IN_itemID == spawnegg) then _result = 1 end - if (IN_itemID == bookandquill) then _result = 1 end - if (IN_itemID == writtenbook) then _result = 1 end - if (IN_itemID == carrotonastick) then _result = 1 end - - if (IN_itemID == goldrecord) then _result = 1 end - if (IN_itemID == greenrecord) then _result = 1 end - if (IN_itemID == blocksrecord) then _result = 1 end - if (IN_itemID == chirprecord) then _result = 1 end - if (IN_itemID == farrecord) then _result = 1 end - if (IN_itemID == mallrecord) then _result = 1 end - if (IN_itemID == mellohirecord) then _result = 1 end - if (IN_itemID == stalrecord) then _result = 1 end - if (IN_itemID == stradrecord) then _result = 1 end - if (IN_itemID == wardrecord) then _result = 1 end - if (IN_itemID == oneonerecord) then _result = 1 end - if (IN_itemID == waitrecord) then _result = 1 end - - --if (IN_itemID == xxxxxxxxx) then _result = 1 end - - if (IN_itemID == leatherhelmet) then _result = 1 end - if (IN_itemID == leatherchestplate) then _result = 1 end - if (IN_itemID == leatherpants) then _result = 1 end - if (IN_itemID == leatherboots) then _result = 1 end - - if (IN_itemID == chainmailhelmet) then _result = 1 end - if (IN_itemID == chainmailchestplate) then _result = 1 end - if (IN_itemID == chainmailpants) then _result = 1 end - if (IN_itemID == chainmailboots) then _result = 1 end +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 (IN_itemID == ironhelmet) then _result = 1 end - if (IN_itemID == ironchestplate) then _result = 1 end - if (IN_itemID == ironpants) then _result = 1 end - if (IN_itemID == ironboots) then _result = 1 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 (IN_itemID == diamondhelmet) then _result = 1 end - if (IN_itemID == diamondchestplate) then _result = 1 end - if (IN_itemID == diamondpants) then _result = 1 end - if (IN_itemID == diamondboots) then _result = 1 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 (IN_itemID == goldenhelmet) then _result = 1 end - if (IN_itemID == goldenchestplate) then _result = 1 end - if (IN_itemID == goldenpants) then _result = 1 end - if (IN_itemID == goldenboots) then _result = 1 end - return _result -end -function ItemIsArmor(IN_itemID) - local _result = false - if (IN_itemID == leatherhelmet) then _result = true end - if (IN_itemID == leatherchestplate) then _result = true end - if (IN_itemID == leatherpants) then _result = true end - if (IN_itemID == leatherboots) then _result = true end - - if (IN_itemID == chainmailhelmet) then _result = true end - if (IN_itemID == chainmailchestplate) then _result = true end - if (IN_itemID == chainmailpants) then _result = true end - if (IN_itemID == chainmailboots) then _result = true end - - if (IN_itemID == ironhelmet) then _result = true end - if (IN_itemID == ironchestplate) then _result = true end - if (IN_itemID == ironpants) then _result = true end - if (IN_itemID == ironboots) then _result = 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 (IN_itemID == diamondhelmet) then _result = true end - if (IN_itemID == diamondchestplate) then _result = true end - if (IN_itemID == diamondpants) then _result = true end - if (IN_itemID == diamondboots) then _result = 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 (IN_itemID == goldenhelmet) then _result = true end - if (IN_itemID == goldenchestplate) then _result = true end - if (IN_itemID == goldenpants) then _result = true end - if (IN_itemID == goldenboots) then _result = true end - return _result + 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(IN_playername) - local _result = IN_playername - local function SetProcessingPlayername(IN_player) - _result = IN_player:GetName() +-- 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(IN_playername, SetProcessingPlayername) + cRoot:Get():FindAndDoWithPlayer( inPlayerName, SetProcessingPlayername ) return _result end -function GetPlayerByName(IN_playername) +function GetPlayerByName( inPlayerName ) local _player - local PlayerSetter = function (Player) + local PlayerSetter = function( Player ) _player = Player end - cRoot:Get():FindAndDoWithPlayer(IN_playername, PlayerSetter) + 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(IN_x) - if (IN_x%2 ~= 0.5) then - return math.floor(IN_x+0.5) +function round( inX ) + if( inX%2 ~= 0.5 ) then + return math.floor( inX + 0.5 ) end - return IN_x-0.5 + return inX - 0.5 end --[[ Functions I use for filework and stringswork ]] -function PluralString(IN_value, IN_singular_string, IN_plural_string) - local _value_string = tostring(IN_value) - if (_value_string[#_value_string] == "1") then - return IN_singular_string +function PluralString( inValue, inSingularString, inPluralString ) + local _value_string = tostring( inValue ) + if( _value_string[#_value_string] == "1" ) then + return inSingularString end - return IN_plural_string + return inPluralString end -function PluralItemName(IN_itemID, IN_ammount) -- BEWARE! TEMPORAL SOLUTION THERE! :D - local _value_string = tostring(IN_value) +function PluralItemName( inItemID, inAmount ) -- BEWARE! TEMPORAL SOLUTION THERE! :D + local _value_string = tostring( inValue ) local _name = "" - if (_value_string[#_value_string] == "1") then + if( _value_string[#_value_string] == "1" ) then -- singular names - _name = ItemTypeToString(IN_itemID) + _name = ItemTypeToString( inItemID ) else -- plural names - _name = ItemTypeToString(IN_itemID).."s" + _name = ItemTypeToString( inItemID ).."s" end return _name end -- for filewriting purposes. 0 = false, 1 = true -function StringToBool(value) - if value=="1" then return true end +function StringToBool( inValue ) + if( inValue == "1" ) then return true end return false end -- same, but reversal -function BoolToString(value) - if value==true then return 1 end +function BoolToString( inValue ) + if( inValue == true ) then return 1 end return 0 end
\ No newline at end of file |