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);
@ -51,18 +63,19 @@ public class Replace {
final String[] ka = format.split("_", 2); final String[] ka = format.split("_", 2);
String value = null; String value = null;
switch (ka[0]) { switch (ka[0]) {
case "player": case "player":
value = player(player, ka[1]); value = player(player, ka[1]);
break; break;
case "server": case "server":
value = server(player, ka[1]); value = server(player, ka[1]);
break; break;
case "plugin": case "plugin":
value = plugin(player, ka[1]); value = plugin(player, ka[1]);
break; break;
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));
@ -74,63 +87,63 @@ public class Replace {
private static String player(final Player player, final String key) { private static String player(final Player player, final String key) {
switch (key) { switch (key) {
case "x": case "x":
return String.valueOf(player.getLocation().getBlockX()); return String.valueOf(player.getLocation().getBlockX());
case "y": case "y":
return String.valueOf(player.getLocation().getBlockY()); return String.valueOf(player.getLocation().getBlockY());
case "z": case "z":
return String.valueOf(player.getLocation().getBlockZ()); return String.valueOf(player.getLocation().getBlockZ());
case "yaw": case "yaw":
return String.valueOf(Math.round(player.getLocation().getYaw() * 100) / 100); return String.valueOf(Math.round(player.getLocation().getYaw() * 100) / 100);
case "pitch": case "pitch":
return String.valueOf(Math.round(player.getLocation().getPitch() * 100) / 100); return String.valueOf(Math.round(player.getLocation().getPitch() * 100) / 100);
case "world": case "world":
return player.getWorld().getName(); return player.getWorld().getName();
case "name": case "name":
return player.getName(); return player.getName();
case "displayname": case "displayname":
return player.getDisplayName(); return player.getDisplayName();
case "health": case "health":
return String.valueOf(player.getHealth()); return String.valueOf(player.getHealth());
case "max_health": case "max_health":
return String.valueOf(player.getMaxHealth()); return String.valueOf(player.getMaxHealth());
default: default:
return String.format("%%player_%s%%", key); return String.format("%%player_%s%%", key);
} }
} }
private static String plugin(final Player player, final String key) { private static String plugin(final Player player, final String key) {
switch (key) { switch (key) {
case "version": case "version":
return P.getDescription().getVersion().split("-")[0]; return P.getDescription().getVersion().split("-")[0];
case "name": case "name":
return P.getName(); return P.getName();
case "author": case "author":
return Arrays.toString(P.getDescription().getAuthors().toArray()); return Arrays.toString(P.getDescription().getAuthors().toArray());
default: default:
return String.format("%%plugin_%s%%", key); return String.format("%%plugin_%s%%", key);
} }
} }
private static String server(final Player player, final String key) { private static String server(final Player player, final String key) {
final Runtime runtime = Runtime.getRuntime(); final Runtime runtime = Runtime.getRuntime();
switch (key) { switch (key) {
case "online": case "online":
return String.valueOf(C.Player.getOnlinePlayers().size()); return String.valueOf(C.Player.getOnlinePlayers().size());
case "max": case "max":
return String.valueOf(Bukkit.getMaxPlayers()); return String.valueOf(Bukkit.getMaxPlayers());
case "unique_joins": case "unique_joins":
return String.valueOf(Bukkit.getOfflinePlayers().length); return String.valueOf(Bukkit.getOfflinePlayers().length);
case "ram_used": case "ram_used":
return String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / 1048576L); return String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / 1048576L);
case "ram_free": case "ram_free":
return String.valueOf(runtime.freeMemory() / 1048576L); return String.valueOf(runtime.freeMemory() / 1048576L);
case "ram_total": case "ram_total":
return String.valueOf(runtime.totalMemory() / 1048576L); return String.valueOf(runtime.totalMemory() / 1048576L);
case "ram_max": case "ram_max":
return String.valueOf(runtime.maxMemory() / 1048576L); return String.valueOf(runtime.maxMemory() / 1048576L);
default: default:
return String.format("%%server_%s%%", key); return String.format("%%server_%s%%", key);
} }
} }
@ -140,27 +153,28 @@ 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) {
} }
return value; return value;
} }
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":
return String.valueOf(date.getMonth() + 1); return String.valueOf(date.getMonth() + 1);
case "day": case "day":
return String.valueOf(date.getDate()); return String.valueOf(date.getDate());
case "hour": case "hour":
return String.valueOf(date.getHours() + 1); return String.valueOf(date.getHours() + 1);
case "minute": case "minute":
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);
} }