Skip to content

Commit 49339fe

Browse files
author
Kim
committed
Messages under five characters are not translated, updated sys-prompt, edited messages, updated readme and added basic information command
1 parent 6c7e776 commit 49339fe

File tree

7 files changed

+107
-46
lines changed

7 files changed

+107
-46
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ From now on with minecraft 1.20.6 minecraft/spigot etc. use Java 21!
99

1010

1111
## Features
12-
- Real-time translation of player messages.
13-
- Configurable translation settings.
12+
- Real-time translation of all player messages (longer than five characters).
13+
- Configurable settings.
1414
- Easy integration with Ollama API.
15-
- Support for local hosting of translation models.
15+
- (ONLY) Support for local hosting of translation models (for now).
1616
- Quick setup and minimal configuration.
1717

1818

@@ -47,7 +47,7 @@ ollama:
4747

4848
cooldown:
4949
enabled: true
50-
milliseconds: 2000
50+
milliseconds: 1000
5151
message: §cPlease wait...
5252

5353
translation:
@@ -59,16 +59,18 @@ translation:
5959
```
6060
6161
## Usage
62+
0. Start ollama and download the model of choice
6263
1. Install the plugin in your Spigot Minecraft server's plugins directory.
6364
2. Configure the `options.yml` file according to your preferences.
6465
3. Restart/Reload the server to apply the changes.
6566
4. Players' messages will now be automatically translated as per the configured settings.
6667

6768

6869
## Note
69-
The bigger the model the better the outcome, mistral showed to be very good but sometimes it is acting weird, llama3:8b (instruct-fp16) was amazing.
70-
Please note that LLM/SLM require (a significant amount of) memory, with a minimum of 5-8 GB for small and 15-30 GB for middle-sized models or even more.
71-
You don't need a 30gb (file size) model if llama3:8b for example produces a good outcome then it is alright, I tested mistral and llama3 so test it yourself.
70+
- ALL messages (longer than five characters) are translated, also native ones, so this plugin is really only for servers with a mixed-language player-base.
71+
- The bigger the model the better the outcome, mistral showed to be very good but sometimes it is acting weird, llama3:8b (instruct-fp16) was very good but still not like a native speaker.
72+
- Please note that LLM/SLM require (a significant amount of) memory, with a minimum of 5-8 GB for small and 15-30 GB for middle-sized models or even more.
73+
- You don't need a 30gb (file size) model if llama3:8b for example produces a good outcome then it is alright, I tested mistral and llama3 so test it yourself.
7274

7375

7476
## Disclaimer

src/main/java/de/liebki/Start.java

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package de.liebki;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
import java.util.UUID;
6-
import java.util.concurrent.CompletableFuture;
7-
3+
import de.liebki.utils.Config;
4+
import de.liebki.utils.InfoCommand;
5+
import de.liebki.utils.MessageUtils;
6+
import de.liebki.utils.Pair;
7+
import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
8+
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
9+
import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
10+
import io.github.amithkoujalgi.ollama4j.core.utils.PromptBuilder;
811
import org.bukkit.Bukkit;
12+
import org.bukkit.command.CommandExecutor;
913
import org.bukkit.entity.Player;
1014
import org.bukkit.event.EventHandler;
1115
import org.bukkit.event.Listener;
1216
import org.bukkit.event.player.AsyncPlayerChatEvent;
1317
import org.bukkit.plugin.java.JavaPlugin;
1418

15-
import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
16-
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
17-
import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
18-
import io.github.amithkoujalgi.ollama4j.core.utils.PromptBuilder;
19+
import java.io.File;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.UUID;
23+
import java.util.concurrent.CompletableFuture;
1924

20-
public class Start extends JavaPlugin implements Listener {
25+
public class Start extends JavaPlugin implements Listener, CommandExecutor {
2126

22-
private final String TRANSLATOR_PROMPT = "Translate the original user message you get from any language to %TARGETLANGUAGE% without commenting or mentioning the source of translation. You can correct grammatical errors but dont alter the text too much and dont tell if you changed it. Avoid speaking with the user besides the translation, as everything is for someone else and not you, you focus on translating.";
27+
private final String PREFIX = "&f[&9OT&f]&r ";
28+
private final String TRANSLATOR_PROMPT = "Translate the user message you get from it's language to %TARGETLANGUAGE% without commenting or mentioning the source of translation. You can correct grammatical errors but dont alter the text too much and dont tell if you changed it. Avoid speaking with the user besides the translation, as everything is for someone else and not you, you focus on translating. Just translate the message, no comment, no code, no formatting just the translation.";
2329

2430
private Config config;
2531
private OllamaAPI ollamaAPI;
@@ -34,10 +40,21 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
3440
String msgContent = event.getMessage();
3541
UUID playerId = player.getUniqueId();
3642

43+
if(msgContent.length() < 5) {
44+
String rawAnswerString = (String) config.get("translation.broadcastmessage");
45+
rawAnswerString = rawAnswerString.replace("%TRANSLATION%", msgContent);
46+
47+
rawAnswerString = rawAnswerString.replace("%PLAYER%", player.getDisplayName());
48+
Bukkit.broadcastMessage(MessageUtils.ColorConvert(rawAnswerString));
49+
50+
event.setCancelled(true);
51+
return;
52+
}
53+
3754
if ((Boolean) config.get("cooldown.enabled")) {
3855
if (hasCooldown(playerId)) {
3956
event.setCancelled(true);
40-
player.sendMessage(cPlease wait..");
57+
player.sendMessage(MessageUtils.ColorConvert(PREFIX + "&cPlease wait before chatting.."));
4158
return;
4259
}
4360

@@ -73,7 +90,8 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
7390
event.setCancelled(true);
7491

7592
if ((Boolean) config.get("translation.notify")) {
76-
player.sendMessage((String) config.get("translation.cancelmessage"));
93+
String message = (String) config.get("translation.cancelmessage");
94+
player.sendMessage(MessageUtils.ColorConvert(PREFIX + message));
7795
}
7896
}
7997
}
@@ -94,7 +112,7 @@ private void handleResponse(Pair<String, Player> response) {
94112
rawAnswerString = rawAnswerString.replace("%TRANSLATION%", message);
95113
rawAnswerString = rawAnswerString.replace("%PLAYER%", receiver.getDisplayName());
96114

97-
Bukkit.broadcastMessage(rawAnswerString);
115+
Bukkit.broadcastMessage(MessageUtils.ColorConvert(rawAnswerString));
98116
}
99117

100118
}
@@ -114,43 +132,50 @@ private void cleanExpiredCooldowns() {
114132

115133
@Override
116134
public void onEnable() {
117-
config = new Config("plugins/ollamatranslator", "options.yml", this);
135+
File configFile = new File("plugins/ollamatranslator/options.yml");
118136

119-
if (!config.check("configexists")) {
137+
if (!configFile.exists()) {
138+
config = new Config("plugins/ollamatranslator", "options.yml", this);
120139

121-
config.set("donottouch.configexists", true);
140+
if (!config.check("configexists")) {
141+
config.set("donottouch.configexists", true);
122142

123-
config.set("ollama.secondstimeout", 20);
124-
config.set("ollama.modelname", "mistral:instruct");
125-
config.set("ollama.apiaddress", "http://localhost:11434/");
143+
config.set("ollama.secondstimeout", 20);
144+
config.set("ollama.modelname", "mistral:instruct");
145+
config.set("ollama.apiaddress", "http://localhost:11434/");
126146

127-
config.set("cooldown.enabled", true);
128-
config.set("cooldown.miliseconds", 1000);
129-
config.set("cooldown.message", "§cPlease wait..");
147+
config.set("cooldown.enabled", true);
148+
config.set("cooldown.miliseconds", 1000);
149+
config.set("cooldown.message", "&cPlease wait..");
130150

131-
config.set("translation.canceloriginalmessage", true);
132-
config.set("translation.notify", true);
133-
config.set("translation.cancelmessage", "§7Your message is in translation, please wait..");
151+
config.set("translation.canceloriginalmessage", true);
152+
config.set("translation.notify", true);
153+
config.set("translation.cancelmessage", "&7Your message is going to be translated, please wait..");
134154

135-
config.set("translation.targetlanguage", "english");
136-
config.set("translation.broadcastmessage", "§r%PLAYER% §r: §r§b%TRANSLATION%");
155+
config.set("translation.targetlanguage", "english");
156+
config.set("translation.broadcastmessage", "&r%PLAYER% &r: &r&b%TRANSLATION%");
137157

138-
config.saveConfig();
158+
config.saveConfig();
159+
}
160+
} else {
161+
config = new Config("plugins/ollamatranslator", "options.yml", this);
139162
}
140163

141164
ollamaAPI = new OllamaAPI((String) config.get("ollama.apiaddress"));
142165
ollamaAPI.setRequestTimeoutSeconds((Integer) config.get("ollama.secondstimeout"));
143166

144167
getServer().getPluginManager().registerEvents(this, this);
145-
getServer().getScheduler().runTaskTimer(this, this::cleanExpiredCooldowns, 0, 20 * 60);
168+
getServer().getScheduler().runTaskTimer(this, this::cleanExpiredCooldowns, 0, 20 * 5);
146169

147170
cooldown = (Integer) config.get("cooldown.miliseconds");
148-
Bukkit.getConsoleSender().sendMessage("§4ollama-translator powering on");
171+
Bukkit.getConsoleSender().sendMessage("§4OT: ollama-translator powering on");
172+
173+
this.getCommand("ollamatranslator").setExecutor(new InfoCommand());
149174
}
150175

151176
@Override
152177
public void onDisable() {
153-
Bukkit.getConsoleSender().sendMessage(4ollama-translator powering off");
178+
Bukkit.getConsoleSender().sendMessage(4OT: ollama-translator powering off");
154179
}
155180

156181
}

src/main/java/de/liebki/Config.java renamed to src/main/java/de/liebki/utils/Config.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package de.liebki;
2-
3-
import java.io.File;
4-
import java.io.IOException;
1+
package de.liebki.utils;
52

63
import org.bukkit.configuration.file.FileConfiguration;
74
import org.bukkit.configuration.file.YamlConfiguration;
85
import org.bukkit.plugin.Plugin;
96

7+
import java.io.File;
8+
import java.io.IOException;
9+
1010
public class Config<T> {
1111

1212
private File file;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.liebki.utils;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandExecutor;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Player;
7+
8+
public class InfoCommand implements CommandExecutor {
9+
10+
@Override
11+
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
12+
if (sender instanceof Player) {
13+
Player player = (Player) sender;
14+
player.sendMessage(MessageUtils.ColorConvert("&4ollama-translator &f- &bThis plugin helps to translate all messages for all users!"));
15+
}
16+
17+
return true;
18+
}
19+
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package de.liebki.utils;
2+
3+
public class MessageUtils {
4+
5+
public static String ColorConvert(String input) {
6+
String output = input.replace("&", "§");
7+
return output;
8+
}
9+
10+
}

src/main/java/de/liebki/Pair.java renamed to src/main/java/de/liebki/utils/Pair.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package de.liebki;
1+
package de.liebki.utils;
22

33
public class Pair<A, B> {
44

src/main/resources/plugin.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: OllamaTranslator
22
main: de.liebki.Start
3-
version: 1.0.0
3+
version: 1.0.1
44
api-version: "1.16"
5-
commands:
5+
commands:
6+
ollamatranslator:
7+
description: Shows info message
8+
usage: /<command>
9+
aliases: [ot, translator]

0 commit comments

Comments
 (0)