1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-27 07:28:52 +00:00

feat: 更新Replace添加提示

This commit is contained in:
502647092 2018-03-05 18:52:08 +08:00
parent e74c8275e2
commit f692881ec5
2 changed files with 92 additions and 78 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId> <artifactId>YumCore</artifactId>
<version>1.8.7</version> <version>1.8.8</version>
<build> <build>
<finalName>${project.artifactId}</finalName> <finalName>${project.artifactId}</finalName>
<plugins> <plugins>

View File

@ -13,9 +13,13 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C; import pw.yumc.YumCore.bukkit.compatible.C;
/**
* @author MiaoWoo
*/
public class Replace { public class Replace {
private static boolean papi = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI"); private static boolean papi = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
@ -32,16 +36,24 @@ public class Replace {
} }
private static String p(final Player p, final String text) { private static String p(final Player p, final String text) {
return PlaceholderAPI.setPlaceholders(p, s(p, text)); String result = s(p, text);
try {
return PlaceholderAPI.setPlaceholders(p, result);
} catch (Exception ex) {
Log.console("§4[Replace] §cPAPI变量 §a%s 替换时发生错误 §4%s: %s §6请检查 §bPlaceholderAPI §6扩展配置!", text, ex.getClass().getName(), ex.getMessage());
ex.printStackTrace();
return result;
}
} }
private static String s(final Player p, final String text) { private static String s(final Player p, final String text) {
return SimpleRelpace.$(p, text); return SimpleReplace.$(p, text);
} }
static class SimpleRelpace { static class SimpleReplace {
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]"); private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final ThreadLocal<SimpleDateFormat> DF = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
public static String $(final Player player, String text) { public static String $(final Player player, String text) {
final Matcher m = PLACEHOLDER_PATTERN.matcher(text); final Matcher m = PLACEHOLDER_PATTERN.matcher(text);
@ -63,6 +75,7 @@ public class Replace {
case "time": case "time":
value = time(player, ka[1]); value = time(player, ka[1]);
break; break;
default:
} }
if (value != null) { if (value != null) {
text = text.replace("%" + format + "%", Matcher.quoteReplacement(value)); text = text.replace("%" + format + "%", Matcher.quoteReplacement(value));
@ -140,7 +153,7 @@ public class Replace {
final String time = key.split("_")[1]; final String time = key.split("_")[1];
String value = "解析错误"; String value = "解析错误";
try { try {
final long left = df.parse(time).getTime() - System.currentTimeMillis(); final long left = DF.get().parse(time).getTime() - System.currentTimeMillis();
value = String.valueOf(left / 1000); value = String.valueOf(left / 1000);
} catch (final ParseException ignored) { } catch (final ParseException ignored) {
} }
@ -148,7 +161,7 @@ public class Replace {
} }
switch (key) { switch (key) {
case "now": case "now":
return df.format(date); return DF.get().format(date);
case "year": case "year":
return String.valueOf(date.getYear() + 1900); return String.valueOf(date.getYear() + 1900);
case "month": case "month":
@ -161,6 +174,7 @@ public class Replace {
return String.valueOf(date.getMinutes()); return String.valueOf(date.getMinutes());
case "second": case "second":
return String.valueOf(date.getSeconds()); return String.valueOf(date.getSeconds());
default:
} }
return String.format("%%time_%s%%", key); return String.format("%%time_%s%%", key);
} }