Skip to content
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

GH-870 Add /msgtoggle command #896

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b5e54ad
Innit commit
CitralFlo Jan 12, 2025
ffd29c5
In progress: add messages, setup methods and commands
CitralFlo Jan 14, 2025
88a71f7
Merge branch 'refs/heads/master' into msgtoggle
CitralFlo Jan 17, 2025
43ea1b0
Add msgToggle repo, service, API and commands
CitralFlo Jan 17, 2025
f054dbd
Fix logic
CitralFlo Jan 17, 2025
2437624
Fix logic, rename messages
CitralFlo Jan 22, 2025
aa1ff47
Remove comments
CitralFlo Jan 22, 2025
6da3ddf
Delete duplicated message.
CitralFlo Jan 22, 2025
4a77643
Update eternalcore-api/src/main/java/com/eternalcode/core/feature/msg…
CitralFlo Jan 22, 2025
00e97d0
Delete duplicated message.
CitralFlo Jan 22, 2025
5291625
Merge remote-tracking branch 'origin/msgtoggle' into msgtoggle
CitralFlo Jan 22, 2025
a091a18
Fix coderabbit issue
CitralFlo Jan 22, 2025
b3dae93
Fix naming and logic.
CitralFlo Jan 28, 2025
97d5914
Merge branch 'master' into msgtoggle
CitralFlo Jan 28, 2025
76d85d9
Resolve @imDMK review
CitralFlo Jan 28, 2025
df9b84f
Add comments to api
CitralFlo Jan 28, 2025
4709133
Resolve Rollcz's review
CitralFlo Jan 28, 2025
450813c
Resolve coderabbit review
CitralFlo Jan 28, 2025
f22fe08
Update README.md
CitralFlo Jan 30, 2025
98cb286
Fix logic: enable -> enable priv messages.
CitralFlo Feb 5, 2025
f9cd9b7
Resolve @coderabbitai nitpick comment, simplify command and api
CitralFlo Feb 5, 2025
39b6dfc
Refactor names of variables following vLuckyyy comment
CitralFlo Feb 5, 2025
1ed9c34
Change name from toggleState to State
CitralFlo Feb 5, 2025
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Get the latest development builds from our [GitHub Actions](https://github.com/E
- Chat On/Off Switch
- Chat Slow Mode
- /ignore and /unignore (with -all option)
- /msgtoggle
- /msg, /socialspy, and /reply commands
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
- /helpop command
- Advanced Notification System allowing you to customize every message to your liking (Title, Subtitle, Actionbar, Chat, etc.)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.eternalcode.core.feature.privatechat.toggle;

import java.util.UUID;

public class PrivateChatToggle {

UUID uuid;
PrivateChatToggleState state;
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved

public PrivateChatToggle() {

}

public PrivateChatToggle(UUID uuid, PrivateChatToggleState toggle) {
this.uuid = uuid;
this.state = toggle;
}

public UUID getUuid() {
return uuid;
}

public PrivateChatToggleState getState() {
return state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.eternalcode.core.feature.privatechat.toggle;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public interface PrivateChatToggleService {

CompletableFuture<PrivateChatToggleState> getPrivateChatToggleState(UUID uuid);

void togglePrivateChat(UUID uuid, PrivateChatToggleState toggle);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.eternalcode.core.feature.privatechat.toggle;

public enum PrivateChatToggleState {
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved

ON,
OFF

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.eternalcode.core.event.EventCaller;
import com.eternalcode.core.feature.ignore.IgnoreService;
import com.eternalcode.core.feature.privatechat.toggle.PrivateChatToggleService;
import com.eternalcode.core.feature.privatechat.toggle.PrivateChatToggleState;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import com.eternalcode.core.notice.NoticeService;
Expand All @@ -24,6 +26,7 @@ class PrivateChatServiceImpl implements PrivateChatService {
private final UserManager userManager;
private final PrivateChatPresenter presenter;
private final EventCaller eventCaller;
private final PrivateChatToggleService privateChatToggleService;

private final Cache<UUID, UUID> replies = CacheBuilder.newBuilder()
.expireAfterWrite(Duration.ofHours(1))
Expand All @@ -36,12 +39,14 @@ class PrivateChatServiceImpl implements PrivateChatService {
NoticeService noticeService,
IgnoreService ignoreService,
UserManager userManager,
EventCaller eventCaller
EventCaller eventCaller,
PrivateChatToggleService privateChatToggleService
) {
this.noticeService = noticeService;
this.ignoreService = ignoreService;
this.userManager = userManager;
this.eventCaller = eventCaller;
this.privateChatToggleService = privateChatToggleService;

this.presenter = new PrivateChatPresenter(noticeService);
}
Expand All @@ -53,15 +58,25 @@ void privateMessage(User sender, User target, String message) {
return;
}

this.ignoreService.isIgnored(target.getUniqueId(), sender.getUniqueId()).thenAccept(isIgnored -> {
if (!isIgnored) {
this.replies.put(target.getUniqueId(), sender.getUniqueId());
this.replies.put(sender.getUniqueId(), target.getUniqueId());
UUID uniqueId = target.getUniqueId();

this.privateChatToggleService.getPrivateChatToggleState(uniqueId).thenAccept(privateChatToggleState -> {
if (privateChatToggleState.equals(PrivateChatToggleState.ON)) {
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
this.noticeService.player(sender.getUniqueId(), translation -> translation.privateChat().receiverDisabledMessages());

return;
}

PrivateChatEvent event = new PrivateChatEvent(sender.getUniqueId(), target.getUniqueId(), message);
this.eventCaller.callEvent(event);
this.presenter.onPrivate(new PrivateMessage(sender, target, event.getContent(), this.socialSpy, isIgnored));
this.ignoreService.isIgnored(uniqueId, sender.getUniqueId()).thenAccept(isIgnored -> {
if (!isIgnored) {
this.replies.put(uniqueId, sender.getUniqueId());
this.replies.put(sender.getUniqueId(), uniqueId);
}

PrivateChatEvent event = new PrivateChatEvent(sender.getUniqueId(), uniqueId, message);
this.eventCaller.callEvent(event);
this.presenter.onPrivate(new PrivateMessage(sender, target, event.getContent(), this.socialSpy, isIgnored));
Copy link
Member

Choose a reason for hiding this comment

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

trzeba jeszcze przemyśleć trochę to api serwisu - czy jest sens robić 2 osobne serwisy (dla toggle) i czy da się te rzeczy zrobić ładniej bo teraz robi sam się trochę śmietnik w PrivateChatService

Copy link
Member Author

Choose a reason for hiding this comment

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

Tbh I don't understand. Please elaborate

});
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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.optional.OptionalArg;
import dev.rollczi.litecommands.annotations.permission.Permission;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

@Command(name = "msgtoggle")
@Permission("eternalcore.msgtoggle")
public class PrivateChatToggleCommand {

private final PrivateChatToggleService privateChatToggleService;
private final NoticeService noticeService;

@Inject
public PrivateChatToggleCommand(PrivateChatToggleService privateChatToggleService, NoticeService noticeService) {
this.privateChatToggleService = privateChatToggleService;
this.noticeService = noticeService;
}

@Execute
@DescriptionDocs(description = "Toggle receiving private messages on and off")
public void execute(@Context Player context, @OptionalArg PrivateChatToggleState state) {
UUID uniqueId = context.getUniqueId();

if (state == null) {
CompletableFuture<PrivateChatToggleState> privateChatToggleState = this.privateChatToggleService.getPrivateChatToggleState(context.getUniqueId());

privateChatToggleState.thenAccept(toggleState -> {
if (toggleState.equals(PrivateChatToggleState.OFF)) {
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
this.toggleOn(uniqueId);
}
else {
this.toggleOff(uniqueId);
}
});

return;
}
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved

if (state.equals(PrivateChatToggleState.OFF)) {
this.toggleOn(uniqueId);
}
else {
this.toggleOff(uniqueId);
}
}

@Execute
@Permission("eternalcore.msgtoggle.other")
@DescriptionDocs(description = "Toggle private messages for other player", arguments = "<player> <toggle>")
public void other(@Context CommandSender context, @Arg("player") Player player, @OptionalArg PrivateChatToggleState state) {

UUID uniqueId = player.getUniqueId();

if (state == null) {
CompletableFuture<PrivateChatToggleState> privateChatToggleState = this.privateChatToggleService.getPrivateChatToggleState(uniqueId);

privateChatToggleState.thenAccept(toggleState -> {
if (toggleState.equals(PrivateChatToggleState.OFF)) {
this.toggleOn(uniqueId);

this.noticeService.create()
.notice( translation -> translation.privateChat().otherMessagesEnabled())
.sender(context)
.placeholder("{PLAYER}", player.getName())
.send();
}
else {
this.toggleOff(uniqueId);

this.noticeService.create()
.notice( translation -> translation.privateChat().otherMessagesDisabled())
.sender(context)
.placeholder("{PLAYER}", player.getName())
.send();
}
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
});

return;
}
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved

if (state.equals(PrivateChatToggleState.OFF)) {
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
this.toggleOn(uniqueId);

if (this.isCommandSenderSameAsTarget(context, player)) {
this.noticeService.create()
.notice(translation -> translation.privateChat().otherMessagesDisabled())
.sender(context)
.placeholder("{PLAYER}", player.getName())
.send();
}
}
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
else {
this.toggleOff(uniqueId);

if (this.isCommandSenderSameAsTarget(context, player)) {
this.noticeService.create()
.notice(translation -> translation.privateChat().otherMessagesEnabled())
.sender(context)
.placeholder("{PLAYER}", player.getName())
.send();
}
}
}

private void toggleOn(UUID uniqueId) {
this.privateChatToggleService.togglePrivateChat(uniqueId, PrivateChatToggleState.ON);

this.noticeService.create()
.notice(translation -> translation.privateChat().selfMessagesDisabled())
.player(uniqueId)
.send();
}

private void toggleOff(UUID uniqueId) {
this.privateChatToggleService.togglePrivateChat(uniqueId, PrivateChatToggleState.OFF);

this.noticeService.create()
.notice(translation -> translation.privateChat().selfMessagesEnabled())
.player(uniqueId)
.send();
}

private boolean isCommandSenderSameAsTarget(CommandSender context, Player player) {
if (context instanceof Player commandSender) {
return commandSender.getUniqueId().equals(player.getUniqueId());
}
return false;
}
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.eternalcode.core.feature.privatechat.toggle;

import com.eternalcode.core.feature.privatechat.toggle.PrivateChatToggleState;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public interface PrivateChatToggleRepository {
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved

CompletableFuture<PrivateChatToggleState> getPrivateChatToggleState(UUID uuid);

CompletableFuture<Void> setPrivateChatToggle(UUID uuid, PrivateChatToggleState toggledOff);

}
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 PrivateChatToggleRepositoryOrmLite extends AbstractRepositoryOrmLite implements PrivateChatToggleRepository {

@Inject
private PrivateChatToggleRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) throws SQLException {
super(databaseManager, scheduler);
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), PrivateChatToggleWrapper.class);
}
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved

@Override
public CompletableFuture<PrivateChatToggleState> getPrivateChatToggleState(UUID uuid) {
return this.selectSafe(PrivateChatToggleWrapper.class, uuid)
.thenApply(
optional -> optional.map(PrivateChatToggleWrapper::isEnabled).orElse(PrivateChatToggleState.OFF)
);
}

@Override
public CompletableFuture<Void> setPrivateChatToggle(UUID uuid, PrivateChatToggleState toggledOff) {
return this.save(PrivateChatToggleWrapper.class, new PrivateChatToggleWrapper(uuid, toggledOff))
.thenApply(status -> null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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;

@Service
public class PrivateChatToggleServiceImpl implements PrivateChatToggleService {

private final PrivateChatToggleRepository msgToggleRepository;
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved

@Inject
public PrivateChatToggleServiceImpl(PrivateChatToggleRepository msgToggleRepository) {
this.msgToggleRepository = msgToggleRepository;
}


@Override
public CompletableFuture<PrivateChatToggleState> getPrivateChatToggleState(UUID uuid) {
return this.msgToggleRepository.getPrivateChatToggleState(uuid);
}

@Override
public void togglePrivateChat(UUID uuid, PrivateChatToggleState toggle) {
this.msgToggleRepository.setPrivateChatToggle(uuid, toggle);
}
CitralFlo marked this conversation as resolved.
Show resolved Hide resolved


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.eternalcode.core.feature.privatechat.toggle;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.util.UUID;

@DatabaseTable(tableName = "eternal_core_private_chat_toggle")
class PrivateChatToggleWrapper {

@DatabaseField(columnName = "id", id = true)
private UUID uniqueId;

@DatabaseField(columnName = "enabled")
private PrivateChatToggleState state;

PrivateChatToggleWrapper(UUID id, PrivateChatToggleState enabled) {
this.uniqueId = id;
this.state = enabled;
}

PrivateChatToggleWrapper() {}

static PrivateChatToggleWrapper from(PrivateChatToggle msgToggle) {
return new PrivateChatToggleWrapper(msgToggle.uuid, msgToggle.state);
}

PrivateChatToggleState isEnabled() {
return this.state;
}

void setState(PrivateChatToggleState state) {
this.state = state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ interface PrivateChatSection {
Notice socialSpyEnable();
Notice socialSpyDisable();

Notice receiverDisabledMessages();
Notice selfMessagesDisabled();
Notice selfMessagesEnabled();
Notice otherMessagesDisabled();
Notice otherMessagesEnabled();

Notice ignorePlayer();
Notice ignoreAll();
Notice unIgnorePlayer();
Expand Down
Loading