Modding Snippets
[edit]
Please help us build the Modding community by sharing some of your examples. Feel free to update this page via the wiki editor!
When adding code snippets to the wiki, follow this example:
<pre>
// here is some code!
System.out.println("Necesse is cool");
</pre>
Utilities[edit]
Post your Utilities examples here.
Getting All Textures - Author: Kamikaze[edit]
Put this loop into the postInit() function. run buildModJar and then runClient from the gradle tray. The resource folder will output to your project directory.
for (Map.Entry<String, GameTexture> tex : GameTexture.getLoadedTextures()) { tex.getValue().saveTextureImage("resources/" + tex.getKey()); }
Getting server object - Author: kiriharu[edit]
Server object can be obtained from ServerStartEvent. To simple access to it I set server instance to static variable.
import necesse.engine.GameEventListener; import necesse.engine.GameEvents; import necesse.engine.events.ServerStartEvent; import necesse.engine.modLoader.annotations.ModEntry; import necesse.engine.network.server.Server; @ModEntry public class TestMod { public static Server SERVER; public void init() { // Register ServerStartEvent event listener to obtain server instance GameEvents.addListener(ServerStartEvent.class, new GameEventListener<ServerStartEvent>() { @Override public void onEvent(ServerStartEvent e) { // Setting server instance to static variable SERVER, so it can be accessible everywhere in code TestMod.SERVER = e.server; } }); } } });
Get access to to death messages - Author: kiriharu[edit]
To get access to death messages you can patch DeathMessageTable.getDeathMessage().
public class DeathMessagePath { @ModMethodPatch( target = DeathMessageTable.class, name = "getDeathMessage", arguments = {Attacker.class, GameMessage.class} ) public static class MethodPatch { @Advice.OnMethodExit public static void onExit(@Advice.Return(typing = Assigner.Typing.DYNAMIC) LocalMessage returned) { // do everything what you want with death message } } }
Config - Author: kiriharu[edit]
Fair said mod framework have an official way to create config, so add initSettings method in ModEntry class like this:
public static int myInt = 0; // Setting default value to 0 public ModSettings initSettings() { return new ModSettings() { @Override public void addSaveData(SaveData saveData) { // Save data here saveData.addInt("myInt", myInt); } @Override public void applyLoadData(LoadData loadData) { // Load data here savedInt = loadData.getInt("myInt", myInt); } }; }
It will create a file in %APPDATA%/cfg/mods/modid.cfg file like this:
{ SETTINGS = { myInt = 0 } }
Getting the client from anywhere - Author: DimitarBogdanov/m4trixglitch[edit]
Sometimes, it's quite difficult to find an instance of Client (e.g. in a Control's activate method). This snippet should work, as long as the user is ingame (shocker, I know...) Returns null if the user isn't in-game. Not tested thoroughly, but it appears to work.
public static Client getClient() { State gameState = GlobalData.getCurrentState(); if (!(gameState instanceof MainGame)) { return null; } MainGame mainGame = (MainGame) GlobalData.getCurrentState(); Client client = mainGame.getClient(); return client; }
Equipment[edit]
Post your Equipment examples here.
Adding a sword - Author: Quack
To get started start by placing the texture of your sword in /resources/player/weapons/ as well as /resources/items/. Be sure that the name of the image is the same as it's itemStringID. The item icon placed in the items folder should be 32x32 but the actual texture of the sword places in weapons can be bigger.
Within your mod folder (in this example it's named examplemod) create a new folder or "Package". Within this new folder create a new Java class and name it after the weapon. In this case the weapon class is within a package called "tests" and the class itself is named TestSword.java.
The following code is all that it needed to set the properties of you sword. There are also things like attackXOffset and attackYOffset which can be set if the texture doesn't fit in the character's hand quite right.
package examplemod.tests; // Change this to the package name where you have your weapon class import necesse.inventory.item.Item; import necesse.inventory.item.toolItem.swordToolItem.SwordToolItem; // Extends SwordToolItem public class TestSword extends SwordToolItem { // Weapon attack textures are loaded from resources/player/weapons/<itemStringID> public TestSword() { super(400); rarity = Item.Rarity.COMMON; attackAnimTime.setBaseValue(300); // attack time attackDamage.setBaseValue(20) // Base sword damage .setUpgradedValue(1, 95); // Upgraded tier 1 damage attackRange.setBaseValue(60); // range knockback.setBaseValue(100); // knockback } }
In the example above the weapon class is located in /examplemod/tests/TestSword.java.
Now within the main mod java file you want to include three new lines. The first one at the very top to import the newly created weapon class:
import examplemod.tests.*; // Change this to fit your package name
Then in the init() function we need to add:
public void init() { ItemRegistry.registerItem("testsword", new TestSword(), 20, true); }
This registers the weapon in the item registry and sets it's barter value.
Lastly in the postInit() function we need to add:
public void postInit() { Recipes.registerModRecipe(new Recipe("testsword", 1, RecipeTechRegistry.NONE, // Here you define the crafting technology needed to craft new Ingredient[]{ new Ingredient("anylog", 5), new Ingredient("anystone", 5) // Here you define which ingredients are needded to craft }).showAfter("woodboat")); // Change this to which item you want it to show after in the menu }
In this example we have added the test sword as an item which is craftable everywhere for just 5 logs and 5 stones. And it shows after the wood boat item.
NPCs[edit]
Post your NPC examples here.
Mobs[edit]
Post your Mob examples here.