So, I've always wanted to create a big Bukkit plugin. But the problem is, for some reason, using multiple classes is not working! I want to have one command per class or something like that. Do I have to link them together? What do I do?
I'd suggest trying to read up on Object Oriented Programming--using objects in java is probably a good idea if you want to structure your plugin (definitely needed if it's going to be a bigger plugin; this is where all the different classes come in). I don't know a good way to explain it, but... If you wanted to keep certain attributes per player on a team, you might go about making a class to have all the player's stuff, i think code might be a better example: Code:java public class MyPlayer{ //Ideally you would do the variables first.. private String name; //Stores the name of the player; you don't want to keep a Player object here.//if you want to access the player object, you would make a call to Bukkit.getPlayer(name).methodHere();.//we want to simplify this by making a getPlayer() method later on.private int kills; //just storing a variable hereprivate int deaths; //just putting another \o/private int killstreak; //and anotherprivate int swagLevel; //and so forthprivate boolean isAbeast; //you can have different types of objects in here, not just integers or strings! //Now you need a constructor. So when you have a class you made like this one, and want to instantiate it into a//variable you can use...//MyPlayer(event.getPlayer()) would be an example.//instantiating objects, meh, you would just have MyPlayer player = MyPlayer(event.getPlayer());//or alternatively// MyPlayer player; //at the beginning of the class, like above// player = MyPlayer(event.getPlayer()); //somewhere along the line, in a different part of the class// the "player" could be accessed from anywhere in the class, then.//but on to the constructor...public MyPlayer(Player player){ //kind of like a method. When you instantiate the class like above,//this code is called//you probably want to instantiate your variables here... so here we go name = player.getName(); kills = 0; deaths = 0; killstreak = 0; swagLevel = 9000; isAbeast = swagLevel > 9000; // now we can use any of the variables from the class} //let's make a method in the class//like i mentioned earlier... getPlayer() might be helpful in this casepublic Player getPlayer(){ return Bukkit.getPlayer(name);} //another methodpublic float getKDR(){ //get the kill/death ratio return (float)kills/deaths;} //need something to access the kills/deaths, changing them...public void addKill(){ kills += 1;} public void addDeaths(){ deaths += 1;} public String getName(){ return name;} public int getKills(){ return kills;} public int getDeaths(){ return deaths;} Hopefully you get the idea of making another class. But the question can be...why? you could do all of the above with hashmaps, or lists, or something of that sort all in one class... But when we want to structure something in a way that can't be done in a low-level way, OOP seems to be the answer. Using the MyPlayer class above... Code:java public class Manager{private List<MyPlayer> members = new ArrayList<>(); public void addMember(Player player){ members.add(new MyPlayer(player));} public MyPlayer getMember(String name){ //Now...i'm not sure if there's a better way to do this? for (MyPlayer player : members){ if (player.getName().equalsIgnoreCase(name)) return player; } return null;}//you could probably put a lot more in this class...but this post is a bit long and i'm not feeling especially inclined towards//writing more than i believe to be necessary} Right, so now you need to keep an instance of the Manager class somewhere... how about in a main class? Code:java public class MyPlugin extends JavaPlugin implements Listener{ Manager manager; public void onEnable(){ this.getServer().getPluginManager().registerEvents(this,this); manager = new Manager(); } //this method isn't optimal because i'm calling manager.getMember() twice, but meh... this is just supposed to be an//example of using objects in such a way to make your life easier.@EventHandlerpublic void onSomethingRandom(PlayerInteractEvent event){ //just something random, probably wouldn't be used in this way... a player interact event :confused: if (manager.getMember(event.getPlayer().getName()) == null){ manager.addMember(event.getPlayer()); } MyPlayer player = manager.getMember(event.getPlayer()); player.addKill(); event.getPlayer().sendMessage("New KDR: "+String.valueOf(player.getKDR())); }} Anyway, that's just an idea... something i've picked up along the way, hopefully this makes sense! I'd like to say that getting a grasp on OOP would help you code in pretty much any object oriented language, and java doesn't seem to be such a bad way to learn it Good luck dude, hope this was some sort of help EDIT: It came to my attention that the code tag was really hard to read with all the comments... edited, hopefully this makes more sense now
use in your main class at the onEnable part: Code:java this.getCommand("YOUR_COMMAND").setExecutor(new [CLASS_NAME_WHICH_THE_COMMAND_IS_IN]());