Skip to content

Commit

Permalink
Now using a custom translation endpoint with no limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo0111 committed Apr 11, 2022
1 parent 5a899b1 commit 4f76f10
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 140 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,4 @@ run/

ignore/

null/

!libs/GoogleTranslateAPI-1.0-shaded.jar
null/
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.apache.tools.ant.filters.ReplaceTokens
plugins {
id "java"
id "com.github.johnrengelman.shadow" version "7.1.2"
id "net.kyori.indra.publishing" version "2.1.1"
id "net.kyori.indra.publishing" version "2.1.0"
id "net.kyori.indra.publishing.sonatype" version "2.1.1"
id "signing"
id "io.github.slimjar" version "1.3.0"
Expand All @@ -38,7 +38,7 @@ ext {
}

group = "me.lorenzo0111"
version = "1.7.1"
version = "1.7.2"
description = "MultiLang is a library that allows you to easily create multi-language placeholders."

java {
Expand Down Expand Up @@ -66,15 +66,15 @@ dependencies {
implementation("space.rocketplugins.pluginslib:bukkit:2.0.3.2")
implementation("io.github.slimjar:slimjar:1.2.7")
implementation('dev.triumphteam:triumph-gui:3.2')
implementation files("libs/GoogleTranslateAPI-1.0-shaded.jar")
compileOnly("org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT")
compileOnly("com.comphenix.protocol:ProtocolLib:4.7.0")
compileOnly("com.comphenix.protocol:ProtocolLib:4.8.0")
compileOnly("com.github.MilkBowl:VaultAPI:1.7.1")
compileOnly("me.clip:placeholderapi:2.11.1")
compileOnly("me.lorenzo0111:RocketPlaceholders:2.1")
compileOnly('org.jetbrains:annotations:23.0.0')
compileOnly("org.jetbrains:annotations:23.0.0")
slim("com.squareup.okhttp3:okhttp:4.9.3")
slim("com.zaxxer:HikariCP:4.0.3")
slim('com.github.cryptomorin:XSeries:8.6.1')
slim("com.github.cryptomorin:XSeries:8.7.1")
slim("mysql:mysql-connector-java:8.0.28")
slim("org.xerial:sqlite-jdbc:3.36.0.3")
slim("org.slf4j:slf4j-simple:1.7.36")
Expand All @@ -90,6 +90,7 @@ slimJar {
relocate('dev.triumphteam',"${libsBase}")
relocate('com.cryptomorin.xseries',"${libsBase}.xseries")
relocate('com.zaxxer.hikari',"${libsBase}.hikari")
relocate('okhttp3', "${libsBase}.okhttp")
}

shadowJar {
Expand All @@ -107,7 +108,6 @@ shadowJar {
relocate("org.spongepowered.configurate", "${libsBase}.configurate")
relocate("org.bstats", "${libsBase}.bstats")
relocate("com.google.gson", "${libsBase}.google.gson")
relocate("com.gtranslate", "${libsBase}.google.translate")
}


Expand Down
Binary file removed libs/GoogleTranslateAPI-1.0-shaded.jar
Binary file not shown.
35 changes: 21 additions & 14 deletions src/main/java/me/lorenzo0111/multilang/MultiLangPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
import me.lorenzo0111.multilang.cache.PlayersCache;
import me.lorenzo0111.multilang.data.StorageType;
import me.lorenzo0111.multilang.database.DatabaseManager;
import me.lorenzo0111.multilang.exceptions.ApiException;
import me.lorenzo0111.multilang.exceptions.ReloadException;
import me.lorenzo0111.multilang.handlers.ConfigManager;
import me.lorenzo0111.multilang.hooks.Hook;
import me.lorenzo0111.multilang.listeners.JoinListener;
import me.lorenzo0111.multilang.protocol.PacketHandler;
import me.lorenzo0111.multilang.realtime.GoogleTranslator;
import me.lorenzo0111.multilang.realtime.MicrosoftTranslator;
import me.lorenzo0111.multilang.realtime.RealTimeTranslator;
import me.lorenzo0111.multilang.realtime.TranslatorConfig;
import me.lorenzo0111.multilang.storage.StorageManager;
import me.lorenzo0111.multilang.tasks.UpdateTask;
Expand All @@ -62,10 +62,7 @@
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
import java.util.logging.Level;

public final class MultiLangPlugin extends JavaPlugin {
Expand Down Expand Up @@ -138,9 +135,13 @@ public void onEnable() {
e.printStackTrace();
}

this.reloadRealtimeConfig();
if (translators.isUseCache()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> translators.loadCache(databaseManager), 60 * 20L);
try {
this.reloadRealtimeConfig();
if (translators.isUseCache()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> translators.loadCache(databaseManager), 60 * 20L);
}
} catch (ApiException e) {
this.getLogger().severe("An error has occurred while trying to load the realtime configuration: " + e.getMessage());
}

this.cacheFolder = new File(this.getDataFolder(), "cache");
Expand Down Expand Up @@ -319,12 +320,18 @@ public void updateConfig() throws IOException {
ConfigurationSection section = this.getConfig().createSection("real-time");
section.set("enabled",false);
section.set("cache",true);
section.set("api","google");
section.set("server-messages",false);
section.set("ignore", Collections.singletonList("\\\\/"));
section.set("key", "Get one at https://multilang.lorenzo0111.me/");
section.set("autoupdater-comment","This section has been automatically generated by the plugin. Since the configuration api does not allow us to put comments, please read our documentation for more information. https://wiki.lorenzo0111.me/multilang");
this.getConfig().set("real-time",section);
needsSave = true;
}

if (!this.getConfig().getString("realtime.api", "realtime").equalsIgnoreCase("realtime")) {
this.getLogger().severe("RealTime google and bing api has been deprecated. Please retrieve a new api key at https://multilang.lorenzo0111.me");
}

if (needsSave) {
this.saveConfig();
}
Expand Down Expand Up @@ -358,19 +365,19 @@ public PacketHandler getProtocol() {
return protocol;
}

public void reloadRealtimeConfig() {
public void reloadRealtimeConfig() throws ApiException {
if (translators != null && translators.isUseCache()) {
translators.saveCache(databaseManager);
}

translators = new TranslatorConfig();
translators.canRegister(true);
translators.registerTranslator("bing", new MicrosoftTranslator(this.getConfig().getString("real-time.key")));
translators.registerTranslator("google", new GoogleTranslator());
translators.registerTranslator("realtime", new RealTimeTranslator(this.getConfig().getString("real-time.key")));
translators.setInUse("realtime");
translators.setEnabled(this.getConfig().getBoolean("real-time.enabled"));
translators.setInUse(this.getConfig().getString("real-time.api", "google"));
translators.setUseCache(this.getConfig().getBoolean("real-time.cache"));
translators.setPatterns(this.getConfig().getStringList("real-time.ignore"));
translators.setTranslateServer(this.getConfig().getBoolean("real-time.server-messages"));
if (!translators.testConnection()) {
this.getLogger().severe("RealTime API is not working. Please check your api url.");
this.getLogger().severe("Disabling real-time translation.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import me.lorenzo0111.multilang.MultiLangPlugin;
import me.lorenzo0111.multilang.commands.AdminLangCommand;
import me.lorenzo0111.multilang.commands.SubCommand;
import me.lorenzo0111.multilang.exceptions.ApiException;
import me.lorenzo0111.multilang.exceptions.ReloadException;
import me.lorenzo0111.multilang.protocol.adapter.EntityAdapter;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void handleSubcommand(CommandSender commandSender, String[] strings) {


commandSender.sendMessage(this.format(String.format("&7Plugin reloaded in &9%sms", System.currentTimeMillis() - time)));
} catch (ReloadException | SQLException | IOException ex) {
} catch (ReloadException | SQLException | IOException | ApiException ex) {
if (commandSender instanceof Player) {
XSound.play((Player) commandSender,"BLOCK_ANVIL_PLACE");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@

public class ChatAdapter extends BaseAdapter {
private static final List<BukkitRunnable> TASKS = new ArrayList<>();
private final List<String> ignore = new ArrayList<>();

public ChatAdapter(MultiLangPlugin plugin, ListenerPriority listenerPriority) {
super(plugin, listenerPriority, PacketType.Play.Server.CHAT);
}

@Override
public void onPacketSending(@NotNull PacketEvent event) {
if (event.isCancelled()) return;
PacketContainer packet = event.getPacket();
Player player = event.getPlayer();

Expand All @@ -63,6 +65,11 @@ public void onPacketSending(@NotNull PacketEvent event) {
WrappedChatComponent component = packet.getChatComponents().read(0);
if (component == null) return;

if (ignore.contains(component.getJson())) {
ignore.remove(component.getJson());
return;
}

if (type == null || type.equals(EnumWrappers.ChatType.SYSTEM)) {
this.handle(player,component);
}
Expand Down Expand Up @@ -98,6 +105,8 @@ public void onPacketSending(@NotNull PacketEvent event) {
}

private boolean translate(PacketEvent event, PacketContainer packet, @NotNull TranslatorConfig translators, @NotNull LocalizedPlayer p, @NotNull JsonObject object) {
if (!translators.isTranslateServer() && event.isServerPacket()) return false;

String text = translators.tryFromCache(p.getLocale(), object.get("text").getAsString());
if (text != null) {
this.update(object,text);
Expand All @@ -114,25 +123,31 @@ public void queue(LocalizedPlayer p, PacketContainer packet) {
BukkitRunnable task = new BukkitRunnable() {
@Override
public void run() {
WrappedChatComponent component = packet.getChatComponents().read(0);

TranslatorConfig translators = ((MultiLangPlugin) ChatAdapter.this.getPlugin()).getTranslators();

ChatAdapter.this.updateTexts(component, (value) -> {
String text = translators.translate(p.getLocale(), value);
return text != null ? text : value;
});

packet.getChatComponents().write(0, component);
Bukkit.getScheduler().runTask(ChatAdapter.this.getPlugin(), () -> {
try {
ProtocolManager manager = ((MultiLangPlugin) ChatAdapter.this.getPlugin()).getProtocol().getManager();
try {
WrappedChatComponent component = packet.getChatComponents().read(0);

TranslatorConfig translators = ((MultiLangPlugin) ChatAdapter.this.getPlugin()).getTranslators();

ChatAdapter.this.updateTexts(component, (value) -> {
String text = translators.translate(p.getLocale(), value);
return text != null ? text : value;
});

packet.getChatComponents().write(0, component);
Bukkit.getScheduler().runTask(ChatAdapter.this.getPlugin(), () -> {
try {
ignore.add(component.getJson());

ProtocolManager manager = ((MultiLangPlugin) ChatAdapter.this.getPlugin()).getProtocol().getManager();
manager.sendServerPacket(p.getPlayer(), packet);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}

manager.sendServerPacket(p.getPlayer(), packet);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
});
TASKS.remove(this);
}
};
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4f76f10

Please sign in to comment.