Skip to content

Commit

Permalink
Add an Auto Craft checkbox to enable/disable auto crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
ColonolNutty committed Jun 16, 2018
1 parent 54c79f3 commit 822599d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 28 deletions.
27 changes: 24 additions & 3 deletions interface/scripted/recipeCrafterMFM/recipeCrafterMFM.config.patch
Original file line number Diff line number Diff line change
@@ -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" ] }
]
]
83 changes: 83 additions & 0 deletions interface/scripted/recipeCrafterMFM/recipeCrafterMFMFUgui.lua
Original file line number Diff line number Diff line change
@@ -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
97 changes: 72 additions & 25 deletions recipeCrafterMFM/recipeCrafterMFM.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 822599d

Please sign in to comment.