Joining Classes?

Discussion in 'Plugin Development' started by BurnerDiamond, Oct 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    BurnerDiamond

    I basically want to make a plugin like essentials with multiple commands

    Is there anyway I can join different classes so that they all work in one plugin

    Sort of like the heal and test plugin link to the main plugin?

    This is the main and at the bottom are the test and heal

    Code:java
    1. package me.burnerdiamond.plugin;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.plugin.PluginDescriptionFile;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin {
    9. public final Logger logger = Logger.getLogger("Minecraft");
    10. public static Main plugin;
    11.  
    12. @Override
    13. public void onDisable() {
    14. PluginDescriptionFile pdfFile = this.getDescription();
    15. this.logger.info(pdfFile.getName() + " has been disabled!");
    16. }
    17.  
    18. public void onEnable() {
    19. PluginDescriptionFile pdfFile = this.getDescription();
    20. this.logger.info(pdfFile.getName() + " Version" + pdfFile.getVersion() + " has been enabled!");
    21. }
    22. }


    Code:java
    1. package me.burnerdiamond.plugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Heal extends JavaPlugin {
    10.  
    11. private Main plugin;
    12. public Heal(Main plugin) {
    13. this.plugin = plugin;
    14. }
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    17. Player player = (Player) sender;
    18. if(commandLabel.equalsIgnoreCase("heal")) {
    19. player.setHealth(20.0);
    20. player.setFoodLevel(20);
    21. player.sendMessage(ChatColor.AQUA + "You have been" + ChatColor.GOLD + " healed!");
    22. }
    23. return false;
    24. }
    25.  
    26. }
    27.  


    Code:java
    1. package me.burnerdiamond.plugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class FP extends JavaPlugin {
    10.  
    11. private Main plugin;
    12. public FP(Main plugin) {
    13. this.plugin = plugin;
    14. }
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    17. Player player = (Player) sender;
    18. if(commandLabel.equalsIgnoreCase("send")) {
    19. player.sendMessage(ChatColor.AQUA + "Sent");
    20. }
    21. return false;
    22. }
    23.  
    24. }
    25.  
     
  2. Offline

    Deleted user

    onCommand function is called for every command your plugin has registered.
    Code:java
    1. if (commandLabel.equalsIgnoreCase("heal")) {
    2. // Heal command logic
    3. } else if (commandLabel.equalsIgnoreCase("send")) {
    4. // Send command logic
    5. }
     
  3. Offline

    BurnerDiamond

    oncommand?

    What would I have to put in the main class or on the other classes for it to find the command from the heal class and bring it over to the main! Many classes with public booleans all connected to one class.
     
  4. Offline

    stoneminer02

    Export your main plugin, add it to external jar to the others.
    Best way as far as I know.
     
  5. Offline

    BurnerDiamond

    How would I do that?
     
  6. Offline

    Deleted user

  7. Offline

    stoneminer02

    No?
    Oh, then I'm misunderstanding..
     
  8. Offline

    d3v1n302418

    BurnerDiamond create a new class -> implement CommandExecutor -> do stuff in the onCommand -> register with getCommand("commandname").setExecutor(this) -> export -> profit.
     
  9. Offline

    Dubehh

    With joining I assume you made a SQL reference?

    To answer your question,
    • Create a class for each command which implements CommandExcecutor
    • There can be multiple commands in one onCommand boolean
    • There can only be ONE class that extends JavaPlugin (Main class)
    Multiple Classes
    Code:java
    1. public class Main extends JavaPlugin{
    2.  
    3. public void onEnable(){
    4. getCommand("Heal").setExectutor(new HealCommandClass()); // Command is /heal
    5. }
    6. }
    7.  
    8. /*
    9.   Assume this is a different class
    10. */
    11.  
    12. public class HealCommandClass implements CommandExcecutor{ //Class just for /heal command
    13.  
    14. /*
    15.   Let's say the command is /heal
    16.   */
    17. public boolean onCommand(stuff){
    18. //This will run once /heal is fired :)
    19. }
    20. }


    All in main class
    Code:java
    1. public void onEnable(){
    2. //No need to register commands
    3. }
    4.  
    5. @Override
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    7. if(cmd.getName().equalsIgnoreCase("heal")){
    8. }
    9. else if(cmd.getName().equalsIgnoreCase("kill")){
    10. }
    11. }


    Using a switch - case for commands
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. // Assume command is heal
    3. // Assume you need multiple arguments after a command
    4. if(args.length > 0){
    5. switch(args[0].toLowerCase()){ //Check CaSe SenSitive
    6. case "me":
    7. // /heal me
    8. break;
    9. case "all":
    10. // /heal all
    11. break;
    12. }
    13. }
    14. }


     
  10. Offline

    fireblast709

    Joiner Don't use commandLabel to check which command executed, use cmd.getName().
    Dubehh For compatibility with Java 6, you should use if-else if-else.
    BurnerDiamond
    • Don't use the Minecraft Logger, use the Logger object you get from JavaPlugin's getLogger().
    • No need for enable/disable messages, Bukkit does this for you.
    • Remove that static plugin instance field.
    • Check if sender instanceof Player before casting to Player.
    • Don't use commandLabel to check which command executed, use cmd.getName().
    • tl;dr: Don't use Bukkit tutorials on youtube. Learn Java and read the Bukkit Wiki instead.
     
  11. Offline

    Deleted user

Thread Status:
Not open for further replies.

Share This Page