Skip to content

Commit

Permalink
Add msgToggle repo, service, API and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
CitralFlo committed Jan 17, 2025
1 parent 88a71f7 commit 43ea1b0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.eternalcode.core.feature.msgtoggle;

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

public interface MsgToggleService {

boolean hasMsgToggledOff(UUID uuid);
CompletableFuture<Boolean> hasMsgToggledOff(UUID uuid);

void toggleMsg(UUID uuid, boolean toggle);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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")
Expand All @@ -15,23 +18,29 @@ public class MsgToggleCommand {
private final MsgToggleService msgToggleService;
private final NoticeService noticeService;

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

@Execute
@DescriptionDocs(description = "Toggle private messages")
public void execute(@Context Player context) {
boolean hasMsgToggledOff = this.msgToggleService.hasMsgToggledOff(context.getUniqueId());

if (hasMsgToggledOff) {
this.on(context);
} else {
this.off(context);
}
CompletableFuture<Boolean> hasMsgToggledOff = this.msgToggleService.hasMsgToggledOff(context.getUniqueId());

hasMsgToggledOff.thenAccept(toggledOff -> {
if (toggledOff) {
this.on(context);
} else {
this.off(context);
}
});
}

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

Expand All @@ -42,6 +51,7 @@ public void on(@Context Player context) {
}

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

Expand All @@ -53,21 +63,32 @@ public void off(@Context Player context) {

@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) {
boolean hasMsgToggledOff = this.msgToggleService.hasMsgToggledOff(player.getUniqueId());
CompletableFuture<Boolean> hasMsgToggledOff = this.msgToggleService.hasMsgToggledOff(player.getUniqueId());

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

@Execute
@Permission("eternalcore.msgtoggle.other")
public void other(@Context Player context, @Arg("player") Player player, @Arg boolean toggle) {
this.msgToggleService.toggleMsg(player.getUniqueId(), toggle);
@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 -> translation.privateChat().msgToggleOther(toggle))
.notice(
translation -> toggle == STATE.ON ?
translation.privateChat().msgTogglePlayerOn() :
translation.privateChat().msgTogglePlayerOff()
)
.player(player.getUniqueId())
.send();

}

public enum STATE {
ON,
OFF
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.eternalcode.core.feature.msgtoggle;

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

interface MsgToggleRepository {
public interface MsgToggleRepository {

boolean isToggledOff(UUID uuid);
CompletableFuture<Boolean> isToggledOff(UUID uuid);

void setToggledOff(UUID uuid, boolean toggledOff);

void remove(UUID uuid);

void removeAll();

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 {
Expand All @@ -19,25 +20,15 @@ private MsgToggleRepositoryOrmLite(DatabaseManager databaseManager, Scheduler sc
}

@Override
public boolean isToggledOff(UUID uuid) {
// this.selectSafe(MsgToggle.class, uuid).thenApply(msgToggle -> {
// msgToggle.isEmpty();
// });
return false;
public CompletableFuture<Boolean> isToggledOff(UUID uuid) {
return this.selectSafe(MsgToggleWrapper.class, uuid)
.thenApply(
optional -> optional.map(MsgToggleWrapper::isEnabled).orElse(true)
);
}

@Override
public void setToggledOff(UUID uuid, boolean toggledOff) {
this.save(MsgToggle.class, new MsgToggle(uuid, false));
}

@Override
public void remove(UUID uuid) {

}

@Override
public void removeAll() {

this.save(MsgToggle.class, new MsgToggle(uuid, toggledOff));
}
}
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> hasMsgToggledOff(UUID uuid) {
return this.msgToggleRepository.isToggledOff(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
Expand Up @@ -25,4 +25,12 @@ static MsgToggleWrapper from(MsgToggleWrapper msgToggle) {
return new MsgToggleWrapper(msgToggle.id, msgToggle.enabled);
}

boolean isEnabled() {
return this.enabled;
}

void setEnabled(boolean enabled) {
this.enabled = enabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,23 @@ void privateMessage(User sender, User target, String message) {

UUID uniqueId = target.getUniqueId();

if (this.msgToggleService.hasMsgToggledOff(uniqueId)) {
this.noticeService.player(sender.getUniqueId(), translation -> translation.privateChat().msgToggledOff());
this.msgToggleService.hasMsgToggledOff(uniqueId).thenAccept(hasToggledOff -> {
if (hasToggledOff) {
this.noticeService.player(sender.getUniqueId(), translation -> translation.privateChat().msgToggledOff());

return;
}

this.ignoreService.isIgnored(uniqueId, sender.getUniqueId()).thenAccept(isIgnored -> {
if (!isIgnored) {
this.replies.put(uniqueId, sender.getUniqueId());
this.replies.put(sender.getUniqueId(), uniqueId);
return;
}

PrivateChatEvent event = new PrivateChatEvent(sender.getUniqueId(), uniqueId, 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

0 comments on commit 43ea1b0

Please sign in to comment.