-
-
Notifications
You must be signed in to change notification settings - Fork 20
GH-870 Add /msgtoggle command #896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b5e54ad
Innit commit
CitralFlo ffd29c5
In progress: add messages, setup methods and commands
CitralFlo 88a71f7
Merge branch 'refs/heads/master' into msgtoggle
CitralFlo 43ea1b0
Add msgToggle repo, service, API and commands
CitralFlo f054dbd
Fix logic
CitralFlo 2437624
Fix logic, rename messages
CitralFlo aa1ff47
Remove comments
CitralFlo 6da3ddf
Delete duplicated message.
CitralFlo 4a77643
Update eternalcore-api/src/main/java/com/eternalcode/core/feature/msg…
CitralFlo 00e97d0
Delete duplicated message.
CitralFlo 5291625
Merge remote-tracking branch 'origin/msgtoggle' into msgtoggle
CitralFlo a091a18
Fix coderabbit issue
CitralFlo b3dae93
Fix naming and logic.
CitralFlo 97d5914
Merge branch 'master' into msgtoggle
CitralFlo 76d85d9
Resolve @imDMK review
CitralFlo df9b84f
Add comments to api
CitralFlo 4709133
Resolve Rollcz's review
CitralFlo 450813c
Resolve coderabbit review
CitralFlo f22fe08
Update README.md
CitralFlo 98cb286
Fix logic: enable -> enable priv messages.
CitralFlo f9cd9b7
Resolve @coderabbitai nitpick comment, simplify command and api
CitralFlo 39b6dfc
Refactor names of variables following vLuckyyy comment
CitralFlo 1ed9c34
Change name from toggleState to State
CitralFlo d72ae5c
Simplify logic (i hope @Rollczi will like it) refactor to PrivateChat…
CitralFlo 6a12777
Merge branch 'master' into msgtoggle
CitralFlo dabdf7d
Resolve master changes
CitralFlo 297cff4
Resolve @coderabbitai 's review
CitralFlo 1b57a89
Cleanup method and parameters names. Remove unused classes.
Rollczi 6227fa3
Simplify commands
Rollczi 5a41247
Fix command messages
Rollczi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...e-api/src/main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.eternalcode.core.feature.privatechat.toggle; | ||
|
|
||
| /** | ||
| * Enum representing state of blocking incoming private messages by the player. | ||
| */ | ||
| public enum PrivateChatState { | ||
|
|
||
| /** | ||
| * Player can receive private messages. | ||
| */ | ||
| ENABLE, | ||
|
|
||
| /** | ||
| * Player cannot receive private messages. | ||
| */ | ||
| DISABLE; | ||
|
|
||
| PrivateChatState invert() { | ||
| return this == ENABLE ? DISABLE : ENABLE; | ||
| } | ||
|
|
||
| } | ||
36 changes: 36 additions & 0 deletions
36
...rc/main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.eternalcode.core.feature.privatechat.toggle; | ||
|
|
||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * This Service manages player's receiving of private messages | ||
| */ | ||
| public interface PrivateChatStateService { | ||
|
|
||
| /** | ||
| * Checks status of player's private chat messages blocking. | ||
| * | ||
| * @param playerUniqueId player's UUID. | ||
| * @return state of player's private chat messages blocking. | ||
| */ | ||
| CompletableFuture<PrivateChatState> getChatState(UUID playerUniqueId); | ||
|
|
||
| /** | ||
| * Sets blocking of incoming private messages. | ||
| * | ||
| * @param playerUniqueId player's UUID. | ||
| * @param state desired state of player's private chat messages blocking. | ||
| */ | ||
| CompletableFuture<Void> setChatState(UUID playerUniqueId, PrivateChatState state); | ||
|
|
||
|
|
||
| /** | ||
| * Toggle blocking of incoming private messages. | ||
| * | ||
| * @param playerUniqueId player's UUID. | ||
| * @return new state of player's private chat messages blocking. | ||
| */ | ||
| CompletableFuture<PrivateChatState> toggleChatState(UUID playerUniqueId); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...rc/main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.eternalcode.core.feature.privatechat.toggle; | ||
|
|
||
| import com.eternalcode.annotations.scan.command.DescriptionDocs; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.notice.NoticeService; | ||
| import dev.rollczi.litecommands.annotations.argument.Arg; | ||
| import dev.rollczi.litecommands.annotations.command.Command; | ||
| import dev.rollczi.litecommands.annotations.context.Context; | ||
| import dev.rollczi.litecommands.annotations.execute.Execute; | ||
| import dev.rollczi.litecommands.annotations.permission.Permission; | ||
| import java.util.UUID; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| @Command(name = "msgtoggle") | ||
| @Permission("eternalcore.msgtoggle") | ||
| class PrivateChatStateCommand { | ||
|
|
||
| private final PrivateChatStateService privateChatStateService; | ||
| private final NoticeService noticeService; | ||
|
|
||
| @Inject | ||
| PrivateChatStateCommand(PrivateChatStateService privateChatStateService, NoticeService noticeService) { | ||
| this.privateChatStateService = privateChatStateService; | ||
| this.noticeService = noticeService; | ||
| } | ||
|
|
||
| @Execute | ||
| @DescriptionDocs(description = "Toggle receiving private messages") | ||
| void execute(@Context Player sender) { | ||
| UUID player = sender.getUniqueId(); | ||
| this.privateChatStateService.toggleChatState(player) | ||
| .thenAccept(toggledState -> noticePlayer(toggledState, player)); | ||
| } | ||
|
|
||
| @Execute | ||
| @DescriptionDocs(description = "Switch receiving private messages", arguments = "<state>") | ||
| void execute(@Context Player sender, @Arg PrivateChatState state) { | ||
| UUID player = sender.getUniqueId(); | ||
| this.privateChatStateService.setChatState(player, state) | ||
| .thenAccept(ignored -> noticePlayer(state, player)); | ||
| } | ||
|
|
||
| @Execute | ||
| @Permission("eternalcore.msgtoggle.other") | ||
| @DescriptionDocs(description = "Switch receiving private messages for other player", arguments = "<player>") | ||
| void other(@Context CommandSender sender, @Arg Player target) { | ||
| UUID player = target.getUniqueId(); | ||
| this.privateChatStateService.toggleChatState(player) | ||
| .thenAccept(toggledState -> noticeOtherPlayer(sender, toggledState, target)); | ||
| } | ||
|
|
||
| @Execute | ||
| @Permission("eternalcore.msgtoggle.other") | ||
| @DescriptionDocs(description = "Switch receiving private messages for other player", arguments = "<player> <toggle>") | ||
| void other(@Context CommandSender sender, @Arg Player target, @Arg PrivateChatState state) { | ||
| UUID player = target.getUniqueId(); | ||
| this.privateChatStateService.setChatState(player, state) | ||
| .thenAccept(ignored -> noticeOtherPlayer(sender, state, target)); | ||
| } | ||
|
|
||
| private void noticeOtherPlayer(CommandSender sender, PrivateChatState state, Player target) { | ||
| this.noticePlayer(state, target.getUniqueId()); | ||
| if (sender.equals(target)) { | ||
| return; | ||
| } | ||
|
|
||
| this.noticeService.create() | ||
| .sender(sender) | ||
| .notice(translation -> state == PrivateChatState.DISABLE | ||
| ? translation.privateChat().otherMessagesDisabled() | ||
| : translation.privateChat().otherMessagesEnabled() | ||
| ) | ||
| .placeholder("{PLAYER}", target.getName()) | ||
| .send(); | ||
| } | ||
|
|
||
| private void noticePlayer(PrivateChatState state, UUID player) { | ||
| this.noticeService.create() | ||
| .player(player) | ||
| .notice(translation -> state == PrivateChatState.ENABLE | ||
| ? translation.privateChat().selfMessagesEnabled() | ||
| : translation.privateChat().selfMessagesDisabled() | ||
| ) | ||
| .send(); | ||
| } | ||
|
|
||
| } |
13 changes: 13 additions & 0 deletions
13
...main/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.eternalcode.core.feature.privatechat.toggle; | ||
|
|
||
|
|
||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| interface PrivateChatStateRepository { | ||
|
|
||
| CompletableFuture<PrivateChatState> getPrivateChatState(UUID uuid); | ||
|
|
||
| CompletableFuture<Void> setPrivateChatState(UUID uuid, PrivateChatState toggledOff); | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
...va/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateRepositoryOrmLite.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.eternalcode.core.feature.privatechat.toggle; | ||
|
|
||
| import com.eternalcode.commons.scheduler.Scheduler; | ||
| import com.eternalcode.core.database.DatabaseManager; | ||
| import com.eternalcode.core.database.wrapper.AbstractRepositoryOrmLite; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Repository; | ||
| import com.j256.ormlite.table.TableUtils; | ||
| import java.sql.SQLException; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| @Repository | ||
| class PrivateChatStateRepositoryOrmLite extends AbstractRepositoryOrmLite implements PrivateChatStateRepository { | ||
|
|
||
| @Inject | ||
| private PrivateChatStateRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) throws SQLException { | ||
CitralFlo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| super(databaseManager, scheduler); | ||
| TableUtils.createTableIfNotExists(databaseManager.connectionSource(), PrivateChatStateWrapper.class); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<PrivateChatState> getPrivateChatState(UUID uuid) { | ||
| return this.selectSafe(PrivateChatStateWrapper.class, uuid) | ||
| .thenApply( | ||
| optional -> optional.map(PrivateChatStateWrapper::isEnabled).orElse(PrivateChatState.ENABLE) | ||
| ).exceptionally(throwable -> PrivateChatState.ENABLE); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> setPrivateChatState(UUID uuid, PrivateChatState state) { | ||
| return this.save(PrivateChatStateWrapper.class, new PrivateChatStateWrapper(uuid, state)) | ||
| .thenApply(status -> null); | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
...ain/java/com/eternalcode/core/feature/privatechat/toggle/PrivateChatStateServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.eternalcode.core.feature.privatechat.toggle; | ||
|
|
||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Service; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentSkipListMap; | ||
|
|
||
| @Service | ||
| class PrivateChatStateServiceImpl implements PrivateChatStateService { | ||
|
|
||
| private final PrivateChatStateRepository msgToggleRepository; | ||
| private final ConcurrentHashMap<UUID, PrivateChatState> cachedToggleStates; | ||
|
|
||
| @Inject | ||
| PrivateChatStateServiceImpl(PrivateChatStateRepository msgToggleRepository) { | ||
| this.cachedToggleStates = new ConcurrentHashMap<>(); | ||
| this.msgToggleRepository = msgToggleRepository; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public CompletableFuture<PrivateChatState> getChatState(UUID playerUniqueId) { | ||
| if (this.cachedToggleStates.containsKey(playerUniqueId)) { | ||
| return CompletableFuture.completedFuture(this.cachedToggleStates.get(playerUniqueId)); | ||
| } | ||
|
|
||
| return this.msgToggleRepository.getPrivateChatState(playerUniqueId); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> setChatState(UUID playerUniqueId, PrivateChatState state) { | ||
| this.cachedToggleStates.put(playerUniqueId, state); | ||
|
|
||
| return this.msgToggleRepository.setPrivateChatState(playerUniqueId, state) | ||
| .exceptionally(throwable -> { | ||
| this.cachedToggleStates.remove(playerUniqueId); | ||
| return null; | ||
| }); | ||
| } | ||
CitralFlo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public CompletableFuture<PrivateChatState> toggleChatState(UUID playerUniqueId) { | ||
| return this.getChatState(playerUniqueId).thenCompose(state -> { | ||
| PrivateChatState newState = state.invert(); | ||
| return this.setChatState(playerUniqueId, newState) | ||
| .thenApply(aVoid -> newState); | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.