From 3b544cd3adbfa0e9635c5035f99f0eaf7dcce116 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sun, 31 Jan 2016 20:38:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86API?= =?UTF-8?q?=E6=93=8D=E4=BD=9C...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../java/cn/citycraft/VbariableAPI/VAPI.java | 61 +++++++++++++++++-- src/main/resources/plugin.yml | 21 +++++++ 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/plugin.yml diff --git a/src/main/java/cn/citycraft/VbariableAPI/VAPI.java b/src/main/java/cn/citycraft/VbariableAPI/VAPI.java index 4581ed4..47dd63d 100644 --- a/src/main/java/cn/citycraft/VbariableAPI/VAPI.java +++ b/src/main/java/cn/citycraft/VbariableAPI/VAPI.java @@ -1,13 +1,66 @@ -/** - * - */ package cn.citycraft.VbariableAPI; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + /** - * + * * @since 2016年1月30日 上午9:29:54 * @author 喵♂呜 */ public class VAPI { + private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([a-zA-Z0-9_.-@-]+)[%]"); + private static final Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([a-zA-Z0-9_.-@-]+)[}]"); + private static Map placeholders = new HashMap<>(); + + public static boolean registerPlaceholderHook(final String plugin, final VariableHook placeholderHook) { + if (plugin == null || placeholderHook == null || VAPI.placeholders.containsKey(plugin)) { + return false; + } + VAPI.placeholders.put(plugin, placeholderHook); + return true; + } + + public static String replaceVariable(final Player player, String text) { + if (text == null || VAPI.placeholders == null || VAPI.placeholders.isEmpty()) { + return text; + } + final Matcher placeholderMatcher = VAPI.PLACEHOLDER_PATTERN.matcher(text); + while (placeholderMatcher.find()) { + final String format = placeholderMatcher.group(1); + final StringBuilder pluginBuilder = new StringBuilder(); + char[] formatArray; + int i; + for (formatArray = format.toCharArray(), i = 0; i < formatArray.length && formatArray[i] != '_'; ++i) { + pluginBuilder.append(formatArray[i]); + } + final String pl = pluginBuilder.toString(); + final StringBuilder identifierBuilder = new StringBuilder(); + for (int b = i + 1; b < formatArray.length; ++b) { + identifierBuilder.append(formatArray[b]); + } + String identifier = identifierBuilder.toString(); + if (identifier.isEmpty()) { + identifier = pl; + } + for (final Entry registered : placeholders.entrySet()) { + if (pl.equalsIgnoreCase(registered.getKey())) { + final VariableReplaceEvent vre = new VariableReplaceEvent(player, identifier); + final String value = registered.getValue().onVariableReplace(vre); + if (value == null) { + continue; + } + text = text.replaceAll("%" + format + "%", Matcher.quoteReplacement(value)); + } + } + } + return ChatColor.translateAlternateColorCodes('&', text); + } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..05274ce --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,21 @@ +name: ${project.artifactId} +description: ${project.description} +main: ${project.groupId}.${project.artifactId}.${project.artifactId} +version: ${project.version}-git-${env.GIT_COMMIT} +author: 喵♂呜 +website: ${jenkins.url}/job/${project.artifactId}/ +commands: + ${project.artifactId}: + description: ${project.artifactId} - ${project.description} + aliases: + - vapi + usage: §b使用/${project.artifactId} help 查看帮助! + permission: ${project.artifactId}.reload + permission-message: §c你没有 的权限来执行此命令! +permissions: + ${project.artifactId}.use: + description: ${project.artifactId} 使用! + default: op + ${project.artifactId}.reload: + description: 重新载入插件! + default: op \ No newline at end of file