Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines +37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just inline

Suggested change
UUID playerId = targetPlayer.getUniqueId();
CompletableFuture<PrivateChatState> stateFuture = this.privateChatStateService.getChatState(playerId);
CompletableFuture<PrivateChatState> stateFuture = this.privateChatStateService.getChatState(targetPlayer.getUniqueId());


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UUID playerId = targetPlayer.getUniqueId();
Translation messages = this.translationManager.getMessages(playerId);
CompletableFuture<PrivateChatState> stateFuture = this.privateChatStateService.getChatState(playerId);
UUID playerId = targetPlayer.getUniqueId();
Translation messages = this.translationManager.getMessages(playerId);
CompletableFuture<PrivateChatState> stateFuture = this.privateChatStateService.getChatState(playerId);


return new AsyncPlaceholderValue<>(
stateFuture,
state -> state == PrivateChatState.ENABLE
? messages.privateChat().privateMessageEnabledPlaceholder()
: messages.privateChat().privateMessageDisabledPlaceholder()
).toString();
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ public class ENPrivateMessages implements PrivateChatMessages {
@Description({" ", "# {PLAYER} - Ignored player"})
public Notice notIgnorePlayer =
Notice.chat("<red>► <dark_red>You don't ignore this player, so you can unignore him!");

public String privateMessageEnabledPlaceholder = "<green>Enabled";
public String privateMessageDisabledPlaceholder = "<red>Disabled";
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ public class PLPrivateChatMessages implements PrivateChatMessages {
@Description({" ", "# {PLAYER} - Gracz który jest zignorowany"})
public Notice notIgnorePlayer = Notice.chat(
"<red>► <dark_red>Gracz <red>{PLAYER} <dark_red>nie jest przez Ciebie zignorowany. Nie możesz go od ignorować!");

public String privateMessageEnabledPlaceholder = "<green>Włączone";
public String privateMessageDisabledPlaceholder = "<red>Wyłączone";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public interface PrivateChatMessages {
Notice notIgnorePlayer();
Notice cantIgnoreYourself();
Notice cantUnIgnoreYourself();

String privateMessageEnabledPlaceholder();

String privateMessageDisabledPlaceholder();
}
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
public String toString() {
public String getValue() {

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();
}
}
}