Skip to content
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
4 changes: 3 additions & 1 deletion core/src/main/java/net/flectone/pulse/config/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ public record Translateto(
Boolean enable,
Range range,
Service service,
Boolean useMyMemory,
List<String> aliases,
List<String> languages,
Destination destination,
Expand All @@ -775,7 +776,8 @@ public record Translateto(
public enum Service {
DEEPL,
GOOGLE,
YANDEX
YANDEX,
MYMEMORY
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.flectone.pulse.module.command.symbol.SymbolModule;
import net.flectone.pulse.module.command.tell.TellModule;
import net.flectone.pulse.module.command.tictactoe.TictactoeModule;
import net.flectone.pulse.module.command.toggleoriginal.ToggleOriginalModule;
import net.flectone.pulse.module.command.toponline.ToponlineModule;
import net.flectone.pulse.module.command.translateto.TranslatetoModule;
import net.flectone.pulse.module.command.try_.TryModule;
Expand Down Expand Up @@ -105,6 +106,7 @@ public class CommandModule implements ModuleSimple {
SymbolModule.class,
TellModule.class,
TictactoeModule.class,
ToggleOriginalModule.class,
ToponlineModule.class,
TranslatetoModule.class,
TryModule.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package net.flectone.pulse.module.command.toggleoriginal;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import lombok.RequiredArgsConstructor;
import net.flectone.pulse.config.Command;
import net.flectone.pulse.config.Localization;
import net.flectone.pulse.config.Permission;
import net.flectone.pulse.model.entity.FEntity;
import net.flectone.pulse.model.entity.FPlayer;
import net.flectone.pulse.module.ModuleCommand;
import net.flectone.pulse.module.message.format.moderation.delete.DeleteModule;
import net.flectone.pulse.platform.controller.ModuleCommandController;
import net.flectone.pulse.platform.controller.ModuleController;
import net.flectone.pulse.platform.provider.CommandParserProvider;
import net.flectone.pulse.util.constant.ModuleName;
import net.flectone.pulse.util.file.FileFacade;
import org.incendo.cloud.context.CommandContext;

import java.util.UUID;

@Singleton
@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class ToggleOriginalModule implements ModuleCommand<Localization.Command.Translateto> {

private final FileFacade fileFacade;
private final CommandParserProvider commandParserProvider;
private final DeleteModule deleteModule;
private final ModuleController moduleController;
private final ModuleCommandController commandModuleController;

@Override
public void onEnable() {
String promptMessage = commandModuleController.addPrompt(this, 0, Localization.Command.Prompt::message);
commandModuleController.registerCommand(this, manager -> manager
.literal("toggleoriginal")
.required(promptMessage, commandParserProvider.singleMessageParser())
.permission(permission().name())
);
}

@Override
public void onDisable() {
commandModuleController.clearPrompts(this);
}

@Override
public void execute(FPlayer fPlayer, CommandContext<FPlayer> commandContext) {
if (moduleController.isDisabledFor(this, fPlayer, true)) return;

String messageUuidString = commandModuleController.getArgument(this, commandContext, 0);

try {
UUID messageUuid = UUID.fromString(messageUuidString);
deleteModule.toggleOriginal(fPlayer, messageUuid);
} catch (IllegalArgumentException _) {
// Invalid UUID, ignore
}
}

@Override
public ModuleName name() {
return ModuleName.COMMAND_TRANSLATETO;
}

@Override
public Command.Translateto config() {
return fileFacade.command().translateto();
}

@Override
public Permission.Command.Translateto permission() {
return fileFacade.permission().command().translateto();
}

@Override
public Localization.Command.Translateto localization(FEntity sender) {
return fileFacade.localization(sender).command().translateto();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class TranslatetoModule implements ModuleCommand<Localization.Command.Tra
private final MessageDispatcher messageDispatcher;
private final ModuleController moduleController;
private final ModuleCommandController commandModuleController;
private final net.flectone.pulse.service.TranslationCacheService translationCacheService;

@Override
public void onEnable() {
Expand Down Expand Up @@ -145,11 +146,34 @@ public Function<Localization.Command.Translateto, String> replaceLanguage(String
}

public String translate(FPlayer fPlayer, String source, String target, String text) {
return switch (config().service()) {
// Check cache first if MyMemory is enabled
if (config().useMyMemory() != null && config().useMyMemory()) {
String cached = translationCacheService.get(source, target, text);
if (cached != null) {
return cached;
}

// Try MyMemory API
String myMemoryTranslation = translationCacheService.translateWithMyMemory(source, target, text);
if (myMemoryTranslation != null && !myMemoryTranslation.isEmpty()) {
return myMemoryTranslation;
}
}

// Fallback to configured service
String translation = switch (config().service()) {
case DEEPL -> integrationModule.deeplTranslate(fPlayer, source, target, text);
case GOOGLE -> googleTranslate(source, target, text);
case YANDEX -> integrationModule.yandexTranslate(fPlayer, source, target, text);
case MYMEMORY -> translationCacheService.translateWithMyMemory(source, target, text);
};

// Cache the result if MyMemory is enabled
if (config().useMyMemory() != null && config().useMyMemory() && translation != null && !translation.isEmpty()) {
translationCacheService.put(source, target, text, translation);
}

return translation != null ? translation : "";
}

public String googleTranslate(String source, String lang, String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ public MessageContext addTag(MessageContext messageContext) {
}

public void save(FPlayer receiver, UUID messageUUID, Component component, boolean needToCache) {
save(receiver, messageUUID, component, null, needToCache);
}

public void save(FPlayer receiver, UUID messageUUID, Component component, net.flectone.pulse.module.message.format.translate.model.TranslatedMessage translatedMessage, boolean needToCache) {
// skip console
if (receiver.isUnknown()) return;
// skip offline history
if (!receiver.isOnline()) return;

UUID playerUUID = receiver.uuid();
HistoryMessage historyMessage = new HistoryMessage(messageUUID, component);
HistoryMessage historyMessage = new HistoryMessage(messageUUID, component, translatedMessage);

List<HistoryMessage> history = playersHistory.computeIfAbsent(playerUUID, _ -> new ObjectArrayList<>());

Expand Down Expand Up @@ -184,6 +188,7 @@ public void sendUpdate(UUID receiver) {
if (history == null) return;

FPlayer fPlayer = fPlayerService.getFPlayer(receiver);
String playerLocale = fPlayer.getSetting(net.flectone.pulse.util.constant.SettingText.LOCALE);

// empty messages
for (int i = 0; i < config().historyLength(); i++) {
Expand All @@ -193,7 +198,34 @@ public void sendUpdate(UUID receiver) {
}

history.forEach(historyMessage ->
messageSender.sendMessage(fPlayer, historyMessage.component(), true)
messageSender.sendMessage(fPlayer, historyMessage.getDisplayComponent(playerLocale), true)
);
}

public boolean toggleOriginal(FPlayer fPlayer, UUID messageUUID) {
if (moduleController.isDisabledFor(this, fPlayer)) return false;
if (messageUUID == null) return false;

UUID playerUUID = fPlayer.uuid();
List<HistoryMessage> history = playersHistory.get(playerUUID);
if (history == null) return false;

boolean updated = false;
for (int i = 0; i < history.size(); i++) {
HistoryMessage historyMessage = history.get(i);
if (messageUUID.equals(historyMessage.uuid()) && historyMessage.hasTranslations()) {
// Toggle showOriginal flag
HistoryMessage updatedMessage = historyMessage.withShowOriginal(!historyMessage.showOriginal());
history.set(i, updatedMessage);
updated = true;
break;
}
}

if (updated) {
sendUpdate(playerUUID);
}

return updated;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
package net.flectone.pulse.module.message.format.moderation.delete.model;

import lombok.With;
import net.flectone.pulse.module.message.format.translate.model.TranslatedMessage;
import net.kyori.adventure.text.Component;
import org.jspecify.annotations.Nullable;

import java.util.UUID;

public record HistoryMessage(UUID uuid, Component component) {
@With
public record HistoryMessage(
UUID uuid,
Component component,
@Nullable TranslatedMessage translatedMessage,
boolean showOriginal
) {

public HistoryMessage(UUID uuid, Component component) {
this(uuid, component, null, false);
}

public HistoryMessage(UUID uuid, Component component, TranslatedMessage translatedMessage) {
this(uuid, component, translatedMessage, false);
}

/**
* Get the component to display based on showOriginal flag and player locale.
*/
public Component getDisplayComponent(String playerLocale) {
if (translatedMessage == null) {
return component;
}

if (showOriginal) {
return translatedMessage.getOriginal();
}

return translatedMessage.getTranslation(playerLocale);
}

/**
* Check if this message has translations.
*/
public boolean hasTranslations() {
return translatedMessage != null;
}
}
Loading