Bukkit/Spigot HeroChat 5: Custom Replacement in format string

Discussion in 'Programming' started by Foxtrek_64, Jun 9, 2016.

  1. This is something that has eluded me for quite a long time...

    in HeroChat's config.yml, as well as the config for each individual chat channel, you can define a format string. Inside this format string is a set of replacement strings, seen as words inside of curly brackets, e.g. "{prefix}".

    One thing I've never been able to figure out, until now, is how to create my own replacement string. So, having figured it out, I thought I'd provide the solution here as a resource. The following code is a PlayerListener that listens to the HeroChat ChannelChatEvent and modifies it.

    This code assumes that you have some sort of method to generate a player-specific or group-specific replacement string (i.e. what your custom replacement string in the config.yml turns into).

    For the fully commented code, view the StackOverflow thread here:
    https://stackoverflow.com/questions...rochat-5-custom-replacement-in-format-string/

    Here's the code. This code assumes that the formatting string you have inside of your config.yml is "{title}". Please note that this code is provided as-is and I make no disclaimer of warranty, nor do I guarantee that it is the most efficient code or bug-free. If you happen to find a way to make it more efficient or find a bug, feel free to reply and I'll fix it.

    That being said, here's the code.

    Code (Text):
    package your.package.here;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import com.dthielke.herochat.ChannelChatEvent;
    import com.dthielke.herochat.ChannelManager;
    import com.dthielke.herochat.Herochat;
    import your.package.here.MyPlugin;

    public class MyPluginPlayerListener implements Listener {

        private MyPlugin plugin;

        public MyPluginPlayerListener(MyPlugin plugin) {
            this.plugin = plugin;
        }

        @EventHandler
        public void onPlayerChat(ChannelChatEvent event) {
            ChannelManager cm = Herochat.getChannelManager();
            String newFormat;
            Player player = event.getSender().getPlayer();
            String chatTitle = plugin.titlesConfig.getTitle(player);

           if (!chatTitle.equalsIgnoreCase("")) {
                chatTitle = "[" + chatTitle + "]";
            }
            if (event.getFormat().equalsIgnoreCase("{default}")) {
                newFormat = cm.getStandardFormat();
                newFormat = newFormat.replaceAll("\\{title\\}", chatTitle);
            } else {
                newFormat = event.getFormat();
                newFormat = newFormat.replaceAll("\\{title\\}", chatTitle);
            }
            event.setFormat(newFormat);
        }
    }
     
    #1 Foxtrek_64, Jun 9, 2016
    Last edited: Jun 9, 2016
  2. May have put this in the wrong thread by accident. If I did so, would a member of staff mind moving it and deleting this reply?
     
  3. Omnivion

    Patron

    just use .replace

    String#replace(CharSequence target, CharSequence replacement)
     
  4. I use String.replaceAll() because I cannot guarantee that there will only be one instance of the replacement string within the message format.
     
  5. Omnivion

    Patron

    #read #the #javadocs
     
  6. Alright, .replaceAll uses regex, .replace does not. The only difference would be how I format the initial replace string. If changing to .replace increases execution time, great, but StackOverflow does not seem to think so. In other words, I'm going to say it's personal preference. This is provided as a resource - it's designed to be taken, changed, and added to however people see fit.
     
  7. Omnivion

    Patron

    uhhh what

    .replace doesn't compile your sting to regex, or use regex at all. It'll make your code much faster...

    What even is life. Where do you people keep coming from that you don't know that compiling + using regex will make your shit slower
     
    • Agree Agree x 1
  8. I do agree that regex can slow things down, but with this application, I don't believe the difference is big enough to matter. If you'd like, I can install timers into the plugin and do a side-by-side speed comparison, but I have a feeling the difference will be milliseconds.