Solved Config Colour Support!

Discussion in 'Plugin Development' started by xXRobbie21Xx, Jul 24, 2014.

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

    xXRobbie21Xx

    Hey guys, I'm pretty new to coding and I have come to the point in which I want to use colour codes in my config such as &4, &3, &1 etc, and work when sending a player a message. I understand this is probably really easy and obvious, but I cant seem to find the solution. If someone could give me a quick answer it would truly be appreciated!

    Here is what we are working with.
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player)sender;
            if (cmd.getName().equalsIgnoreCase("refill")) {
                if (!p.hasPermission("tcsoups.refill")) {
                    p.sendMessage(getConfig().getString("noPermMessage"));
                    return true;
                }
                else if (!p.getInventory().getItemInHand().getType().equals(Material.BOWL)) {
                    p.sendMessage(getConfig().getString("noBowlInHand");
                    
    Now this is my config.
    Code:
    noBowlInHand: &4Put a bowl in your hand.
    If you need anything else, i will closely watch this thread and be here!
    Thanks -Rob
     
  2. Offline

    Garris0n

    Dragonphase likes this.
  3. Offline

    ARCT3CK

    Use .replaceAll("&", "§")
    Code:java
    1. p.sendMessage(getConfig().getString("noBowlInHand".replaceAll("&","§");
     
  4. Offline

    Dragonphase

  5. Offline

    Garris0n

    NO.

    First of all, replaceAll() is for regex. That is not regex, so you should use replace().

    Second, your method will mess up all & symbols regardless of whether they're followed by a valid color character.

    Third, even if you fix the method to work correctly, it's always better to use the API instead of doing things yourself.
     
    izarooni and ARCT3CK like this.
  6. Offline

    xXRobbie21Xx

    Okay thanks everyone! Garris0n thanks for the input and searing me in the right direction!
     
  7. Offline

    xXRobbie21Xx

    okay, after a day of struggling with this i have yet to find a solution that works for me. Anyone else have an idea?
     
  8. Offline

    dsouzamatt

    What else have you tried? As Dragonphase said, translateAlternateColorCodes should work for what I believe you are trying to do.
     
  9. Offline

    Gamesareme

    This will work.
    Code:
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("noBowlInHand")));
    Just make sure in your config it looks like this.
    Code:
    noBowlInHand: 'Your Message With color codes like this &4 inside single quotes!'
     
    xXRobbie21Xx likes this.
  10. Offline

    xXRobbie21Xx

    Ignore this Gamesareme enlightened me XD

    Gamesareme wow thanks man, this makes a ton of sense now, you really helped me out here! I actually have a list of people who have been asking the same question. Now i can safely let them know :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  11. Offline

    Gamesareme

  12. Offline

    xXRobbie21Xx

    Gamesareme hey one last thing, how do i set up the plugin variable and im getting an error with the "ChatColor"
     
  13. Offline

    Gamesareme

    xXRobbie21Xx Is your code in the main class or a different class?
    If it is not then you will need to register it with your main class. Change main with what you have called your main class.
    Code:
    Main plugin;
    public <What the class the code is in>(Main main){
        plugin = main;
    }
     
  14. Offline

    xXRobbie21Xx

    Gamesareme Main class, everything in this plugin, at this point is in the main class.
     
  15. Offline

    Gamesareme

    xXRobbie21Xx Is the plugin you have got more than one class or is there only one?
     
  16. Offline

    xXRobbie21Xx

    Gamesareme its just a single class right now. And as for the "plugin" part i now understand its purpose and removed it. However, what about the chatcolor part. For some reason im getting an error with that and have no idea why.
     
  17. Offline

    Gamesareme

    Ok then add this to just below the start of your class.
    Code:
    Main plugin;
    Change Main to the name of your class.
     
  18. Offline

    xXRobbie21Xx

    Gamesareme ok done, thanks! Any ideas why the chatcolor part would be giving me troubles?
     
  19. Offline

    Gamesareme

    xXRobbie21Xx No, could you send me your code up to date?
     
  20. Offline

    xXRobbie21Xx

    Code:
    public class SoupsMain extends JavaPlugin implements Listener {
     
        SoupsMain plugin;
     
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
     
        }
     
        @Override
        public void onDisable() {
     
        }
     
     
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
            ItemStack[] inv = p.getInventory().getContents();
            if (cmd.getName().equalsIgnoreCase("refill")) {
                if (!p.hasPermission("tcsoups.refill")) {
             
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("noBowlInHand")));
                    return true;
                }
     
                else if (!p.getInventory().getItemInHand().getType().equals(Material.BOWL)) {
                    p.sendMessage(getConfig().getString("NoBowlInHand"));
                    return true;
     
                }
     
                else if (p.getInventory().getItemInHand().getType().equals(Material.BOWL)) {
                    p.getInventory().getItemInHand().setType(Material.MUSHROOM_SOUP);
                    p.sendMessage(getConfig().getString("RefillMessage"));
                    p.playSound(p.getLocation(), Sound.FIREWORK_TWINKLE2,    1, 1);
                    return true;
     
                }
     
            }
    Allrighty, thats it, if you need anything else just let me know


    EDIT: There is a whole event underneath if your wondering why i registered one, but its irrelevent so i kept it out.
     
  21. Offline

    Gamesareme

    xXRobbie21Xx All right, I added a few of these '}' to the bottom of your project. It now works fine.
    Code:
    public class SoupsMain extends JavaPlugin implements Listener {
     
        SoupsMain plugin;
     
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
     
        }
     
        @Override
        public void onDisable() {
     
        }
     
     
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
            ItemStack[] inv = p.getInventory().getContents();
            if (cmd.getName().equalsIgnoreCase("refill")) {
                if (!p.hasPermission("tcsoups.refill")) {
         
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("noBowlInHand")));
                    return true;
                }
     
                else if (!p.getInventory().getItemInHand().getType().equals(Material.BOWL)) {
                    p.sendMessage(getConfig().getString("NoBowlInHand"));
                    return true;
     
                }
     
                else if (p.getInventory().getItemInHand().getType().equals(Material.BOWL)) {
                    p.getInventory().getItemInHand().setType(Material.MUSHROOM_SOUP);
                    p.sendMessage(getConfig().getString("RefillMessage"));
                    p.playSound(p.getLocation(), Sound.FIREWORK_TWINKLE2,    1, 1);
                    return true;
     
                }
     
            }
            return false;
        }
    }
    If it continues to not work could you send me all the packages you have imported?
     
  22. Offline

    xXRobbie21Xx

    Code:
    package Soups;
     
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SoupsMain extends JavaPlugin implements Listener {
       
        SoupsMain plugin;
     
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
     
        }
     
        @Override
        public void onDisable() {
     
        }
       
       
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
            ItemStack[] inv = p.getInventory().getContents();
            if (cmd.getName().equalsIgnoreCase("refill")) {
                if (!p.hasPermission("tcsoups.refill")) {
               
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("noBowlInHand")));
                    return true;
                }
     
                else if (!p.getInventory().getItemInHand().getType().equals(Material.BOWL)) {
                    p.sendMessage(getConfig().getString("NoBowlInHand"));
                    return true;
     
                }
     
                else if (p.getInventory().getItemInHand().getType().equals(Material.BOWL)) {
                    p.getInventory().getItemInHand().setType(Material.MUSHROOM_SOUP);
                    p.sendMessage(getConfig().getString("RefillMessage"));
                    p.playSound(p.getLocation(), Sound.FIREWORK_TWINKLE2,    1, 1);
                    return true;
     
                }
     
            }
     
            else if (cmd.getName().equalsIgnoreCase("refillall")) {
                if (!p.hasPermission("tcsoups.refillall")) {
                    p.sendMessage(getConfig().getString("NoPermMessage"));
                    return true;
                }
     
                else if (!p.getInventory().contains(Material.BOWL)) {
                    p.sendMessage(getConfig().getString("NoBowlInInventory"));
                    return true;
     
                }
     
                else if (p.getInventory().contains(Material.BOWL)) {
                    for (ItemStack slot : inv) {
                        if (slot != null && slot.getType() == Material.BOWL) {
                            slot.setType(Material.MUSHROOM_SOUP);
                            p.playSound(p.getLocation(), Sound.FIREWORK_TWINKLE2,    1, 1);
                            p.sendMessage(getConfig().getString("RefillAllMessage"));
     
                        }
     
                    }
     
                }
     
            }
     
            return false;
        }
     
        @EventHandler
        public void soup(PlayerInteractEvent e) {
            ItemStack bowl = new ItemStack(Material.BOWL);
            Player p = e.getPlayer();
            Action action = e.getAction();
            if (action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK) {
                if (p.getItemInHand() != null && p.getItemInHand().getType() == Material.MUSHROOM_SOUP) {
                    if (p.getHealth() == 20) {
                        p.sendMessage(getConfig().getString("HealthFullError"));
                        return;
                    }
                   
                    else if (p.getHealth() >= 14) {
                        p.setHealth(20);
                        p.getInventory().setItemInHand(bowl);
                        p.playSound(p.getLocation(), Sound.NOTE_PLING,    1, 1);
                        return;
                       
                    }
                   
                    else if (p.getHealth() <= 14) {
                        p.setHealth(p.getHealth() + getConfig().getInt("Health"));
                        p.getInventory().setItemInHand(bowl);
                        p.playSound(p.getLocation(), Sound.NOTE_PLING,    1, 1);
                        return;
                    }
           
                }
     
            }
     
        }
     
    }
    
    Here is all the code, im extremely sorry for all the trouble, but for some reason it just not cooperating.
     
  23. Offline

    Gamesareme

    xXRobbie21Xx try adding this to it
    Code:
    import org.bukkit.ChatColor;
    @xXRobbie21Xx Hang on I made a mistake it should be this.
    Code:
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("noBowlInHand")
    You will also need to add the import I stated above.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  24. Offline

    xXRobbie21Xx

    Gamesareme okay thanks, sorry for the delay, trying to fix up some errors so i can get back on track to this.
     
  25. Offline

    Gamesareme

  26. Offline

    xXRobbie21Xx

    Gamesareme Okay here is my belated confirmation that your plan worked! Thank you so much for the help i really learned allot!
    -rob
     
Thread Status:
Not open for further replies.

Share This Page