fix prefix sub command and add clearing guild prefixes

fix some issues with hover events in prefixes
This commit is contained in:
Taah 2022-05-07 22:54:22 -07:00
parent f709e76dd5
commit 8bcf28f120
9 changed files with 129 additions and 40 deletions

View File

@ -1,3 +1,4 @@
import java.nio.charset.StandardCharsets
plugins { plugins {
java java
`maven-publish` `maven-publish`
@ -49,4 +50,8 @@ tasks {
javadoc { javadoc {
options.memberLevel = JavadocMemberLevel.PRIVATE options.memberLevel = JavadocMemberLevel.PRIVATE
} }
compileJava {
options.encoding = StandardCharsets.UTF_8.name()
options.release.set(17)
}
} }

View File

@ -54,15 +54,20 @@ public class Guilds extends PlexModule
this.addDefaultMessage("guildsHelpCommand", "<gradient:gold:yellow>======</gradient>Guild Menu<gradient:gold:yellow>======</gradient><newline>\n" + this.addDefaultMessage("guildsHelpCommand", "<gradient:gold:yellow>======</gradient>Guild Menu<gradient:gold:yellow>======</gradient><newline>\n" +
"<newline><gold>/guild <gray>Returns this menu\n" + "<newline><gold>/guild <gray>Returns this menu\n" +
"<newline><gold>/guild help"); "<newline><gold>/guild help");
this.addDefaultMessage("guildCommandNotFound", "<red>'{0}'<gold> is not a valid sub command!", "{0} - The sub command"); this.addDefaultMessage("guildCommandNotFound", "<red>'{0}'<gold> is not a valid sub command!", "0 - The sub command");
this.addDefaultMessage("guildNotFound", "<red>You're currently not a part of a guild!"); this.addDefaultMessage("guildNotFound", "<red>You're currently not a part of a guild!");
this.addDefaultMessage("alreadyInGuild", "<red>You're currently in a guild. Please do <gold>/guild leave<red> if you're a member, or if you're an owner with members, <gold>/guild promote <player><red> then <gold>/guild leave<red>, or just an owner, <gold>/guild disband<red>."); this.addDefaultMessage("alreadyInGuild", "<red>You're currently in a guild. Please do <gold>/guild leave<red> if you're a member, or if you're an owner with members, <gold>/guild promote <player><red> then <gold>/guild leave<red>, or just an owner, <gold>/guild disband<red>.");
this.addDefaultMessage("guildNotOwner", "<red>You're not the owner of this guild!");
this.addDefaultMessage("guildPrefixSet", "<green>You have changed the guild prefix to '<gold>{0}</gold>'", "0 - The new prefix");
this.addDefaultMessage("guildPrefixCleared", "<green>Your guild's prefix has been cleared.");
} }
@Override @Override
public void disable() public void disable()
{ {
// Unregistering listeners / commands is handled by Plex // Unregistering listeners / commands is handled by Plex
this.getGuildHolder().getGuilds().forEach(sqlGuildManager::updateGuild);
this.getPlex().setChatHandler(new ChatListener.ChatHandlerImpl());
} }
private void addDefaultMessage(String message, Object initValue) private void addDefaultMessage(String message, Object initValue)

View File

@ -7,6 +7,7 @@ import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.source.RequiredCommandSource; import dev.plex.command.source.RequiredCommandSource;
import dev.plex.command.sub.CreateSubCommand; import dev.plex.command.sub.CreateSubCommand;
import dev.plex.command.sub.InfoSubCommand; import dev.plex.command.sub.InfoSubCommand;
import dev.plex.command.sub.PrefixSubCommand;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.GuildUtil; import dev.plex.util.GuildUtil;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
@ -29,17 +30,15 @@ public class GuildCommand extends PlexCommand
public GuildCommand() public GuildCommand()
{ {
System.out.println("Aaa");
PlexLog.log("Test");
try try
{ {
this.registerSubCommand(new CreateSubCommand()); this.registerSubCommand(new CreateSubCommand());
this.registerSubCommand(new InfoSubCommand()); this.registerSubCommand(new InfoSubCommand());
this.registerSubCommand(new PrefixSubCommand());
} catch (Exception e) } catch (Exception e)
{ {
GuildUtil.throwExceptionSync(e); GuildUtil.throwExceptionSync(e);
} }
PlexLog.log("Registered");
} }
@Override @Override

