Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.53 KB | None | 0 0
  1. MODDING WITH APIs
  2.  
  3. In this tutorial, I will cover how to install an API in a manner that does not require you to place the entire API directly in your project directory, how to build and package your mod independently from the API so that you are not distributing other people's mods with your own, and how to utilize an API such that your mod will still function without it, but will still be able to take advantage of the extra features when it is present.
  4.  
  5. I struggled with this myself while working on my own mod, but thanks to GotoLink's guidance and extreme patience I finally got everything working, and I figure the things I learned will be greatly beneficial to anyone else looking to hook into another mod's API.
  6.  
  7. Before we start, do note that this tutorial assumes that you are already capable of writing your own mod; I will not be providing any support here for the basics of setting up a mod. Also, this tutorial assumes that you are using the latest versions of Forge, meaning that you are also using Gradle, as the vast majority of mods with APIs all use Forge.
  8.  
  9. If you would like to follow along with the tutorial step-by-step, you can download the API that I will be using here:
  10. https://github.com/coolAlias/ZeldaSwordSkills-1.6.4/releases
  11.  
  12. Step 1: Installing the API
  13.  
  14. 1. Assuming you already have your workspace set up, create a new folder named "/lib" in your project directory. That's the directory that contains your /bin, /build, /gradle, and /src folders, and now also has a /lib folder.
  15.  
  16. 2. Ask the mod author(s) for a binary distributable / source file of their mod and place it in the /lib folder you just created.
  17.  
  18. 3. In Eclipse, right-click on your project, select "Build Path" and then "Configure Build Path". Click the "Libraries" tab, then "Add External JARs" and find the binary file that you just placed in the /lib folder.
  19.  
  20. 4. If the API author provided a source file, open the "Referenced Libraries" tree in your package explorer, find the API binary that you just referenced and right-click on it; go to "Properties" -> "External location" -> "External file" and navigate to wherever you stored the source file, preferably right next to the binary in the /lib folder.
  21.  
  22. 5. Run your debug configuration and see if everything is still working; if so, great! If not, you may need to run setupDev and setupDecomp workspace one more time:
  23.  
  24.  a. gradlew setupDevWorkspace
  25.  b. gradlew setupDecompWorkspace
  26.  c. gradlew eclipse // will vary depending on your IDE
  27.  
  28. Once you have the debug client working, you should see that both your mod and the API are loaded; if you start a new game, anything added by the API mod will be in that game as well and should be fully functional. This is extremely handy while developing an addon or simply testing compatibility between mods.
  29.  
  30. You are now ready to start using the API in your mod!
  31.  
  32. NOTE: Not all APIs will be able to use this setup; core mods, for example, can be tricky to work with due to their access transformers needing to modify the vanilla jar before being usable.
  33.  
  34. Let's take Battlegear2's API as an example.
  35.  
  36. 1. For BG2, the binary distributable should be placed not in the project directory, but wherever you have designated as the working directory in a "/mods" folder. For most people using Eclipse, this will be your "projectDirectory/eclipse/mods", or if you followed Lex's multi-project workspace video, in "workspaceLocation/run/mods", though I recommend pointing your API-dependent mod's workspace at its own local eclipse directory instead of the /run directory so you don't have to perform all of the following steps for every single mod in your workspace.
  37.  
  38. 2. Then, follow steps 3, 4, and 5 above, run the debug client, start a game and try to open the inventory. The game should crash at this point with an Illegal Access Error - that's the access transformer failing.
  39.  
  40. 3. Find the "battlegear_at.cfg" file, place that in your resources directory and re-run all of the commands from step 5, then try again.
  41.  
  42. 4. If it crashes again, check in your /build directory for a "deobfuscated-bin.jar" file - this is the modified Minecraft jar - and change your library reference from "forgeSrc..." to that file instead. If you don't see that file, you may need to close Eclipse and run the commands again with "--refresh-dependencies". Note that in 1.7.2, I have never found this file and it still seems to work, but in 1.6.4 it seems to be required. Your mileage may vary.
  43.  
  44. 5. At this point it should work, but if it doesn't, make sure that you are using EXACTLY the same version of Forge for which the core mod was written, because if any of the fields it tries to modify have different names, the process will fail and you will most likely get an exception when attempting to access that field.
  45.  
  46. It can be very frustrating and time-consuming working with core mod APIs, sometimes requiring those same steps to be repeated over and over again even after you have successfully set it up once when, for example, you clean your project or otherwise re-reference the Forge Minecraft jar instead of the one modified by the access transformers.
  47.  
  48. Step 2: Implementing an API Interface
  49.  
  50. Our first order of business will be creating a new Item that can pick up blocks using Zelda Sword Skills' ILiftBlock interface. Simply create a new Item class and implement ILiftBlock. You should be able to import it right away, but you may need to explicitly tell Eclipse where to look, or fix your project setup if you forgot to add the API as a referenced library. Be sure to link the API source if you have it so you get readable names as method parameters, rather than arg0, d1, etc., and then let Eclipse add the unimplemented methods for you.
  51.  
  52. [code]
  53. public class ItemLifter extends Item implements ILiftBlock {
  54.  
  55.     public ItemLifter(int id) {
  56.         super(id);
  57.     }
  58.  
  59.     @Override
  60.     public BlockWeight getLiftStrength(EntityPlayer player, ItemStack stack, Block block, int meta) {
  61.         // TODO Auto-generated method stub
  62.         return null;
  63.     }
  64.  
  65.     @Override
  66.     public ItemStack onLiftBlock(EntityPlayer player, ItemStack stack, Block block, int meta) {
  67.         // TODO Auto-generated method stub
  68.         return null;
  69.     }
  70. }
  71. [/code]
  72. I'll let you handle the basics of the Item and focus on the API methods. There are just two methods to implement for this interface, and the first one requires another API class, BlockWeight, which should already have been imported. Just type in BlockWeight followed by a period to see a list of options; we'll choose "EXTREME_II" just for fun, even though there aren't any blocks in that category (yet).
  73.  
  74. The second method asks us to return an ItemStack, and the java-docs explain that this is the stack that will be returned to the player when the block is placed. We want to get our item back, so we will return "stack" instead of "null", but first we will damage the stack by 1 so that it will eventually break. Now our methods look like this:
  75.  
  76. [code]
  77. @Override
  78. public BlockWeight getLiftStrength(EntityPlayer player, ItemStack stack, Block block, int meta) {
  79. return BlockWeight.EXTREME_II;
  80. }
  81.  
  82. @Override
  83. public ItemStack onLiftBlock(EntityPlayer player, ItemStack stack, Block block, int meta) {
  84. stack.damageItem(1, player);
  85. return stack;
  86. }
  87. [/code]
  88.  
  89. Go ahead and start up the client in Eclipse; you should have an item that picks up any solid block that you right-click on, and returns the same item but slightly damaged when you place the block down.
  90.  
  91. Now, if you were to build the mod and attempt to play it in Minecraft without Zelda Sword Skills installed, your game will crash with a No Class Definition Found error, since we are trying to access several API classes that are not present. So, before we build the mod, we will first add some code that will strip the API interface and methods if the required mod classes are not present.
  92.  
  93. Step 3: Stripping Interfaces
  94.  
  95. Luckily for us, FML is capable of stripping interfaces by using cpw's awesome @Optional.Interface annotation.
  96.  
  97. There are 3 fields we need to fill out: iface, modid, and striprefs:
  98.  
  99. 1. "iface" is the complete package path of the interface we wish to strip, so it is best to simply copy the import path and paste that in to avoid any typos. If you get the path incorrect, it will not work.
  100.  
  101. 2. "modid" is obviously the modid of the mod that owns the interface to strip. Again, be sure to spell it correctly.
  102.  
  103. 3. "striprefs" set this to true to strip interface and method references that are not found. I don't know why anyone would ever set this to false, but I'm sure there are uses for that as well.
  104.  
  105. Add the annotation above the class declaration:
  106. [code]
  107. @Optional.Interface(iface="zeldaswordskills.api.item.ILiftBlock", modid="zeldaswordskills", striprefs=true)
  108. public class ItemLifter extends Item implements ILiftBlock {
  109. [/code]
  110.  
  111. Make sure you import "cpw.mods.fml.common.Optional" and not the google Optional class.
  112.  
  113. One last thing we should do is strip the API methods from the class, though the mod will probably still work just fine even if you do not. It's just one simple line: @Method(modid="apiModId"), and you absolutely should use it if the API method in question is not part of an interface that you are stripping.
  114.  
  115. [code]
  116. // put this same line above all API methods:
  117. @Method(modid="zeldaswordskills")
  118. @Override
  119. public BlockWeight getLiftStrength(EntityPlayer player, ItemStack stack, Block block, int meta) {
  120. return BlockWeight.EXTREME_II;
  121. }
  122. [/code]
  123.  
  124. Step 4: Load Order
  125.  
  126. Once you have your API-utilizing mod all working in the debug environment, there are a few things that should be done before building and packaging the final product. The first is to make sure your mod will load after the mod whose API you are using; this is done in the mcmod.info file. Let's take a look at some of the available fields:
  127.  
  128. 1. "requiredMods": [ "Forge", "someOtherMod" ],
  129. Any mods you list here will be required for your mod to load; your mod cannot load without them. Since we want our mod to be functional even if the other mod is not present, we will not be using this field here, but it is very useful if your mod requires some other mod's functionality in order to be usable.
  130.  
  131. 2. "dependencies": [ "zeldaswordskills" ],
  132. Any mods that your mod is dependent upon will be loaded before yours; this is very important if you need to know that a mod is present or not during the pre-initialization stages, and is generally a good idea to put any mod that your mod might rely upon as a dependency, even if you do not need to do anything with it in the early stages of mod loading.
  133.  
  134. 3. "dependants": [ "ModSubmodule" ]
  135. Any mods listed here are mods that depend upon and will be loaded after your mod; useful if you make a submodule of your own mod. Note that the submodule should list the parent mod as a dependency and possibly required mod.
  136.  
  137. 4. "useDependencyInformation": "true"
  138. You MUST set this to "true" or the above three fields will be meaningless as far as mod order is concerned. If you omit this field, chances are someone will crash when your mod is installed alongside the dependency, even if you don't. This is because if our mod loads before the api, when we try to access the api methods, they will not yet exist.
  139.  
  140. You can learn all about how FML uses the mcmod.info file and other things that you can do with it on the wiki:
  141. https://github.com/MinecraftForge/FML/wiki/FML-mod-information-file
  142.  
  143. For this tutorial, we will only add the "dependencies" and "useDependencyInformation" lines, ensuring that the API we want to use is loaded first if present.
  144. [code]
  145. "dependencies": ["zeldaswordskills"],
  146. "useDependencyInformation": "true"
  147. [/code]
  148.  
  149. For many APIs, assigning dependencies in mcmod.info should be enough to guarantee load order. This is the case for any API that does not require access during the pre-initialization stages of a mod, for example if you were using a structure generation or mob animation API. Before using any methods or classes from such APIs, it would be sufficient simply to check if the mod is loaded:
  150.  
  151. [code]
  152. if (Loader.isModLoaded("api_modid")) {
  153. // do something with the API
  154. }
  155.  
  156. // If I need to access this frequently, I typically store the mod loaded state during pre-initialization:
  157. public static boolean isWhateverApiLoaded;
  158.  
  159. @EventHandler
  160. public void preInit(FMLPreInitializationEvent event) {
  161. isWhateverApiLoaded = Loader.isModLoaded("whateverAPI");
  162. // note that this only means that the mod has been recognized, not necessarily that it is fully
  163. // initialized and ready to go yet; you should NOT try to use anything to do with the API
  164. // until the FMLInitializationEvent at the earliest, even with dependency information sorting
  165. // the load order, just to be safe
  166. }
  167. [/code]
  168.  
  169. However, since we require the API to be loaded in time for our new Item, we need access to a fully-functional API during our mod's pre-initialization event. The only way to load an API prior to pre-init is if the author included API markers for each of the API packages:
  170. [code]
  171. // filename is "package-info.java" for every one of these files
  172. @API(owner = "zeldaswordskills", provides = "ZeldaAPI", apiVersion = "0.1")
  173. package zeldaswordskills.api;
  174.  
  175. import cpw.mods.fml.common.API;
  176. [/code]
  177. There should be one of these files in every package that provides API features; if not, you will need to contact the API author and ask them to provide these files, or your mod is quite likely to crash if not for you, then for the majority of people using your mod. Remember, these markers are ONLY needed if you require the API during pre-initialization, such as for implementing specific interfaces in your Items or Blocks, so don't trouble the author if the API does not truly require them.
  178.  
  179. Step 5: Building the Mod
  180.  
  181. The final step is of course building the mod. Since our mod is dependent upon an API, we need to let the compiler know where to find that code during the build process or we will get lots of Class Not Found and similar errors.
  182.  
  183. To do so, simply add any dependencies to a "compile files()" method in the build.gradle file, using the full path relative to your project directory and separating each file path with a comma. Note that you can use "../" to move up a folder if, for example, you have a dependency in the working directory instead of the project directory.
  184.  
  185. "lib/zeldaswordskills-1.6.4-0.6.3.jar" -> located in /workingDirectory/projectDirectory/lib/
  186.  
  187. "../run/mods/zeldaswordskills-1.6.4-0.6.3.jar" -> located in /workingDirectory/run/mods/
  188.  
  189. [code]
  190. version = "1.0"
  191. group= "com.google.coolalias008.modwithapi"
  192. archivesBaseName = "modwithapi"
  193.  
  194. dependencies {
  195.     compile files (
  196.         "lib/zeldaswordskills-1.6.4-0.6.3.jar"
  197.     )
  198. }
  199. [/code]
  200.  
  201. Once the build file is saved, open up a command console and run "gradlew build"; it should compile with no errors, but if it doesn't, double-check the file paths and make sure nothing is misspelled.
  202.  
  203. Time to load it up in Minecraft and give it a go! Try first with just your mod alone and make sure that is working, then exit the Minecraft launcher completely (just to be safe), add the API-providing mod to your /mods folder, and launch once more. If you followed the tutorial and are using my ZeldaAPI, you should now have an item that can pick up any solid block, just by implementing a single interface! Pretty awesome.
  204.  
  205. Step 6: Multiple Interfaces
  206.  
  207. Alright, you can do a single interface, but what about when you want to implement several API interfaces in a single item, block, or other class? You cannot simply stack @Optionals on top of each other, but you can use an InterfaceList:
  208. [code]
  209. @Optional.InterfaceList(value={
  210.     @Optional.Interface(iface="zeldaswordskills.api.block.ILiftable", modid="zeldaswordskills", striprefs=true),
  211.     @Optional.Interface(iface="zeldaswordskills.api.block.ISmashable", modid="zeldaswordskills", striprefs=true)
  212. })
  213. [/code]
  214. The individual @Optional.Interfaces are exactly the same as before, but separtated by commas and nested inside of an array, all enclosed by the @Optional.InterfaceList annotation.
  215.  
  216. Alright, so let's make a block that is both liftable AND smashable by implementing ILiftable and ISmashable. Import those two classes and let Eclipse add the unimplemented methods for you, and be sure to set up the rest of the Block class (texture, creative tab, etc.). I will only cover the API methods here.
  217. [code]
  218. @Method(modid="zeldaswordskills")
  219. @Override
  220. public BlockWeight getSmashWeight(EntityPlayer player, ItemStack stack, int meta) {
  221. // let's make our block very easy to smash, since we do not have any smashing items yet, the only
  222. // way to smash our block would be using one of the Zelda Hammers
  223. return BlockWeight.VERY_LIGHT;
  224. }
  225.  
  226. @Method(modid="zeldaswordskills")
  227. @Override
  228. public Result onSmashed(World world, EntityPlayer player, ItemStack stack, int x, int y, int z, int side) {
  229. // for the sake of simplicity, we will just use the default smashing mechanics
  230. return Result.DEFAULT;
  231. }
  232.  
  233. @Method(modid="zeldaswordskills")
  234. @Override
  235. public BlockWeight getLiftWeight(EntityPlayer player, ItemStack stack, int meta) {
  236. // we want our block to be extremely difficult to lift, giving our custom itemLifter a purpose;
  237. // not even any of the items in Zelda can lift our block! mwa ha ha!
  238. return BlockWeight.EXTREME_II;
  239. }
  240.  
  241. @Method(modid="zeldaswordskills")
  242. @Override
  243. public void onLifted(World world, EntityPlayer player, ItemStack stack, int x, int y, int z, int meta) {
  244. // if you need to handle a tile entity or do anything else before the block is lifted,
  245. // you can do so here; however, we do not need to for our simple block
  246. }
  247.  
  248. @Method(modid="zeldaswordskills")
  249. @Override
  250. public void onHeldBlockPlaced(World world, ItemStack stack, int x, int y, int z, int meta) {
  251. // if you want to do something special when the block is placed, you can do so here,
  252. // but we will leave it empty for this tutorial
  253. }
  254. [/code]
  255. That's it for the API methods. Once the rest of the block is set up and registered, you can go ahead and give it a try! You should have a block that can only be lifted with the special itemLifter that we made earlier, but can be smashed even with just the wooden hammer from Zelda. If you run your mod by itself, you will still have the item and block in the game, but without the lifting and smashing mechanics provided by the API. This is great if your items and blocks have other functions that still make them useful independently.
  256.  
  257. You should now be able to work with pretty much any mod API in a manner that will still allow your mod to function independently should the API-providing mod not be present, as well as avoid including the API code in your mod directly. Good luck! Don't forget to give GotoLink kudos if you see him around - he likes to hang out over on MinecraftForge forums, so go bump up his karma from time to time!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement