Jump to content

Recommended Posts

Hey, so...
I need help with a character who should gain some sanity when he plants seeds.
Previously I used this code in modmain, but it seems that after the Reap What You Sow update, it stopped working for seeds because of the new farm system.
The rest like bushes and pinecones still work.
But it doesn't work when planting seeds in farm soils.

I have already searched in many places and even in the wormwood codes and nothing has worked for me
Some help please.

local function SanityOnDeploy(inst)
    if inst.components.deployable then
        local oldondeploy = inst.components.deployable.ondeploy
        inst.components.deployable.ondeploy = function(inst, pt, deployer)
        if oldondeploy then oldondeploy(inst, pt, deployer) end
            if deployer.prefab == "rossa" then
                deployer.components.sanity:DoDelta(3)
            end
        end
    end
end
AddPrefabPostInit("pinecone", SanityOnDeploy)
AddPrefabPostInit("twiggy_nut", SanityOnDeploy)
AddPrefabPostInit("acorn", SanityOnDeploy)
AddPrefabPostInit("dug_grass", SanityOnDeploy)
AddPrefabPostInit("dug_sapling", SanityOnDeploy)
AddPrefabPostInit("dug_berrybush", SanityOnDeploy)
AddPrefabPostInit("dug_berrybush2", SanityOnDeploy)
AddPrefabPostInit("butterfly", SanityOnDeploy)
AddPrefabPostInit("berrybush_juicy", SanityOnDeploy)
AddPrefabPostInit("marsh_bush", SanityOnDeploy)
AddPrefabPostInit("dug_rock_avocado_bush", SanityOnDeploy)
AddPrefabPostInit("dug_sapling_moon", SanityOnDeploy)
AddPrefabPostInit("seeds", SanityOnDeploy)
AddPrefabPostInit("asparagus_seeds", SanityOnDeploy)
AddPrefabPostInit("carrot_seeds", SanityOnDeploy)
AddPrefabPostInit("corn_seeds", SanityOnDeploy)
AddPrefabPostInit("dragonfruit_seeds", SanityOnDeploy)
AddPrefabPostInit("durian_seeds", SanityOnDeploy)
AddPrefabPostInit("eggplant_seeds", SanityOnDeploy)
AddPrefabPostInit("garlic_seeds", SanityOnDeploy)
AddPrefabPostInit("onion_seeds", SanityOnDeploy)
AddPrefabPostInit("pepper_seeds", SanityOnDeploy)
AddPrefabPostInit("pomegranate_seeds", SanityOnDeploy)
AddPrefabPostInit("potato_seeds", SanityOnDeploy)
AddPrefabPostInit("pumpkin_seeds", SanityOnDeploy)
AddPrefabPostInit("tomato_seeds", SanityOnDeploy)
AddPrefabPostInit("watermelon_seeds", SanityOnDeploy)

 

The Deployable component pushes an event to TheWorld if specfiically a PLANT is planted, with info about who planted it. We can use this to elegantly grant sanity (or other effects) to the planters. Info about the event itself and when it gets pushed can be found in a few places, namely deployable.lua and farmplantable.lua.

Anyway, since we have access to this event, we can listen for it on specific characters using an AddPlayerPostInit() in modmain. Here's an example I threw together that should work for your needs. Make sure to change the tag to one only your character has, or change it to detect your character in some way, like a prefab name.

AddPlayerPostInit(function(inst)
	-- This will run on any characters that have the tag below, whenever they plant something in a plot
	-- "world" is needed as the first parameter due to listening from TheWorld.
	local function OnPlanted(world, data)
		-- Grant sanity, or whatever else, to the planter here
		if data.doer.components.sanity then
			data.doer.components.sanity:DoDelta(3)
		end
	end

	if inst:HasTag("MY_TAG") then -- Make sure to set this tag to your character's tag
		GLOBAL.TheWorld:ListenForEvent("itemplanted", OnPlanted) -- Pushed in FarmPlantable.lua, Deployable.lua, and more.
	end
end)

That should work for you.

If you wanted to put this in your character's prefab script instead of modmain, that can also be done with some adjustment. We'd have to listen for the event in master_postinit() instead of inside an AddPlayerPostinit(), and we wouldn't have to filter by tag since we know 100% this will only run on our character. Other than that, we could reuse the same OnPlanted() function as I put above.

In master_postinit()

TheWorld:ListenForEvent("itemplanted", OnPlanted)

and somewhere above master_postinit()

local function OnPlanted(world, data)
	-- Grant sanity, or whatever else, to the planter here
	if data.doer.components.sanity then
		data.doer.components.sanity:DoDelta(3)
	end
end

I would only do this though if you're overly bothered about having your modmain filled up with stuff that could be somewhere else, like I always am. Either solution will perform just fine. Good luck!

Edited by TheSkylarr

Oh man, I love you. It works 100%, Thank you.
Another thing if you could, how can I ban a crafting recipe of my character?
I need to ban the Construction Amulet, since it already has the ability to craft half the resources, but when putting the amulet on and taking it off, the discount disappears from the character and he has to use the full resources until he re-enters the server.
That's why I want to remove the amulet from his crafting menu, or at least ban him from using it.

Edited by Yoyo_Dodo
40 minutes ago, Yoyo_Dodo said:

Oh man, I love you. It works 100%, Thank you.
Another thing if you could, how can I ban a crafting recipe of my character?
I need to ban the Construction Amulet, since it already has the ability to craft half the resources, but when putting the amulet on and taking it off, the discount disappears from the character and he has to use the full resources until he re-enters the server.
That's why I want to remove the amulet from his crafting menu, or at least ban him from using it.

I would just make the amulet have no effect while equipped, and fix the bug, that way it can still be crafted when needed by your character, even if they aren't going to be using it.

Can I see the code you're using to make them have the crafting discount inherently?

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
×
  • Create New...