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