diff --git a/api/src/main/java/com/gmail/chickenpowerrr/ranksync/api/rank/RankFactory.java b/api/src/main/java/com/gmail/chickenpowerrr/ranksync/api/rank/RankFactory.java index b4bc83c..ddf40cf 100644 --- a/api/src/main/java/com/gmail/chickenpowerrr/ranksync/api/rank/RankFactory.java +++ b/api/src/main/java/com/gmail/chickenpowerrr/ranksync/api/rank/RankFactory.java @@ -67,4 +67,19 @@ public interface RankFactory { * Returns the Bot that uses this factory */ Bot getBot(); + + /** + * Adds a RankHelper to validate all ranks + * + * @param rankHelper the helper that validates if the given ranks exist + */ + void addRankHelper(RankHelper rankHelper); + + /** + * Returns if the given rank is a valid platform rank according to the RankHelpers + * + * @param rank the rank that could be synchronized + * @return if the given rank is a valid platform rank according to the RankHelpers + */ + boolean isValidRank(Rank rank); } diff --git a/api/src/main/java/com/gmail/chickenpowerrr/ranksync/api/rank/RankHelper.java b/api/src/main/java/com/gmail/chickenpowerrr/ranksync/api/rank/RankHelper.java new file mode 100644 index 0000000..3ecb624 --- /dev/null +++ b/api/src/main/java/com/gmail/chickenpowerrr/ranksync/api/rank/RankHelper.java @@ -0,0 +1,37 @@ +package com.gmail.chickenpowerrr.ranksync.api.rank; + +import com.gmail.chickenpowerrr.ranksync.api.bot.Bot; + +/** + * This interface contains the methods needed to validate all given ranks + * + * @author Chickenpowerrr + * @since 1.2.1 + */ +public interface RankHelper { + + /** + * Returns if the given rank has been synchronized over the platforms + * + * @param bot the bot that could synchronize the rank + * @param rank the rank that could be synchronized + * @return if the given rank has been synchronized over the platforms + */ + boolean isSynchronized(Bot bot, Rank rank); + + /** + * Returns a Rank based on the Bot that is able to give to users and the name of the service's + * Rank + * + * @param bot the running Bot + * @param serviceGroupName the name of the Minecraft Rank + * @return a Rank based on the Bot that is able to give to users and the name of the Rank + */ + Rank getRank(Bot bot, String serviceGroupName); + + /** + * Validates all cached ranks and if they don't exist in the given Bot or service, a message will + * be send to the console + */ + void validateRanks(); +} diff --git a/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/player/Player.java b/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/player/Player.java index 5ff17bd..8caf7f5 100644 --- a/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/player/Player.java +++ b/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/player/Player.java @@ -97,7 +97,10 @@ public void setRanks(Collection ranks) { Collection roles = this.member.getRoles(); Collection toRemove = new HashSet<>(roles); - toRemove.removeIf(role -> ranks.contains(this.rankFactory.getRankFromRole(role))); + toRemove.removeIf(role -> { + Rank rank = this.rankFactory.getRankFromRole(role); + return ranks.contains(rank) || !this.rankFactory.isValidRank(rank); + }); Collection toAdd = this.rankFactory.getRolesFromRanks(ranks); toAdd.removeIf(roles::contains); diff --git a/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/rank/RankFactory.java b/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/rank/RankFactory.java index c45e9de..3d425a7 100644 --- a/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/rank/RankFactory.java +++ b/discord/src/main/java/com/gmail/chickenpowerrr/ranksync/discord/rank/RankFactory.java @@ -2,15 +2,16 @@ import com.gmail.chickenpowerrr.ranksync.api.bot.Bot; import com.gmail.chickenpowerrr.ranksync.api.rank.Rank; -import lombok.Getter; -import net.dv8tion.jda.core.entities.Guild; -import net.dv8tion.jda.core.entities.Role; - +import com.gmail.chickenpowerrr.ranksync.api.rank.RankHelper; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import lombok.Getter; +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.Role; /** * This class contains the functionalities to get a representation of a Discord role @@ -25,7 +26,9 @@ public class RankFactory implements com.gmail.chickenpowerrr.ranksync.api.rank.R private final Map ranks = new HashMap<>(); private final Map roles = new HashMap<>(); private final Guild guild; - @Getter private final Bot bot; + @Getter + private final Bot bot; + private final Collection rankHelpers = new HashSet<>(); /** * @param bot the Bot that is running @@ -150,4 +153,26 @@ public Role getRoleFromRank(Rank rank) { public Collection getRolesFromRanks(Collection ranks) { return ranks.stream().map(this::getRoleFromRank).collect(Collectors.toSet()); } + + /** + * Adds a RankHelper to validate all ranks + * + * @param rankHelper the helper that validates if the given ranks exist + */ + @Override + public void addRankHelper(RankHelper rankHelper) { + this.rankHelpers.add(rankHelper); + } + + /** + * Returns if the given rank is a valid platform rank according to the RankHelpers + * + * @param rank the rank that could be synchronized + * @return if the given rank is a valid platform rank according to the RankHelpers + */ + @Override + public boolean isValidRank(Rank rank) { + return this.rankHelpers.stream() + .anyMatch(rankHelper -> rankHelper.isSynchronized(this.bot, rank)); + } } diff --git a/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/listener/ranksyc/BotEnabledEventListener.java b/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/listener/ranksyc/BotEnabledEventListener.java index 1d2fbc8..4354dfc 100644 --- a/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/listener/ranksyc/BotEnabledEventListener.java +++ b/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/listener/ranksyc/BotEnabledEventListener.java @@ -22,12 +22,13 @@ public Class getTarget() { } /** - * Validates the ranks used by this Bot + * Validates the ranks used by this Bot and adds the validator to the RankFactory * * @param event the event that triggered the method */ @Override public void onEvent(BotEnabledEvent event) { this.rankHelper.validateRanks(); + event.getBot().getRankFactory().addRankHelper(this.rankHelper); } } diff --git a/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/roleresource/RankHelper.java b/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/roleresource/RankHelper.java index ca10081..3cb3b01 100644 --- a/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/roleresource/RankHelper.java +++ b/spigot/src/main/java/com/gmail/chickenpowerrr/ranksync/spigot/roleresource/RankHelper.java @@ -15,7 +15,7 @@ * @since 1.0.0 */ @Slf4j -public class RankHelper { +public class RankHelper implements com.gmail.chickenpowerrr.ranksync.api.rank.RankHelper { private static final Logger LOGGER = Logger.getLogger(RankHelper.class.getSimpleName()); @@ -29,6 +29,19 @@ public RankHelper(Map, String>> ranks) { this.ranks = ranks; } + /** + * Returns if the given rank has been synchronized over the platforms + * + * @param bot the bot that could synchronize the rank + * @param rank the rank that could be synchronized + * @return if the given rank has been synchronized over the platforms + */ + @Override + public boolean isSynchronized(Bot bot, Rank rank) { + return this.ranks.values().stream() + .anyMatch((map) -> map.containsKey(bot) && map.get(bot).equalsIgnoreCase(rank.getName())); + } + /** * Returns a Rank based on the Bot that is able to give to users and the name of the Minecraft * Rank @@ -38,7 +51,7 @@ public RankHelper(Map, String>> ranks) { * @return a Rank based on the Bot that is able to give to users and the name of the Rank */ @SuppressWarnings("unchecked") - Rank getRank(Bot bot, String minecraftGroupName) { + public Rank getRank(Bot bot, String minecraftGroupName) { if (this.ranks.containsKey(minecraftGroupName) && this.ranks.get(minecraftGroupName) .containsKey(bot)) { return bot.getRankFactory().getRankFromRole( @@ -49,9 +62,10 @@ Rank getRank(Bot bot, String minecraftGroupName) { } /** - * Validates all cached and if they don't exist in the given Bot or Minecraft server, a message - * will be send to the console + * Validates all cached ranks and if they don't exist in the given Bot or Minecraft server, a + * message will be send to the console */ + @Override public void validateRanks() { this.ranks.forEach((minecraftRank, syncedRanks) -> { AtomicBoolean minecraftChecked = new AtomicBoolean(false);