Skip to content

Commit

Permalink
Merge pull request #8 from slashkeyvalue/keyvalue-craft-mult-inputs
Browse files Browse the repository at this point in the history
Implements the ability to accept more than 1 item per slot on the crafti…
  • Loading branch information
Ktos93 committed Nov 21, 2020
2 parents b08ced4 + 7199150 commit ae2371b
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 67 deletions.
2 changes: 1 addition & 1 deletion config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config.Crafting = {

items = {
"empty","empty","empty",
"wheat","wheat","wheat",
"wheat","wheat, 2","wheat",
"empty","empty","empty"
},
requireJob = "empty",
Expand Down
18 changes: 7 additions & 11 deletions html/js/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,13 @@ function GetNumberOfItems(data) {
}

$( "#craftButton" ).click(function() {
$.post('http:/redemrp_inventory/craft', JSON.stringify({
slot_1: GetCraftingSlotData(4),
slot_2: GetCraftingSlotData(5),
slot_3: GetCraftingSlotData(6),
slot_4: GetCraftingSlotData(7),
slot_5: GetCraftingSlotData(8),
slot_6: GetCraftingSlotData(9),
slot_7: GetCraftingSlotData(10),
slot_8: GetCraftingSlotData(11),
slot_9: GetCraftingSlotData(12),
}), );
const message = [];

for (i = 4; i <= 12; i++) {
message.push(GetCraftingSlotData(i));
}

$.post('http:/redemrp_inventory/craft', JSON.stringify(message));
});

function GetCraftingSlotData(id)
Expand Down
214 changes: 159 additions & 55 deletions server/sv_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -846,82 +846,186 @@ RegisterServerEvent("redemrp_inventory:craft")
AddEventHandler("redemrp_inventory:craft", function(data , type)
local _source = source
local _type = type
local itemtoCraft
local table_value = {}
for v,k in pairs(data) do
if k[1] ~= "empty" then
table.insert(table_value, k[2])
end
end
if table_value[1] then
local arraymin = math.min(table.unpack(table_value))
for a,b in pairs(Config.Crafting) do
local craftCheck = true
if data.slot_1[1] ~= b.items[1] then
craftCheck = false
end
if data.slot_2[1] ~= b.items[2] then
craftCheck = false
end
if data.slot_3[1] ~= b.items[3] then
craftCheck = false
end
if data.slot_4[1] ~= b.items[4] then
craftCheck = false
end
if data.slot_5[1] ~= b.items[5] then
craftCheck = false
end
if data.slot_6[1] ~= b.items[6] then
craftCheck = false
end
if data.slot_7[1] ~= b.items[7] then
craftCheck = false
end
if data.slot_8[1] ~= b.items[8] then
craftCheck = false
end
if data.slot_9[1] ~= b.items[9] then
craftCheck = false
end
if craftCheck then
itemtoCraft = a
break
end

local outputItem, outputItemAmount = CheckForSingleBlueprintCollision(data)

if not outputItem then
outputItem, outputItemAmount = CheckForSingleBlueprintMultipleCollisions(data)
end
if itemtoCraft ~= nil then
local CraftData = Config.Crafting[itemtoCraft]

if outputItem then
local CraftData = Config.Crafting[outputItem]
if CraftData.type == _type or CraftData.type == "empty" then
TriggerEvent('redemrp:getPlayerFromId', _source, function(user)
local job = user.getJob()
if CraftData.requireJob == job or CraftData.requireJob == "empty" then
local bpInputs = CraftData.items

local strmatch = string.match

for v,k in pairs(data) do
if k[1] ~= "empty" and k[1] ~= "WEAPON_MELEE_KNIFE" then
local itemData1 = SharedInventoryFunctions.getItem(_source, k[1])
itemData1.RemoveItem(arraymin)

local inputItem = k[1]

if inputItem ~= "empty" and inputItem ~= "WEAPON_MELEE_KNIFE" then
local bpInput = bpInputs[v]
local _, bpInputAmount = strmatch(bpInput, '(%a+)%s*,%s*(%d+)')

bpInputAmount = tonumber(bpInputAmount) or 1

local itemData1 = SharedInventoryFunctions.getItem(_source, inputItem)
itemData1.RemoveItem(bpInputAmount * outputItemAmount)
end
end
local itemData2
if itemtoCraft == "flask_clean" then
if outputItem == "flask_clean" then
local meta = {}
meta.waterlevel = data.slot_5[3].waterlevel
itemData2 = SharedInventoryFunctions.getItem(_source, itemtoCraft , meta)
elseif itemtoCraft == "flask" then
itemData2 = SharedInventoryFunctions.getItem(_source, outputItem , meta)
elseif outputItem == "flask" then
local meta = {}
meta.waterlevel = 0
itemData2 = SharedInventoryFunctions.getItem(_source, itemtoCraft , meta)
itemData2 = SharedInventoryFunctions.getItem(_source, outputItem , meta)
else
itemData2 = SharedInventoryFunctions.getItem(_source, itemtoCraft)
end
itemData2.AddItem(arraymin * CraftData.amount)
itemData2 = SharedInventoryFunctions.getItem(_source, outputItem)
end

itemData2.AddItem(outputItemAmount * CraftData.amount)
end
end)
end
end
end
end)

function IterateThroughBlueprints(inputSlots, next)

local collisions = {}

local strmatch = string.match

for bpOutputItem, bp in pairs(Config.Crafting) do
local bpOutputAmount = bp.amount
local bpInputs = bp.items

local itSucceeded = true
local multiplier

for i = 1, 9 do
local bpInput = bpInputs[i]

local bpInputItem, bpInputAmount = strmatch(bpInput, '(%a+)%s*,%s*(%d+)')
bpInputItem = bpInputItem or bpInput
bpInputAmount = tonumber(bpInputAmount) or 1

if bpInputItem == 'empty' then
bpInputAmount = 0
end

local input = inputSlots[i]
local inputItem = input[1]
local inputAmount = input[2]

local continue, mult = next(multiplier, bpInputItem, bpInputAmount, inputItem, inputAmount)
multiplier = mult

if not continue then
itSucceeded = false
break
end
end

if itSucceeded then
table.insert(collisions, {bpOutputItem, bpOutputAmount, multiplier or 1.0})
end
end

local retItem
local retAmount

local numCollisions = #collisions

if numCollisions == 1 then

local col = collisions[1]

retItem = col[1]
retAmount = col[2] * col[3]

elseif numCollisions > 1 then

local chosenCollision
local highestMultiplier = 0

for _, col in ipairs(collisions) do

local mult = col[3]

if mult > highestMultiplier then
chosenCollision = col
highestMultiplier = mult
end
end

if chosenCollision then
retItem = chosenCollision[1]
retAmount = chosenCollision[2] * chosenCollision[3]
end
end

return retItem, retAmount
end

function CheckForSingleBlueprintCollision(inputSlots)
local insert = table.insert

local t = {}
for _, d in ipairs(inputSlots) do
if d[1] ~= 'empty' then
insert(t, d[2])
end
end

local lowestInputAmount = math.min(table.unpack(t))

next = function(_, bpInputItem, bpInputAmount, inputItem, inputAmount)

if inputItem ~= bpInputItem then
return false
end

if inputAmount ~= bpInputAmount and inputAmount ~= (bpInputAmount * lowestInputAmount) then
return false
end

return true, lowestInputAmount
end

return IterateThroughBlueprints(inputSlots, next)
end

function CheckForSingleBlueprintMultipleCollisions(inputSlots)
next = function(mult, bpInputItem, bpInputAmount, inputItem, inputAmount)

if inputItem ~= bpInputItem then
return false
end

if inputAmount ~= bpInputAmount and (bpInputAmount ~= 0 and inputAmount % bpInputAmount ~= 0) then
return false
end

if inputItem ~= 'empty' and bpInputAmount ~= 0 then
local _ = inputAmount / bpInputAmount
if mult == nil or _ < mult then
mult = _
end
end

return true, mult
end

return IterateThroughBlueprints(inputSlots, next)
end



Expand Down

0 comments on commit ae2371b

Please sign in to comment.