'How can I use bukkit Javaplugin without 'extends javaplugin'?
I use paper plugin, java. I want to use getLogger(bukkit's)code. I know I cannot use ' extends JavaPlugin ' code. so, how?
Source code
package com;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class (class name spot) extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getLogger().warning("Server started.");
first_function first = new first_function();
first.onEnable();
getLogger().info("Nice try!"); // check point
}
}
class first_function implements Listener {
void onEnable() {
String abcd = "abc";
getLogger().info(abcd); // I want to use getLogger(bukkit's).
}
}
Solution 1:[1]
The standard way to do this in Bukkit, is passing the instance of your plugin to the constructor of the listener
public class (class name spot) extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getLogger().warning("Server started.");
first_function first = new first_function(this);
first.onEnable();
getLogger().info("Nice try!"); // check point
}
}
class first_function implements Listener {
private final (class name spot) plugin;
first_function((class name spot) plugin) {
this.plugin = plugin;
}
void onEnable() {
String abcd = "abc";
this.plugin.getLogger().info(abcd);
}
}
We modify the place where you create the listener to pass the plugin, and then store this inside the listener via the constructor. Then later we refer to this.plugin to access methods on the plugin instance.
Solution 2:[2]
There is multiple way.
You can do such as Ferrybig's said: Pass the instance of the JavaPlugin's into the constructor.
Make the class into the JavaPlugin's one. Here, You put the class below the JavaPlugin's, but you can do like this:
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class (class name spot) extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getLogger().warning("Server started.");
FirstFunction first = new FirstFunction();
first.onEnable();
getLogger().info("Nice try!"); // check point
}
class FirstFunction implements Listener {
void onEnable() {
String abcd = "abc";
getLogger().info(abcd);
}
}
}
- You can create a singleton (What it is ?) of your JavaPlugin object: save the instance of it to get it everywhere, like that:
public class ClassNameSpot extends JavaPlugin implements Listener {
private static ClassNameSpot instance;
public static ClassNameSpot getInstance() {
return instance;
}
@Override
public void onEnable() {
instance = this; // set the instance
getLogger().warning("Server started.");
FirstFunction first = new FirstFunction();
first.onEnable();
getLogger().info("Nice try!"); // check point
}
}
class first_function implements Listener {
void onEnable() {
String abcd = "abc";
ClassNameSpot.getInstance().getLogger().info(abcd); // I want to use getLogger(bukkit's).
}
}
This solution is better if you have lot of class (instead of create a variable everywhere)
BONUS: You seems to use implements Listener but never register them. I don't know if it's intentionate or not, but don't forget to, in your onEnable's method, call: getServer().getPluginManager().registerEvents(first, this);.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ferrybig |
| Solution 2 | Elikill58 |
