fix loading members

create invitation system, still wip but so far working, no confirmation needed yet
This commit is contained in:
Taah 2022-05-08 22:39:47 -07:00
parent 9eb85613f9
commit 81e437dcf0
7 changed files with 179 additions and 10 deletions

View File

@ -80,6 +80,16 @@ public class Guilds extends PlexModule
this.addDefaultMessage("guildChatMessage", "<blue>[GUILD] <aqua>{0} <yellow>{1}", "0 - The player name", "1 - The message"); this.addDefaultMessage("guildChatMessage", "<blue>[GUILD] <aqua>{0} <yellow>{1}", "0 - The player name", "1 - The message");
this.addDefaultMessage("guildChatToggled", "<green>Your chat has been toggled {0}", "0 - On / Off"); this.addDefaultMessage("guildChatToggled", "<green>Your chat has been toggled {0}", "0 - On / Off");
this.addDefaultMessage("guildChatConsoleLog", "<blue>[GUILD - {0}:{1}] <aqua>{2} <yellow>{3}", "0 - The guild name", "1 - The guild unique identifier", "2 - The player name", "3 - The message"); this.addDefaultMessage("guildChatConsoleLog", "<blue>[GUILD - {0}:{1}] <aqua>{2} <yellow>{3}", "0 - The guild name", "1 - The guild unique identifier", "2 - The player name", "3 - The message");
this.addDefaultMessage("guildNoInvite", "<red>You don't have any pending invitations!");
this.addDefaultMessage("guildNotValidInvite", "<red>You don't have an invite from this guild!");
this.addDefaultMessage("guildInviteExists", "<red>You've already sent an invite to this person!");
this.addDefaultMessage("guildInviteSent", "<green>You have sent an invite to <dark_green>{0}", "0 - The invitee");
this.addDefaultMessage("guildInviteReceived", "<green>You have received an invite from <dark_green>{0}<green> for the guild <dark_green>{1}", "0 - The inviter", "1 - The guild name");
this.addDefaultMessage("guildMemberJoined", "<green>{0} has joined the guild!", "0 - The player who joined");
this.addDefaultMessage("guildMemberLeft", "<green>{0} has left the guild!", "0 - The player who left");
this.addDefaultMessage("guildDisbandNeeded", "<red>You need to disband your guild using <gold>/guild disband<red> or promote a new owner using <gold>/guild owner <player>");
this.addDefaultMessage("guildAutoDisbanded", "<green>Auto-disbanding your guild since there were no members");
} }
@Override @Override

View File

@ -41,6 +41,7 @@ public class GuildCommand extends PlexCommand
this.registerSubCommand(new SetHomeSubCommand()); this.registerSubCommand(new SetHomeSubCommand());
this.registerSubCommand(new HomeSubCommand()); this.registerSubCommand(new HomeSubCommand());
this.registerSubCommand(new OwnerSubCommand()); this.registerSubCommand(new OwnerSubCommand());
this.registerSubCommand(new InviteSubCommand());
} catch (Exception e) } catch (Exception e)
{ {
GuildUtil.throwExceptionSync(e); GuildUtil.throwExceptionSync(e);
@ -137,7 +138,7 @@ public class GuildCommand extends PlexCommand
} }
return possibleCommands; return possibleCommands;
} }
if (args.length == 2) if (args.length >= 2)
{ {
PlexCommand subCommand = getSubCommand(args[0]); PlexCommand subCommand = getSubCommand(args[0]);
if (subCommand != null) if (subCommand != null)

View File

@ -43,11 +43,11 @@ public class ChatSubCommand extends PlexCommand
} }
guild.getMembers().stream().map(Member::getPlayer).filter(Objects::nonNull).forEach(player1 -> guild.getMembers().stream().map(Member::getPlayer).filter(Objects::nonNull).forEach(player1 ->
{ {
send(player1, messageComponent("guildChatMessage", player.getName(), StringUtils.join(args, ", "))); send(player1, messageComponent("guildChatMessage", player.getName(), StringUtils.join(args, " ")));
}); });
if (Guilds.get().getConfig().isBoolean("guilds.log-chat-message")) if (Guilds.get().getConfig().isBoolean("guilds.log-chat-message"))
{ {
send(Bukkit.getConsoleSender(), messageComponent("guildChatConsoleLog", guild.getName(), guild.getGuildUuid(), player.getName(), StringUtils.join(args, ", "))); send(Bukkit.getConsoleSender(), messageComponent("guildChatConsoleLog", guild.getName(), guild.getGuildUuid(), player.getName(), StringUtils.join(args, " ")));
} }
}, () -> send(player, messageComponent("guildNotFound"))); }, () -> send(player, messageComponent("guildNotFound")));
return null; return null;

