mirror of
https://e.coding.net/circlecloud/VbariableAPI.git
synced 2024-11-23 01:58:46 +00:00
更新部分API操作...
Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
parent
c974c98a19
commit
3b544cd3ad
@ -1,13 +1,66 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package cn.citycraft.VbariableAPI;
|
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
|
* @since 2016年1月30日 上午9:29:54
|
||||||
* @author 喵♂呜
|
* @author 喵♂呜
|
||||||
*/
|
*/
|
||||||
public class VAPI {
|
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<String, VariableHook> 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<String, VariableHook> 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
21
src/main/resources/plugin.yml
Normal file
21
src/main/resources/plugin.yml
Normal file
@ -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你没有 <permission> 的权限来执行此命令!
|
||||||
|
permissions:
|
||||||
|
${project.artifactId}.use:
|
||||||
|
description: ${project.artifactId} 使用!
|
||||||
|
default: op
|
||||||
|
${project.artifactId}.reload:
|
||||||
|
description: 重新载入插件!
|
||||||
|
default: op
|
Loading…
Reference in New Issue
Block a user