Sync roles

This commit is contained in:
Teriuihi 2021-09-20 03:05:27 +02:00
parent 42931b2761
commit f1cb98da10
6 changed files with 60 additions and 15 deletions

View File

@ -94,6 +94,7 @@ public class DiscordLinkCommand extends DiscordCommand {
DiscordLinkPlayer.addDiscordLinkPlayer(discordLinkPlayer);
DiscordLink.getPlugin().getDatabase().syncPlayer(discordLinkPlayer);
DiscordLink.getPlugin().getDatabase().syncRoles(discordLinkPlayer);
DiscordLink.getPlugin().getCache().removeCachedPlayer(discordLinkPlayer.getUuid());
}

View File

@ -41,24 +41,26 @@ public class DiscordRoleListener extends ListenerAdapter {
if (added_roles.isEmpty())
return;
DiscordLinkPlayer player = DiscordLinkPlayer.getDiscordLinkPlayer(event.getUser().getIdLong());
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(event.getUser().getIdLong());
if (player == null) {
if (discordLinkPlayer == null) {
//TODO mayb ask the player to link to get their in game rank?
return;
}
player.updateMinecraft(added_roles, true);
discordLinkPlayer.updateMinecraft(added_roles, true);
added_roles.forEach(discordRole -> {
if (!discordRole.getAnnouncement().isEmpty()) {
Component component = miniMessage.parse(
discordRole.getAnnouncement(),
Template.of("player", player.getUsername()));
Template.of("player", discordLinkPlayer.getUsername()));
DiscordLink.getPlugin().getProxy().getAllPlayers()
.forEach(onlinePlayer -> onlinePlayer.sendMessage(component));
}
});
DiscordLink.getPlugin().getDatabase().syncRoles(discordLinkPlayer);
}
/**
@ -70,10 +72,18 @@ public class DiscordRoleListener extends ListenerAdapter {
.filter(discordRole -> event.getRoles().stream()
.anyMatch(role -> role.getIdLong() == discordRole.getId()))
.collect(Collectors.toList());
if (removed_roles.isEmpty())
return;
DiscordLinkPlayer.getDiscordLinkPlayer(event.getUser().getIdLong())
.updateMinecraft(removed_roles, false);
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(event.getUser().getIdLong());
if (discordLinkPlayer == null) {
//TODO mayb ask the player to link to get their in game rank?
return;
}
discordLinkPlayer.updateMinecraft(removed_roles, false);
DiscordLink.getPlugin().getDatabase().syncRoles(discordLinkPlayer);
}
}

View File

@ -68,6 +68,43 @@ public class Database {
}
}
public void syncRoles(DiscordLinkPlayer player) {
//Delete all roles
try {
String sql = "DELETE FROM account_roles WHERE uuid = ?";
PreparedStatement statement = DatabaseConnection.getConnection().prepareStatement(sql);
statement.setString(1, player.getUuid().toString());
statement.execute();
} catch (SQLException exception) {
exception.printStackTrace();
}
//Add all roles back
try {
String sql = "INSERT INTO account_roles " +
"VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE " +
"uuid = ?, " +
"role_name = ?";
PreparedStatement statement = DatabaseConnection.getConnection().prepareStatement(sql);
statement.setString(1, player.getUuid().toString());
statement.setString(3, player.getUuid().toString());
for (String role : player.getRoles()) {
statement.setString(2, role);
statement.setString(4, role);
statement.addBatch();
}
statement.executeBatch();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
public boolean playerIsLinked(Player player) { //TODO maybe this can be using the discord api instead? (or a cache idk)
try {
PreparedStatement statement = DatabaseConnection.getConnection()

View File

@ -42,6 +42,7 @@ public class LuckpermsEvents {
return;
discordLinkPlayer.updateDiscord(List.of(optional.get()), added);
DiscordLink.getPlugin().getDatabase().syncRoles(discordLinkPlayer);
}
}

View File

@ -11,7 +11,7 @@ public class PlayerJoin {
@Subscribe(order = PostOrder.LATE)
public void playerConnected(ServerConnectedEvent event) {
if (event.getPreviousServer().isEmpty())
if (event.getPreviousServer().isPresent())
return;
boolean sync = false;

View File

@ -66,11 +66,9 @@ public class DiscordLinkPlayer {
public void updateDiscord(List<DiscordRole> roles, boolean added) {
if (added)
roles.stream().filter(DiscordRole::isUpdateToDiscord).forEach(role -> DiscordLink.getPlugin().getBot().addRole(userId, role.getId(), BotConfig.GUILD_ID)); //TODO test
roles.stream().filter(DiscordRole::isUpdateToDiscord).forEach(role -> DiscordLink.getPlugin().getBot().addRole(userId, role.getId(), BotConfig.GUILD_ID));
else
roles.stream().filter(DiscordRole::isUpdateToDiscord).forEach(role -> DiscordLink.getPlugin().getBot().removeRole(userId, role.getId(), BotConfig.GUILD_ID)); //TODO test
//TODO implement
//TODO SYNC ROLES TO DATABASE
roles.stream().filter(DiscordRole::isUpdateToDiscord).forEach(role -> DiscordLink.getPlugin().getBot().removeRole(userId, role.getId(), BotConfig.GUILD_ID));
}
public void updateMinecraft(List<DiscordRole> roles, boolean added) {
@ -86,15 +84,13 @@ public class DiscordLinkPlayer {
user.data().remove(group);
});
});
//TODO implement
//TODO SYNC ROLES TO DATABASE
}
public void linkedRole(boolean add) {
if (add)
DiscordLink.getPlugin().getBot().addRole(userId, BotConfig.LINKED_ROLE_ID, BotConfig.GUILD_ID); //TODO test
DiscordLink.getPlugin().getBot().addRole(userId, BotConfig.LINKED_ROLE_ID, BotConfig.GUILD_ID);
else
DiscordLink.getPlugin().getBot().removeRole(userId, BotConfig.LINKED_ROLE_ID, BotConfig.GUILD_ID); //TODO test
DiscordLink.getPlugin().getBot().removeRole(userId, BotConfig.LINKED_ROLE_ID, BotConfig.GUILD_ID);
}
//Static stuff