Calling a method from within a different class.

Discussion in 'Plugin Development' started by x17Andrew71x, Jan 18, 2014.

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

    x17Andrew71x

    Alright, so my question is... How do I call a method from another class? So that I can have all my code for doing something in a separate class from my class that handles the commands.

    Main:
    Code:java
    1. package com.enderfalls.Main;
    2.  
    3. public class Main extends JavaPlugin {
    4. public final Logger logger = Logger.getLogger("Minecraft");
    5.  
    6. public void onEnable() {
    7. PluginDescriptionFile pdfFile = getDescription();
    8. this.logger.info(pdfFile.getName() + " " + pdfFile.getVersion() + " has been Enabled.");
    9. getServer().getPluginManager().registerEvents(new TDM_Listener(this), this);
    10. getServer().getPluginManager().registerEvents(new TheAbyss(this), this);
    11. getServer().getPluginManager().registerEvents(new NADing(this), this);
    12. }
    13. ---
    14. ---
    15. ---
    16. ---
    17. ---
    18.  
    19. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("Joingame")) {
    22. if (player.getWorld().getName().equals("TheAbyss")) {
    23. if (player.hasPermission("efpvpmgr.tdm.join")) {
    24. ***<Call another class' method here.>*** example: GiveKits();?
    25. }
    26. }
    27.  
    28. } else if (){
    29. }
    30. return false;
    31. }
    32. }
    33.  


    and Class containing code being called:

    Code:java
    1. package com.enderfalls.Main.Worlds;
    2.  
    3. public class TheAbyss implements Listener {
    4.  
    5. public Main plugin;
    6.  
    7. public TheAbyss(Main instance) {
    8. plugin = instance;
    9. }
    10.  
    11. public void GiveKits(final Player player) {
    12. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    13.  
    14. public void run() {
    15.  
    16. if (player.hasPermission("efpvpmgr.theabyss.blueteam")) {
    17.  
    18. Run Code That Goes Here.
    19. }
    20. }
    21. }, 20L);
    22.  
    23. }
    24.  
    25. }
    26.  


    Thanks in advance!
     
  2. Offline

    Compressions

    x17Andrew71x You can pass the method through a constructor or declare it as static.
     
  3. x17Andrew71x
    Create an instance of the class, then use its methods. You could also use static methods, but your task requires the main class instance, so in this case, that's not an option.
    Code:
    TheAbyss abyss = new TheAbyss(this);
    abyss.GiveKits(some player);
    Also, just a couple notes.
    1. Method names should start with a lowercase letter, so the correct look of your GiveKits() method would be giveKits().
    2. Your scheduler won't work. It wants your main class instance, but you're trying to give it the TheAbyss class instead. In the scheduler, replace "(Plugin) this" with "plugin".

    Edit: Compressions ninja'd me while I was writing this. Ps: so many new DBO staff members, grats to you too.
     
    Compressions likes this.
Thread Status:
Not open for further replies.

Share This Page