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

Merged
merged 30 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 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
d72ae5c
Simplify logic (i hope @Rollczi will like it) refactor to PrivateChat…
CitralFlo Feb 6, 2025
6a12777
Merge branch 'master' into msgtoggle
CitralFlo Feb 12, 2025
dabdf7d
Resolve master changes
CitralFlo Feb 12, 2025
297cff4
Resolve @coderabbitai 's review
CitralFlo Feb 12, 2025
1b57a89
Cleanup method and parameters names. Remove unused classes.
Rollczi Feb 13, 2025
6227fa3
Simplify commands
Rollczi Feb 13, 2025
5a41247
Fix command messages
Rollczi Feb 13, 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
- /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.msgtoggle;

import java.util.UUID;

public class MsgToggle {

UUID uuid;
boolean toggle;

public MsgToggle() {

}

public MsgToggle(UUID uuid, boolean toggle) {
this.uuid = uuid;
this.toggle = toggle;
}

public UUID getUuid() {
return uuid;
}

public boolean getToggle() {
return toggle;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.eternalcode.core.feature.msgtoggle;

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

public interface MsgToggleService {

CompletableFuture<Boolean> hasMsgToggled(UUID uuid);

void toggleMsg(UUID uuid, boolean toggle);

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

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.concurrent.CompletableFuture;
import org.bukkit.entity.Player;

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

private final MsgToggleService msgToggleService;
private final NoticeService noticeService;

@Inject
public MsgToggleCommand(MsgToggleService msgToggleService, NoticeService noticeService) {
this.msgToggleService = msgToggleService;
this.noticeService = noticeService;
}

public enum STATE {
ON,
OFF
}

@Execute
@DescriptionDocs(description = "Toggle receiving private messages on and off")
public void execute(@Context Player context) {
CompletableFuture<Boolean> hasMsgToggledOff = this.msgToggleService.hasMsgToggled(context.getUniqueId());

hasMsgToggledOff.thenAccept(toggledOff -> {
if (toggledOff) {
this.toggleOff(context);
} else {
this.toggleOn(context);
}
});
}

@Execute(name = "on")
@DescriptionDocs(description = "Block incoming private messages")
public void toggleOn(@Context Player context) {
this.msgToggleService.toggleMsg(context.getUniqueId(), true);

this.noticeService.create()
.notice(translation -> translation.privateChat().msgToggledSelf())
.player(context.getUniqueId())
.send();
}

@Execute(name = "off")
@DescriptionDocs(description = "Unblock incoming private messages")
public void toggleOff(@Context Player context) {
this.msgToggleService.toggleMsg(context.getUniqueId(), false);

this.noticeService.create()
.notice(translation -> translation.privateChat().msgUntoggleSelf())
.player(context.getUniqueId())
.send();
}

@Execute
@Permission("eternalcore.msgtoggle.other")
@DescriptionDocs(description = "Toggle private messages for other player", arguments = "<player>")
public void other(@Context Player context, @Arg("player") Player player) {
CompletableFuture<Boolean> hasMsgToggledOff = this.msgToggleService.hasMsgToggled(player.getUniqueId());

hasMsgToggledOff.thenAccept(toggledOff -> this.other(context, player, !toggledOff ? STATE.ON : STATE.OFF));
}

@Execute
@Permission("eternalcore.msgtoggle.other")
@DescriptionDocs(description = "Toggle private messages for other player", arguments = "<player> <toggle>")
public void other(@Context Player context, @Arg("player") Player player, @Arg("<on/off>") STATE toggle) {
this.msgToggleService.toggleMsg(player.getUniqueId(), toggle == STATE.ON);

this.noticeService.create()
.notice(
translation -> toggle == STATE.ON ?
translation.privateChat().msgUntogglePlayer() :
translation.privateChat().msgToggledPlayer()
)
.player(context.getUniqueId())
.placeholder("{PLAYER}", player.getName())
.send();

}

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

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

public interface MsgToggleRepository {

CompletableFuture<Boolean> isToggled(UUID uuid);

CompletableFuture<Void> setToggledOff(UUID uuid, boolean toggledOff);

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

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 MsgToggleRepositoryOrmLite extends AbstractRepositoryOrmLite implements MsgToggleRepository {

@Inject
private MsgToggleRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) throws SQLException {
super(databaseManager, scheduler);
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), MsgToggleWrapper.class);
}

@Override
public CompletableFuture<Boolean> isToggled(UUID uuid) {
return this.selectSafe(MsgToggleWrapper.class, uuid)
.thenApply(
optional -> optional.map(MsgToggleWrapper::isEnabled).orElse(true)
);
}

@Override
public CompletableFuture<Void> setToggledOff(UUID uuid, boolean toggledOff) {
return this.save(MsgToggleWrapper.class, new MsgToggleWrapper(uuid, toggledOff))
.thenApply(status -> null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.eternalcode.core.feature.msgtoggle;

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 org.jetbrains.annotations.Blocking;

@Service
public class MsgToggleServiceImpl implements MsgToggleService{

private final MsgToggleRepository msgToggleRepository;

@Inject
public MsgToggleServiceImpl(MsgToggleRepository msgToggleRepository) {
this.msgToggleRepository = msgToggleRepository;
}


@Override
public CompletableFuture<Boolean> hasMsgToggled(UUID uuid) {
return this.msgToggleRepository.isToggled(uuid);
}

@Override
@Blocking
public void toggleMsg(UUID uuid, boolean toggle) {
this.msgToggleRepository.setToggledOff(uuid, toggle);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.eternalcode.core.feature.msgtoggle;

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

@DatabaseTable(tableName = "eternal_core_msg_toggles")
class MsgToggleWrapper {

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

@DatabaseField(columnName = "enabled")
private boolean enabled;

MsgToggleWrapper(UUID id, boolean enabled) {
this.uniqueId = id;
this.enabled = enabled;
}

MsgToggleWrapper() {}

static MsgToggleWrapper from(MsgToggle msgToggle) {
return new MsgToggleWrapper(msgToggle.uuid, msgToggle.toggle);
}

boolean isEnabled() {
return this.enabled;
}

void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eternalcode.core.event.EventCaller;
import com.eternalcode.core.feature.ignore.IgnoreService;
import com.eternalcode.core.feature.msgtoggle.MsgToggleService;
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 +25,7 @@ class PrivateChatServiceImpl implements PrivateChatService {
private final UserManager userManager;
private final PrivateChatPresenter presenter;
private final EventCaller eventCaller;
private final MsgToggleService msgToggleService;

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

this.presenter = new PrivateChatPresenter(noticeService);
}
Expand All @@ -53,15 +57,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.msgToggleService.hasMsgToggled(uniqueId).thenAccept(hasMsgToggled -> {
if (hasMsgToggled) {
this.noticeService.player(sender.getUniqueId(), translation -> translation.privateChat().msgToggledOff());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ interface PrivateChatSection {
Notice socialSpyEnable();
Notice socialSpyDisable();

Notice msgToggledOff();
Notice msgToggledSelf();
Notice msgUntoggleSelf();
Notice msgToggledPlayer();
Notice msgUntogglePlayer();

Notice ignorePlayer();
Notice ignoreAll();
Notice unIgnorePlayer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,18 @@ public static class ENPrivateSection implements PrivateChatSection {
public Notice socialSpyEnable = Notice.chat("<green>► <white>SocialSpy has been {STATE}<white>!");
public Notice socialSpyDisable = Notice.chat("<red>► <white>SocialSpy has been {STATE}<white>!");


public Notice msgToggledOff = Notice.chat("<red>► <dark_red>This player has disabled private messages!");

public Notice msgToggledSelf = Notice.chat("<green>► <white>Private messages have been <red>disabled<white>!");
public Notice msgUntoggleSelf = Notice.chat("<green>► <white>Private messages have been <green>enabled<white>!");

@Description("# {PLAYER} - Player")
public Notice msgToggledPlayer = Notice.chat("<green>► <white>Private messages have been disabled for <green>{PLAYER}<white>!");
public Notice msgUntogglePlayer = Notice.chat("<green>► <white>Private messages have been enabled for <green>{PLAYER}<white>!");



@Description({" ", "# {PLAYER} - Ignored player"})
public Notice ignorePlayer = Notice.chat("<green>► {PLAYER} <white>player has been ignored!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ public static class PLPrivateChatSection implements PrivateChatSection {
public Notice socialSpyEnable = Notice.chat("<green>► <white>SocialSpy został {STATE}<white>!");
public Notice socialSpyDisable = Notice.chat("<red>► <white>SocialSpy został {STATE}<white>!");

public Notice msgToggledOff = Notice.chat("<red>► <dark_red>Wiadomości prywatne zostały wyłączone!");

public Notice msgToggledSelf = Notice.chat("<green>► <white>Wiadomości prywatne zostały <red>wyłączone!");
public Notice msgUntoggleSelf = Notice.chat("<green>► <white>Wiadomości prywatne zostały <green>włączone!");

@Description({" ", "# {PLAYER} - Gracz któremu wyłączono wiadomości prywatne"})
public Notice msgToggledPlayer = Notice.chat("<green>► <white>Wiadomości prywatne zostały <red>wyłączone <white>dla gracza <green>{PLAYER}<white>!");
public Notice msgUntogglePlayer = Notice.chat("<green>► <white>Wiadomości prywatne zostały <green>włączone <white>dla gracza <green>{PLAYER}<white>!");

@Description({" ", "# {PLAYER} - Gracz który jest zignorowany"})
public Notice ignorePlayer = Notice.chat("<green>► <white>Zignorowano gracza <red>{PLAYER}<white>!");

Expand Down
Loading