-
-
Notifications
You must be signed in to change notification settings - Fork 21
GH-930 Msg placeholders & Async placeholder for future #1203
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
Open
P1otrulla
wants to merge
16
commits into
master
Choose a base branch
from
2.0/msg-placeholders
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+327
−73
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b3764bd
GH-1142 Add message placeholders for `msg_toggle` and `socialspy_status`
P1otrulla af593f1
GH-1143 Enhance `MsgPlaceholderSetup` with caching and new placeholders
P1otrulla 1f98392
GH-1144 Update `MsgPlaceholderSetup` caching and integrate `Placehold…
P1otrulla cf7151e
Refactor `MsgPlaceholderSetup` with new caching system
P1otrulla 1407a28
Refactor `MsgPlaceholderSetup` to enhance caching with `AsyncPlacehol…
P1otrulla a8c6659
Enhance `AsyncPlaceholderCacheController` with event-based cache inva…
P1otrulla 2633ef6
Refactor `AsyncPlaceholderCacheController` event methods
P1otrulla dc6f963
the bomb was defused xd
P1otrulla aa360dd
Follow gemini comment
P1otrulla a128154
Follow gemini comment
P1otrulla 6e7e842
CR
Rollczi 81b610e
CR
Rollczi 450a2d7
CR
Rollczi 9f0d5eb
CR
Rollczi 3e5a511
Merge branch 'master' into 2.0/msg-placeholders
Rollczi 082e063
CR
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
3 changes: 1 addition & 2 deletions
3
...laceholderapi/PlaceholderApiReplacer.java → ...eholderapi/PlaceholderAPIPlaceholder.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
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
73 changes: 73 additions & 0 deletions
73
eternalcore-core/src/main/java/com/eternalcode/core/feature/msg/MsgPlaceholderSetup.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,73 @@ | ||
| package com.eternalcode.core.feature.msg; | ||
|
|
||
| import com.eternalcode.core.feature.msg.toggle.MsgState; | ||
| import com.eternalcode.core.feature.msg.toggle.MsgToggleRepository; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Controller; | ||
| import com.eternalcode.core.placeholder.PlaceholderRegistry; | ||
| import com.eternalcode.core.placeholder.Placeholder; | ||
| import com.eternalcode.core.placeholder.watcher.PlaceholderWatcherKey; | ||
| import com.eternalcode.core.publish.Subscribe; | ||
| import com.eternalcode.core.publish.event.EternalInitializeEvent; | ||
| import com.eternalcode.core.translation.Translation; | ||
| import com.eternalcode.core.translation.TranslationManager; | ||
| import java.util.UUID; | ||
|
|
||
| @Controller | ||
| public class MsgPlaceholderSetup { | ||
|
|
||
| public static final PlaceholderWatcherKey<MsgState> MSG_STATE = PlaceholderWatcherKey.of("msg_state", MsgState.class); | ||
|
|
||
| private final MsgService msgService; | ||
| private final MsgToggleRepository msgToggleRepository; | ||
| private final TranslationManager translationManager; | ||
|
|
||
| @Inject | ||
| MsgPlaceholderSetup( | ||
| MsgService msgService, | ||
| MsgToggleRepository msgToggleRepository, | ||
| TranslationManager translationManager | ||
| ) { | ||
| this.msgService = msgService; | ||
| this.msgToggleRepository = msgToggleRepository; | ||
| this.translationManager = translationManager; | ||
| } | ||
|
|
||
| @Subscribe(EternalInitializeEvent.class) | ||
| void setUpPlaceholders(PlaceholderRegistry registry) { | ||
| Translation translation = this.translationManager.getMessages(); | ||
|
|
||
| registry.registerPlaceholder(Placeholder.of( | ||
| "socialspy_status", | ||
| player -> String.valueOf(this.msgService.isSpy(player.getUniqueId())) | ||
Rollczi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| )); | ||
|
|
||
| registry.registerPlaceholder(Placeholder.of( | ||
| "socialspy_status_formatted", | ||
| player -> { | ||
| UUID uuid = player.getUniqueId(); | ||
| return this.msgService.isSpy(uuid) | ||
| ? translation.msg().placeholders().socialSpyEnabled() | ||
| : translation.msg().placeholders().socialSpyDisabled(); | ||
| } | ||
| )); | ||
|
|
||
| registry.registerPlaceholder(Placeholder.async( | ||
| "msg_status", | ||
| MSG_STATE, | ||
| player -> msgToggleRepository.getPrivateChatState(player), | ||
| state -> state.name().toLowerCase() | ||
| )); | ||
|
|
||
| registry.registerPlaceholder(Placeholder.async( | ||
| "msg_status_formatted", | ||
| MSG_STATE, | ||
| player -> msgToggleRepository.getPrivateChatState(player), | ||
| state -> state == MsgState.ENABLED | ||
| ? translation.msg().placeholders().msgEnabled() | ||
| : translation.msg().placeholders().msgDisabled() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| } | ||
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
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
34 changes: 13 additions & 21 deletions
34
...core-core/src/main/java/com/eternalcode/core/feature/msg/toggle/MsgToggleServiceImpl.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 |
|---|---|---|
| @@ -1,51 +1,43 @@ | ||
| package com.eternalcode.core.feature.msg.toggle; | ||
|
|
||
| import com.eternalcode.core.feature.msg.MsgPlaceholderSetup; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Service; | ||
| import com.eternalcode.core.placeholder.PlaceholderRegistry; | ||
| import com.eternalcode.core.placeholder.watcher.PlaceholderWatcher; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| @Service | ||
| class MsgToggleServiceImpl implements MsgToggleService { | ||
Rollczi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final MsgToggleRepository msgToggleRepository; | ||
| private final ConcurrentHashMap<UUID, MsgState> cachedToggleStates; | ||
| private final PlaceholderWatcher<MsgState> watcher; | ||
|
|
||
| @Inject | ||
| MsgToggleServiceImpl(MsgToggleRepository msgToggleRepository) { | ||
| this.cachedToggleStates = new ConcurrentHashMap<>(); | ||
| MsgToggleServiceImpl(MsgToggleRepository msgToggleRepository, PlaceholderRegistry registry) { | ||
| this.msgToggleRepository = msgToggleRepository; | ||
|
|
||
| this.watcher = registry.createWatcher(MsgPlaceholderSetup.MSG_STATE); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public CompletableFuture<MsgState> getState(UUID playerUniqueId) { | ||
| if (this.cachedToggleStates.containsKey(playerUniqueId)) { | ||
| return CompletableFuture.completedFuture(this.cachedToggleStates.get(playerUniqueId)); | ||
| } | ||
|
|
||
| return this.msgToggleRepository.getPrivateChatState(playerUniqueId); | ||
| public CompletableFuture<MsgState> getState(UUID player) { | ||
| return this.watcher.track(player, this.msgToggleRepository.getPrivateChatState(player)); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> setState(UUID playerUniqueId, MsgState state) { | ||
| this.cachedToggleStates.put(playerUniqueId, state); | ||
|
|
||
| return this.msgToggleRepository.setPrivateChatState(playerUniqueId, state) | ||
| .exceptionally(throwable -> { | ||
| this.cachedToggleStates.remove(playerUniqueId); | ||
| return null; | ||
| }); | ||
| public CompletableFuture<Void> setState(UUID player, MsgState state) { | ||
| return this.watcher.track(player, this.msgToggleRepository.setPrivateChatState(player, state)) | ||
| .thenApply(unused -> null); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<MsgState> toggleState(UUID playerUniqueId) { | ||
| return this.getState(playerUniqueId).thenCompose(state -> { | ||
| MsgState newState = state.invert(); | ||
| return this.setState(playerUniqueId, newState) | ||
| .thenApply(aVoid -> newState); | ||
| .thenApply(unused -> newState); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
16 changes: 16 additions & 0 deletions
16
eternalcore-core/src/main/java/com/eternalcode/core/placeholder/NamedPlaceholder.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,16 @@ | ||
| package com.eternalcode.core.placeholder; | ||
|
|
||
| import org.bukkit.entity.Player; | ||
|
|
||
| public interface NamedPlaceholder extends Placeholder { | ||
|
|
||
| @Override | ||
| default String apply(String text, Player targetPlayer) { | ||
| return text.replace("{" + this.getName() + "}", this.provideValue(targetPlayer)); | ||
| } | ||
|
|
||
| String getName(); | ||
|
|
||
| String provideValue(Player targetPlayer); | ||
|
|
||
| } |
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 |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| package com.eternalcode.core.placeholder; | ||
|
|
||
| import com.eternalcode.core.placeholder.watcher.PlaceholderWatcherKey; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
@@ -9,27 +12,36 @@ public interface Placeholder { | |
| String apply(String text, Player targetPlayer); | ||
|
|
||
| static Placeholder of(String target, String replacement) { | ||
| return new PlaceholderRaw(target, player -> replacement); | ||
| return new StaticValuePlaceholder(target, player -> replacement); | ||
| } | ||
|
|
||
| static Placeholder of(String target, Function<Player, String> replacement) { | ||
| return new PlaceholderRaw(target, replacement); | ||
| return new StaticValuePlaceholder(target, replacement); | ||
| } | ||
|
|
||
| static Placeholder ofInt(String target, Function<Player, Integer> replacement) { | ||
| return new PlaceholderRaw(target, player -> String.valueOf(replacement.apply(player))); | ||
| return new StaticValuePlaceholder(target, player -> String.valueOf(replacement.apply(player))); | ||
| } | ||
|
|
||
| static Placeholder ofBoolean(String target, Function<Player, Boolean> replacement) { | ||
| return new PlaceholderRaw(target, player -> String.valueOf(replacement.apply(player))); | ||
| return new StaticValuePlaceholder(target, player -> String.valueOf(replacement.apply(player))); | ||
| } | ||
|
|
||
| static Placeholder ofLong(String target, Function<Player, Long> replacement) { | ||
| return new PlaceholderRaw(target, player -> String.valueOf(replacement.apply(player))); | ||
| return new StaticValuePlaceholder(target, player -> String.valueOf(replacement.apply(player))); | ||
| } | ||
|
|
||
| static Placeholder of(String target, Placeholder placeholder) { | ||
| return new PlaceholderRaw(target, player -> placeholder.apply(target, player)); | ||
| return new StaticValuePlaceholder(target, player -> placeholder.apply(target, player)); | ||
| } | ||
|
|
||
| static <T> Placeholder async( | ||
| String target, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rename target to sth like "placeholderName" |
||
| PlaceholderWatcherKey<T> key, | ||
| Function<UUID, CompletableFuture<T>> loading, | ||
| Function<T, String> mapper | ||
| ) { | ||
| return new PlaceholderAsync<>(target, key, loading, mapper); | ||
| } | ||
|
|
||
| } | ||
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.