-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dd25f3
commit 6878dd6
Showing
10 changed files
with
280 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/net/azisaba/ryuzupluginchat/message/data/MessageData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package net.azisaba.ryuzupluginchat.message.data; | ||
|
||
public interface MessageData { | ||
void setMessage(String message); | ||
String getMessage(); | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/net/azisaba/ryuzupluginchat/redis/LanguageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package net.azisaba.ryuzupluginchat.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import net.azisaba.ryuzupluginchat.RyuZUPluginChat; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
@RequiredArgsConstructor | ||
public class LanguageController { | ||
|
||
private final Map<UUID, String> languages = new HashMap<>(); | ||
private final ReentrantLock lock = new ReentrantLock(); | ||
|
||
private final JedisPool jedisPool; | ||
private final String groupName; | ||
|
||
public @NotNull String getLanguage(@NotNull Player player) { | ||
String language = getLanguage(player.getUniqueId()); | ||
if (language == null) { | ||
String locale = player.getLocale().replaceAll("(.+)_.+", "$1"); | ||
if (Locale.forLanguageTag(locale).toLanguageTag().equalsIgnoreCase("und")) { | ||
language = "ja"; | ||
} else { | ||
language = locale; | ||
} | ||
} | ||
return language; | ||
} | ||
|
||
public @Nullable String getLanguage(UUID uuid) { | ||
if (uuid == null) return Locale.JAPANESE.toLanguageTag(); | ||
lock.lock(); | ||
try { | ||
return languages.get(uuid); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public @NotNull String getLanguageOrDefault(UUID uuid, String defaultValue) { | ||
if (uuid == null) return Locale.JAPANESE.toLanguageTag(); | ||
lock.lock(); | ||
try { | ||
return languages.getOrDefault(uuid, Locale.JAPANESE.toLanguageTag()); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public void refreshAllAsync() { | ||
RyuZUPluginChat.newChain() | ||
.async( | ||
() -> { | ||
try (Jedis jedis = jedisPool.getResource()) { | ||
String keyPrefix = | ||
"rpc:" + groupName + ":language:"; | ||
|
||
try { | ||
lock.lock(); | ||
languages.clear(); | ||
for (String s : jedis.keys(keyPrefix + "*")) { | ||
try { | ||
UUID uuid = UUID.fromString(s.replace(keyPrefix, "")); | ||
languages.put(uuid, jedis.get(s)); | ||
} catch (IllegalArgumentException ignored) { | ||
} | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
}) | ||
.execute(); | ||
} | ||
|
||
public void setLanguage(@NotNull UUID uuid, @NotNull String language) { | ||
lock.lock(); | ||
try { | ||
languages.put(uuid, language); | ||
RyuZUPluginChat.newChain() | ||
.async( | ||
() -> { | ||
try (Jedis jedis = jedisPool.getResource()) { | ||
String key = | ||
"rpc:" + groupName + ":language:" + uuid; | ||
jedis.set(key, language); | ||
} | ||
}) | ||
.execute(); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/net/azisaba/ryuzupluginchat/util/OpenAIUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package net.azisaba.ryuzupluginchat.util; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import net.azisaba.ryuzupluginchat.config.RPCConfig; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class OpenAIUtils { | ||
public static @NotNull Map<String, String> getOpenAIRequestHeaders(@NotNull RPCConfig config) { | ||
Map<String, String> map = new HashMap<>(); | ||
map.put("Content-Type", "application/json"); | ||
map.put("Authorization", "Bearer " + config.getTranslatorOpenAIApiKey()); | ||
if (config.getTranslatorOpenAIOrganization() != null) { | ||
map.put("OpenAI-Organization", config.getTranslatorOpenAIOrganization()); | ||
} | ||
return map; | ||
} | ||
|
||
public static @NotNull String ask(@NotNull RPCConfig config, @NotNull String language, @Nullable String user, @NotNull String message) { | ||
JsonObject obj = getRequestBody(config.getTranslatorPrompt().replace("%language%", language), user, message); | ||
JsonObject responseObject = new Gson().fromJson(Reqwest.post(obj.toString(), "https://api.openai.com/v1/chat/completions", getOpenAIRequestHeaders(config)), JsonObject.class); | ||
return responseObject.getAsJsonArray("choices") | ||
.get(0) | ||
.getAsJsonObject() | ||
.getAsJsonObject("message") | ||
.get("content") | ||
.getAsString(); | ||
} | ||
|
||
private static @NotNull JsonObject getRequestBody(@NotNull String prompt, @Nullable String user, @NotNull String message) { | ||
JsonObject systemMessage = new JsonObject(); | ||
systemMessage.addProperty("role", "system"); | ||
systemMessage.addProperty("content", prompt); | ||
JsonObject userMessage = new JsonObject(); | ||
userMessage.addProperty("role", "user"); | ||
userMessage.addProperty("content", message); | ||
JsonArray messages = new JsonArray(); | ||
messages.add(systemMessage); | ||
messages.add(userMessage); | ||
JsonObject obj = new JsonObject(); | ||
obj.addProperty("model", "gpt-4o"); | ||
obj.addProperty("user", user); | ||
obj.addProperty("max_tokens", 250); | ||
obj.add("messages", messages); | ||
return obj; | ||
} | ||
} |
Oops, something went wrong.