-1

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).
    }
}
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
nooob
  • 1
  • 2
  • Please show the concrete error message – Jens Feb 23 '22 at 13:31
  • 1
    BTW: Take care of java naming conventions. Class Names should start with upper case character. All names should be camelCase not snake_case – Jens Feb 23 '22 at 13:32

2 Answers2

0

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.

Ferrybig
  • 18,194
  • 6
  • 57
  • 79
0

There is multiple way.

  1. You can do such as Ferrybig's said: Pass the instance of the JavaPlugin's into the constructor.

  2. 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);
        }
    }
}
  1. 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);.

Elikill58
  • 4,050
  • 24
  • 23
  • 45