drok0920 Posted July 27, 2014 Posted July 27, 2014 Hello, I would like to make an api for my medieval/battle themed mod i'm working on. But i'm not sure how to make an api and can i use the api in my own mod? In the api i think i know what im doing but it gives an error. I have this method in my api: public void registerElemental(IBFMEntityElemental elemental) { } and it gives me this error: Illegal modifier for the interface method registerElemental; only public & abstract are permitted I have folowed the Creating an api topic but it gives me an error. Please help. Thank you, Drok Quote
Casual Dutchman Posted July 27, 2014 Posted July 27, 2014 When making an API, you MUST use it in your own mod! Your mod need to refer to the API methodes. so when other modders are using your API, your mod will also refer to their's Quote Coding, Testing, Smiling, Publishing!
drok0920 Posted July 27, 2014 Author Posted July 27, 2014 By the "can i use it in my mod" statement all i mean was when I want to make a mob based on the api i make can i actually implement IElemental or do i have to do it a different way? Quote
jabelar Posted July 27, 2014 Posted July 27, 2014 By the "can i use it in my mod" statement all i mean was when I want to make a mob based on the api i make can i actually implement IElemental or do i have to do it a different way? It is your code, so you can do what you want. I think it is good practice that if you are creating an API for others to use, that you also use it within your mod(s) because this helps ensure that it is correctly implemented and behaves identically to your own mod. Note that an API doesn't have to be a Java Interface. Even though the "I" in API means interface, interface is just a generic term regarding accessing code from other code. An API just means that you are presenting some public methods in an un-obfuscated (and ideally well documented) way so that other code can call it. From the error, it seems that you're trying to implement a Java interface. That is fine (an API can include Java interfaces). Can you post the whole code for your interface class? Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
drok0920 Posted July 27, 2014 Author Posted July 27, 2014 Oh that is why i did make it an actual interface so i should make it a class? and because you asked here is what i have in there so far: http://pastebin.com/AbKv4PA8 Quote
shieldbug1 Posted July 27, 2014 Posted July 27, 2014 This is pretty basic java. An interface method cannot have a body. Quote BEFORE ASKING FOR HELP READ THE EAQ! I'll help if I can. Apologies if I do something obviously stupid. If you don't know basic Java yet, go and follow these tutorials.
jabelar Posted July 27, 2014 Posted July 27, 2014 This is pretty basic java. An interface method cannot have a body. His code doesn't have a body. That isn't the problem. I think the problem is that for interfaces you don't need to declare scope -- it is automatically public. Secondly, you can't use static methods in interfaces in Java. So I think you should delete all the public scoping and static declarations. Instead something like: interface IBFMEntityElemental { void registerElemental(IBFMEntityElemental elemental) { } } Anyway, back to your earlier questions, how you develop your API depends on what you want to allow people to do with it. For example, a Java interface is useful for categorizing (like using instanceof to confirm that some class implements it). So imagine that you want to allow other modders to indicate that their entities should be treated specially by code in your mod. You can then provide an API for a Java interface called something like ISpecialEntity, and then in your code you can check for instanceof ISpecialEntity to find those that others have added the interface to. Alternatively, maybe you want to make sure you can get information about other modders' classes. For example maybe you have a werefwolf mod and you want other modders to make other were creatures and you will need to know whether they are in "were-form". Then you could make an API for a Java interface called something like IWerewolf that has a method called inWereForm(). The other modders that decide to extend IWerewolf would be reminded to implement the inWereForm() method so then your mod would be able to check them for the were state. However, an API can also contain classes and regular methods. Don't get confused and think that an API has to be Java interfaces only. For example, if you have a method in your mod called spawnCoolEntity() and you want to allow others to spawn them in their code then you could make that method an API. So it really depends if your API is proving access to functionality in your mod, or instead you're asking others to implement functionality that you can call on. What exactly are you making your API for? I think that in your case that registering an elemental would be an actual class method -- not an interface -- because the code for registering would be written by you. Other mods could call your registration method. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
Kwibble Posted July 28, 2014 Posted July 28, 2014 No, shieldbug1 is correct. The method body is the { and } When making an interface, the methods are declared as: public interface SomeInterface { void someMethod(int par1, String par2String, Object par3Object); } And then we can get onto the actual topic of API's themselves. Anything that is a public/public static method can be called an API. The definition of what is an API in one's code... It's a very obscure line. Quote We all stuff up sometimes... But I seem to be at the bottom of that pot.
drok0920 Posted July 28, 2014 Author Posted July 28, 2014 Ok i think i understand now i will post another reply if i still need help? But i do still have one question. Do i have to only use Interfaces for an api or can i also use classes? Quote
shieldbug1 Posted July 28, 2014 Posted July 28, 2014 You use whatever you want to use. There is a lot of debate about what is considered an 'API'. Generally, an API in this case would be an interface that you handle yourself, but it is not limited to it. Example: You have an item that transforms blocks into other blocks. You create an interface called something like ITransformable. Inside this interface you'd have methods like resultBlock() and resultMetadata(), that have return types of Block and int. Other things could include void methods like onTransform() and postTransform(). Then if you or anyone else would like your block to be transformable you make it implement ITransformable, and return the appropriate block and metadata. In your item class you'd check if the block you're clicking is an instance of ITransformable. If it is, you then call onTransform() so you can let the modder do their own functionality, then transform the block with world.setBlock(transformable.resultBlock(), x, y, z) and metadata with world.setBlockMetadata(transformable.resultMetadata(), x, y, z), then call transformable.postTransform() to allow the modder to do any finishing up they want. You want to think an interface out as much as you can, because changing an interface is looked down upon a lot. Quote BEFORE ASKING FOR HELP READ THE EAQ! I'll help if I can. Apologies if I do something obviously stupid. If you don't know basic Java yet, go and follow these tutorials.
drok0920 Posted July 28, 2014 Author Posted July 28, 2014 ok i got it now but lets say this is an entity can you give some examples for an api for that? Quote
jabelar Posted July 28, 2014 Posted July 28, 2014 ok i got it now but lets say this is an entity can you give some examples for an api for that? You should first decide what you want other mods to be able to do, then figure out what API you need to support that. For example, do you want them to be able extend your entity class? In that case you could go as far as putting the the whole entity class into the API. Do you want them to just be able to control the custom functionality of your entity in some ways? Then make those special methods in your entity class an API (i.e. create another API class that calls the methods from your mod class). Do you want to have them create entities that you can interact with? then make a Java interface that you want them to implement. It all depends on what you want the interaction between the mods to be. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
drok0920 Posted July 28, 2014 Author Posted July 28, 2014 Ok thank you jabelar that helped alot so i now know that i need to put actual classes in the api Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.