1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-26 07:18: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>
<groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId>
<version>1.8.7</version>
<version>1.8.8</version>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>

View File

@ -13,9 +13,13 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import me.clip.placeholderapi.PlaceholderAPI;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C;
/**
* @author MiaoWoo
*/
public class Replace {
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) {
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) {
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 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) {
final Matcher m = PLACEHOLDER_PATTERN.matcher(text);
@ -51,18 +63,19 @@ public class Replace {
final String[] ka = format.split("_", 2);
String value = null;
switch (ka[0]) {
case "player":
value = player(player, ka[1]);
break;
case "server":
value = server(player, ka[1]);
break;
case "plugin":
value = plugin(player, ka[1]);
break;
case "time":
value = time(player, ka[1]);
break;
case "player":
value = player(player, ka[1]);
break;
case "server":
value = server(player, ka[1]);
break;
case "plugin":
value = plugin(player, ka[1]);
break;
case "time":
value = time(player, ka[1]);
break;
default:
}
if (value != null) {
text = text.replace("%" + format + "%", Matcher.quoteReplacement(value));
@ -74,63 +87,63 @@ public class Replace {
private static String player(final Player player, final String key) {
switch (key) {
case "x":
return String.valueOf(player.getLocation().getBlockX());
case "y":
return String.valueOf(player.getLocation().getBlockY());
case "z":
return String.valueOf(player.getLocation().getBlockZ());
case "yaw":
return String.valueOf(Math.round(player.getLocation().getYaw() * 100) / 100);
case "pitch":
return String.valueOf(Math.round(player.getLocation().getPitch() * 100) / 100);
case "world":
return player.getWorld().getName();
case "name":
return player.getName();
case "displayname":
return player.getDisplayName();
case "health":
return String.valueOf(player.getHealth());
case "max_health":
return String.valueOf(player.getMaxHealth());
default:
return String.format("%%player_%s%%", key);
case "x":
return String.valueOf(player.getLocation().getBlockX());
case "y":
return String.valueOf(player.getLocation().getBlockY());
case "z":
return String.valueOf(player.getLocation().getBlockZ());
case "yaw":
return String.valueOf(Math.round(player.getLocation().getYaw() * 100) / 100);
case "pitch":
return String.valueOf(Math.round(player.getLocation().getPitch() * 100) / 100);
case "world":
return player.getWorld().getName();
case "name":
return player.getName();
case "displayname":
return player.getDisplayName();
case "health":
return String.valueOf(player.getHealth());
case "max_health":
return String.valueOf(player.getMaxHealth());
default:
return String.format("%%player_%s%%", key);
}
}
private static String plugin(final Player player, final String key) {
switch (key) {
case "version":
return P.getDescription().getVersion().split("-")[0];
case "name":
return P.getName();
case "author":
return Arrays.toString(P.getDescription().getAuthors().toArray());
default:
return String.format("%%plugin_%s%%", key);
case "version":
return P.getDescription().getVersion().split("-")[0];
case "name":
return P.getName();
case "author":
return Arrays.toString(P.getDescription().getAuthors().toArray());
default:
return String.format("%%plugin_%s%%", key);
}
}
private static String server(final Player player, final String key) {
final Runtime runtime = Runtime.getRuntime();
switch (key) {
case "online":
return String.valueOf(C.Player.getOnlinePlayers().size());
case "max":
return String.valueOf(Bukkit.getMaxPlayers());
case "unique_joins":
return String.valueOf(Bukkit.getOfflinePlayers().length);
case "ram_used":
return String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / 1048576L);
case "ram_free":
return String.valueOf(runtime.freeMemory() / 1048576L);
case "ram_total":
return String.valueOf(runtime.totalMemory() / 1048576L);
case "ram_max":
return String.valueOf(runtime.maxMemory() / 1048576L);
default:
return String.format("%%server_%s%%", key);
case "online":
return String.valueOf(C.Player.getOnlinePlayers().size());
case "max":
return String.valueOf(Bukkit.getMaxPlayers());
case "unique_joins":
return String.valueOf(Bukkit.getOfflinePlayers().length);
case "ram_used":
return String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / 1048576L);
case "ram_free":
return String.valueOf(runtime.freeMemory() / 1048576L);
case "ram_total":
return String.valueOf(runtime.totalMemory() / 1048576L);
case "ram_max":
return String.valueOf(runtime.maxMemory() / 1048576L);
default:
return String.format("%%server_%s%%", key);
}
}
@ -140,27 +153,28 @@ public class Replace {
final String time = key.split("_")[1];
String value = "解析错误";
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);
} catch (final ParseException ignored) {
}
return value;
}
switch (key) {
case "now":
return df.format(date);
case "year":
return String.valueOf(date.getYear() + 1900);
case "month":
return String.valueOf(date.getMonth() + 1);
case "day":
return String.valueOf(date.getDate());
case "hour":
return String.valueOf(date.getHours() + 1);
case "minute":
return String.valueOf(date.getMinutes());
case "second":
return String.valueOf(date.getSeconds());
case "now":
return DF.get().format(date);
case "year":
return String.valueOf(date.getYear() + 1900);
case "month":
return String.valueOf(date.getMonth() + 1);
case "day":
return String.valueOf(date.getDate());
case "hour":
return String.valueOf(date.getHours() + 1);
case "minute":
return String.valueOf(date.getMinutes());
case "second":
return String.valueOf(date.getSeconds());
default:
}
return String.format("%%time_%s%%", key);
}