From 20c65e9bd829df337fd50b891680a1bdc4e5b67f Mon Sep 17 00:00:00 2001 From: "Vincent B." <79211348+Snabeldier@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:11:09 +0100 Subject: [PATCH] feat: implement TeamMember as a subinterface of OnlineUser (#267) --- .../minevalley/core/api/users/OnlineUser.java | 44 +++++- .../minevalley/core/api/users/TeamMember.java | 125 ++++++++++++++++++ .../java/minevalley/core/api/users/User.java | 12 ++ .../core/api/users/enums/Fraction.java | 1 + .../exceptions/UserNotOnlineException.java | 17 +++ .../exceptions/UserNotPermittedException.java | 22 +++ 6 files changed, 217 insertions(+), 4 deletions(-) create mode 100644 src/main/java/minevalley/core/api/users/TeamMember.java create mode 100644 src/main/java/minevalley/core/api/users/exceptions/UserNotOnlineException.java create mode 100644 src/main/java/minevalley/core/api/users/exceptions/UserNotPermittedException.java diff --git a/src/main/java/minevalley/core/api/users/OnlineUser.java b/src/main/java/minevalley/core/api/users/OnlineUser.java index 14815d73..b77704b5 100644 --- a/src/main/java/minevalley/core/api/users/OnlineUser.java +++ b/src/main/java/minevalley/core/api/users/OnlineUser.java @@ -12,6 +12,7 @@ import minevalley.core.api.users.enums.McVersion; import minevalley.core.api.users.enums.TabListView; import minevalley.core.api.users.enums.TeamRank; +import minevalley.core.api.users.exceptions.UserNotPermittedException; import minevalley.core.api.utils.ChatHandler; import minevalley.core.api.utils.ClickableMessage; import minevalley.core.api.vehicles.LoadedVehicle; @@ -386,12 +387,34 @@ default void closeInventory() { @Contract(pure = true) Fraction getFractionService(); - void enterFractionService(@Nonnull Fraction service) throws IllegalArgumentException; + /** + * Sets the user's current fraction service. + * + * @param service fraction to enter service + * @throws IllegalArgumentException if the service is null + * @throws UserNotPermittedException if the user is not allowed to enter the service + * @throws IllegalStateException if the user is already in a service + */ + void enterFractionService(@Nonnull Fraction service) throws IllegalArgumentException, UserNotPermittedException, IllegalStateException; - void leaveFractionService(); + /** + * Lets the user leave the fraction-service. + * + * @throws IllegalStateException if the user is not in a service + */ + void leaveFractionService() throws IllegalStateException; // TeamRank + /** + * Gets the team-member object of this user. + * + * @return team-member object + * @throws UserNotPermittedException if the user is no team-member + */ + @Nonnull + TeamMember team() throws UserNotPermittedException; + /** * Gets if the player has any type of team-rank. * @@ -405,6 +428,7 @@ default void closeInventory() { * * @return true, if the user is team-plus-member */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean isTeamPlus(); @@ -413,7 +437,8 @@ default void closeInventory() { * * @return users team-rank */ - @Nullable + @Deprecated(forRemoval = true) + @Nonnull @Contract(pure = true) TeamRank getTeamRank(); @@ -423,6 +448,7 @@ default void closeInventory() { * * @return [custom] team rank name */ + @Deprecated(forRemoval = true) @Nullable @Contract(pure = true) String getCustomTeamRankName(); @@ -435,6 +461,7 @@ default void closeInventory() { * * @return true, if this user is displayed as team member in chat, tab list, etc. */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean isDisplayedAsTeamler(); @@ -444,6 +471,7 @@ default void closeInventory() { * @param ranks list of team-ranks to be checked for * @return true, if the user has one of the ranks */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean hasTeamRank(@Nonnull TeamRank... ranks); @@ -452,6 +480,7 @@ default void closeInventory() { * * @return true, if the player is allowed to use a general-key */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean isAllowedToUseGeneralKey(); @@ -460,6 +489,7 @@ default void closeInventory() { * * @return true, if the player is using a general-key */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean isUsingGeneralKey(); @@ -471,6 +501,7 @@ default void closeInventory() { /** * Lets the user leave the team-service. */ + @Deprecated(forRemoval = true) void leaveTeamService() throws IllegalStateException; /** @@ -478,17 +509,20 @@ default void closeInventory() { * * @return true, if the user is allowed to enter the support-service */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean canEnterSupportService(); /** * Lets the user enter the support-service. If the user isn't allowed to, nothing happens. */ + @Deprecated(forRemoval = true) void joinSupportService() throws UnsupportedOperationException, IllegalStateException; /** * Lets the user leave the support-service. */ + @Deprecated(forRemoval = true) void leaveSupportService() throws IllegalStateException; /** @@ -496,6 +530,7 @@ default void closeInventory() { * * @return true, if the user is marked as server-operator */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean isOperator(); @@ -504,6 +539,7 @@ default void closeInventory() { * * @return true, if the user is in support-service */ + @Deprecated(forRemoval = true) @Contract(pure = true) boolean isInSupportService(); @@ -564,7 +600,7 @@ default void closeInventory() { @Contract(pure = true) boolean isAllowedToUse(Block block); - void changeSign(@Nonnull Block block, @Nonnull String line1, @Nonnull String line2, @Nonnull String line3, @Nonnull String line4) throws IllegalArgumentException; + void changeSign(@Nonnull Block block, @Nullable String line1, @Nullable String line2, @Nullable String line3, @Nullable String line4) throws IllegalArgumentException; void resetSign(@Nonnull Block block) throws IllegalArgumentException; diff --git a/src/main/java/minevalley/core/api/users/TeamMember.java b/src/main/java/minevalley/core/api/users/TeamMember.java new file mode 100644 index 00000000..607e9b5b --- /dev/null +++ b/src/main/java/minevalley/core/api/users/TeamMember.java @@ -0,0 +1,125 @@ +package minevalley.core.api.users; + +import minevalley.core.api.users.enums.TeamRank; +import org.jetbrains.annotations.Contract; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public interface TeamMember extends OnlineUser { + + /** + * Gets whether the player is team-member with the plus-rank. + * + * @return true, if the user is team-plus-member + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean isTeamPlus(); + + /** + * Gets the team-rank of this user. + * + * @return users team-rank + */ + @Nonnull + @Contract(pure = true) + @SuppressWarnings("removal") + TeamRank getTeamRank(); + + /** + * Gets the custom team rank name. + * If no custom name is defined, this returns the name of the users team rank. + * + * @return [custom] team rank name + */ + @Nullable + @Contract(pure = true) + @SuppressWarnings("removal") + String getCustomTeamRankName(); + + /** + * Sometimes a player is assigned a team rank to grant them permission or access. + * Because team members are highlighted in the chat and tab list, + * they appear representative of the project and players may assume they have the permission and knowledge + * to guide other players. Therefore, the rank assigned to the player can be hidden. + * + * @return true, if this user is displayed as team member in chat, tab list, etc. + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean isDisplayedAsTeamler(); + + /** + * Gets whether the user has any of the listed team-ranks. + * + * @param ranks list of team-ranks to be checked for + * @return true, if the user has one of the ranks + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean hasTeamRank(@Nonnull TeamRank... ranks); + + /** + * Gets whether the user is allowed to use a general-key + * + * @return true, if the player is allowed to use a general-key + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean isAllowedToUseGeneralKey(); + + /** + * Gets whether the user is using a general-key at the moment, by checking the item in his hand. If he is using a general-key without the permission to do, his key gets removed automatically. + * + * @return true, if the player is using a general-key + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean isUsingGeneralKey(); + + /** + * Lets the user leave the team-service. + */ + @SuppressWarnings("removal") + void leaveTeamService() throws IllegalStateException; + + /** + * Gets whether the user is allowed to enter the support-service. + * + * @return true, if the user is allowed to enter the support-service + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean canEnterSupportService(); + + /** + * Lets the user enter the support-service. If the user isn't allowed to, nothing happens. + */ + @SuppressWarnings("removal") + void joinSupportService() throws UnsupportedOperationException, IllegalStateException; + + /** + * Lets the user leave the support-service. + */ + @SuppressWarnings("removal") + void leaveSupportService() throws IllegalStateException; + + /** + * Gets whether the user is marked as server-operator (!= OP-permission) + * + * @return true, if the user is marked as server-operator + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean isOperator(); + + /** + * Gets whether the user is in support-service + * + * @return true, if the user is in support-service + */ + @Contract(pure = true) + @SuppressWarnings("removal") + boolean isInSupportService(); +} diff --git a/src/main/java/minevalley/core/api/users/User.java b/src/main/java/minevalley/core/api/users/User.java index bf6012b5..c8e31973 100644 --- a/src/main/java/minevalley/core/api/users/User.java +++ b/src/main/java/minevalley/core/api/users/User.java @@ -4,11 +4,13 @@ import minevalley.core.api.corporations.Member; import minevalley.core.api.enums.DebugType; import minevalley.core.api.phone.Telephone; +import minevalley.core.api.users.exceptions.UserNotOnlineException; import minevalley.core.api.users.friends.FriendRequest; import minevalley.core.api.users.friends.Friendship; import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; +import javax.annotation.Nonnull; import java.time.LocalDate; import java.util.List; import java.util.Map; @@ -16,6 +18,16 @@ public interface User extends Registrant { + /** + * Gets the online user-object of this user. + * + * @return online user-object + * @throws UserNotOnlineException if the user is not online + */ + @Nonnull + OnlineUser online() throws UserNotOnlineException; + + @Deprecated(forRemoval = true) OnlineUser getOnlineUser(); boolean isOnline(); diff --git a/src/main/java/minevalley/core/api/users/enums/Fraction.java b/src/main/java/minevalley/core/api/users/enums/Fraction.java index aa4e8445..6aef363b 100644 --- a/src/main/java/minevalley/core/api/users/enums/Fraction.java +++ b/src/main/java/minevalley/core/api/users/enums/Fraction.java @@ -6,6 +6,7 @@ @Getter @RequiredArgsConstructor(access = AccessLevel.PRIVATE) +@SuppressWarnings("unused") public enum Fraction { POLICE("Polizei"), diff --git a/src/main/java/minevalley/core/api/users/exceptions/UserNotOnlineException.java b/src/main/java/minevalley/core/api/users/exceptions/UserNotOnlineException.java new file mode 100644 index 00000000..a2a445e5 --- /dev/null +++ b/src/main/java/minevalley/core/api/users/exceptions/UserNotOnlineException.java @@ -0,0 +1,17 @@ +package minevalley.core.api.users.exceptions; + +import lombok.Getter; +import minevalley.core.api.users.User; + +import javax.annotation.Nonnull; + +@Getter +public class UserNotOnlineException extends RuntimeException { + + private final User user; + + public UserNotOnlineException(@Nonnull User user) { + super("User " + user.getName() + " is not online."); + this.user = user; + } +} \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/users/exceptions/UserNotPermittedException.java b/src/main/java/minevalley/core/api/users/exceptions/UserNotPermittedException.java new file mode 100644 index 00000000..b8108b67 --- /dev/null +++ b/src/main/java/minevalley/core/api/users/exceptions/UserNotPermittedException.java @@ -0,0 +1,22 @@ +package minevalley.core.api.users.exceptions; + +import lombok.Getter; +import minevalley.core.api.users.User; + +import javax.annotation.Nonnull; + +@Getter +public class UserNotPermittedException extends RuntimeException { + + private final User user; + + public UserNotPermittedException(@Nonnull User user) { + super("User " + user.getName() + " is not permitted to perform this action."); + this.user = user; + } + + public UserNotPermittedException(@Nonnull User user, @Nonnull String message) { + super(message); + this.user = user; + } +} \ No newline at end of file