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