Run class file on command ?

Discussion in 'Plugin Development' started by Adriani6, Oct 5, 2013.

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

    Adriani6

    I'm writing a plugin and sadly got stuck..

    How would I go on about running another class file when a command is executed.

    I have 3 classes inside my package.

    - Main
    - Save
    - Upload

    I want Main to work as a plugin (Command listener), when command is executed it runs class Save which then once code was executed goes onto upload class on completion it stops, and waits until command is run again to repeat the whole process.
     
  2. Adriani6 Use methods or constructors so you can just do: Save save = new Save(<Your Parameter>);
    Then in the save class:
    public Save(<Your parameter>){ //do save stuff
    new Upload(this);
    }

    and then in the upload class:

    public Upload(Save save){
    //upload stuff from save class to whatever you want
    }
     
  3. Offline

    Adriani6

    Still can't get it to work >.>
     
  4. Offline

    1Achmed1

    Calling a new class is kinda basic, and if a method isn't void, it needs to return. I recommend setting a variable to represent the class and return with var.method();
     
  5. Offline

    Adriani6

    Hmm.... I'm just a beginner. I don't understand everything about Java sorry :p
    This is in my first class where I try to call my 2nd class called "Main"

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("backup")){ // If the player typed /basic then do the following...
    3. public static Main getInstance(){
    4. return Main.getInstance();
    5. }
    6. sender.sendMessage("BackUp Started !");
    7. return true;
    8. } //If this has happened the function will return true.
    9. // If this hasn't happened the a value of false will be returned.
    10. return false;
    11. }


    Here's code of my 2nd class "Main"

    Code:java
    1. public class Main {
    2. private static Main instance;
    3.  
    4. public void main(String[] a) throws Exception {
    5. instance = this;
    6. zipFolder("folder", "folder.zip");
    7. }
    8.  
    9. static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    10. ZipOutputStream zip = null;
    11. FileOutputStream fileWriter = null;
    12.  
    13. fileWriter = new FileOutputStream(destZipFile);
    14. zip = new ZipOutputStream(fileWriter);
    15.  
    16. addFolderToZip("", srcFolder, zip);
    17. zip.flush();
    18. zip.close();
    19. }
     
  6. Offline

    Fozie

    Well do Main main;
    and in the command main = new Main(Your String Array);
     
  7. Offline

    kevinspl2000

    Code:
    package example;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class example1 extends JavaPlugin {
     
    public void onEnable()
    {
        PluginManager pm = getServer().getPluginManager();
     
        getCommand("examplecommand").setExecutor(new classnametoexecute());
       
       
        System.out.println("examplename enabled!");
    }
    public void onDisable(){
        System.out.println("examplename Disabled!");
    }
        
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    Adriani6 likes this.
  8. Offline

    Adriani6

    Thanks kevinspl2000 looks a bit clearer :p will give it a try once I get home, thank you.
     
Thread Status:
Not open for further replies.

Share This Page