Skip to content

Commit

Permalink
Translator improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Kade-N committed Mar 29, 2019
1 parent 7901fbe commit f467035
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,32 @@ public String translate(String message, String targetLanguageCode) {
startingIndex += 6;
int endingIndex = response.indexOf("</text>");

return response.substring(startingIndex, endingIndex);
String translation = response.substring(startingIndex, endingIndex);

if(translation.equalsIgnoreCase(message)) return null;

return translation;
}


@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.MONITOR)
public void onChat(AsyncPlayerChatEvent event) {
// If event is cancelled, do nothing
if (event.isCancelled())
return;
if (event.isCancelled()) return;

String message = ChatColor.stripColor(event.getMessage());
// If message starts with / or ./, it's probably a command, ignore it
if(message.startsWith("/") || message.startsWith("./")) return;

// Get languages to translate into
Set<String> targetLanguages = new HashSet<String>(playerLanguages.values());

for (String languageCode : targetLanguages) {
String message = translate(event.getMessage(), languageCode);
if(message!=null){
String translation = translate(message, languageCode);
if(translation!=null){
for (Player player : Bukkit.getOnlinePlayers()) {
// Delay message, so it shows after the chat message
Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, () -> player.sendMessage(ChatColor.DARK_GRAY + "[Translation] " + ChatColor.RESET + message));
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.sendMessage(ChatColor.DARK_GRAY + "[Translation] " + ChatColor.RESET + translation));
}
}
}
Expand Down

0 comments on commit f467035

Please # to comment.