View File

@ -0,0 +1,118 @@
package dev.plex.command.sub;
import com.google.common.collect.ImmutableList;
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.guild.GuildHolder;
import dev.plex.guild.data.Member;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
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;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@CommandParameters(name = "invite", aliases = "inv", usage = "/guild <command> <player name>", description = "Invites a player to the guild")
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.IN_GAME, permission = "plex.guilds.invite")
public class InviteSubCommand extends PlexCommand
{
public InviteSubCommand()
{
super(false);
}
@Override
protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args)
{
if (args.length == 0)
{
return usage();
}
assert player != null;
if (args[0].equalsIgnoreCase("accept"))
{
if (!GuildHolder.PENDING_INVITES.containsKey(player.getUniqueId()))
{
return messageComponent("guildNoInvite");
}
String guildName = StringUtils.join(args, " ", 1, args.length);
GuildHolder.PENDING_INVITES.get(player.getUniqueId()).stream().filter(guild -> guild.getName().equalsIgnoreCase(guildName)).findFirst().ifPresentOrElse(guild ->
{
Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresent(guild1 ->
{
if (guild1.getOwner().getUuid().equals(player.getUniqueId()))
{
if (guild1.getMembers().size() - 1 > 0)
{
send(player, messageComponent("guildDisbandNeeded"));
return;
} else
{
Guilds.get().getSqlGuildManager().deleteGuild(guild1.getGuildUuid()).whenComplete((unused, throwable) ->
{
send(player, messageComponent("guildAutoDisbanded"));
});
}
}
guild1.getMembers().stream().map(Member::getPlayer).filter(Objects::nonNull).forEach(player1 ->
{
send(player1, messageComponent("guildMemberLeft", player.getName()));
});
guild1.getMembers().removeIf(member -> member.getUuid().equals(player.getUniqueId()));
});
GuildHolder.PENDING_INVITES.remove(player.getUniqueId());
guild.addMember(player.getUniqueId());
guild.getMembers().stream().map(Member::getPlayer).filter(Objects::nonNull).forEach(player1 ->
{
send(player1, messageComponent("guildMemberJoined", player.getName()));
});
}, () -> send(player, messageComponent("guildNotValidInvite")));
return null;
}
Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresentOrElse(guild ->
{
if (!guild.getOwner().getUuid().equals(player.getUniqueId()))
{
send(player, messageComponent("guildNotOwner"));
return;
}
Player target = getNonNullPlayer(args[0]);
boolean invite = GuildHolder.sendInvite(target.getUniqueId(), guild);
if (!invite)
{
send(player, messageComponent("guildInviteExists"));
return;
}
send(player, messageComponent("guildInviteSent", target.getName()));
send(target, messageComponent("guildInviteReceived", player.getName(), guild.getName()));
}, () -> send(player, messageComponent("guildNotFound")));
return null;
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (!(sender instanceof Player player)) return ImmutableList.of();
if (args.length == 0) return ImmutableList.of();
if (args[0].equalsIgnoreCase("accept") && args.length == 2)
{
if (!GuildHolder.PENDING_INVITES.containsKey(player.getUniqueId()))
{
return ImmutableList.of();
}
PlexLog.debug("Tab Completing moment");
return GuildHolder.PENDING_INVITES.get(player.getUniqueId()).stream().map(Guild::getName).collect(Collectors.toList());
}
return ImmutableList.of();
}
}

