Solved Method doesn`t calling if some plugin enabled

Discussion in 'Spigot Plugin Development' started by MiMiMiEnot, Mar 8, 2020.

  1. Hi! I create two plugins

    One - simple world loader
    Code (Java):
    public class WillWorldLoader extends JavaPlugin {

        public static Plugin getPlugin(){
            return WillWorldLoader.getPlugin(WillWorldLoader.class);
        }


        @Override
        public void onEnable() {
            Settings.reload();

            for(String s : Settings.getWorldsToLoad()){
                long tyme = System.currentTimeMillis();

                new WorldCreator(s).createWorld();

                Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&',
                        "&fПрогружен мир &a" + s + " &fза &a" + (System.currentTimeMillis() - tyme) + "мс&f!"
                ));
            }
        }

        @Override
        public void onDisable() {}
    }

    And spawnevents blocker
    Code (Java):
     public static Plugin getPlugin(){
            return WillSpawn.getPlugin(WillSpawn.class);
        }

        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(new SpawnEvents(), this);
            Settings.reload();
           }

        @Override
        public void onDisable() {

        }

    In all plugins i use that configs code (HOCON typesafe)

    Code (Java):
    private static Plugin plugin = WillSpawn.getPlugin();
        private static Config settingscnf;
        private static File file = new File(plugin.getDataFolder(), "settings.conf");
        private static Settings settings = new Settings();

        private static void create(){
            if(!plugin.getDataFolder().exists()){
                plugin.getDataFolder().mkdir();
            }
            try {
                if(!file.exists()) {
                    InputStream is = settings.getClass().getResourceAsStream("/settings.conf");
                    if (is != null) {
                        Files.copy(is, file.toPath());
                    }
                }
                settingscnf = ConfigFactory.parseFile(file).resolve();
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        public static Config getSettings() {
            return settingscnf;
        }

        public static void reload(){
            if(file == null || settingscnf == null){
                create();
            }
            settingscnf = ConfigFactory.parseFile(file);
        }
     
    But...

    WorldLoader work correctly but spawns.. not

    If WorldLoader doesn`t enable - spawns work correctly, but if he enabled - Spawn plugin doesn`t work. onEnable doesn`t call Settings.reload(); and if l not misstake also doesn`t call Bukkit.getPluginManager().registryEvents(...); but Bukkit.getConsoleSender().sendMessage(); work fine.

    I doesn`t know what to do.. help me please
     
  2. Add your WorkLoader plugin to your other plugins dependency list
     
  3. i haven`t dependency list on any plugins what i write
    i must add to Spawn plugin dependency worldloader?
     
  4. Yes. In your plugin.yml, add this to the list under depend (or depends, not quite sure about the exact spelling)
     
    • Like Like x 1
  5. That`s doesn`t help
     
  6. Can you please provide some more details like your plugin.yml?
     
    • Agree Agree x 1
  7. Code (Java):
    package me.enot;

    import me.enot.configs.Settings;
    import me.enot.events.SpawnEvents;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class WillSpawn extends JavaPlugin {

        public static Plugin getPlugin(){
            return WillSpawn.getPlugin(WillSpawn.class);
        }

        @Override
        public void onEnable() {


            Bukkit.getPluginManager().registerEvents(new SpawnEvents(), this);
            Settings.reload();
            //Bukkit.getConsoleSender().sendMessage("//////////////");
        }

        @Override
        public void onDisable() {

        }
    }
     
    Code (Java):
    package me.enot.configs;

    import com.typesafe.config.Config;
    import com.typesafe.config.ConfigFactory;
    import com.typesafe.config.ConfigRenderOptions;
    import me.enot.WillSpawn;
    import me.enot.utils.Corner;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.Plugin;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.StandardOpenOption;

    public class Settings {

        private static Plugin plugin = WillSpawn.getPlugin();
        private static Config settingscnf;
        private static File file = new File(plugin.getDataFolder(), "settings.conf");
        private static Settings settings = new Settings();

        private static void create(){
            if(!plugin.getDataFolder().exists()){
                Bukkit.getConsoleSender().sendMessage("" + plugin.getDataFolder().mkdir());
            } else {
                Bukkit.getConsoleSender().sendMessage("s");
            }
            try {
                if(!file.exists()) {
                    InputStream is = settings.getClass().getResourceAsStream("/settings.conf");
                    if (is != null) {
                        Files.copy(is, file.toPath());
                    }
                }
                settingscnf = ConfigFactory.parseFile(file).resolve();
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        public static Config getSettings() {
            return settingscnf;
        }

        private static String worldname;
        private static Corner corner1;
        private static Corner corner2;

        public static void reload(){
            if(file == null || settingscnf == null){
                create();
                Bukkit.getConsoleSender().sendMessage("1111111111111111");
            }
            Bukkit.getConsoleSender().sendMessage("222222222");
            settingscnf = ConfigFactory.parseFile(file);

            worldname = getSettings().getString("blocking-location.world");
            corner1 = new Corner(getSettings().getInt("blocking-location.corner1.X"),
                    getSettings().getInt("blocking-location.corner1.Y"),
                    getSettings().getInt("blocking-location.corner1.Z"));
            corner2 = new Corner(getSettings().getInt("blocking-location.corner2.X"),
                    getSettings().getInt("blocking-location.corner2.Y"),
                    getSettings().getInt("blocking-location.corner2.Z"));

            Bukkit.getConsoleSender().sendMessage("444444444444444444");
        }

        public static Corner getCorner1() {
            return corner1;
        }

        public static Corner getCorner2() {
            return corner2;
        }

        public static String getWorldname() {
            return worldname;
        }

        public static void save(){
            ConfigRenderOptions cro = ConfigRenderOptions.defaults().setJson(false).setOriginComments(false);
            try {
                Files.write(file.toPath(), settingscnf.root().render(cro).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
            } catch (IOException e){
                e.printStackTrace();
            }
        }

    }
     
    Code (YAML):
    name: WillSpawn
    main
    : me.enot.WillSpawn
    version
    : 1.0
     
    Code (Java):
    package me.enot;

    import me.enot.configs.Settings;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class WillWorldLoader extends JavaPlugin {

        public static Plugin getPlugin(){
            return WillWorldLoader.getPlugin(WillWorldLoader.class);
        }


        @Override
        public void onEnable() {
            Settings.reload();

            for(String s : Settings.getWorldsToLoad()){
                long tyme = System.currentTimeMillis();

                new WorldCreator(s).createWorld();

                Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&',
                        "&fПрогружен мир &a" + s + " &fза &a" + (System.currentTimeMillis() - tyme) + "мс&f!"
                ));
            }
        }

        @Override
        public void onDisable() {}
    Code (Java):

    package me.enot.configs;

    import com.typesafe.config.Config;
    import com.typesafe.config.ConfigFactory;
    import com.typesafe.config.ConfigRenderOptions;
    import me.enot.WillWorldLoader;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.Plugin;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.StandardOpenOption;
    import java.util.List;

    public class Settings {

        private static Plugin plugin = WillWorldLoader.getPlugin();
        private static Config settingscnf;
        private static File file = new File(plugin.getDataFolder(), "settings.conf");
        private static Settings settings = new Settings();

        private static void create(){
            if(!plugin.getDataFolder().exists()){
                plugin.getDataFolder().mkdir();
            }
            try {
                if(!file.exists()) {
                    InputStream is = settings.getClass().getResourceAsStream("/settings.conf");
                    if (is != null) {
                        Files.copy(is, file.toPath());
                    }
                }
                settingscnf = ConfigFactory.parseFile(file).resolve();
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        public static Config getSettings() {
            return settingscnf;
        }

        private static List<String> worldsToLoad;

        public static void reload(){
            if(file == null || settingscnf == null){
                create();
            }
            settingscnf = ConfigFactory.parseFile(file);

            worldsToLoad = getSettings().getStringList("world-to-load");
        }

        public static List<String> getWorldsToLoad() {
            return worldsToLoad;
        }

        public static void save(){
            ConfigRenderOptions cro = ConfigRenderOptions.defaults().setJson(false).setOriginComments(false);
            try {
                Files.write(file.toPath(), settingscnf.root().render(cro).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
            } catch (IOException e){
                e.printStackTrace();
            }
        }


    }
     
    Code (YAML):
    name: WillWorldLoader
    main
    : me.enot.WillWorldLoader
    version
    : 1.0

    How you see i add messages and none of them (in spawn plugin) if world loader plugin enabled - doesn`t sends to console. If world loader doesn`t enabled - all works fine
     
    #7 MiMiMiEnot, Mar 8, 2020
    Last edited: Mar 8, 2020
  8. As I said, add depend: [WillSpawn] to your WillWorldLoader plugin.yml
    Also, I believe you linked the wrong class to your WillWorldLoader-plugin (or are the main-class and the settings the same thing?)
     
    • Agree Agree x 1
  9. oh sorry
    i edit
     
  10. that`s work but in worldloader plugin i get error
    Code (Java):

    [20:09:15 ERROR]: Error occurred while enabling WillWorldLoader v1.0 (Is it up t
    o date?)
    java.lang.NoSuchMethodError: me.enot.configs.Settings.getWorldsToLoad()Ljava/uti
    l/List;
            at me.enot.WillWorldLoader.onEnable(WillWorldLoader.java:22) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[c
    ore.jar:git-Paper-1618]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:316) ~[core.jar:git-Paper-1618]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) ~[core.jar:git-Paper-1618]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.
    java:395) ~[core.jar:git-Paper-1618]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer
    .java:344) ~[core.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:
    442) ~[core.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:
    403) ~[core.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:
    341) ~[core.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.ja
    va:289) ~[core.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.jav
    a:616) ~[core.jar:git-Paper-1618]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
    [20:09:15 INFO]: [WillWorldLoader] Disabling WillWorldLoader v1.0
    [20:09:15 INFO]: Done (9,606s)! For help, type "help" or "?"
    [20:09:15 INFO]: Timings Reset
    >
    WillWorldLoader:22 =
    for(String s : Settings.getWorldsToLoad()){

    Can you help with that?
     
  11. Your two Settings-classes are called exactly the same way... I‘m not sure if that causes the issue (probably yes), so try changing one (maybe move to a different package, or just name differently). Also, please get rid of that static abuse
     
    • Agree Agree x 1
  12. thank you!