Introduce recipe override support for SimpleElectricItemProcessor

This commit is contained in:
2025-11-11 19:51:24 +01:00
parent d13d2b9dfd
commit 28ff1aeb22

View File

@@ -14,6 +14,8 @@
-- You should have received a copy of the GNU General Public License -- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
local S=minetest.get_translator("industrialtest")
industrialtest.SimpleElectricItemProcessor=table.copy(industrialtest.ActivatedElectricMachine) industrialtest.SimpleElectricItemProcessor=table.copy(industrialtest.ActivatedElectricMachine)
industrialtest.internal.unpackTableInto(industrialtest.SimpleElectricItemProcessor,{ industrialtest.internal.unpackTableInto(industrialtest.SimpleElectricItemProcessor,{
facedir=true, facedir=true,
@@ -51,7 +53,9 @@ function industrialtest.SimpleElectricItemProcessor.getFormspec(self,pos)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100 local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100
local srcPercent=meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100 local srcPercent=meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100
local recipeOverride=self.isRecipeOverride(meta)
local formspec={ local formspec={
(recipeOverride and "label[3.4,1.5;"..S("Recipe override: @1", minetest.registered_items[recipeOverride].description).."]" or ""),
"list[context;src;3.4,1.8;1,1]", "list[context;src;3.4,1.8;1,1]",
industrialtest.internal.getItemSlotBg(3.4,1.8,1,1), industrialtest.internal.getItemSlotBg(3.4,1.8,1,1),
(powerPercent>0 and "image[3.4,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]" (powerPercent>0 and "image[3.4,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]"
@@ -151,7 +155,8 @@ function industrialtest.SimpleElectricItemProcessor.shouldActivate(self,pos)
local output=self:getCraftResult(srcSlot) local output=self:getCraftResult(srcSlot)
return output and output.time>0 and inv:room_for_item("dst",output.item) return output and output.time>0 and inv:room_for_item("dst",output.item)
end end
return false
return meta:contains("recipeOverride") and meta:contains("recipeOverrideMaxTime") and meta:get_string("recipeOverride")~=""
end end
function industrialtest.SimpleElectricItemProcessor.shouldDeactivate(self,pos) function industrialtest.SimpleElectricItemProcessor.shouldDeactivate(self,pos)
@@ -164,6 +169,10 @@ function industrialtest.SimpleElectricItemProcessor.shouldDeactivate(self,pos)
return true return true
end end
if meta:contains("recipeOverride") and meta:contains("recipeOverrideMaxTime") and meta:get_string("recipeOverride")~="" then
return false
end
local srcSlot=inv:get_stack("src",1) local srcSlot=inv:get_stack("src",1)
if srcSlot:is_empty() then if srcSlot:is_empty() then
return true return true
@@ -179,11 +188,15 @@ function industrialtest.SimpleElectricItemProcessor.activeUpdate(self,pos,elapse
local srcTime=0 local srcTime=0
local maxSrcTime local maxSrcTime
if meta:get_float("maxSrcTime")<=0 then if meta:get_float("maxSrcTime")<=0 then
local recipeOverride=self.isRecipeOverride(meta)
if recipeOverride then
maxSrcTime=meta:get_int("recipeOverrideMaxTime")
else
local output=self:getCraftResult(srcSlot) local output=self:getCraftResult(srcSlot)
maxSrcTime=output.time*self.efficiency maxSrcTime=output.time*self.efficiency
end
meta:set_float("srcTime",0) meta:set_float("srcTime",0)
meta:set_float("maxSrcTime",maxSrcTime) meta:set_float("maxSrcTime",maxSrcTime)
shouldUpdateFormspec=true
else else
srcTime=meta:get_float("srcTime") srcTime=meta:get_float("srcTime")
maxSrcTime=meta:get_float("maxSrcTime") maxSrcTime=meta:get_float("maxSrcTime")
@@ -196,6 +209,11 @@ function industrialtest.SimpleElectricItemProcessor.activeUpdate(self,pos,elapse
meta:set_int("srcTime",srcTime) meta:set_int("srcTime",srcTime)
if srcTime>=maxSrcTime then if srcTime>=maxSrcTime then
local recipeOverride=self.isRecipeOverride(meta)
if recipeOverride then
inv:add_item("dst",ItemStack(recipeOverride))
meta:set_string("recipeOverride","")
else
local output=self:getCraftResult(srcSlot) local output=self:getCraftResult(srcSlot)
local usedItems=srcSlot:get_count()-output.src:get_count() local usedItems=srcSlot:get_count()-output.src:get_count()
local multiplier=1 local multiplier=1
@@ -206,15 +224,23 @@ function industrialtest.SimpleElectricItemProcessor.activeUpdate(self,pos,elapse
output.item:set_count(output.item:get_count()*multiplier) output.item:set_count(output.item:get_count()*multiplier)
inv:add_item("dst",output.item) inv:add_item("dst",output.item)
end end
meta:set_float("srcTime",-1)
meta:set_float("maxSrcTime",0)
srcSlot:set_count(srcSlot:get_count()-multiplier*usedItems) srcSlot:set_count(srcSlot:get_count()-multiplier*usedItems)
inv:set_stack("src",1,srcSlot) inv:set_stack("src",1,srcSlot)
end end
meta:set_float("srcTime",-1)
meta:set_float("maxSrcTime",0)
end
return true return true
end end
function industrialtest.SimpleElectricItemProcessor.isRecipeOverride(meta)
if meta:contains("recipeOverride") and meta:contains("recipeOverrideMaxTime") and meta:get_string("recipeOverride")~="" then
return meta:get_string("recipeOverride")
end
return false
end
function industrialtest.SimpleElectricItemProcessor.getCraftResult(self,itemstack) function industrialtest.SimpleElectricItemProcessor.getCraftResult(self,itemstack)
-- Dummy method -- Dummy method
end end