Solved How to "connect" two class ?

Discussion in 'Plugin Development' started by da_Do, Jun 10, 2017.

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

    da_Do

    Hello , i want to ask you , how to "connect" two classes. More in the photos . Thank you
     

    Attached Files:

    • 11.PNG
      11.PNG
      File size:
      121 KB
      Views:
      7
    • 22.PNG
      22.PNG
      File size:
      118.3 KB
      Views:
      7
  2. Offline

    seasonguy

    only main class should extend JavaPlugin. registerevent for Listener class only so it is not proper way. it seems your axe class is a command and should implement CommandExecutor. Then you may getCommand then setExecutor an instance of your axe class.
     
  3. Offline

    Max8801

    What you are doing wrong:
    1. You are using the method registerEvents() but in the Axe class you are defining a command. Use:
    Code:java
    1. getCommand("YOUR COMMAND").setExecutor(new Axe());

    2. Your class Axe must implement the interface CommandExecutor. Also, only the main class can extend the class JavaPlugin. Correct:
    Code:java
    1. public class Axe implements CommandExecutor {
     
  4. Offline

    YoloSanta

    Like this I believe
    Code:java
    1. package //package here
    2. import //imports here
    3. public class MyPlugin extends JavaPlugin {
    4. //declare the inventory object
    5. public static Inventory inventory = Bukkit.createInventory(null, 18, "Guns");
    6. //whatever methods you use will go here
    7. //whenever you call your other class, do this:
    8. MyOtherClass myOtherClass = new MyOtherClass(this);
    9. //"this" is a reference to your main class
    10. //you will be passing this instance of the main class to your other class
    11. }
     
  5. Offline

    da_Do

  6. Offline

    Max8801

    With this you will need a constructor in the class MyOtherClass. Add one in like that:
    Code:java
    1.  
    2. private MyPlugin plugin;
    3. public MyOtherClass(MyPlugin plugin){
    4. this.plugin = plugin
    5. }
    6.  

    From there on, you will be able to use the field plugin to access the class MyPlugin
     
Thread Status:
Not open for further replies.

Share This Page