Make config with plugin prefix

Discussion in 'Plugin Development' started by tvhee, Jul 20, 2020.

  1. Offline

    tvhee

    This is my main
    Code:
    package me.tvhee.chatradius;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ChatRadiusClass extends JavaPlugin {
    
        FileConfiguration config = this.getConfig();
        String igprefix = config.getString("pluginprefix");
        String coloredString = ChatColor.translateAlternateColorCodes('&', igprefix);
    
        @Override
        public void onEnable()
            {
                Bukkit.getServer().getPluginManager().registerEvents(new ListenerClass(this), this);
                Bukkit.getServer().getPluginCommand("cr").setExecutor((CommandExecutor) new Commands(this));
    
                this.getConfig().addDefault("chatradius", 100);
                this.getConfig().addDefault("pluginprefix", "&7[&6ChatRadius&7] ");
                this.getConfig().options().copyDefaults(true);
                saveConfig();
                Settings.current_chat_radius = getConfig().getInt("chatradius");
    
                getLogger().info("has been enabeled!");
                getLogger().info("made by " + getDescription().getAuthors());
            }
    
        @Override
        public void onDisable()
        {
            getLogger().info("made by " + getDescription().getAuthors());
            getLogger().info("has been disabled!");
        }
    }
    
    
    Problem: & color codes aren't working
     
    Last edited: Jul 20, 2020
  2. Offline

    timtower Moderator Moderator

    @tvhee Don't call the config before the onEnable, call it in the onEnable instead.
     
  3. Offline

    tvhee

    If i set it in the OnEnable then I get much errors

    New: I changed my main and now I get an error by loading the plugin

    Main (open)

    package me.tvhee.chatradius;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class ChatRadiusClass extends JavaPlugin {

    @Override

    public void onEnable()
    {
    Bukkit.getServer().getPluginManager().registerEvents(new ListenerClass(this), this);
    Bukkit.getServer().getPluginCommand("cr").setExecutor((CommandExecutor) new Commands(this));

    this.getConfig().addDefault("chatradius", 100);
    this.getConfig().addDefault("pluginprefix", "&7[&6ChatRadius&7] ");
    this.getConfig().options().copyDefaults(true);
    saveConfig();
    Settings.current_chat_radius = getConfig().getInt("chatradius");

    getLogger().info("has been enabeled!");
    getLogger().info("made by " + getDescription().getAuthors());
    }

    FileConfiguration config = this.getConfig();
    String prefix = config.getString("pluginprefix");
    String igprefix = ChatColor.translateAlternateColorCodes('&', prefix);

    @Override
    public void onDisable()
    {
    getLogger().info("made by " + getDescription().getAuthors());
    getLogger().info("has been disabled!");
    }
    }


    Error (open)

    [17:29:03 ERROR]: Could not load 'plugins\ChatRadius 1.5.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Cannot translate null text
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:141) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:397) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:305) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.loadPlugins(CraftServer.java:388) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.reload(CraftServer.java:930) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.Bukkit.reload(Bukkit.java:688) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:54) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.dispatchCommand(CraftServer.java:794) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.dispatchServerCommand(CraftServer.java:756) ~[patched_1.16.1.jar:git-Paper-79]
    at net.minecraft.server.v1_16_R1.DedicatedServer.handleCommandQueue(DedicatedServer.java:411) ~[patched_1.16.1.jar:git-Paper-79]
    at net.minecraft.server.v1_16_R1.DedicatedServer.b(DedicatedServer.java:378) ~[patched_1.16.1.jar:git-Paper-79]
    at net.minecraft.server.v1_16_R1.MinecraftServer.a(MinecraftServer.java:1212) ~[patched_1.16.1.jar:git-Paper-79]
    at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:1000) ~[patched_1.16.1.jar:git-Paper-79]
    at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$a$0(MinecraftServer.java:177) ~[patched_1.16.1.jar:git-Paper-79]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_261]
    Caused by: java.lang.IllegalArgumentException: Cannot translate null text
    at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:354) ~[patched_1.16.1.jar:git-Paper-79]
    at me.tvhee.chatradius.ChatRadiusClass.<init>(ChatRadiusClass.java:30) ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_261]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_261]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_261]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_261]
    at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_261]
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:80) ~[patched_1.16.1.jar:git-Paper-79]
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:137) ~[patched_1.16.1.jar:git-Paper-79]
    ... 15 more


    Main:
    Main (open)

    package me.tvhee.chatradius;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.plugin.java.JavaPlugin;

    public class ChatRadiusClass extends JavaPlugin {

    String igprefix = getConfig().getString(ChatColor.translateAlternateColorCodes('&', "pluginprefix"));

    @Override
    public void onEnable()
    {
    Bukkit.getServer().getPluginManager().registerEvents(new ListenerClass(this), this);
    Bukkit.getServer().getPluginCommand("cr").setExecutor((CommandExecutor) new Commands(this));

    this.getConfig().addDefault("chatradius", 100);
    this.getConfig().addDefault("pluginprefix", "&7[&6ChatRadius&7] ");
    this.getConfig().options().copyDefaults(true);
    saveConfig();
    Settings.current_chat_radius = getConfig().getInt("chatradius");
    Settings.current_igprefix = getConfig().getString("pluginprefix");


    getLogger().info("has been enabeled!");
    getLogger().info("made by " + getDescription().getAuthors());
    }
    @Override
    public void onDisable()
    {
    getLogger().info("made by " + getDescription().getAuthors());
    getLogger().info("has been disabled!");
    }
    }


    Now I see this in game: &7[&6ChatRadius&7], When I use the ยง I get an error

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 20, 2020
  4. Offline

    timtower Moderator Moderator

    @tvhee Stick to 1 thing and post the error.
    And please use [code] <code here> [/code] instead of spoilers.
     
  5. Offline

    tvhee

    Okay I think I can solve this today and you mean my Java code need between [code.] blocks?[/code.]
     
  6. Offline

    timtower Moderator Moderator

    @tvhee Yeah, same for errors, it keeps formatting and you can scroll through it.
     

Share This Page