Directly use Regex group matching to get playername (Fixes #428)

This commit is contained in:
Phoenix616 2021-03-27 18:34:03 +01:00
parent ce7cade0a5
commit 66201a9062
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0

View File

@ -20,6 +20,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
@ -157,12 +158,11 @@ public class ChestShopSign {
// Prepare regexp patterns // Prepare regexp patterns
Pattern playernamePattern = Pattern.compile(Properties.VALID_PLAYERNAME_REGEXP); // regexp from config file Pattern playernamePattern = Pattern.compile(Properties.VALID_PLAYERNAME_REGEXP); // regexp from config file
Pattern playernameWithIdPattern = Pattern.compile(":[A-Za-z0-9]+$"); // regexp to match ':' and a base62 encoded string Matcher playernameWithIdMatcher = Pattern.compile("^(.+):[A-Za-z0-9]+$").matcher(playername); // regexp to match ':' and a base62 encoded string
// Check if the playername has an ID. This can happen on duplicate or too long names // Check if the playername has an ID. This can happen on duplicate or too long names
if (playernameWithIdPattern.matcher(playername).matches()) { if (playernameWithIdMatcher.matches()) {
// Playername matches the id pattern, so validate everything before the last ':' // Playername matches the id pattern, so validate everything before the last ':'
playername = playername.substring(0, playername.lastIndexOf(":") - 1); playername = playernameWithIdMatcher.group(1);
} }
// If the playername doesn't match, this is not a valid sign, so return // If the playername doesn't match, this is not a valid sign, so return