Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ollama run deepseek-coder-v2
- >>> Make a Minetest Lua mod that adds custom metadata to an itemstack of a new tool called customsword that has the powe
- ... r of a diamond sword, but the custom property stunchance=0.2 and stuntime=.8 and that metadata is preserved when the
- ... itemstack is moved from the ground to an inventory slot and vise versa. The 'Stun 2' label is applied after the cap
- ... tion automatically, depending on the metadata stunchance multiplied by 10
- To create a Minetest Lua mod that adds a custom tool called `customsword` with specific metadata properties and
- ensures that this metadata is preserved across inventory slots, you can follow these steps. This example assumes
- you have basic knowledge of Lua and how to set up a Minetest mod.
- ### Step 1: Create the Mod File Structure
- First, create a new folder for your mod, say `customsword_mod`. Inside this folder, create several files as
- follows:
- - `init.lua` - This is the main file where you define your tool and its metadata.
- - `functions.lua` - Additional functions can be placed here if needed.
- - `item.conf` - Configuration for the item properties.
- ### Step 2: Write the Main Mod File (`init.lua`)
- In `init.lua`, write the Lua code to define your custom sword and set its metadata correctly.
- ```lua
- -- Define the mod and its version
- local S = minetest.get_translator("customsword_mod")
- -- Register a new tool called "customsword" with diamond sword's power
- minetest.register_tool("customsword_mod:customsword", {
- description = S("Custom Sword"),
- inventory_image = "default_diamondsword.png", -- Use the texture of a diamond sword
- tool_capabilities = {
- damage_groups = {fleshy=7}, -- Power similar to a diamond sword
- },
- })
- -- Define how metadata is handled for the customsword
- local function handle_metadata(pos, oldnode, node, player)
- if not minetest.get_item_group(oldnode.name, "customsword") == 0 then
- local item = ItemStack("customsword_mod:customsword")
- item:set_meta({stunchance=0.2, stuntime=0.8})
- minetest.add_item(pos, item)
- end
- end
- -- Event handler for when the item is picked up or placed on the ground
- minetest.register_on_placeplayer(function(itemstack, placer, pointed_thing)
- local meta = itemstack:get_meta()
- if not meta then return end
- local stunchance = meta:get("stunchance") or 0
- local stuntime = meta:get("stuntime") or 0
- placer:set_burneable(true)
- -- Apply "Stun 2" effect based on stunchance
- if placer and placer:is_player() then
- local effect_name = "customsword_mod:stun_effect_" .. math.ceil(stunchance * 10)
- placer:set_hp(placer:get_hp() - (stuntime * 2)) -- Adjust damage based on stuntime
- minetest.add_particlespawner({
- amount = 50,
- time = 1,
- minpos = {x=pointed_thing.under.x, y=pointed_thing.under.y+1, z=pointed_thing.under.z},
- maxpos = {x=pointed_thing.above.x, y=pointed_thing.above.y+1, z=pointed_thing.above.z},
- minvel = {x=-0.5, y=0.5, z=-0.5},
- maxvel = {x=0.5, y=0.5, z=0.5},
- minacc = {x=0, y=-9.81, z=0},
- maxacc = {x=0, y=-9.81, z=0},
- minexptime = 0.5,
- maxexptime = 1,
- minsize = 1,
- maxsize = 2,
- collisiondetection = true,
- vertical = false,
- texture = "default_particle.png"
- })
- end
- end)
- ```
- ### Step 3: Define Metadata Handling in `functions.lua` (Optional)
- For more complex interactions or additional functions, you might want to define them in a separate file
- (`functions.lua`). For now, this example focuses on the main script.
- ### Step 4: Configure Item Properties in `item.conf`
- In `item.conf`, configure your tool's properties and how it interacts with other mods or nodes. Since you are
- defining a new tool here, no additional configuration might be needed if the mod itself covers everything.
- ### Step 5: Testing Your Mod
- To test your mod, place the folder in your Minetest `mods` directory and start Minetest. If all is set up
- correctly, you should find your customsword among the default tools available for crafting or use.
- This script provides a basic framework to create a custom sword with specific properties that are preserved across
- inventory slots and can apply a "Stun 2" effect based on its metadata values. Adjustments might be needed based on
- how other aspects of Minetest (like effects, damage calculation) handle these settings.
Add Comment
Please, Sign In to add comment