Jump to content

How to make my item compatible with other mods ?


Recommended Posts

Hi,

I have an item that allows to press veggies and other stuff to obtain juice. At the moment, the code of my item is working but probably not clean at all. What i would like to do is having an easier way to add items to the list (so i can expand it more easily), something more clean, and especially, if possible, something that would allow other mods to make a product able to be pressed.

Here is the current code (i still have to change a few things here and here) :

require "prefabutil"

local assets =
{
    Asset("ANIM", "anim/press.zip"),
}

local prefabs =
{
    "raisin",
    "carrot",
    -- "berries",
    "medicinal_root",
	"alcohol",
	"succulent_picked",
	"aloevera",
	"carrot_juice",
	"root_juice",
	"collapse_small",
	--helianti
}

local function GiveItem(inst, item)
local drop= nil
    if item.prefab == "raisin" then
        drop=inst.components.lootdropper:SpawnLootPrefab("alcohol")
    end
	-- if item.prefab == "berries" then
    -- inst.components.lootdropper:SpawnLootPrefab("alcohol")
	-- end
	if item.prefab == "medicinal_root" then
	drop=inst.components.lootdropper:SpawnLootPrefab("root_juice")
	end
	if item.prefab == "carrot" then
	drop=inst.components.lootdropper:SpawnLootPrefab("carrot_juice")
	end
	if item.prefab == "succulent_picked" then
	drop=inst.components.lootdropper:SpawnLootPrefab("aloevera")
	end
	
			-- else
			-- if item.prefab == "helianti_flower" then
					-- inst.components.lootdropper:SpawnLootPrefab("helianti_oil")
			-- end
if drop then
if item.components.perishable and drop.components.perishable then
 drop.components.perishable.perishremainingtime= item.components.perishable:GetPercent()*drop.components.perishable.perishtime*1.2 end
end
end

local function OnGetItemFromPlayer(inst, giver, item)
    if item.prefab == "raisin"
		-- or item.prefab == "berries"
		or item.prefab == "carrot"
		or item.prefab == "medicinal_root"
		or item.prefab == "succulent_picked"
		-- or item.prefab == "helianti_flower"
		then
		inst.AnimState:PlayAnimation("pressing", true)
        inst:DoTaskInTime(20/30, GiveItem, item)
		inst.AnimState:PushAnimation("idle", true)
    end
end

local function OnRefuseItem(inst, giver, item)
    inst.AnimState:PushAnimation("idle", true)
end

local function CouldPress(inst, item)
    local can_accept =
		item.prefab == "raisin"
		-- or item.prefab == "berries"
		or item.prefab == "carrot"
		or item.prefab == "medicinal_root"
		or item.prefab == "succulent_picked"
		-- or item.prefab == "helianti_flower"

		
    return can_accept
end


local function OnBuilt(inst)
    inst.AnimState:PlayAnimation("pressing", true)
    inst.AnimState:PushAnimation("idle", true)
end

local function OnSave(inst, data)
end

local function OnLoad(inst, data)
    inst.AnimState:PlayAnimation("pressing", true)
	inst.AnimState:PushAnimation("idle", true)
end



local function OnHammered(inst, worker)
    -- if inst.components.burnable ~= nil and inst.components.burnable:IsBurning() then
        -- inst.components.burnable:Extinguish()
    -- end
    inst.components.lootdropper:DropLoot()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("wood")
    inst:Remove()
end

local function OnHit(inst)
    if not inst:HasTag("burnt") then
        inst.AnimState:PlayAnimation("pressing")
        inst.AnimState:PushAnimation("idle", true)
    end
end



local function pressfn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    MakeObstaclePhysics(inst, .5)	
	
    inst:AddTag("structure")
	
	inst.MiniMapEntity:SetIcon("press.tex")


    inst.AnimState:SetBank("press")
    inst.AnimState:SetBuild("press")
    inst.AnimState:PlayAnimation("idle", true)

    --trader (from trader component) added to pristine state for optimization
    inst:AddTag("trader")

    MakeSnowCoveredPristine(inst)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")
	
    inst:AddComponent("lootdropper")
        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(2)
        inst.components.workable:SetOnFinishCallback(OnHammered)
        inst.components.workable:SetOnWorkCallback(OnHit)

        -- MakeLargeBurnable(inst, nil, nil, true)
        -- MakeLargePropagator(inst)

    inst:AddComponent("trader")
	
    inst:ListenForEvent("onbuilt", OnBuilt)
        MakeSnowCovered(inst)

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad
	
	
    inst.components.trader:SetAcceptTest(CouldPress)
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader.onrefuse = OnRefuseItem
	
    return inst

end

return Prefab("press", pressfn, assets, prefabs),
MakePlacer( "common/press_placer", "press", "press", "idle" )

 

So i was wondering what would be the best solution :

- a component ? Kinda like dryable, with a setproduct, and test if the item is "pressable" (whatever the right word is) and has a product then the press gives the product ?

- a table, associating the item given and the product ? Will other mods be able to add their own item via insert table ?

 

I'm quickly lost outside of very basic modding. I never did any custom component and i'm not familiar with table. What would be the best option ? Component ? Table ? Something else entirely ? What is the easiest thing to do ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...