diff --git a/interface/scripted/recipeCrafterMFM/recipeCrafterMFM.config.patch b/interface/scripted/recipeCrafterMFM/recipeCrafterMFM.config.patch index c8e77ef..6712ac7 100644 --- a/interface/scripted/recipeCrafterMFM/recipeCrafterMFM.config.patch +++ b/interface/scripted/recipeCrafterMFM/recipeCrafterMFM.config.patch @@ -1,9 +1,30 @@ [ [ - { "op": "test", "path": "/gui/craft", "inverse": true }, - { "op": "add", "path": "/gui/craft", "value": {} } + { "op": "test", "path": "/gui/toggleAutoCraft", "inverse": true }, + { "op": "add", "path": "/gui/toggleAutoCraft", "value": {} } ], [ - { "op": "remove", "path": "/gui/craft" } + { "op": "test", "path": "/gui/lblAutoCraft", "inverse": true }, + { "op": "add", "path": "/gui/lblAutoCraft", "value": {} } + ], + [ + { "op": "replace", "path": "/gui/toggleAutoCraft", "value": { + "type": "button", + "checkable" : true, + "base" : "/interface/crafting/checkboxnocheck.png", + "baseImageChecked" : "/interface/crafting/checkboxcheck.png", + "checked" : false, + "position" : [45, 7] + } + }, + { "op": "replace", "path": "/gui/lblAutoCraft", "value": { + "type" : "label", + "position" : [55, 7], + "hAnchor" : "left", + "value" : "AUTO CRAFT" + } + }, + { "op": "add", "path": "/scriptWidgetCallbacks/-", "value": "toggleAutoCraft" }, + { "op": "replace", "path": "/scripts", "value": [ "/interface/scripted/recipeCrafterMFM/recipeCrafterMFMFUgui.lua" ] } ] ] \ No newline at end of file diff --git a/interface/scripted/recipeCrafterMFM/recipeCrafterMFMFUgui.lua b/interface/scripted/recipeCrafterMFM/recipeCrafterMFMFUgui.lua new file mode 100644 index 0000000..5541994 --- /dev/null +++ b/interface/scripted/recipeCrafterMFM/recipeCrafterMFMFUgui.lua @@ -0,0 +1,83 @@ +require "/scripts/MFM/entityQueryAPI.lua" +require "/interface/scripted/recipeCrafterMFM/recipeCrafterMFMgui.lua" + +local autoCraftStateUpdated = false; +local entityId = nil; +local settings = { + autoCraftState = false +}; + +TOGGLE_AUTOCRAFT_NAME = "toggleAutoCraft"; + +function init() + EntityQueryAPI.init() + entityId = pane.containerEntityId() + autoCraftStateUpdated = false; + RecipeCrafterMFMGui.init() + hideCraftButtonIfAutoCraftEnabled() +end + +function update(dt) + updateAutoCraftState() + RecipeCrafterMFMGui.update(dt) +end + +--------------------------------------------------------------------- + +function getAutoCraftState() + if(storage) then + return storage.autoCraftState + else + return settings.autoCraftState + end +end + +function setAutoCraftState(val) + if(storage) then + storage.autoCraftState = toEnable + else + settings.autoCraftState = toEnable + end + hideCraftButtonIfAutoCraftEnabled() +end + +function toggleAutoCraft() + if(entityId == nil) then + return + end + local toEnable = widget.getChecked(TOGGLE_AUTOCRAFT_NAME) + setAutoCraftState(toEnable) + hideCraftButtonIfAutoCraftEnabled() + world.sendEntityMessage(entityId, "setAutoCraftState", toEnable) +end + +function hideCraftButtonIfAutoCraftEnabled() + if(getAutoCraftState()) then + widget.setVisible("craft", false) + else + widget.setVisible("craft", true) + end +end + +function updateAutoCraftState() + if(autoCraftStateUpdated) then + return + end + local handle = function() + local result = EntityQueryAPI.requestData(entityId, "getAutoCraftState", 0, nil) + if(result ~= nil) then + return true, result + end + return false, nil + end + + local onCompleted = function(result) + local autoCraftState = result.autoCraftState + setAutoCraftState(autoCraftState) + hideCraftButtonIfAutoCraftEnabled() + widget.setChecked(TOGGLE_AUTOCRAFT_NAME, autoCraftState) + autoCraftStateUpdated = true + end + + EntityQueryAPI.addRequest("RGMFMFUGui.updateAutoCraftState", handle, onCompleted) +end \ No newline at end of file diff --git a/recipeCrafterMFM/recipeCrafterMFM.lua b/recipeCrafterMFM/recipeCrafterMFM.lua index 97d0d4a..5a19789 100644 --- a/recipeCrafterMFM/recipeCrafterMFM.lua +++ b/recipeCrafterMFM/recipeCrafterMFM.lua @@ -7,40 +7,49 @@ local logger = nil; function init(virtual) logger = DebugUtilsCN.init("[RCFU]") - RecipeCrafterMFMApi.init() - RecipeLocatorAPI.init(); + RecipeCrafterMFMApi.init(virtual); + RecipeLocatorAPI.init(virtual); ----- Configuration ----- - storage.consumeIngredientsOnCraft = false; - storage.holdIngredientsOnCraft = false; - storage.playSoundBeforeOutputPlaced = false; - storage.appendNewOutputToCurrentOutput = false; + logger.setDebugState(false); + if(storage.autoCraftState == nil) then + storage.autoCraftState = false; + end + rcUtilsFU.updateCraftSettings(); ------------------------- + message.setHandler("setAutoCraftState", rcUtilsFU.setAutoCraftState); + message.setHandler("getAutoCraftState", rcUtilsFU.getAutoCraftState); end function update(dt) RecipeCrafterMFMApi.update(dt) - if(RecipeCrafterMFMApi.containerContentsChanged) then - if(rcUtilsFU.isOutputSlotModified()) then - rcUtilsFU.consumeIngredients() - storage.currentlySelectedRecipe = nil; - elseif(rcUtilsFU.shouldRemoveOutput()) then - rcUtilsFU.removeOutput() - storage.currentlySelectedRecipe = nil; - else - RecipeCrafterMFMApi.craftItem() - end + if(not RecipeCrafterMFMApi.containerContentsChanged or not getAutoCraftState()) then + return + end + if(storage.currentlySelectedRecipe ~= nil and rcUtilsFU.isOutputSlotModified()) then + rcUtilsFU.consumeIngredients() + storage.currentlySelectedRecipe = nil; + elseif(storage.currentlySelectedRecipe ~= nil and rcUtilsFU.shouldRemoveOutput()) then + rcUtilsFU.removeOutput() + storage.currentlySelectedRecipe = nil; + elseif(getAutoCraftState()) then + RecipeCrafterMFMApi.craftItem() end end function die() - rcUtilsFU.removeOutput() + if(getAutoCraftState()) then + rcUtilsFU.removeOutput() + end RecipeCrafterMFMApi.die() end -------------------------------Callback Hooks------------------------------------ function RecipeCrafterMFMApi.isOutputSlotAvailable() + if(not getAutoCraftState()) then + return RecipeCrafterMFMApi.isOutputSlotAvailableBase() + end local outputSlotItem = world.containerItemAt(entity.id(), storage.outputSlot) -- Output slot is empty if(outputSlotItem == nil) then @@ -70,21 +79,60 @@ function RecipeCrafterMFMApi.isOutputSlotAvailable() return not rcUtilsFU.isOutputSlotModified(); end -function RecipeCrafterMFMApi.onCraftStart() +function RecipeCrafterMFMApi.onNoRecipeFound() + if(not getAutoCraftState()) then + return RecipeCrafterMFMApi.onNoRecipeFoundBase() + end + rcUtilsFU.removeOutput(); + storage.currentlySelectedRecipe = nil; end -function RecipeCrafterMFMApi.onRecipeFound() +------------------------------------------------------------------- + +function rcUtilsFU.updateCraftSettings() + if(getAutoCraftState()) then + storage.consumeIngredientsOnCraft = false; + storage.holdIngredientsOnCraft = false; + storage.playSoundBeforeOutputPlaced = false; + storage.appendNewOutputToCurrentOutput = false; + else + storage.consumeIngredientsOnCraft = true; + storage.holdIngredientsOnCraft = true; + storage.playSoundBeforeOutputPlaced = true; + storage.appendNewOutputToCurrentOutput = true; + end end -function RecipeCrafterMFMApi.onNoIngredientsFound() +function getAutoCraftState() + return rcUtilsFU.getAutoCraftState().autoCraftState end -function RecipeCrafterMFMApi.onNoRecipeFound() - rcUtilsFU.removeOutput(); - storage.currentlySelectedRecipe = nil; +function rcUtilsFU.getAutoCraftState() + if(not storage or not storage.autoCraftState) then + if (storage) then + storage.autoCraftState = false; + end + return { + autoCraftState = false + } + end + return { + autoCraftState = storage.autoCraftState + } end -------------------------------------------------------------------- +function rcUtilsFU.setAutoCraftState(id, name, newValue) + if (storage.autoCraftState ~= newValue) then + if (newValue) then + storage.currentlySelectedRecipe = nil; + else + rcUtilsFU.consumeIngredients() + storage.currentlySelectedRecipe = nil; + end + end + storage.autoCraftState = newValue or false; + rcUtilsFU.updateCraftSettings() +end function rcUtilsFU.isOutputSlotModified() local outputSlotItem = world.containerItemAt(entity.id(), storage.outputSlot) @@ -94,7 +142,6 @@ function rcUtilsFU.isOutputSlotModified() -- If we haven't crafted and there is an output slot item, then it is modified -- If we haven't crafted and there is no output slot item, then it is not modified if(currentlySelectedRecipe == nil or currentlySelectedRecipe.output == nil) then - logger.logDebug("No selected recipe"); return outputSlotItem ~= nil; end