Skip to content

Commit 5b26459

Browse files
committed
Cache regex pattern
1 parent dcf2b4a commit 5b26459

File tree

10 files changed

+70
-36
lines changed

10 files changed

+70
-36
lines changed

DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/hooks/DiscordSRVExtension.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
public class DiscordSRVExtension {
3030

31-
private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + "§" + "[0-9A-FK-ORX]");
32-
3331
public DiscordSRVExtension() {
3432
}
3533

@@ -71,7 +69,7 @@ public void sendDiscordMessage(TextComponent[] components, MessageType messageTy
7169

7270
// Try to strip Minecraft format code to plain text
7371
if (message.contains("§")) {
74-
message = STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
72+
message = Util.STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
7573
}
7674

7775
if (getMessages().getString("Discord.DeathMessage.Text").isEmpty()) {
@@ -128,7 +126,7 @@ public void sendEntityDiscordMessage(TextComponent[] components, MessageType mes
128126

129127
// Try to strip Minecraft format code to plain text
130128
if (message.contains("§")) {
131-
message = STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
129+
message = Util.STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
132130
}
133131

134132
if (getMessages().getString("Discord.DeathMessage.Text").isEmpty()) {

DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/Assets.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import java.util.Optional;
5454
import java.util.concurrent.ThreadLocalRandom;
5555
import java.util.regex.Matcher;
56-
import java.util.regex.Pattern;
5756

5857
public class Assets {
5958

@@ -822,7 +821,7 @@ public static List<String> sortList(List<String> list, Player victim, Entity kil
822821
for (String s : list) {
823822
// Check for victim permission messages
824823
if (s.contains("PERMISSION[")) {
825-
Matcher m = Pattern.compile("PERMISSION\\[(.*?)]").matcher(s);
824+
Matcher m = Util.DM_PERM_PATTERN.matcher(s);
826825
while (m.find()) {
827826
String perm = m.group(1);
828827
s = victim.hasPermission(perm)
@@ -831,7 +830,7 @@ public static List<String> sortList(List<String> list, Player victim, Entity kil
831830
}
832831
// Check for killer permission messages
833832
if (s.contains("PERMISSION_KILLER[")) {
834-
Matcher m = Pattern.compile("PERMISSION_KILLER\\[(.*?)]").matcher(s);
833+
Matcher m = Util.DM_KILLER_PERM_PATTERN.matcher(s);
835834
while (m.find()) {
836835
String perm = m.group(1);
837836
s = killer.hasPermission(perm)
@@ -840,7 +839,7 @@ public static List<String> sortList(List<String> list, Player victim, Entity kil
840839
}
841840
// Check for region specific messages
842841
if (s.contains("REGION[")) {
843-
Matcher m = Pattern.compile("REGION\\[(.*?)]").matcher(s);
842+
Matcher m = Util.DM_REGION_PATTERN.matcher(s);
844843
while (m.find()) {
845844
String regionID = m.group(1);
846845
s = DeathMessages.getHooks().worldGuardExtension.isInRegion(victim, regionID)
@@ -893,7 +892,7 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
893892
}
894893

895894
if (DeathMessages.getHooks().placeholderAPIEnabled) {
896-
Matcher identifiers = Pattern.compile("%([^%]+)%").matcher(Util.convertToLegacy(msg));
895+
Matcher identifiers = Util.PAPI_PLACEHOLDER_PATTERN.matcher(Util.convertToLegacy(msg));
897896

898897
while (identifiers.find()) {
899898
String identifier = identifiers.group(0);
@@ -1008,7 +1007,7 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,
10081007
}
10091008

10101009
if (DeathMessages.getHooks().placeholderAPIEnabled) {
1011-
Matcher params = Pattern.compile("%([^%]+)%").matcher(Util.convertToLegacy(msg));
1010+
Matcher params = Util.PAPI_PLACEHOLDER_PATTERN.matcher(Util.convertToLegacy(msg));
10121011

10131012
while (params.find()) {
10141013
String param = params.group(0);

DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/ComponentUtil.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public static String sortHoverEvents(String msg, List<String> rawEvents) {
3434
// If contains event string, process, otherwise return original msg directly
3535
if (msg.contains("[")) {
3636
int index = 0;
37-
// Match all string between [ and ], e.g. aaa[123]bbb -> [123]
38-
Pattern pattern = Pattern.compile("\\[(.*?)]");
39-
Matcher matcher = pattern.matcher(msg);
37+
Matcher matcher = Util.DM_HOVER_EVENT_PATTERN.matcher(msg);
4038

4139
while (matcher.find()) {
4240
String group = matcher.group(1);

DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/MaterialUtil.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bukkit.event.entity.EntityDamageEvent;
88
import org.bukkit.inventory.ItemStack;
99

10+
import java.util.List;
1011
import java.util.regex.Matcher;
1112
import java.util.regex.Pattern;
1213

@@ -63,14 +64,27 @@ public static boolean itemNameIsWeapon(ItemStack itemStack) {
6364
if (itemStack == null || !itemStack.hasItemMeta() || !itemStack.getItemMeta().hasDisplayName()) return false;
6465

6566
String displayName = itemStack.getItemMeta().getDisplayName();
67+
Pattern[] customWeaponNames = Util.customWeaponNamePatterns;
6668

67-
for (String s : Settings.getInstance().getConfig().getStringList(Config.CUSTOM_ITEM_DISPLAY_NAMES_IS_WEAPON.getPath())) {
68-
Pattern pattern = Pattern.compile(s);
69+
if (customWeaponNames == null) {
70+
List<String> customWeaponNameList = Settings.getInstance().getConfig().getStringList(Config.CUSTOM_ITEM_DISPLAY_NAMES_IS_WEAPON.getPath());
71+
customWeaponNames = new Pattern[customWeaponNameList.size()];
72+
73+
for (int i = 0; i < customWeaponNameList.size(); i++) {
74+
String customWeaponName = customWeaponNameList.get(i);
75+
customWeaponNames[i] = Pattern.compile(customWeaponName);
76+
}
77+
78+
Util.customWeaponNamePatterns = customWeaponNames; // Update cache
79+
}
80+
81+
for (Pattern pattern : customWeaponNames) {
6982
Matcher matcher = pattern.matcher(displayName);
7083
if (matcher.find()) {
7184
return true;
7285
}
7386
}
87+
7488
return false;
7589
}
7690

DeathMessagesLegacy/src/main/java/dev/mrshawn/deathmessages/utils/Util.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public class Util {
4747
.replacement(convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix")))
4848
.build();
4949

50+
private static final Pattern BUNGEE_RGB_PATTERN = Pattern.compile("(?<!&)(#[0-9a-fA-F]{6})"); // Match bungee RGB color code only, use Negative Lookbehind to avoid matching code begin with &
51+
public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + "§" + "[0-9A-FK-ORX]");
52+
public static final Pattern DM_PERM_PATTERN = Pattern.compile("PERMISSION\\[(.*?)]");
53+
public static final Pattern DM_KILLER_PERM_PATTERN = Pattern.compile("PERMISSION_KILLER\\[(.*?)]");
54+
public static final Pattern DM_REGION_PATTERN = Pattern.compile("REGION\\[(.*?)]");
55+
public static final Pattern DM_HOVER_EVENT_PATTERN = Pattern.compile("\\[(.*?)]"); // Match all string between [ and ], e.g. aaa[123]bbb -> [123]
56+
public static final Pattern PAPI_PLACEHOLDER_PATTERN = Pattern.compile("%([^%]+)%");
57+
58+
public static Pattern[] customWeaponNamePatterns;
59+
5060
public static final Map<UUID, LivingEntity> crystalDeathData = new HashMap<>(); // <EndCrystal UUID, Causing Entity instance>
5161

5262
public static TextReplacementConfig replace(String matchLiteral, String replace) {
@@ -85,9 +95,7 @@ public static String convertToLegacy(Component component) {
8595
*/
8696
private static String colorizeBungeeRGB(String s) {
8797
if (isNewerAndEqual(16, 0)) {
88-
// Match bungee RGB color code only, use Negative Lookbehind to avoid matching code begin with &
89-
Pattern pattern = Pattern.compile("(?<!&)(#[0-9a-fA-F]{6})");
90-
Matcher matcher = pattern.matcher(s);
98+
Matcher matcher = BUNGEE_RGB_PATTERN.matcher(s);
9199
StringBuilder result = new StringBuilder();
92100

93101
while (matcher.find()) {

DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/hooks/DiscordSRVExtension.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
public class DiscordSRVExtension {
3030

31-
private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + "§" + "[0-9A-FK-ORX]");
32-
3331
public DiscordSRVExtension() {
3432
}
3533

@@ -71,7 +69,7 @@ public void sendDiscordMessage(TextComponent[] components, MessageType messageTy
7169

7270
// Try to strip Minecraft format code to plain text
7371
if (message.contains("§")) {
74-
message = STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
72+
message = Util.STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
7573
}
7674

7775
if (getMessages().getString("Discord.DeathMessage.Text").isEmpty()) {
@@ -128,7 +126,7 @@ public void sendEntityDiscordMessage(TextComponent[] components, MessageType mes
128126

129127
// Try to strip Minecraft format code to plain text
130128
if (message.contains("§")) {
131-
message = STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
129+
message = Util.STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
132130
}
133131

134132
if (getMessages().getString("Discord.DeathMessage.Text").isEmpty()) {

DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/Assets.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import java.util.Optional;
5353
import java.util.concurrent.ThreadLocalRandom;
5454
import java.util.regex.Matcher;
55-
import java.util.regex.Pattern;
5655

5756
public class Assets {
5857

@@ -806,7 +805,7 @@ public static List<String> sortList(List<String> list, Player victim, Entity kil
806805
for (String s : list) {
807806
// Check for victim permission messages
808807
if (s.contains("PERMISSION[")) {
809-
Matcher m = Pattern.compile("PERMISSION\\[(.*?)]").matcher(s);
808+
Matcher m = Util.DM_PERM_PATTERN.matcher(s);
810809
while (m.find()) {
811810
String perm = m.group(1);
812811
s = victim.hasPermission(perm)
@@ -815,7 +814,7 @@ public static List<String> sortList(List<String> list, Player victim, Entity kil
815814
}
816815
// Check for killer permission messages
817816
if (s.contains("PERMISSION_KILLER[")) {
818-
Matcher m = Pattern.compile("PERMISSION_KILLER\\[(.*?)]").matcher(s);
817+
Matcher m = Util.DM_KILLER_PERM_PATTERN.matcher(s);
819818
while (m.find()) {
820819
String perm = m.group(1);
821820
s = killer.hasPermission(perm)
@@ -824,7 +823,7 @@ public static List<String> sortList(List<String> list, Player victim, Entity kil
824823
}
825824
// Check for region specific messages
826825
if (s.contains("REGION[")) {
827-
Matcher m = Pattern.compile("REGION\\[(.*?)]").matcher(s);
826+
Matcher m = Util.DM_REGION_PATTERN.matcher(s);
828827
while (m.find()) {
829828
String regionID = m.group(1);
830829
s = DeathMessages.getHooks().worldGuardExtension.isInRegion(victim, regionID)
@@ -877,7 +876,7 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
877876
}
878877

879878
if (DeathMessages.getHooks().placeholderAPIEnabled) {
880-
Matcher identifiers = Pattern.compile("%([^%]+)%").matcher(Util.convertToLegacy(msg));
879+
Matcher identifiers = Util.PAPI_PLACEHOLDER_PATTERN.matcher(Util.convertToLegacy(msg));
881880

882881
while (identifiers.find()) {
883882
String identifier = identifiers.group(0);
@@ -992,7 +991,7 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,
992991
}
993992

994993
if (DeathMessages.getHooks().placeholderAPIEnabled) {
995-
Matcher params = Pattern.compile("%([^%]+)%").matcher(Util.convertToLegacy(msg));
994+
Matcher params = Util.PAPI_PLACEHOLDER_PATTERN.matcher(Util.convertToLegacy(msg));
996995

997996
while (params.find()) {
998997
String param = params.group(0);

DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/ComponentUtil.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public static String sortHoverEvents(String msg, List<String> rawEvents) {
2828
// If contains event string, process, otherwise return original msg directly
2929
if (msg.contains("[")) {
3030
int index = 0;
31-
// Match all string between [ and ], e.g. aaa[123]bbb -> [123]
32-
Pattern pattern = Pattern.compile("\\[(.*?)]");
33-
Matcher matcher = pattern.matcher(msg);
31+
Matcher matcher = Util.DM_HOVER_EVENT_PATTERN.matcher(msg);
3432

3533
while (matcher.find()) {
3634
String group = matcher.group(1);

DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/MaterialUtil.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bukkit.event.entity.EntityDamageEvent;
88
import org.bukkit.inventory.ItemStack;
99

10+
import java.util.List;
1011
import java.util.regex.Matcher;
1112
import java.util.regex.Pattern;
1213

@@ -32,14 +33,27 @@ public static boolean itemNameIsWeapon(ItemStack itemStack) {
3233
if (itemStack == null || !itemStack.hasItemMeta() || !itemStack.getItemMeta().hasDisplayName()) return false;
3334

3435
String displayName = itemStack.getItemMeta().getDisplayName();
36+
Pattern[] customWeaponNames = Util.customWeaponNamePatterns;
3537

36-
for (String s : Settings.getInstance().getConfig().getStringList(Config.CUSTOM_ITEM_DISPLAY_NAMES_IS_WEAPON.getPath())) {
37-
Pattern pattern = Pattern.compile(s);
38+
if (customWeaponNames == null) {
39+
List<String> customWeaponNameList = Settings.getInstance().getConfig().getStringList(Config.CUSTOM_ITEM_DISPLAY_NAMES_IS_WEAPON.getPath());
40+
customWeaponNames = new Pattern[customWeaponNameList.size()];
41+
42+
for (int i = 0; i < customWeaponNameList.size(); i++) {
43+
String customWeaponName = customWeaponNameList.get(i);
44+
customWeaponNames[i] = Pattern.compile(customWeaponName);
45+
}
46+
47+
Util.customWeaponNamePatterns = customWeaponNames; // Update cache
48+
}
49+
50+
for (Pattern pattern : customWeaponNames) {
3851
Matcher matcher = pattern.matcher(displayName);
3952
if (matcher.find()) {
4053
return true;
4154
}
4255
}
56+
4357
return false;
4458
}
4559

DeathMessagesModern/src/main/java/dev/mrshawn/deathmessages/utils/Util.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public class Util {
4747
.replacement(convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix")))
4848
.build();
4949

50+
private static final Pattern BUNGEE_RGB_PATTERN = Pattern.compile("(?<!&)(#[0-9a-fA-F]{6})"); // Match bungee RGB color code only, use Negative Lookbehind to avoid matching code begin with &
51+
public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + "§" + "[0-9A-FK-ORX]");
52+
public static final Pattern DM_PERM_PATTERN = Pattern.compile("PERMISSION\\[(.*?)]");
53+
public static final Pattern DM_KILLER_PERM_PATTERN = Pattern.compile("PERMISSION_KILLER\\[(.*?)]");
54+
public static final Pattern DM_REGION_PATTERN = Pattern.compile("REGION\\[(.*?)]");
55+
public static final Pattern DM_HOVER_EVENT_PATTERN = Pattern.compile("\\[(.*?)]"); // Match all string between [ and ], e.g. aaa[123]bbb -> [123]
56+
public static final Pattern PAPI_PLACEHOLDER_PATTERN = Pattern.compile("%([^%]+)%");
57+
58+
public static Pattern[] customWeaponNamePatterns;
59+
5060
public static final Map<UUID, LivingEntity> crystalDeathData = new HashMap<>(); // <EndCrystal UUID, Causing Entity instance>
5161

5262
public static TextReplacementConfig replace(String matchLiteral, String replace) {
@@ -84,9 +94,7 @@ public static String convertToLegacy(Component component) {
8494
To support both bungee and adventure RGB color code in legacy
8595
*/
8696
private static String colorizeBungeeRGB(String s) {
87-
// Match bungee RGB color code only, use Negative Lookbehind to avoid matching code begin with &
88-
Pattern pattern = Pattern.compile("(?<!&)(#[0-9a-fA-F]{6})");
89-
Matcher matcher = pattern.matcher(s);
97+
Matcher matcher = BUNGEE_RGB_PATTERN.matcher(s);
9098
StringBuilder result = new StringBuilder();
9199

92100
while (matcher.find()) {

0 commit comments

Comments
 (0)