|
| 1 | +package simplexity.simplehomes.commands; |
| 2 | + |
| 3 | +import net.kyori.adventure.text.minimessage.MiniMessage; |
| 4 | +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; |
| 5 | +import org.bukkit.Location; |
| 6 | +import org.bukkit.command.Command; |
| 7 | +import org.bukkit.command.CommandSender; |
| 8 | +import org.bukkit.command.TabExecutor; |
| 9 | +import org.bukkit.entity.Player; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | +import simplexity.simplehomes.Home; |
| 13 | +import simplexity.simplehomes.SimpleHomes; |
| 14 | +import simplexity.simplehomes.configs.LocaleHandler; |
| 15 | +import simplexity.simplehomes.saving.SQLiteHandler; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class SetHomeCommand implements TabExecutor { |
| 20 | + |
| 21 | + MiniMessage miniMessage = SimpleHomes.getMiniMessage(); |
| 22 | + // /sethome name |
| 23 | + /* |
| 24 | + /sethome - no args, if has no home set, will set first home, if has home, will ask for additional arguments |
| 25 | + /sethome <name> - check if another home has that name, if it does, return and let player know |
| 26 | + /sethome <name> -o - overwrites the previous home named that if one existed |
| 27 | + each home set checks on how many homes the person has permissions to have, returns if the person has too many |
| 28 | + */ |
| 29 | + @Override |
| 30 | + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) { |
| 31 | + if (!(sender instanceof Player player)) { |
| 32 | + sender.sendRichMessage(LocaleHandler.getInstance().getMustBePlayer()); |
| 33 | + return false; |
| 34 | + } |
| 35 | + List<Home> playerHomes = SQLiteHandler.getInstance().getHomes(player); |
| 36 | + if (args.length < 1 && !playerHomes.isEmpty()) { |
| 37 | + sender.sendRichMessage(LocaleHandler.getInstance().getProvideHomeName()); |
| 38 | + return false; |
| 39 | + } |
| 40 | + Location playerLocation = player.getLocation().toCenterLocation(); |
| 41 | + String homeName = args[0]; |
| 42 | + boolean overwrite = false; |
| 43 | + if (args.length > 1) { |
| 44 | + if (args[1].equalsIgnoreCase("-o")) { |
| 45 | + overwrite = true; |
| 46 | + } |
| 47 | + } |
| 48 | + if (homeExists(playerHomes, homeName) && !overwrite) { |
| 49 | + player.sendRichMessage(LocaleHandler.getInstance().getHomeExists()); |
| 50 | + return false; |
| 51 | + } |
| 52 | + SQLiteHandler.getInstance().setHome(player, homeName, playerLocation, overwrite); |
| 53 | + player.sendMessage(miniMessage.deserialize(LocaleHandler.getInstance().getHomeSet(), |
| 54 | + Placeholder.unparsed("name", homeName))); |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) { |
| 60 | + return List.of(""); |
| 61 | + } |
| 62 | + |
| 63 | + private boolean homeExists(List<Home> homes, String homeName) { |
| 64 | + for (Home home : homes) { |
| 65 | + if (home.getName().equalsIgnoreCase(homeName)) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | +} |
0 commit comments