Added 'island.name.uniqueness' in the config

Implements https://github.com/BentoBoxWorld/BentoBox/issues/899
Edited IslandsManager#nameExists(...) to strip colors.
This commit is contained in:
Florian CUNY 2019-08-28 14:44:55 +02:00
parent f63f73a3b7
commit eea91b6269
4 changed files with 40 additions and 13 deletions

View File

@ -149,6 +149,11 @@ public class Settings implements ConfigObject {
@ConfigComment("Sets the maximum length an island custom name cannot exceed.") @ConfigComment("Sets the maximum length an island custom name cannot exceed.")
@ConfigEntry(path = "island.name.max-length") @ConfigEntry(path = "island.name.max-length")
private int nameMaxLength = 20; private int nameMaxLength = 20;
@ConfigComment("Requires island custom names to be unique in the gamemode the island is in.")
@ConfigComment("As a result, only one island per gamemode are allowed to share the same name.")
@ConfigComment("Note that island names are purely cosmetics and are not used as a way to programmatically identify islands.")
@ConfigEntry(path = "island.name.uniqueness", since = "1.7.0")
private boolean nameUniqueness = false;
@ConfigComment("Remove hostile mob on teleport box radius") @ConfigComment("Remove hostile mob on teleport box radius")
@ConfigComment("If hostile mobs are cleared on player teleport, then this sized box will be cleared") @ConfigComment("If hostile mobs are cleared on player teleport, then this sized box will be cleared")
@ -395,6 +400,20 @@ public class Settings implements ConfigObject {
this.nameMaxLength = nameMaxLength; this.nameMaxLength = nameMaxLength;
} }
/**
* @since 1.7.0
*/
public boolean isNameUniqueness() {
return nameUniqueness;
}
/**
* @since 1.7.0
*/
public void setNameUniqueness(boolean nameUniqueness) {
this.nameUniqueness = nameUniqueness;
}
/** /**
* @param pasteSpeed the pasteSpeed to set * @param pasteSpeed the pasteSpeed to set
*/ */

View File

@ -29,22 +29,22 @@ public class IslandSetnameCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
// Explain command
if (args.isEmpty()) {
showHelp(this, user);
return false;
}
UUID playerUUID = user.getUniqueId(); UUID playerUUID = user.getUniqueId();
if (!getIslands().hasIsland(getWorld(), playerUUID)) { if (!getIslands().hasIsland(getWorld(), playerUUID)) {
user.sendMessage("general.errors.no-island"); user.sendMessage("general.errors.no-island");
return false; return false;
} }
if (!getIslands().isOwner(getWorld(), playerUUID)) { if (!getIslands().isOwner(getWorld(), playerUUID)) {
user.sendMessage("general.errors.not-owner"); user.sendMessage("general.errors.not-owner");
return false; return false;
} }
// Explain command
if (args.isEmpty()) {
showHelp(this, user);
return false;
}
// Naming the island - join all the arguments with spaces. // Naming the island - join all the arguments with spaces.
String name = String.join(" ", args); String name = String.join(" ", args);
@ -59,15 +59,20 @@ public class IslandSetnameCommand extends CompositeCommand {
return false; return false;
} }
// Set the name // Apply colors
if (user.hasPermission(this.getPermissionPrefix() + "island.name.format")) { if (user.hasPermission(getPermissionPrefix() + "island.name.format")) {
getIslands().getIsland(getWorld(), playerUUID).setName(ChatColor.translateAlternateColorCodes('&', name)); name = ChatColor.translateAlternateColorCodes('&', name);
} else {
getIslands().getIsland(getWorld(), playerUUID).setName(name);
} }
// Check if the name doesn't already exist in the gamemode
if (getSettings().isNameUniqueness() && getIslands().nameExists(getWorld(), name)) {
user.sendMessage("commands.island.setname.name-already-exists");
return false;
}
// Everything's good!
getIslands().getIsland(getWorld(), playerUUID).setName(name);
user.sendMessage("general.success"); user.sendMessage("general.success");
return true; return true;
} }
} }

View File

@ -14,6 +14,7 @@ import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.TreeSpecies; import org.bukkit.TreeSpecies;
@ -1208,6 +1209,7 @@ public class IslandsManager {
* @since 1.7.0 * @since 1.7.0
*/ */
public boolean nameExists(@NonNull World world, @NonNull String name) { public boolean nameExists(@NonNull World world, @NonNull String name) {
return getIslands(world).stream().filter(island -> island.getName() != null).map(Island::getName).anyMatch(n -> n.equals(name)); return getIslands(world).stream().filter(island -> island.getName() != null).map(Island::getName)
.anyMatch(n -> ChatColor.stripColor(n).equals(ChatColor.stripColor(name)));
} }
} }

View File

@ -439,6 +439,7 @@ commands:
description: "set a name for your island" description: "set a name for your island"
name-too-short: "&cToo short. Minimum size is [number] characters." name-too-short: "&cToo short. Minimum size is [number] characters."
name-too-long: "&cToo long. Maximum size is [number] characters." name-too-long: "&cToo long. Maximum size is [number] characters."
name-already-exists: "&cThere is already an island with that name in this gamemode."
parameters: "<name>" parameters: "<name>"
resetname: resetname:
description: "reset your island name" description: "reset your island name"