Towny chat formatter How can this be improved,to make it easier to maintain? There is so much boilerplate being thrown around, so is it possible to use annotations? I've seen one example from Stack Overflow, but it ended up using reflection in order to call the method.

I was already planning on somehow annotating these "variables" in order to automatically produce the documentation, to prevent the documentation going out of date.

GitHub

public class TownyChatFormatter {
private static StringReplaceManager&lt;LocalTownyChatEvent&gt; replacer = new StringReplaceManager&lt;LocalTownyChatEvent&gt;();

static {
    replacer.registerFormatReplacement(Pattern.quote("{worldname}"), new TownyChatReplacerCallable() {
        @Override
        public String call(String match, LocalTownyChatEvent event) throws Exception {
            return String.format(ChatSettings.getWorldTag(), event.getEvent().getPlayer().getWorld().getName());
        }
    });
    replacer.registerFormatReplacement(Pattern.quote("{town}"), new TownyChatReplacerCallable() {
        @Override
        public String call(String match, LocalTownyChatEvent event) throws Exception {
            return event.getResident().hasTown() ? event.getResident().getTown().getName() : "";
        }
    });
If you don't mind using a String switch, and can't use Java 8 yet (lambda expressions should greatly simplify your existing code), you could use something like the following (which replaces the use of inner classes with a single switch):

static {
    register("{worldname}", "{town}");
}

private static void register(final String... patterns) {
    for (final String pattern : patterns) {
        replacer.registerFormatReplacement(Pattern.quote(pattern), new TownyChatReplacerCallable() {
            @Override
            public String call(String match, LocalTownyChatEvent event) throws Exception {
                switch (pattern) {
                    case "{worldname}": return String.format(ChatSettings.getWorldTag(), event.getEvent().getPlayer().getWorld().getName());
                    case "{town}": return event.getResident().hasTown() ? event.getResident().getTown().getName() : "";
                    default: throw new IllegalArgumentException();
                }
            }
        });
    }
}


You would have to profile this to see whether the use of a switch made performance worse or better though (could be better, as the single registered function might be inlined and the switch should be fast).  You could also use constants variables instead of strings, but that would increase code size.