Hi Everyone, All this forum is about, is code structure. How do you plan your plugin? What sections do you separate classes into? I continually find myself creating empty packages, or classes that can't reference other classes they are dependent on... Any discussion of this topic is welcome!
When I start a plugin, I normally do these few things (if it's a fairly large plugin): [1] List all of the features/commands/permissions which the plugin will have and do [2] List all of the classes which will carry out what action [3] Write pseudo (fake) code for all of the complicated methods.
FlareLine I tend to have the "Main" class inside its' own package then everything else I put into its own folder inside the mains' package. For example, all the event handler would go in a "handlers" folder and all extraneous methods go into a "methods" folder and, well I think you get the point
I see. How do you organize your references? Do you create a constructor in every class and reference back to instances of others, or do you simply instantiate them again?
In terms of planning, I write a lot of the methods I'll need regularly into an API before I create event listeners, commands, etc. I take quite a lot of time over this because I don't want to have to go back and re-write most of it later on because I hadn't accounted for certain things. I've made that mistake before.. Usually my main package only contains the main class. I then put classes that do similar things into groups, like this: http://gyazo.com/01b5fd36f1b4cc708a81fb59b16474e5 I create a new class for every command, event listener custom inventory, timer, etc Sometimes I even have sub-packages in my API if I've got a load of related classes, like this: http://gyazo.com/52e3c24da0302b8355174fd8e074c97f
FlareLine It depends really.. If it's a class that has other useful methods inside, I will generally instance it but, if it's just a one time method call, i'll just create a new instance there and then..
lukewizzy This is very helpful to me. I never previously thought to throw all my utilities into an API package. Cheers!
You're welcome Also it's not a major thing but I forgot to mention it, I generally have a config data file with variables from the config in a class (set in the onEnable) so I don't have to keep referencing the config directly And sometimes I'll make a class that just holds itemstacks so they're all in the same place if I want to change them, so if I use the same item in multiple places or can't find it quickly then I can easily edit it