Code not working

Discussion in 'Plugin Development' started by Assult, Dec 6, 2012.

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

    Assult

    Main class
    Code:
    package com.assult.prefixer;
     
    import java.io.File;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin{
        public Main plugin;
        public Main test;
        public Logger log = Logger.getLogger("Minecraft");
     
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new ChatListener(this), this);
            if(!getConfig().contains("chat.format"))
            {
                getConfig().set("chat.format", "{Prefix} {Name}: {Message}");
                saveConfig();
            }
            log.info("Loading Prefixer 2.1");
        }
     
        public void onDisable(){
            log.info("Disabling Prefixer 2.1");
        }
     
        public String format = (ChatColor.RED +"[Prefixer]" + ChatColor.LIGHT_PURPLE);
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            Player player1 = getServer().getPlayer(args[0]);
            if(label.equalsIgnoreCase("setNick"))
            {
                if(args.length == 1)
                {
                 
                if(player.hasPermission("Prefixer.setNick"))
                {
                getConfig().set("Players." + player.getName() + "nickname", args[0]); 
                saveConfig();
             
            }else
            {
                player.sendMessage(format + "You dont have permission to do that!");
            }
         
            }
                else if(args.length == 2)
            {
                    if(player.hasPermission("Prefixer.setNick.others"))
                    {
                        player.sendMessage(format + "Player " + ChatColor.YELLOW + player1.getName() + ChatColor.RED + "'s prefix has been set to " + "'" + args[1] + "'S");
                        getConfig().set("Players." + player1.getName() + "nickname", args[1]);
                        saveConfig();
                    }else
                    {
                        player.sendMessage(format + "You dont have permission to that!");
                    }
                 
            }
             
            }
     
            return false;
     
        }
    }
    
    And heres my Listener class

    Code:
    package com.assult.prefixer;
    package com.assult.prefixer;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
     
    public class ChatListener implements Listener{
        public Main plugin;
       
        public ChatListener(Main instance){
            plugin = instance;
        }
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event){
            Player player = event.getPlayer();
            String shortcut = plugin.getConfig().getString("chat.format");
            shortcut.replace("{Prefix}", plugin.getConfig().getString("Players." + player.getName() + ".prefix"));
            shortcut.replace("{Name}", player.getDisplayName());
            shortcut.replace("{Message}", event.getMessage());
            event.setFormat(shortcut);
            System.out.print(shortcut);
            System.out.print(plugin);
        }
     
    }
     
     
    
    The problem is that when i type something in the chat it will display like this. {Prefix} {Name}: {Message}. And thats where the code stops working...

    My Config.yml

    Code:
    Players:
      AssultMan:
        nickname: '&aHerman'
        prefix: '&cOwner'
    chat:
      format: '{Prefix} {Name}: {Message}'
    
    So any ideas on how to fix?

    Sorry for bumping but i really need this fixed

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    Malikk

    No one's helping because this is formatted extremely poorly and they can't figure out what the problem is besides "the code stops working"
     
  3. Offline

    Assult

    Theres no errors i n the console and in the chat is just says '{Prefix} {Name}: {Message}' I gave you all my code so i think there is no other information i can give
     
  4. Offline

    HON95

    Because strings are immutable, they can't be changed. Thus thre reason why String.replace() returns a string.
    Code:JAVA
    1. shortcut = shortcut.replace(a, b)

    Oh, btw, that's not how you're supposed to use the setFormat() method.

    EDIT: About the setFormat() method: use "%1$s" for the player's display name and "%2$s" for the message. They will obz get replaced. You can change the message using setMessage().
     
    Assult likes this.
  5. Offline

    nisovin

    Strings are immutable. Using String.replace returns a new string, it doesn't modify the string it's used on.
     
  6. Offline

    Assult

    How am i supposed to use it? Im trying to add a prefix, thats all
     
  7. Offline

    HON95

    The easiest solution is to just write "shortcut = " in front of your three "shortcut.replace(...)" lines.
     
    Assult likes this.
  8. Offline

    Assult

    Thank you so much! Its working perfectly
     
    HON95 likes this.
Thread Status:
Not open for further replies.

Share This Page