Solved Cant get config to work... Again

Discussion in 'Spigot Plugin Development' started by OrangeLion, Jan 12, 2020.

Thread Status:
Not open for further replies.
  1. The config file never works, I have listed the class files, the .yml files, and the logs, if you can help, please do, I cant get this to work at all...

    config.yml:
    Code (Text):

    ####################################################################################
    ##                           Broadcast+ Config File                               ##
    ##                           BCPlus | By OrangeLion                               ##
    ####################################################################################

    ####################################################################################
    ## Placeholders: ##
    ## <MESSAGE> - The message for the broaccast (ONLY USE IN THE BROADCAST MESSAGES) ##
    ## <SENDER> - The person who sent the message (ONLY USE IN NO-PERMISSION // BAD-SYNTAX)##
    ## <T> - » <-- That Symbol ##
    ####################################################################################

    ## All messages can be configured here! ##
    Message:
       no-permission: '&cNo Permission!'
       broadcast: '&6&lBROADCAST &7[T] <MESSAGE>'
       info-broadcast: '&d&lINFO &7[T] <MESSAGE>'
       bad-syntax: '&cPlease supply a message!'

    ## This is just where the plugin saves date for other stuff, most of wich you will never see in game. ##
    Data:
       last-bc: 'none'
       last-bc-sender: '?'
       
       last-ibc: 'none'
       last-ibc-sender: '?'
     
    Plugin.yml
    Code (Text):

    name: BroadcastPlus

    author: Orange Lion

    description: An alternitive to essentials broadcast

    version: 1.3

    main: me.bentonjohnson.broadcastplus.Main


    commands:
       broadcast:
          usage: /<command>
          description: The simple broadcast command, but better
          aliases: [bc]
       infobroadcast:
          usage: /<command>
          description: A broadcast message, but prefixed Info
          aliases: [ibc]
     
    Commands.java
    Code (Text):

    package me.bentonjohnson.broadcastplus;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;

    import net.md_5.bungee.api.ChatColor;

    public class Commands implements Listener, CommandExecutor {
        private Main plugin = Main.getPlugin(Main.class);

        /**
        * Author: Orange Lion [BROADCAST+]
        */
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("broadcast")) {
                if (sender.hasPermission("broadcastplus.bc")) {
                    if (args.length == 0) {

                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Message.bad-syntax").replace("<SENDER>", sender.getName())));
                        return true;
                    } else {
                        String bc_msg = String.join(" "); // we're going to store the arguments here

                        for (int i = 0; i < args.length; i++) { // loop threw all the arguments
                            String arg = args[i] + " "; // get the argument, and add a space so that the words get spaced
                                                        // out
                            bc_msg = bc_msg + arg; // add the argument to myString

                        }
                        Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Message.broadcast").replace("<MESSAGE>", bc_msg)));
                        plugin.getConfig().set("Data.last-bc", bc_msg);
                        plugin.saveConfig();
                        plugin.getConfig().set("Date.last-bc-sender", sender.getName());
                        plugin.saveConfig();
                        return true;

                    }

                } else {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
                            plugin.getConfig().getString("Message.no-permission").replace("<SENDER>", sender.getName())));
                    return true;

                }

            } else {
                if (cmd.getName().equalsIgnoreCase("infobroadcast")) {
                    if (sender.hasPermission("broadcastplus.ibc")) {
                        if (args.length == 0) {

                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig()
                                    .getString("Message.bad-syntax").replace("<SENDER>", sender.getName())));
                            return true;
                        } else {

                            String joinedString = String.join(" "); // we're going to store the arguments here

                            for (int i = 0; i < args.length; i++) { // loop threw all the arguments
                                String arg = args[i] + " "; // get the argument, and add a space so that the words get
                                                            // spaced out
                                joinedString = joinedString + arg; // add the argument to myString

                            }

                            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig()
                                    .getString("Message.info-broadcast").replace("<MESSAGE>", joinedString)));
                            plugin.getConfig().set("Data.last-ibc", joinedString);
                            plugin.saveConfig();
                            plugin.getConfig().set("Date.last-ibc-sender", sender.getName());
                            plugin.saveConfig();
                            return true;

                        }

                    } else {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig()
                                .getString("Message.no-permission").replace("<SENDER>", sender.getName())));
                        return true;

                    }

                }

            }
            return false;
        }

    }


     
    Main.java (Ik, I made this plugin a while ago, i stopped naming the main class Main):
    Code (Text):

    package me.bentonjohnson.broadcastplus;

    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin{
        private Commands commands = new Commands();
        /**
          * Author: Orange Lion [BROADCAST+]
        * @return
          */
        @Override
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            getCommand("broadcast").setExecutor(commands);
            getCommand("infobroadcast").setExecutor(commands);
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "BROADCASTPLUS " + ChatColor.GRAY + "» " + ChatColor.GREEN + "Plugin enabled!");
           
        }
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "BROADCASTPLUS " + ChatColor.GRAY + "» " + ChatColor.RED+ "Plugin enabled!");
           
           
        }

    }


     
    Thanks in advance!
     
  2. md_5

    Administrator Developer

    Which part doesn't work?
     
  3. When the server starts, the plugin wont load hers the log, the plugin jar is called BCPlus.Jar:
    Code (Text):

    [19:28:49] [Server thread/ERROR]: Could not load 'plugins\BCPlus.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalStateException: Cannot get plugin for class me.bentonjohnson.broadcastplus.Main from a static initializer
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[srv.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[srv.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [srv.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [srv.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [srv.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [srv.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
    Caused by: java.lang.IllegalStateException: Cannot get plugin for class me.bentonjohnson.broadcastplus.Main from a static initializer
        at org.bukkit.plugin.java.JavaPlugin.getPlugin(JavaPlugin.java:531) ~[srv.jar:git-Spigot-db6de12-18fbb24]
        at me.bentonjohnson.broadcastplus.Commands.<init>(Commands.java:12) ~[?:?]
        at me.bentonjohnson.broadcastplus.Main.<init>(Main.java:9) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_221]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_221]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_221]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_221]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_221]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[srv.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[srv.jar:git-Spigot-db6de12-18fbb24]
        ... 6 more

     
     
  4. md_5

    Administrator Developer

    private Commands commands = new Commands();


    Put the new Commands() in onEnable
     
  5. sorry. but I dont quit get what you are saying, do you mean I would do somthing like this:
    Code (Text):

    public void onEnable() {
    new Commands();
    }
     
     
  6. yes. split the declaration. because Commands is being instantiated before the plugin enables, everything in your Commands class that relies on the plugin is failing. splitting the declaration to where the Commands class is instantiated after (technically while it enables) your plugin enables in your onEnable wont cause your errors. so inside your onEnable do
    Code (Java):
    commands = new Commands();
    and just dont declare the field outside of the onEnable.
     
  7. This is what I did, and it still doesn't work, I don't know what I'm doing wrong:
    Code (Text):

    package me.bentonjohnson.broadcastplus;

    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin{
        private Commands commands;
        /**
          * Author: Orange Lion [BROADCAST+]
        * @return
          */
        @Override
        public void onEnable() {
            commands = new Commands();
           
            getConfig().options().copyDefaults(true);
            saveConfig();
            getCommand("broadcast").setExecutor(commands);
            getCommand("infobroadcast").setExecutor(commands);
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "BROADCASTPLUS " + ChatColor.GRAY + "» " + ChatColor.GREEN + "Broadcast enabled!");
           
        }
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "BROADCASTPLUS " + ChatColor.GRAY + "» " + ChatColor.RED+ "Broadcast enabled!");
           
           
        }

    }


     
     
  8. so you recompiled, shut down the server, installed the newly compiled plugin, and started it up again? whats the new error? even if it has the same words, the technicalities can change so post the new one again

    /e also, you do the same error as you have with your commands class in your Main class. you are using a method that requires the plugin to be enabled to get the plugin in your Commands class. split the declaration so that you're declaring the plugin instance in your Commands class in its constructor, as the constructor will only ever be called during the plugin's enable process.
     
  9. wow, im stupid, it worked, Thanks a lot :)
     
Thread Status:
Not open for further replies.