001/*
002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
003 *
004 *  Permission is hereby granted, free of charge, to any person obtaining
005 *  a copy of this software and associated documentation files (the
006 *  "Software"), to deal in the Software without restriction, including
007 *  without limitation the rights to use, copy, modify, merge, publish,
008 *  distribute, sublicense, and/or sell copies of the Software, and to
009 *  permit persons to whom the Software is furnished to do so, subject to
010 *  the following conditions:
011 *
012 *  The above copyright notice and this permission notice shall be
013 *  included in all copies or substantial portions of the Software.
014 *
015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022 */
023
024package co.aikar.commands;
025
026import org.jetbrains.annotations.Nullable;
027
028import java.util.AbstractMap;
029import java.util.LinkedHashMap;
030import java.util.Map;
031import java.util.regex.Matcher;
032import java.util.regex.Pattern;
033
034/**
035 * Manages replacement template strings
036 */
037public class CommandReplacements {
038
039    private final CommandManager manager;
040    private final Map<String, Map.Entry<Pattern, String>> replacements = new LinkedHashMap<>();
041
042    CommandReplacements(CommandManager manager) {
043        this.manager = manager;
044        addReplacement0("truthy", "true|false|yes|no|1|0|on|off|t|f");
045    }
046
047    public void addReplacements(String... replacements) {
048        if (replacements.length == 0 || replacements.length % 2 != 0) {
049            throw new IllegalArgumentException("Must pass a number of arguments divisible by 2.");
050        }
051        for (int i = 0; i < replacements.length; i += 2) {
052            addReplacement(replacements[i], replacements[i+1]);
053        }
054    }
055
056    public String addReplacement(String key, String val) {
057        return addReplacement0(key, val);
058    }
059
060    @Nullable
061    private String addReplacement0(String key, String val) {
062        key = ACFPatterns.PERCENTAGE.matcher(key.toLowerCase()).replaceAll("");
063        Pattern pattern = Pattern.compile("%" + Pattern.quote(key) + "\\b", Pattern.CASE_INSENSITIVE);
064
065        Map.Entry<Pattern, String> entry = new AbstractMap.SimpleImmutableEntry<>(pattern, val);
066        Map.Entry<Pattern, String> replaced = replacements.put(key, entry);
067
068        if (replaced != null) {
069            return replaced.getValue();
070        }
071
072        return null;
073    }
074
075    public String replace(String text) {
076        if (text == null) {
077            return null;
078        }
079
080        for (Map.Entry<Pattern, String> entry : replacements.values()) {
081            text = entry.getKey().matcher(text).replaceAll(entry.getValue());
082        }
083
084        // check for unregistered replacements
085        Pattern pattern = Pattern.compile("%.[^\\s]*");
086        Matcher matcher = pattern.matcher(text);
087        while (matcher.find()) {
088            this.manager.log(LogLevel.ERROR, "Found unregistered replacement: " + matcher.group());
089        }
090
091        return text;
092    }
093}