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.Pattern;
032
033/**
034 * Manages replacement template strings
035 */
036public class CommandReplacements {
037
038    private final CommandManager manager;
039    private final Map<String, Map.Entry<Pattern, String>> replacements = new LinkedHashMap<>();
040
041    CommandReplacements(CommandManager manager) {
042        this.manager = manager;
043        addReplacement0("truthy", "true|false|yes|no|1|0|on|off|t|f");
044    }
045
046    public void addReplacements(String... replacements) {
047        if (replacements.length == 0 || replacements.length % 2 != 0) {
048            throw new IllegalArgumentException("Must pass a number of arguments divisible by 2.");
049        }
050        for (int i = 0; i < replacements.length; i += 2) {
051            addReplacement(replacements[i], replacements[i+1]);
052        }
053    }
054
055    public String addReplacement(String key, String val) {
056        if (this.manager.hasRegisteredCommands()) {
057            this.manager.log(LogLevel.ERROR, "You are registering replacements after you have registered your commands!");
058            this.manager.log(LogLevel.ERROR, "This is not allowed, and this replacement (" + key + ") will not work for any previously registered command.");
059        }
060
061        return addReplacement0(key, val);
062    }
063
064    @Nullable
065    private String addReplacement0(String key, String val) {
066        key = ACFPatterns.PERCENTAGE.matcher(key.toLowerCase()).replaceAll("");
067        Pattern pattern = Pattern.compile("%" + Pattern.quote(key) + "\\b", Pattern.CASE_INSENSITIVE);
068
069        Map.Entry<Pattern, String> entry = new AbstractMap.SimpleImmutableEntry<>(pattern, val);
070        Map.Entry<Pattern, String> replaced = replacements.put(key, entry);
071
072        if (replaced != null) {
073            return replaced.getValue();
074        }
075
076        return null;
077    }
078
079    public String replace(String text) {
080        if (text == null) {
081            return null;
082        }
083
084        for (Map.Entry<Pattern, String> entry : replacements.values()) {
085            text = entry.getKey().matcher(text).replaceAll(entry.getValue());
086        }
087
088        return manager.getLocales().replaceI18NStrings(text);
089    }
090}