Skip to content

Commit

Permalink
Hex color support and papi placeholders support
Browse files Browse the repository at this point in the history
  • Loading branch information
StarchierOrb committed Jul 24, 2021
1 parent 86e327e commit a7b5d93
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.starchier.inventorykeeper.InventoryKeeper;
import me.starchier.inventorykeeper.util.PluginHandler;
import me.starchier.inventorykeeper.util.StringUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void runCommands(Player player, boolean isDeathCommand, String path, bool
continue;
}
if (isDeathCommand) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), s.replace("%player%", player.getName()));
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), StringUtil.replacePlaceholder(s.replace("%player%", player.getName()), player));
} else {
runCmds.add(s.replace("%player%", player.getName()));
}
Expand All @@ -74,13 +75,13 @@ public void run() {
boolean isOP = player.isOp();
try {
player.setOp(true);
Bukkit.dispatchCommand(player, s.replace("[op]", ""));
Bukkit.dispatchCommand(player, StringUtil.replacePlaceholder(s.replace("[op]", ""), player));
} catch (Exception e) {
} finally {
player.setOp(isOP);
}
} else {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), s);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), StringUtil.replacePlaceholder(s, player));
}
}
}
Expand All @@ -95,7 +96,7 @@ private boolean runOpCommand(Player player, boolean isDeath, String s) {
boolean isOP = player.isOp();
try {
player.setOp(true);
Bukkit.dispatchCommand(player, fixCmd.replace("%player%", player.getName()));
Bukkit.dispatchCommand(player, StringUtil.replacePlaceholder(fixCmd.replace("%player%", player.getName()), player));
} catch (Exception e) {
} finally {
player.setOp(isOP);
Expand Down Expand Up @@ -131,7 +132,7 @@ public void runRandomCommands(Player player, boolean isDeath, String path, boole
runCmds.add(s.replace("%player%", player.getName()));
continue;
}
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), s.replace("%player%", player.getName()));
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), StringUtil.replacePlaceholder(s.replace("%player%", player.getName()), player));
}
runCommandsOnRespawn(player, isDeath, runCmds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public static boolean isLegacy() {

public String getConfigValue(String path, boolean isGeneralConfig) {
if (isGeneralConfig) {
return ChatColor.translateAlternateColorCodes('&', generalConfig.getString("settings." + path, null));
return StringUtil.transform(ChatColor.translateAlternateColorCodes('&', generalConfig.getString("settings." + path, null)));
} else {
return ChatColor.translateAlternateColorCodes('&', itemsConfig.getString("items." + path, null));
return StringUtil.transform(ChatColor.translateAlternateColorCodes('&', itemsConfig.getString("items." + path, null)));
}
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public Boolean isBlackList(boolean isEntityList, String itemGroup) {
}

public String getMessage(String path) {
return ChatColor.translateAlternateColorCodes('&', generalConfig.getString("messages." + path, null));
return StringUtil.transform(ChatColor.translateAlternateColorCodes('&', generalConfig.getString("messages." + path, null)));
}

public List<String> getList(String path, boolean isGeneralConfig) {
Expand All @@ -106,6 +106,7 @@ public List<String> getList(String path, boolean isGeneralConfig) {
List<String> fixList = new ArrayList<>();
for (String s : originList) {
s = ChatColor.translateAlternateColorCodes('&', s);
s = StringUtil.transform(s);
fixList.add(s);
}
return fixList;
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/me/starchier/inventorykeeper/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.starchier.inventorykeeper.util;

import me.clip.placeholderapi.PlaceholderAPI;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.entity.Player;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
private static final Pattern hexPattern = Pattern.compile("(#[a-fA-F0-9]{6})");

public static String transform(String str) {
try {
Matcher matcher = hexPattern.matcher(str);
while (matcher.find()) {
String hexCode = str.substring(matcher.start(), matcher.end());
str = str.replace(hexCode, ChatColor.of(hexCode).toString());
}
} catch (Exception ignored) {
}
return str;
}

public static String replacePlaceholder(String str, Player player) {
if (PlaceholderAPI.containsPlaceholders(str)) {
return PlaceholderAPI.setPlaceholders(player, str);
}
return str;
}
}

0 comments on commit a7b5d93

Please sign in to comment.