View File

@ -63,6 +63,22 @@ public class SQLGuildManager
}); });
} }
public CompletableFuture<Void> deleteGuild(UUID uuid)
{
return CompletableFuture.runAsync(() ->
{
try (Connection connection = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = connection.prepareStatement(DELETE_GUILD);
statement.setString(1, uuid.toString());
statement.execute();
} catch (SQLException e)
{
GuildUtil.throwExceptionSync(e);
}
});
}
public CompletableFuture<Guild> updateGuild(Guild guild) public CompletableFuture<Guild> updateGuild(Guild guild)
{ {
return CompletableFuture.supplyAsync(() -> return CompletableFuture.supplyAsync(() ->
@ -106,9 +122,10 @@ public class SQLGuildManager
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())));
guild.setName(set.getString("name")); guild.setName(set.getString("name"));
guild.setOwner(GSON.fromJson(set.getString("owner"), Member.class)); guild.setOwner(GSON.fromJson(set.getString("owner"), Member.class));
guild.getMembers().addAll(new Gson().fromJson(set.getString("members"), new TypeToken<List<Member>>() List<Member> members = new Gson().fromJson(set.getString("members"), new TypeToken<List<Member>>()
{ {
}.getType())); }.getType());
members.forEach(guild::addMember);
guild.getModerators().addAll(new Gson().fromJson(set.getString("moderators"), new TypeToken<List<String>>() guild.getModerators().addAll(new Gson().fromJson(set.getString("moderators"), new TypeToken<List<String>>()
{ {
}.getType())); }.getType()));

View File

@ -23,7 +23,6 @@ public class Guild
{ {
private final UUID guildUuid; private final UUID guildUuid;
private final ZonedDateTime createdAt; private final ZonedDateTime createdAt;
private transient final List<UUID> outgoingInvitations = Lists.newArrayList();
private final List<Member> members = Lists.newArrayList(); private final List<Member> members = Lists.newArrayList();
private final List<UUID> moderators = Lists.newArrayList(); private final List<UUID> moderators = Lists.newArrayList();
private final List<Rank> ranks = Lists.newArrayList(); private final List<Rank> ranks = Lists.newArrayList();
@ -55,6 +54,16 @@ public class Guild
return members.stream().filter(m -> m.getUuid().equals(uuid)).findFirst().orElse(null); return members.stream().filter(m -> m.getUuid().equals(uuid)).findFirst().orElse(null);
} }
public void addMember(UUID uuid)
{
addMember(new Member(uuid));
}
public void addMember(Member member)
{
this.members.add(member);
}
public List<Member> getMembers() public List<Member> getMembers()
{ {
List<Member> allMembers = Lists.newArrayList(members); List<Member> allMembers = Lists.newArrayList(members);

View File

@ -1,17 +1,31 @@
package dev.plex.guild; package dev.plex.guild;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.plex.guild.data.Member; import dev.plex.guild.data.Member;
import java.util.Collection; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class GuildHolder public class GuildHolder
{ {
public static final Map<UUID, List<Guild>> PENDING_INVITES = Maps.newHashMap();
private static final List<Guild> GUILDS = Lists.newArrayList(); private static final List<Guild> GUILDS = Lists.newArrayList();
public static boolean sendInvite(UUID uuid, Guild guild)
{
if (PENDING_INVITES.containsKey(uuid) && PENDING_INVITES.get(uuid).stream().anyMatch(guild1 -> guild1.getGuildUuid().equals(guild.getGuildUuid())))
{
return false;
}
if (PENDING_INVITES.containsKey(uuid))
{
PENDING_INVITES.get(uuid).add(guild);
} else {
PENDING_INVITES.put(uuid, Lists.newArrayList(guild));
}
return true;
}
public void addGuild(Guild guild) public void addGuild(Guild guild)
{ {
GUILDS.add(guild); GUILDS.add(guild);