From e55c83aa2d5bea51f77dc503d388435e84cfe0c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Serhat=20Han?= <59025999+UsainSrht@users.noreply.github.com> Date: Tue, 26 Dec 2023 10:21:27 +0300 Subject: [PATCH] work on home creation SetHomeCommand --- pom.xml | 6 ++ .../java/me/usainsrht/uhomes/HomeManager.java | 45 +++++++++ src/main/java/me/usainsrht/uhomes/UHomes.java | 8 +- .../usainsrht/uhomes/command/HomeCommand.java | 3 +- .../uhomes/command/SetHomeCommand.java | 96 ++++++++++++++++++- .../usainsrht/uhomes/config/MainConfig.java | 6 ++ .../me/usainsrht/uhomes/gui/HomesGUI.java | 2 + src/main/resources/config.yml | 23 ++++- 8 files changed, 185 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 2c852fc..54559d5 100644 --- a/pom.xml +++ b/pom.xml @@ -111,5 +111,11 @@ commodore 2.2 + + net.luckperms + api + 5.4 + provided + diff --git a/src/main/java/me/usainsrht/uhomes/HomeManager.java b/src/main/java/me/usainsrht/uhomes/HomeManager.java index 754d05b..9b075c6 100644 --- a/src/main/java/me/usainsrht/uhomes/HomeManager.java +++ b/src/main/java/me/usainsrht/uhomes/HomeManager.java @@ -4,10 +4,14 @@ import de.tr7zw.changeme.nbtapi.NBTFile; import de.tr7zw.changeme.nbtapi.NBTList; import de.tr7zw.changeme.nbtapi.NBTListCompound; +import me.usainsrht.uhomes.config.MainConfig; import me.usainsrht.uhomes.util.NBTUtil; import net.kyori.adventure.text.minimessage.tag.resolver.Formatter; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.permissions.Permissible; +import org.bukkit.permissions.PermissionAttachmentInfo; import javax.annotation.Nullable; import java.io.File; @@ -113,6 +117,47 @@ public NBTFile getNBTFile(UUID uuid) { return nbtFile; } + public int getHomeLimit(UUID uuid) { + return getHomeLimit(Bukkit.getEntity(uuid)); + } + + public int getHomeLimit(Permissible permissible) { + if (MainConfig.isSumHomeLimits()) { + int total = 0; + for (PermissionAttachmentInfo permInfo : permissible.getEffectivePermissions()) { + String perm = permInfo.getPermission(); + if (!perm.startsWith(MainConfig.getHomeLimitPermission())) continue; + String substr = perm.substring(MainConfig.getHomeLimitPermission().length()); + try { + int number = Integer.parseInt(substr); + total += number; + } catch (NumberFormatException ignore) {} + } + return total; + } else { + int highest = 0; + for (PermissionAttachmentInfo permInfo : permissible.getEffectivePermissions()) { + String perm = permInfo.getPermission(); + if (!perm.startsWith(MainConfig.getHomeLimitPermission())) continue; + String substr = perm.substring(MainConfig.getHomeLimitPermission().length()); + try { + int number = Integer.parseInt(substr); + if (number > highest) highest = number; + } catch (NumberFormatException ignore) {} + } + return highest; + } + } + + public CompletableFuture canRegisterHome(UUID uuid) { + CompletableFuture> future = getHomes(uuid); + CompletableFuture canRegisterFuture = new CompletableFuture<>(); + future.thenAccept(homes -> canRegisterFuture.complete(homes.size() < getHomeLimit(uuid))); + return canRegisterFuture; + } + + + public UHomes getPlugin() { return plugin; } diff --git a/src/main/java/me/usainsrht/uhomes/UHomes.java b/src/main/java/me/usainsrht/uhomes/UHomes.java index 98722c1..c23ab69 100644 --- a/src/main/java/me/usainsrht/uhomes/UHomes.java +++ b/src/main/java/me/usainsrht/uhomes/UHomes.java @@ -4,7 +4,9 @@ import me.lucko.commodore.CommodoreProvider; import me.usainsrht.uhomes.command.CommandHandler; import me.usainsrht.uhomes.command.HomeCommand; +import me.usainsrht.uhomes.command.SetHomeCommand; import me.usainsrht.uhomes.config.MainConfig; +import net.luckperms.api.LuckPerms; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; @@ -15,6 +17,7 @@ public final class UHomes extends JavaPlugin { private static final int pluginID = 20539; private Metrics metrics; private HomeManager homeManager; + private LuckPerms luckPerms; private Commodore commodore; public File HOMES_FOLDER; @@ -54,7 +57,10 @@ public void registerCommands() { HomeCommand homeCommand = new HomeCommand(MainConfig.getHomeCommand()); CommandHandler.register("uhomes", homeCommand); commodore.register(homeCommand, homeCommand.getCommodoreCommand()); - getLogger().info("home command registered " + homeCommand + " " + homeCommand.isRegistered()); + + SetHomeCommand setHomeCommand = new SetHomeCommand(MainConfig.getSetHomeCommand()); + CommandHandler.register("uhomes", setHomeCommand); + commodore.register(setHomeCommand, setHomeCommand.getCommodoreCommand()); } public Metrics getMetrics() { diff --git a/src/main/java/me/usainsrht/uhomes/command/HomeCommand.java b/src/main/java/me/usainsrht/uhomes/command/HomeCommand.java index e72d450..6f83250 100644 --- a/src/main/java/me/usainsrht/uhomes/command/HomeCommand.java +++ b/src/main/java/me/usainsrht/uhomes/command/HomeCommand.java @@ -35,7 +35,8 @@ public HomeCommand(YamlCommand cmd) { public LiteralCommandNode getCommodoreCommand() { return LiteralArgumentBuilder.literal(super.getName()) .then(RequiredArgumentBuilder.argument("home-name", StringArgumentType.greedyString())) - .then(RequiredArgumentBuilder.argument("player", StringArgumentType.word())) + .then(RequiredArgumentBuilder.argument("player", StringArgumentType.word()) + .then(RequiredArgumentBuilder.argument("home-name", StringArgumentType.greedyString()))) .build(); } diff --git a/src/main/java/me/usainsrht/uhomes/command/SetHomeCommand.java b/src/main/java/me/usainsrht/uhomes/command/SetHomeCommand.java index ccde2b1..759b830 100644 --- a/src/main/java/me/usainsrht/uhomes/command/SetHomeCommand.java +++ b/src/main/java/me/usainsrht/uhomes/command/SetHomeCommand.java @@ -1,4 +1,98 @@ package me.usainsrht.uhomes.command; -public class SetHomeCommand { +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.tree.LiteralCommandNode; +import me.usainsrht.uhomes.Home; +import me.usainsrht.uhomes.HomeManager; +import me.usainsrht.uhomes.UHomes; +import me.usainsrht.uhomes.config.MainConfig; +import me.usainsrht.uhomes.util.MessageUtil; +import me.usainsrht.uhomes.util.SoundUtil; +import net.kyori.adventure.sound.Sound; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Formatter; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class SetHomeCommand extends Command { + + private String permission; + private String permissionMessage; + private Collection permissionSounds; + + public SetHomeCommand(YamlCommand cmd) { + super(cmd.getName(), cmd.getDescription(), cmd.getUsage(), cmd.getAliases()); + this.permission = cmd.getPermission(); + this.permissionMessage = cmd.getPermissionMessage(); + this.permissionSounds = cmd.getPermissionSounds(); + } + + public LiteralCommandNode getCommodoreCommand() { + return LiteralArgumentBuilder.literal(super.getName()) + .then(RequiredArgumentBuilder.argument("home-name", StringArgumentType.greedyString())) + .build(); + } + + @Override + public boolean execute(CommandSender sender, String command, String[] args) { + if (!sender.hasPermission(permission)) { + Component permMsg = MiniMessage.miniMessage().deserialize(permissionMessage, + Placeholder.unparsed("permission", permission)); + sender.sendMessage(permMsg); + SoundUtil.play(sender, permissionSounds); + return false; + } + if (!(sender instanceof Player)) { + MessageUtil.send(sender, MainConfig.getMessage("player_only_command")); + return false; + } + Player player = (Player) sender; + HomeManager homeManager = UHomes.getInstance().getHomeManager(); + UUID uuid = player.getUniqueId(); + String name = null; + if (args.length > 0) { + name = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + if (name.length() > MainConfig.getHomeNameCharLimit()) { + MessageUtil.send(sender, MainConfig.getMessage("home_name_limit"), + Formatter.number("amount", MainConfig.getHomeNameCharLimit()), + Formatter.number("home_name_char_size", name.length()), + Placeholder.unparsed("home_name", name)); + SoundUtil.play(sender, MainConfig.getSound("home_name_limit")); + return false; + } + if (!name.matches(MainConfig.getHomeNameValidChars())) { + MessageUtil.send(sender, MainConfig.getMessage("home_name_not_valid"), + Placeholder.unparsed("home_name", name), + Placeholder.unparsed("invalid_characters", String.join(" ", name.split(MainConfig.getHomeNameValidChars())))); + SoundUtil.play(sender, MainConfig.getSound("home_name_not_valid")); + return false; + } + } + String finalName = name; + CompletableFuture> homesFuture = homeManager.getHomes(uuid); + homesFuture.thenAccept(homes -> { + if (homes.stream().anyMatch(home -> home.getName() != null && home.getName().equalsIgnoreCase(finalName))) { + MessageUtil.send(sender, MainConfig.getMessage("home_name_already_in_use"), Placeholder.unparsed("home_name", finalName)); + SoundUtil.play(sender, MainConfig.getSound("home_name_already_in_use")); + return false; + } + + Home home = new Home(uuid, player.getLocation().clone()); + if (finalName != null) home.setName(finalName); + homeManager.addHome(uuid, home); + }); + return true; + } + } diff --git a/src/main/java/me/usainsrht/uhomes/config/MainConfig.java b/src/main/java/me/usainsrht/uhomes/config/MainConfig.java index 7976383..07582b4 100644 --- a/src/main/java/me/usainsrht/uhomes/config/MainConfig.java +++ b/src/main/java/me/usainsrht/uhomes/config/MainConfig.java @@ -21,6 +21,7 @@ public class MainConfig { private static String homeLimitPermission; private static boolean sumHomeLimits; private static int homeNameCharLimit; + private static String homeNameValidChars; private static boolean askForNameBeforeSave; private static String homesGuiTitle; @@ -59,6 +60,7 @@ public static void create(ConfigurationSection config) { homeLimitPermission = config.getString("home_limit_permission"); sumHomeLimits = config.getBoolean("sum_limit_permissions"); homeNameCharLimit = config.getInt("home_name_character_limit"); + homeNameValidChars = config.getString("home_name_valid_characters"); askForNameBeforeSave = config.getBoolean("ask_for_name_before_save"); homesGuiTitle = config.getString("gui.title"); @@ -125,6 +127,10 @@ public static int getHomeNameCharLimit() { return homeNameCharLimit; } + public static String getHomeNameValidChars() { + return homeNameValidChars; + } + public static ConfigurationSection getDefaultHomeItem() { return defaultHomeItem; } diff --git a/src/main/java/me/usainsrht/uhomes/gui/HomesGUI.java b/src/main/java/me/usainsrht/uhomes/gui/HomesGUI.java index 2f493f5..b4ae4d0 100644 --- a/src/main/java/me/usainsrht/uhomes/gui/HomesGUI.java +++ b/src/main/java/me/usainsrht/uhomes/gui/HomesGUI.java @@ -54,6 +54,8 @@ public static void open(Player player, List homes, int size) { inventory.setItem(13, ItemUtil.getItemFromYaml(MainConfig.getNoHomeItem())); } + inventory.setItem(size-5, ItemUtil.getItemFromYaml(MainConfig.getSetHomeItem())); + int i = 0; for (Home home : homes) { Location location = home.getLocation(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d31c942..ace084a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,9 +2,24 @@ prefix: "µHomes »" messages: reload: "Config reloaded!" + player_only_command: "This command can only be used by players!" + home_limit: + - "You have reached your home limit ." + - "You can delete or relocate one of your homes." + home_name_limit: + - "Your home name can only be up to characters." + - "<#F78383> is characters." + home_name_not_valid: + - "Invalid characters found in <#F78383>." + - "<#F78383>" + home_name_already_in_use: "You already have a home named <#F78383>." sounds: reload: entity.villager.yes + home_limit: entity.villager.no + home_name_limit: entity.villager.no + home_name_not_valid: entity.villager.no + home_name_already_in_use: entity.villager.no # permission to calculate home limit of a player # for example if player has a "home.limit.5" permission he/she @@ -19,6 +34,8 @@ sum_limit_permissions: true home_name_character_limit: 32 +home_name_valid_characters: "/^[\w\-\s]+$/" + # true: opens an input gui to player asking for name of the home # false: saves the home without a name (can be changed later) ask_for_name_before_save: false @@ -66,7 +83,11 @@ gui: - "" sethome: material: CARTOGRAPHY_TABLE - name: "" + name: "/" + lore: + - "" + - " Click to create new home! " + - "" world_names: world: "World"