Command of the method in another class (JavaPlugin)?

Pa
- in Plugins
3

I'm currently programming a Minecraft plugin. However, I have come to a point where I have no experience. I want to program a command that executes a method in another class, but I have no idea how to do it. (A gui should be opened in the method) If someone has a suggestion, please write.

First of all, thank you for your suggestions

PS: I'm pretty new to Java programming

An

You create an instance of your class and call the method there… What's the problem?

Bu

You need an instance of this other class in order to be able to call the method from it.

If you don't need this instance anywhere else, it would be sufficient to create it in the onCommand method (or as a field):

@Override
public boolean onCommand (CommandSender sender, Command cmd, String label, String [] args) {
SomeType someObject = new SomeType ();
// …
}

But assuming that this foreign instance is also used elsewhere, you should have its reference passed to you via a setter or constructor to save it in a field.

public class MyCommand implements CommandExecutor {
private SomeType someObject;

public MyCommand (SomeType someObject) {
this.someObject = someObject;
}

// …

The transfer of values can then take place where you create and register the MyCommand object.

SomeType someObject = // get other object…
this.getCommand ("…"). SetExecutor (new MyCommand (someObject));

Ma

Either you make the method static or you create an instance of the class