View File

@ -0,0 +1,55 @@
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.guild.Guild;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
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 = "prefix", aliases = "tag,settag,setprefix", usage = "/guild <command> <prefix>")
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.IN_GAME, permission = "plex.guilds.prefix")
public class PrefixSubCommand extends PlexCommand
{
public PrefixSubCommand()
{
super(false);
}
@Override
protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args)
{
if (args.length == 0)
{
return usage();
}
assert player != null;
Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresentOrElse(guild -> {
if (!guild.getOwner().equals(player.getUniqueId()))
{
send(player, messageComponent("guildNotOwner"));
return;
}
if (args[0].equalsIgnoreCase("clear") || args[0].equalsIgnoreCase("off"))
{
guild.setPrefix(null);
send(player, messageComponent("guildPrefixCleared"));
return;
}
guild.setPrefix(StringUtils.join(args, " "));
send(player, messageComponent("guildPrefixSet", SafeMiniMessage.mmDeserializeWithoutEvents(guild.getPrefix())));
}, () -> send(player, messageComponent("alreadyInGuild")));
return null;
}
}

View File

@ -28,8 +28,9 @@ public class SQLGuildManager
private static final String SELECT_GUILD = "SELECT * FROM `guilds`"; private static final String SELECT_GUILD = "SELECT * FROM `guilds`";
private static final String SELECT_GUILD_OWNER = "SELECT * FROM `guilds` WHERE owner=?"; private static final String SELECT_GUILD_OWNER = "SELECT * FROM `guilds` WHERE owner=?";
private static final String SELECT_GUILD_MEMBER_MYSQL = "SELECT * FROM `guilds` WHERE json_search(members, ?, ?) IS NOT NULL LIMIT 1"; private static final String SELECT_GUILD_MEMBER_MYSQL = "SELECT * FROM `guilds` WHERE json_search(members, ?, ?) IS NOT NULL LIMIT 1";
private static final String INSERT_GUILD = "INSERT INTO `guilds` (`name`, `owner`, `createdAt`, `members`, `moderators`, `prefix`, `motd`, `ranks`, `defaultRank`, `home`, `tagEnabled`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; private static final String INSERT_GUILD = "INSERT INTO `guilds` (`guildUuid`, `name`, `owner`, `createdAt`, `members`, `moderators`, `prefix`, `motd`, `ranks`, `defaultRank`, `home`, `tagEnabled`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
private static final String DELETE_GUILD = "DELETE FROM `guilds` WHERE owner=?"; private static final String DELETE_GUILD = "DELETE FROM `guilds` WHERE owner=?";
private static final String UPDATE_GUILD = "UPDATE `guilds` SET name=?, owner=?, members=?, moderators=?, prefix=?, motd=?, ranks=?, defaultRank=?, home=?, tagEnabled=? WHERE guildUuid=?";
public CompletableFuture<Guild> insertGuild(Guild guild) public CompletableFuture<Guild> insertGuild(Guild guild)
{ {
@ -38,18 +39,47 @@ public class SQLGuildManager
try (Connection connection = Plex.get().getSqlConnection().getCon()) try (Connection connection = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = connection.prepareStatement(INSERT_GUILD); PreparedStatement statement = connection.prepareStatement(INSERT_GUILD);
statement.setString(1, guild.getGuildUuid().toString());
statement.setString(2, guild.getName());
statement.setString(3, guild.getOwner().toString());
statement.setLong(4, guild.getCreatedAt().toInstant().toEpochMilli());
statement.setString(5, GSON.toJson(guild.getMembers()));
statement.setString(6, GSON.toJson(guild.getModerators().stream().map(UUID::toString).collect(Collectors.toList())));
statement.setString(7, guild.getPrefix());
statement.setString(8, guild.getMotd());
statement.setString(9, GSON.toJson(guild.getRanks()));
statement.setString(10, GSON.toJson(guild.getDefaultRank()));
statement.setString(11, GSON.toJson(guild.getHome()));
statement.setBoolean(12, guild.isTagEnabled());
statement.execute();
return guild;
} catch (SQLException e)
{
GuildUtil.throwExceptionSync(e);
return null;
}
});
}
public CompletableFuture<Guild> updateGuild(Guild guild)
{
return CompletableFuture.supplyAsync(() ->
{
try (Connection connection = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = connection.prepareStatement(UPDATE_GUILD);
statement.setString(1, guild.getName()); statement.setString(1, guild.getName());
statement.setString(2, guild.getOwner().toString()); statement.setString(2, guild.getOwner().toString());
statement.setLong(3, guild.getCreatedAt().toInstant().toEpochMilli()); statement.setString(3, GSON.toJson(guild.getMembers()));
statement.setString(4, GSON.toJson(guild.getMembers())); statement.setString(4, GSON.toJson(guild.getModerators().stream().map(UUID::toString).collect(Collectors.toList())));
statement.setString(5, GSON.toJson(guild.getModerators().stream().map(UUID::toString).collect(Collectors.toList()))); statement.setString(5, guild.getPrefix());
statement.setString(6, guild.getPrefix()); statement.setString(6, guild.getMotd());
statement.setString(7, guild.getMotd()); statement.setString(7, GSON.toJson(guild.getRanks()));
statement.setString(8, GSON.toJson(guild.getRanks())); statement.setString(8, GSON.toJson(guild.getDefaultRank()));
statement.setString(9, GSON.toJson(guild.getDefaultRank())); statement.setString(9, GSON.toJson(guild.getHome()));
statement.setString(10, GSON.toJson(guild.getHome())); statement.setBoolean(10, guild.isTagEnabled());
statement.setBoolean(11, guild.isTagEnabled()); statement.setString(11, guild.getGuildUuid().toString());
statement.execute(); statement.executeUpdate();
return guild; return guild;
} catch (SQLException e) } catch (SQLException e)
{ {
@ -70,7 +100,7 @@ public class SQLGuildManager
ResultSet set = statement.executeQuery(); ResultSet set = statement.executeQuery();
while (set.next()) while (set.next())
{ {
Guild guild = new Guild( Guild guild = new Guild(UUID.fromString(set.getString("guildUuid")),
set.getString("name"), set.getString("name"),
UUID.fromString(set.getString("owner")), UUID.fromString(set.getString("owner")),
ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("createdAt")), ZoneId.of(Plex.get().config.getString("server.timezone")).getRules().getOffset(Instant.now()))); ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("createdAt")), ZoneId.of(Plex.get().config.getString("server.timezone")).getRules().getOffset(Instant.now())));

View File

@ -19,6 +19,7 @@ public class SQLManager
{ {
connection.prepareStatement( connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS `guilds` (" + "CREATE TABLE IF NOT EXISTS `guilds` (" +
"`guildUuid` VARCHAR(46) NOT NULL, " +
"`name` VARCHAR(2000) NOT NULL, " + "`name` VARCHAR(2000) NOT NULL, " +
"`owner` VARCHAR(46) NOT NULL, " + "`owner` VARCHAR(46) NOT NULL, " +
"`createdAt` BIGINT NOT NULL, " + "`createdAt` BIGINT NOT NULL, " +
@ -29,7 +30,8 @@ public class SQLManager
"`moderators` LONGTEXT, " + "`moderators` LONGTEXT, " +
"`ranks` LONGTEXT, " + "`ranks` LONGTEXT, " +
"`defaultRank` LONGTEXT, " + "`defaultRank` LONGTEXT, " +
"`tagEnabled` BOOLEAN" + "`tagEnabled` BOOLEAN, " +
"PRIMARY KEY (`guildUuid`)" +
");" ");"
).execute(); ).execute();
} catch (SQLException e) } catch (SQLException e)

View File

@ -19,6 +19,7 @@ import java.util.UUID;
@Entity("guilds") @Entity("guilds")
public class Guild public class Guild
{ {
private final UUID guildUuid;
private final String name; private final String name;
private final UUID owner; private final UUID owner;
private final ZonedDateTime createdAt; private final ZonedDateTime createdAt;
@ -34,6 +35,6 @@ public class Guild
public static Guild create(Player player, String guildName) public static Guild create(Player player, String guildName)
{ {
return new Guild(guildName, player.getUniqueId(), ZonedDateTime.now(ZoneId.of(Plex.get().config.getString("server.timezone")))); return new Guild(UUID.randomUUID(), guildName, player.getUniqueId(), ZonedDateTime.now(ZoneId.of(Plex.get().config.getString("server.timezone"))));
} }
} }

View File

@ -3,11 +3,10 @@ package dev.plex.guild;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import dev.plex.guild.data.Member; import dev.plex.guild.data.Member;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
public class GuildHolder public class GuildHolder
{ {
@ -25,7 +24,12 @@ public class GuildHolder
public Optional<Guild> getGuild(UUID uuid) public Optional<Guild> getGuild(UUID uuid)
{ {
return GUILDS.stream().filter(guild -> guild.getOwner().equals(uuid) || guild.getMembers().stream().map(Member::uuid).toList().contains(uuid)).findFirst(); return GUILDS.stream().filter(guild -> (guild.getOwner() != null && guild.getOwner().equals(uuid)) || guild.getMembers().stream().map(Member::uuid).toList().contains(uuid)).findFirst();
}
public Collection<Guild> getGuilds()
{
return GUILDS.stream().toList();
} }
} }

View File

@ -31,43 +31,31 @@ public class ChatHandlerImpl implements IChatHandler
@Override @Override
public void doChat(AsyncChatEvent event) public void doChat(AsyncChatEvent event)
{ {
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId());
Component prefix = Plex.get().getRankManager().getPrefix(plexPlayer);
if (prefix != null)
{
renderer.hasPrefix = true;
renderer.prefix = prefix;
} else
{
renderer.hasPrefix = false;
renderer.prefix = null;
}
event.renderer(renderer); event.renderer(renderer);
} }
public static class PlexChatRenderer implements ChatRenderer public static class PlexChatRenderer implements ChatRenderer
{ {
public boolean hasPrefix;
public Component prefix;
@Override @Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer)
{ {
String text = PlexUtils.getTextFromComponent(message); String text = PlexUtils.getTextFromComponent(message);
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(source.getUniqueId());
Component prefix = Plex.get().getRankManager().getPrefix(plexPlayer);
AtomicBoolean guildPrefix = new AtomicBoolean(false); AtomicBoolean guildPrefix = new AtomicBoolean(false);
AtomicReference<Component> component = new AtomicReference<>(Component.empty()); AtomicReference<Component> component = new AtomicReference<>(Component.empty());
Guilds.get().getGuildHolder().getGuild(source.getUniqueId()).ifPresent(guild -> { Guilds.get().getGuildHolder().getGuild(source.getUniqueId()).ifPresent(guild ->
{
if (guild.getPrefix() != null) if (guild.getPrefix() != null)
{ {
component.set(component.get().append(MiniMessage.miniMessage().deserialize(guild.getPrefix()))); component.set(component.get().append(SafeMiniMessage.mmDeserializeWithoutEvents(guild.getPrefix())));
guildPrefix.set(true); guildPrefix.set(true);
} }
}); });
if (hasPrefix) if (prefix != null)
{ {
if (guildPrefix.get()) if (guildPrefix.get())
{ {