Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
package me.glaremasters.guilds.utils;

import ch.jalu.configme.SettingsManager;
import co.aikar.commands.ACFBukkitUtil;
import me.glaremasters.guilds.configuration.sections.GuildSettings;
import me.glaremasters.guilds.guild.Guild;

import java.util.Collection;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -72,6 +75,21 @@ public static boolean isValidPrefix(String input, SettingsManager settingsManage
);
}

/**
* Check whether a guild name is already used, ignoring color formatting and case.
*
* @param input the raw proposed guild name
* @param guilds the existing guilds
* @return true when an existing guild has the same visible name
*/
public static boolean isNameTaken(String input, Collection<Guild> guilds) {
final String normalizedInput = normalizeName(input);
return guilds.stream()
.map(Guild::getName)
.map(GuildInputValidator::normalizeName)
.anyMatch(normalizedInput::equalsIgnoreCase);
}

private static boolean matchesRequirements(String input, String regex, boolean includeColorCodes) {
if (includeColorCodes) {
return input.matches(regex);
Expand All @@ -91,6 +109,10 @@ private static boolean matchesRequirements(String input, String regex, boolean i
|| regexAcceptsCharacter(visibleInput, regex, SECTION_SIGN);
}

private static String normalizeName(String input) {
return ACFBukkitUtil.removeColors(StringUtils.color(input));
}

private static boolean regexAcceptsCharacter(String validInput, String regex, char character) {
if (validInput.isEmpty()) {
return String.valueOf(character).matches(regex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal class CommandCreate : BaseCommand() {

val cost = settingsManager.getProperty(CostSettings.CREATION)

if (guildHandler.checkGuildNames(name)) {
if (GuildInputValidator.isNameTaken(name, guildHandler.guilds.values)) {
throw ExpectationNotMet(Messages.CREATE__GUILD_NAME_TAKEN)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class CommandRename : BaseCommand() {
@CommandPermission(Constants.BASE_PERM + "rename")
@Syntax("%name")
fun rename(player: Player, @Conditions("perm:perm=RENAME") guild: Guild, name: String) {
if (guildHandler.checkGuildNames(name)) {
if (GuildInputValidator.isNameTaken(name, guildHandler.guilds.values)) {
throw ExpectationNotMet(Messages.CREATE__GUILD_NAME_TAKEN)
}

Expand Down
Loading