diff --git a/src/main/java/dev/plex/Guilds.java b/src/main/java/dev/plex/Guilds.java index db89e9c..887e1c7 100644 --- a/src/main/java/dev/plex/Guilds.java +++ b/src/main/java/dev/plex/Guilds.java @@ -63,12 +63,19 @@ public class Guilds extends PlexModule this.addDefaultMessage("guildNotFound", "You're currently not a part of a guild!"); this.addDefaultMessage("alreadyInGuild", "You're currently in a guild. Please do /guild leave if you're a member, or if you're an owner with members, /guild promote then /guild leave, or just an owner, /guild disband."); this.addDefaultMessage("guildNotOwner", "You're not the owner of this guild!"); + this.addDefaultMessage("guildPrefixSet", "You have changed the guild prefix to '{0}'", "0 - The new prefix"); this.addDefaultMessage("guildPrefixCleared", "Your guild's prefix has been cleared."); + this.addDefaultMessage("guildWarpAlphanumeric", "Warp names may only contain alphabetical and/or numerical characters."); this.addDefaultMessage("guildWarpExists", "'{0}' is already an existing warp!", "0 - The warp name"); this.addDefaultMessage("guildWarpNotFound", "'{0}' is not a valid warp!", "0 - The warp name"); this.addDefaultMessage("guildWarpCreated", "You have created a warp called '{0}'", "0 - The warp name"); + + this.addDefaultMessage("guildHomeRemoved", "You have removed the guild's home!"); + this.addDefaultMessage("guildHomeSet", "You have changed the guild's home!"); + this.addDefaultMessage("guildHomeNotFound", "This guild currently has no home set."); + this.addDefaultMessage("guildChatMessage", "[GUILD] {0} {1}", "0 - The player name", "1 - The message"); this.addDefaultMessage("guildChatToggled", "Your chat has been toggled {0}", "0 - On / Off"); this.addDefaultMessage("guildChatConsoleLog", "[GUILD - {0}:{1}] {2} {3}", "0 - The guild name", "1 - The guild unique identifier", "2 - The player name", "3 - The message"); diff --git a/src/main/java/dev/plex/command/GuildCommand.java b/src/main/java/dev/plex/command/GuildCommand.java index 0e2e776..ce65101 100644 --- a/src/main/java/dev/plex/command/GuildCommand.java +++ b/src/main/java/dev/plex/command/GuildCommand.java @@ -37,6 +37,8 @@ public class GuildCommand extends PlexCommand this.registerSubCommand(new WarpSubCommand()); this.registerSubCommand(new WarpListSubCommand()); this.registerSubCommand(new ChatSubCommand()); + this.registerSubCommand(new SetHomeSubCommand()); + this.registerSubCommand(new HomeSubCommand()); } catch (Exception e) { GuildUtil.throwExceptionSync(e); diff --git a/src/main/java/dev/plex/command/sub/HomeSubCommand.java b/src/main/java/dev/plex/command/sub/HomeSubCommand.java new file mode 100644 index 0000000..dd0ffc7 --- /dev/null +++ b/src/main/java/dev/plex/command/sub/HomeSubCommand.java @@ -0,0 +1,38 @@ +package dev.plex.command.sub; + +import dev.plex.Guilds; +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.command.source.RequiredCommandSource; +import dev.plex.rank.enums.Rank; +import net.kyori.adventure.text.Component; +import org.apache.commons.lang.StringUtils; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@CommandParameters(name = "home", aliases = "spawn", usage = "/guild ") +@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.IN_GAME, permission = "plex.guilds.home") +public class HomeSubCommand extends PlexCommand +{ + public HomeSubCommand() + { + super(false); + } + @Override + protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args) + { + assert player != null; + Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresentOrElse(guild -> { + if (guild.getHome() == null) + { + send(player, messageComponent("guildHomeNotFound")); + return; + } + player.teleportAsync(guild.getHome().toLocation()); + }, () -> send(player, messageComponent("guildNotFound"))); + return null; + } +} diff --git a/src/main/java/dev/plex/command/sub/PrefixSubCommand.java b/src/main/java/dev/plex/command/sub/PrefixSubCommand.java index 4af7ae5..2f514ed 100644 --- a/src/main/java/dev/plex/command/sub/PrefixSubCommand.java +++ b/src/main/java/dev/plex/command/sub/PrefixSubCommand.java @@ -36,7 +36,7 @@ public class PrefixSubCommand extends PlexCommand } assert player != null; Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresentOrElse(guild -> { - if (!guild.getOwner().equals(player.getUniqueId())) + if (!guild.getOwner().getUuid().equals(player.getUniqueId())) { send(player, messageComponent("guildNotOwner")); return; diff --git a/src/main/java/dev/plex/command/sub/SetHomeSubCommand.java b/src/main/java/dev/plex/command/sub/SetHomeSubCommand.java new file mode 100644 index 0000000..f42c157 --- /dev/null +++ b/src/main/java/dev/plex/command/sub/SetHomeSubCommand.java @@ -0,0 +1,53 @@ +package dev.plex.command.sub; + +import dev.plex.Guilds; +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.command.source.RequiredCommandSource; +import dev.plex.rank.enums.Rank; +import dev.plex.util.CustomLocation; +import net.kyori.adventure.text.Component; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@CommandParameters(name = "sethome", aliases = "setspawn", usage = "/guild ") +@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.IN_GAME, permission = "plex.guilds.sethome") +public class SetHomeSubCommand extends PlexCommand +{ + + public SetHomeSubCommand() + { + super(false); + } + + @Override + protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args) + { + assert player != null; + Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresentOrElse(guild -> + { + if (!guild.getOwner().getUuid().equals(player.getUniqueId())) + { + send(player, messageComponent("guildNotOwner")); + return; + } + if (args.length > 0 && (args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("unset") || args[0].equalsIgnoreCase("clear"))) + { + if (guild.getHome() == null) + { + send(player, messageComponent("guildHomeNotFound")); + return; + } + guild.setHome(null); + send(player, messageComponent("guildHomeRemoved")); + return; + } + guild.setHome(CustomLocation.fromLocation(player.getLocation())); + send(player, messageComponent("guildHomeSet")); + }, () -> send(player, messageComponent("guildNotFound"))); + return null; + } +} diff --git a/src/main/java/dev/plex/command/sub/SetWarpSubCommand.java b/src/main/java/dev/plex/command/sub/SetWarpSubCommand.java index c251fbd..4108c19 100644 --- a/src/main/java/dev/plex/command/sub/SetWarpSubCommand.java +++ b/src/main/java/dev/plex/command/sub/SetWarpSubCommand.java @@ -36,7 +36,7 @@ public class SetWarpSubCommand extends PlexCommand } assert player != null; Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresentOrElse(guild -> { - if (!guild.getOwner().equals(player.getUniqueId())) + if (!guild.getOwner().getUuid().equals(player.getUniqueId())) { send(player, messageComponent("guildNotOwner")); return; diff --git a/src/main/java/dev/plex/data/SQLGuildManager.java b/src/main/java/dev/plex/data/SQLGuildManager.java index 022c524..aca0f88 100644 --- a/src/main/java/dev/plex/data/SQLGuildManager.java +++ b/src/main/java/dev/plex/data/SQLGuildManager.java @@ -15,7 +15,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.time.Instant; -import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.List; @@ -94,39 +93,49 @@ public class SQLGuildManager }); } + private List getGuildsSync() + { + List guilds = Lists.newArrayList(); + try (Connection connection = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = connection.prepareStatement(SELECT_GUILD); + ResultSet set = statement.executeQuery(); + while (set.next()) + { + Guild guild = new Guild(UUID.fromString(set.getString("guildUuid")), + set.getString("name"), + GSON.fromJson(set.getString("owner"), Member.class), + ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("createdAt")), ZoneId.of(Plex.get().config.getString("server.timezone")).getRules().getOffset(Instant.now()))); + guild.getMembers().addAll(new Gson().fromJson(set.getString("members"), new TypeToken>() + { + }.getType())); + guild.getModerators().addAll(new Gson().fromJson(set.getString("moderators"), new TypeToken>() + { + }.getType())); + guild.setPrefix(set.getString("prefix")); + guild.setMotd(set.getString("motd")); + guild.setHome(GSON.fromJson(set.getString("home"), CustomLocation.class)); + guild.setTagEnabled(set.getBoolean("tagEnabled")); + Map warps = GSON.fromJson(set.getString("warps"), new TypeToken>() + { + }.getType()); + PlexLog.debug("Loaded {0} warps for {1} guild", warps.size(), guild.getName()); + guild.getWarps().putAll(GSON.fromJson(set.getString("warps"), new TypeToken>() + { + }.getType())); + guild.setPublic(set.getBoolean("isPublic")); + guilds.add(guild); + } + } catch (SQLException e) + { + GuildUtil.throwExceptionSync(e); + } + return guilds; + } + public CompletableFuture> getGuilds() { - return CompletableFuture.supplyAsync(() -> - { - List guilds = Lists.newArrayList(); - try (Connection connection = Plex.get().getSqlConnection().getCon()) - { - PreparedStatement statement = connection.prepareStatement(SELECT_GUILD); - ResultSet set = statement.executeQuery(); - while (set.next()) - { - Guild guild = new Guild(UUID.fromString(set.getString("guildUuid")), - set.getString("name"), - GSON.fromJson(set.getString("owner"), Member.class), - ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("createdAt")), ZoneId.of(Plex.get().config.getString("server.timezone")).getRules().getOffset(Instant.now()))); - guild.getMembers().addAll(new Gson().fromJson(set.getString("members"), new TypeToken>(){}.getType())); - guild.getModerators().addAll(new Gson().fromJson(set.getString("moderators"), new TypeToken>(){}.getType())); - guild.setPrefix(set.getString("prefix")); - guild.setMotd(set.getString("motd")); - guild.setHome(GSON.fromJson(set.getString("home"), CustomLocation.class)); - guild.setTagEnabled(set.getBoolean("tagEnabled")); - Map warps = GSON.fromJson(set.getString("warps"), new TypeToken>(){}.getType()); - PlexLog.debug("Loaded {0} warps for {1} guild", warps.size(), guild.getName()); - guild.getWarps().putAll(GSON.fromJson(set.getString("warps"), new TypeToken>(){}.getType())); - guild.setPublic(set.getBoolean("isPublic")); - guilds.add(guild); - } - } catch (SQLException e) - { - GuildUtil.throwExceptionSync(e); - } - return guilds; - }); + return CompletableFuture.supplyAsync(this::getGuildsSync); } }