Modding:Advanced Items: Difference between revisions
From Vintage Story Wiki
Ninjanomnom (talk | contribs) m (Double checked information and updated version) |
m (Major changes to tutorial explanations and updated game version.) |
||
Line 1: | Line 1: | ||
__FORCETOC__ | __FORCETOC__ | ||
{{GameVersion|1. | {{GameVersion|1.19.3}} | ||
This ''code mod'' tutorial requires a development environment. If you do not have one, please read and complete the [[Setting up your Development Environment]] page. It is also highly recommended to have read and completed the [[Basic Item]] tutorial. | |||
= Tunnel | = Creating a Tunnel Pickaxe = | ||
In this tutorial will we create a item with more advanced functionality: A pickaxe which allows you to dig a 3x3 tunnel by mining just one block. | |||
== Item Assets == | == Item Assets == | ||
Similar to the [[Basic Item]], we need to create our item's assets, including an itemtype, texture and a lang file. Those assets are pretty straight forward and you can download them [https://wiki.vintagestory.at/images/c/cd/Tunnler_-_No_CS_File.zip here]. Extract the file to your mods folder, and you are ready to start programming. | |||
There is only one new property in your json | There is only one new property in your itemtype json, <code>class</code>. This property tells our new item to be controlled by a certain C# class. | ||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
class: "tunnler", | class: "tunnler", | ||
</syntaxhighlight> | </syntaxhighlight> | ||
We will create this class | We will create this class to give the item the desired functionality. | ||
== The Item Class == | == The Item Class == | ||
Creating our item requires a couple new <code>*.cs</code> files in our project. | |||
If you have read the [[Advanced Blocks]] Tutorial already, this should be familar to you. | If you have read the [[Advanced Blocks]] Tutorial already, this should be familar to you. | ||
In order to register your item class, we need to create a mod, which is | === The Mod System === | ||
In order to register your item class, we need to create a mod, which is a class extending ModSystem: | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 32: | Line 35: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
By overriding the <code>Start(ICoreAPI)</code> method, we can register our class. | By overriding the <code>Start(ICoreAPI)</code> method, we can register our class. The <code>RegisterItemClass</code> function has two parameters: the first is our item class ID, noteably this is how we link to this class in our itemtype json files. Ensure that this is identical to the class we specified in our earlier asset file. The second parameter is the type of our item class. | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 49: | Line 52: | ||
=== The Item Class === | === The Item Class === | ||
When naming item scripts, it is recommended to name them in the format "{Name}Item". In the case of the tunnler pickaxe, we shall name our script <code>TunnlerItem.cs</code>. Any itemclass has to extend Item, giving it the functionality we need to access: | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 60: | Line 63: | ||
This should solve all syntax errors. | This should solve all syntax errors. | ||
We need to | '''So what should our tool do?''' Once the player mines a block with this tool every block around it should be mined as well. | ||
As always, we can refer to the [https://apidocs.vintagestory.at/api/Vintagestory.API.Common.Item.html#methods item api docs] to find a function we can use. Although the item class itself does not contain an appropriate function, we can also check the [https://apidocs.vintagestory.at/api/Vintagestory.API.Common.CollectibleObject.html CollectibleObject api docs], which the item class extends from. | |||
In our specific case, we can override the method <code>bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel, float dropQuantityMultiplier = 1)</code>. | |||
We need to be aware of the facing (which side the player is focusing) and if the player is in creative or survival mode (whether items should be dropped or not). Before we are going to override <code>OnBlockBrokenWith</code> we should create a method which destroys all blocks between two block positions (min & max). It should also only drop items if the player is in survival mode: | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public void | public void DestroyBlocks(IWorldAccessor world, BlockPos min, BlockPos max, IPlayer player) | ||
{ | { | ||
BlockPos tempPos = new BlockPos(); | BlockPos tempPos = new BlockPos(); | ||
Line 87: | Line 95: | ||
Now we can implement <code>OnBlockBroken</code> rather easily, by taken care of every possible axis the player could face: | Now we can implement <code>OnBlockBroken</code> rather easily, by taken care of every possible axis the player could face: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel) | public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel, float dropQuantityMultiplier = 1) | ||
{ | { | ||
if (base.OnBlockBrokenWith(world, byEntity, itemslot, blockSel)) | if (base.OnBlockBrokenWith(world, byEntity, itemslot, blockSel)) | ||
Line 112: | Line 120: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
---- | ---- | ||
Line 138: | Line 145: | ||
{ | { | ||
public void | public void DestroyBlocks(IWorldAccessor world, BlockPos min, BlockPos max, IPlayer player) | ||
{ | { | ||
BlockPos tempPos = new BlockPos(); | BlockPos tempPos = new BlockPos(); | ||
Line 157: | Line 164: | ||
} | } | ||
public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel) | public override bool OnBlockBrokenWith(IWorldAccessor world, Entity byEntity, ItemSlot itemslot, BlockSelection blockSel, float dropQuantityMultiplier = 1) | ||
{ | { | ||
if (base.OnBlockBrokenWith(world, byEntity, itemslot, blockSel)) | if (base.OnBlockBrokenWith(world, byEntity, itemslot, blockSel)) | ||
Line 197: | Line 204: | ||
== Distribution == | == Distribution == | ||
=== Using the new Mod Template === | |||
If using the mod template setup, follow the instructions on [[Modding:Setting up your Development Environment#Packaging%20the%20Mod|Setting up your Development Environment]] to pack and distribute your mod. | |||
=== Using the (old) Modtools === | |||
If using the modtools program, open the modtools and type in <code>pack <your mod id></code>. Now you can take the zip file and share it with other people. It will work in the same way as ordinary mods, you can install it by copying it into the <code>mods</code> folder. | |||
= Mod Download = | = Mod Download = | ||