Island name (#8)
* Add island name checking and renaming functionality Added a method in the IslandData class to check for duplicate island names and a new class IslandName to handle island name-related commands. These changes allow players to rename their islands while enforcing uniqueness and certain length restrictions on the names. * Update island name in island data cache when renamed Modified the 'islandName' method in the 'Island' class to make sure that any changes made to an island's name are also updated in the island data cache. This ensures that all references to the island's name are consistent and accurate throughout the application.
This commit is contained in:
parent
b78736fcc3
commit
0974423e3f
|
|
@ -17,6 +17,7 @@ public class IslandCommand extends PlayerSubCommand {
|
|||
|
||||
registerSubCommand(new IslandGo(plugin)); // TODO -- Add some more output
|
||||
registerSubCommand(new IslandTop(plugin));
|
||||
registerSubCommand(new IslandName(plugin));
|
||||
registerSubCommand(new IslandRestart(plugin)); // TODO -- Add IslandRestartCommand
|
||||
registerSubCommand(new IslandAccept(plugin));
|
||||
registerSubCommand(new IslandDeny(plugin));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
package com.alttd.cometskyblock.commands.island;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.commands.PlayerSubCommand;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandData;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class IslandName extends PlayerSubCommand {
|
||||
|
||||
protected IslandName(CometSkyBlockPlugin plugin) {
|
||||
super(plugin, "name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean execute(Player player, IslandPlayer islandPlayer, String... args) {
|
||||
if (islandPlayer.islandId() == 0) {
|
||||
player.sendRichMessage(plugin.messagesConfiguration().get().commands().island().noIsland());
|
||||
return true;
|
||||
}
|
||||
if (args.length < 1) {
|
||||
player.sendRichMessage(plugin.messagesConfiguration().get().commands().island().commandUsage());
|
||||
return true;
|
||||
}
|
||||
//TODO filter name
|
||||
String islandName = args[0];
|
||||
|
||||
// if (islandName.matches("someRegexPreferabllyAWholeListOfThem")) {
|
||||
// player.sendRichMessage("<red>You used prohibited language in your island name, please keep your names appropriate and suitable for all ages.</red>");
|
||||
// return true;
|
||||
// }
|
||||
|
||||
if (islandName.length() < 3 || islandName.length() > 16) {
|
||||
player.sendRichMessage("<red>Island names have to be between 3 and 16 characters</red>");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (IslandData.islandNameIsDuplicate(islandName)) {
|
||||
player.sendRichMessage("<red>That island name already exists, please choose another</red>");
|
||||
return true;
|
||||
}
|
||||
Island island = Island.getIsland(islandPlayer.islandUUID());
|
||||
String oldName = island.islandName();
|
||||
island.islandName(islandName);
|
||||
player.sendRichMessage("<green>Your island has been renamed from <old_name> to <new_name>!</green>", TagResolver.resolver(
|
||||
Placeholder.parsed("old_name", oldName),
|
||||
Placeholder.parsed("new_name", islandName)));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -110,6 +110,7 @@ public class Island extends YamlConfiguration {
|
|||
}
|
||||
|
||||
public void islandName(String islandName) {
|
||||
IslandData.updateIsland(new IslandData(islandId(), islandName, level()));
|
||||
set("island.islandname", islandName);
|
||||
save();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,6 +65,10 @@ public record IslandData(int islandId, String name, int level) {
|
|||
}
|
||||
}
|
||||
|
||||
public static boolean islandNameIsDuplicate(String islandName) {
|
||||
return islandDataMap.values().stream().anyMatch(islandData -> islandData.name().equalsIgnoreCase(islandName));
|
||||
}
|
||||
|
||||
public String format(int playerIslandId) {
|
||||
if (playerIslandId == islandId) {
|
||||
return "<green>" + format() + "</green>";
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user