-
-
Notifications
You must be signed in to change notification settings - Fork 19
GH-930 Add private chat toggle placeholder. #931
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||||||||||||
package com.eternalcode.core.feature.privatechat; | ||||||||||||||||
|
||||||||||||||||
import com.eternalcode.core.feature.privatechat.toggle.PrivateChatState; | ||||||||||||||||
import com.eternalcode.core.feature.privatechat.toggle.PrivateChatStateService; | ||||||||||||||||
import com.eternalcode.core.injector.annotations.Inject; | ||||||||||||||||
import com.eternalcode.core.injector.annotations.component.Controller; | ||||||||||||||||
import com.eternalcode.core.placeholder.AsyncPlaceholderValue; | ||||||||||||||||
import com.eternalcode.core.placeholder.PlaceholderRegistry; | ||||||||||||||||
import com.eternalcode.core.placeholder.PlaceholderReplacer; | ||||||||||||||||
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.concurrent.CompletableFuture; | ||||||||||||||||
import java.util.UUID; | ||||||||||||||||
|
||||||||||||||||
@Controller | ||||||||||||||||
public class PrivateChatPlaceholderSetup { | ||||||||||||||||
|
||||||||||||||||
private final PrivateChatStateService privateChatStateService; | ||||||||||||||||
private final TranslationManager translationManager; | ||||||||||||||||
|
||||||||||||||||
@Inject | ||||||||||||||||
public PrivateChatPlaceholderSetup( | ||||||||||||||||
PrivateChatStateService privateChatStateService, | ||||||||||||||||
TranslationManager translationManager | ||||||||||||||||
) { | ||||||||||||||||
this.privateChatStateService = privateChatStateService; | ||||||||||||||||
this.translationManager = translationManager; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Subscribe(EternalInitializeEvent.class) | ||||||||||||||||
void setUp(PlaceholderRegistry placeholderRegistry) { | ||||||||||||||||
placeholderRegistry.registerPlaceholder( | ||||||||||||||||
PlaceholderReplacer.of("eternalcore_msgtoggle", (text, targetPlayer) -> { | ||||||||||||||||
UUID playerId = targetPlayer.getUniqueId(); | ||||||||||||||||
CompletableFuture<PrivateChatState> stateFuture = this.privateChatStateService.getChatState(playerId); | ||||||||||||||||
|
||||||||||||||||
return new AsyncPlaceholderValue<>( | ||||||||||||||||
stateFuture, | ||||||||||||||||
state -> state == PrivateChatState.ENABLE ? "true" : "false" | ||||||||||||||||
).toString(); | ||||||||||||||||
}) | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
placeholderRegistry.registerPlaceholder( | ||||||||||||||||
PlaceholderReplacer.of("eternalcore_msgtoggle_formatted", (text, targetPlayer) -> { | ||||||||||||||||
UUID playerId = targetPlayer.getUniqueId(); | ||||||||||||||||
Translation messages = this.translationManager.getMessages(playerId); | ||||||||||||||||
CompletableFuture<PrivateChatState> stateFuture = this.privateChatStateService.getChatState(playerId); | ||||||||||||||||
Comment on lines
+49
to
+51
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.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
return new AsyncPlaceholderValue<>( | ||||||||||||||||
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. Wydaje mi się, że to nie będzie działać poprawnie tzn. |
||||||||||||||||
stateFuture, | ||||||||||||||||
state -> state == PrivateChatState.ENABLE | ||||||||||||||||
? messages.privateChat().privateMessageEnabledPlaceholder() | ||||||||||||||||
: messages.privateChat().privateMessageDisabledPlaceholder() | ||||||||||||||||
).toString(); | ||||||||||||||||
}) | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||
package com.eternalcode.core.placeholder; | ||||||
|
||||||
import java.util.concurrent.CompletableFuture; | ||||||
import java.util.function.Function; | ||||||
|
||||||
public class AsyncPlaceholderValue<T> { | ||||||
private final CompletableFuture<T> future; | ||||||
private final Function<T, String> formatter; | ||||||
|
||||||
public AsyncPlaceholderValue(CompletableFuture<T> future, Function<T, String> formatter) { | ||||||
this.future = future; | ||||||
this.formatter = formatter; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public String toString() { | ||||||
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 don't think that this name is good. I would suggest change to maybe "getValue" or smth like this.
Suggested change
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. rel |
||||||
if (!future.isDone()) { | ||||||
return "Loading..."; | ||||||
} | ||||||
|
||||||
try { | ||||||
T value = future.getNow(null); | ||||||
return value != null ? formatter.apply(value) : "Unknown"; | ||||||
} | ||||||
catch (Exception e) { | ||||||
return "Error: " + e.getMessage(); | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just inline