Help with config

Discussion in 'Plugin Development' started by BaconStripzMan, Jan 10, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    BaconStripzMan

    Hi, so I've created a plugin that changes the join message and leave message (also can disable them) with a config and it works fine except when I use &(number) (which replaces) it will revert back to the default config but if I don't use them it works fine
    Code:
    package me.BaconStripsMan.CCM;
    
    import java.util.logging.Logger;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
       
       
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Main plugin;
       
       
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
       
        public void onEnable(){
            initialiseConfig();
            getServer().getPluginManager().registerEvents(this, this);
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Enabled!");
        }
       
        public void initialiseConfig(){
            FileConfiguration config = getConfig();
            config.addDefault("Join.EnableMessage", true);
            config.addDefault("Join.JoinMessage", "&2%p &ahas joined the game! &bThis Message was configurated by CustomConnectionMessages!");
            config.options().copyDefaults(true);
            saveConfig();
        }
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e){
            if(getConfig().getBoolean("Join.EnableMessage", true)) {
                e.setJoinMessage((getConfig().getString("Join.JoinMessage").replaceAll("%p", e.getPlayer().getName()).replaceAll("&0", "§0").replaceAll("&1", "§1").replaceAll("&2", "§2").replaceAll("&3", "§3").replaceAll("&4", "§4").replaceAll("&4", "§4").replaceAll("&5", "§5").replaceAll("&6", "§6").replaceAll("&7", "§7").replaceAll("&8", "§8").replaceAll("&9", "§9").replaceAll("&a", "§a").replaceAll("&b", "§b").replaceAll("&c", "§c").replaceAll("&d", "§d").replaceAll("&e", "§e").replaceAll("&f", "§f").replaceAll("&k", "§k").replaceAll("&l", "§l").replaceAll("&o", "§o").replaceAll("&r", "§r").replaceAll("&n", "§n").replaceAll("&m", "§m")));
            } else {
                e.setJoinMessage(null);
            }
        }
    }
     
  2. Offline

    mine-care

    Use don't replace every single color code by hand, use Chatcolor.translatealtrtnatecolorcodes('&',<your string>)
    Also you don't need enable and disable messages because bukkit throws them already, so they kinda spams the console, don't use getLogger("minecraft"); use getServer().getLogger();
    And prefer to load things from config to a variable rather than opening the config, reading them, closing the config every time you wana access them.
     
  3. Offline

    BaconStripzMan

    Doesn't help.. It's just cleaning up..
     
  4. Offline

    mythbusterma

    @BaconStripzMan

    No, but if you make those changes, your code may actually have some semblance of readability so we can get to the underlying issue.
     
    mine-care likes this.
  5. Offline

    mine-care

    Then I didn't get it, what you mean it reverts back?
     
  6. Offline

    BaconStripzMan

    Goes back to the default Join Message: &2%p &ahas joined the game! &bThis Message was configurated by CustomConnectionMessages!
     
  7. Offline

    mythbusterma

    @BaconStripzMan

     
  8. Offline

    mine-care

    I think it's because of your initialiseConfig method that overrides the existing config when called.
     
  9. Offline

    BaconStripzMan

    Alright one second then..

    I don't understand, could you explain it in more depth
     
    Last edited: Jan 11, 2015
  10. Offline

    mythbusterma

    @BaconStripzMan

    Certainly, stop using BCBlowz and read the Plugin Tutorial.

    Just use the function JavaPlugin#saveDefaultConfig() and include a config.yml in the root of your jar-file and you don't have to bother with whatever awful implementation you have above.
     
  11. Offline

    BaconStripzMan

    I have no idea what you're talking about
     
  12. Invisible

    nverdier

    @BaconStripzMan

    1) Stop watching the BCBroz (BCBlowz).
    2) Ditch all of the config stuff you have currently.
    3) Use JavaPlugin#saveDefaultConfig()
     
  13. Offline

    BaconStripzMan

    Could you link me a good tutorial on configs?
     
  14. Invisible

    nverdier

    @BaconStripzMan You don't need one, we just said everything you needed to do...
     
  15. Offline

    BaconStripzMan

    Alright :D
     
  16. Offline

    teej107

  17. Offline

    BaconStripzMan

Thread Status:
Not open for further replies.

Share This Page