Skip to content

Commit

Permalink
Translations improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
booksaw committed Aug 7, 2024
1 parent 5389c9f commit 2dec71a
Show file tree
Hide file tree
Showing 7 changed files with 790 additions and 405 deletions.
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,10 @@ Features:
- Added TeamWithdrawEvent, TeamDepositEvent and TeamDisallowedPvpEvent (#613) - Credit Hellinduction
- Added a title message when allying another team (#629) - Credit svaningelgem
- Only tab complete teams that are allies for /team neutral (#632) - Credit svaningelgem
- Added /teama importmessages to import translations from "missingmessages.txt"

Improvements:
- Added missing Spanish messages (both translations) - Credit LeadVic

Bug Fixes:
- Fixed typo in config.yml (#620) - Credit dacoder101
Expand All @@ -832,6 +836,7 @@ Bug Fixes:
- Improved logging for /team baltop error (#623)
- Hopefully fixed a problem causing players to be kicked from teams (#618)
- Fixed problem causing '?' in an item name to break SQL commands (#644)
- Fixed missingmessages.txt getting overwritten when the server restarts losing translated messages

Code improvements:
- Replaced equals/length with isEmpty (#630) - Credit svaningelgem
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/booksaw/betterTeams/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void loadCustomConfigs() {

ConfigManager messagesConfigManager = new ConfigManager(language, true);

MessageManager.addMessages(messagesConfigManager.config);
MessageManager.addMessages(messagesConfigManager);

if (!language.equals("messages")) {
messagesConfigManager = new ConfigManager("messages", true);
Expand Down Expand Up @@ -292,7 +292,7 @@ public void setupCommands() {
new LeaveTeama(), new PromoteTeama(), new DemoteTeama(), new WarpTeama(), new SetwarpTeama(),
new DelwarpTeama(), new PurgeTeama(), new DisbandTeama(), new ColorTeama(), new EchestTeama(),
new SetrankTeama(teamaCommand), new TagTeama(), new TeleportTeama(teamaCommand), new AllyTeama(),
new NeutralTeama());
new NeutralTeama(), new ImportmessagesTeama());

if (getConfig().getBoolean("singleOwner")) {
teamaCommand.addSubCommand(new SetOwnerTeama());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.booksaw.betterTeams.commands.teama;

import com.booksaw.betterTeams.CommandResponse;
import com.booksaw.betterTeams.Main;
import com.booksaw.betterTeams.commands.SubCommand;
import com.booksaw.betterTeams.message.MessageManager;
import org.bukkit.command.CommandSender;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class ImportmessagesTeama extends SubCommand {
@Override
public CommandResponse onCommand(CommandSender sender, String label, String[] args) {
try {
File f = new File(Main.plugin.getDataFolder() + File.separator + MessageManager.MISSINGMESSAGES_FILENAME);
BufferedReader reader = new BufferedReader(new FileReader(f));

String line;
while ((line = reader.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#")) {
continue;
}
String[] split = line.split(": ", 2);
if (split.length != 2) {
continue;
}
MessageManager.getDefaultMessages().set(split[0], split[1]);
}
MessageManager.getDefaultMessagesConfigManager().save(true);
f.delete();
} catch (IOException e) {
e.printStackTrace();
return new CommandResponse(false, "admin.import.fail");
}
return new CommandResponse(true, "admin.import.success");
}

@Override
public String getCommand() {
return "importmessages";
}

@Override
public String getNode() {
return "admin.importmessages";
}

@Override
public String getHelp() {
return "Import the messages from missingmessages.txt the current language";
}

@Override
public String getArguments() {
return "";
}

@Override
public int getMinimumArguments() {
return 0;
}

@Override
public int getMaximumArguments() {
return 0;
}

@Override
public void onTabComplete(List<String> options, CommandSender sender, String label, String[] args) {
}
}
76 changes: 54 additions & 22 deletions src/main/java/com/booksaw/betterTeams/message/MessageManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.booksaw.betterTeams.message;

import com.booksaw.betterTeams.ConfigManager;
import com.booksaw.betterTeams.Main;
import me.clip.placeholderapi.PlaceholderAPI;
import net.md_5.bungee.api.ChatColor;
Expand All @@ -10,8 +11,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.PrintWriter;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -25,12 +25,14 @@
*/
public class MessageManager {

public static final String MISSINGMESSAGES_FILENAME = "missingmessages.txt";

/**
* Used to store all loaded messages
*/
private static HashMap<String, String> messages = new HashMap<>();

private static FileConfiguration defaultMessages;
private static ConfigManager defaultMessagesConfigManager;

/**
* This is the prefix which goes before all messages related to this plugin
Expand Down Expand Up @@ -60,20 +62,20 @@ public static void setLanguage(String lang) {
* This method is used to provide the configuration file in which all the
* message references are stored, this method also loads the default prefix
*
* @param file the configuration file
* @param configManager the configuration manager
*/
public static void addMessages(FileConfiguration file) {
public static void addMessages(ConfigManager configManager) {
prefix = ChatColor.translateAlternateColorCodes('&',
Objects.requireNonNull(Main.plugin.getConfig().getString("prefixFormat")));
defaultMessages = file;
addMessages(file, false);
defaultMessagesConfigManager = configManager;

addMessages(configManager.config, false);
}

/**
* Used to select a file to contain backup messages in the event that the
* community translations are incomplete
*
*
* @param file The file to load the backup messages from
*/
public static void addBackupMessages(YamlConfiguration file) {
Expand Down Expand Up @@ -114,17 +116,37 @@ private static void addMessages(FileConfiguration file, boolean backup) {
logger.info(
"[BetterTeams] If you are able to help with translation please join the discord server and make yourself known (https://discord.gg/JF9DNs3)");
logger.info(
"[BetterTeams] A file called `missingMessages.txt` has been created within this plugins folder. To contribute to the community translations, translate the messages within it and submit it to the discord");
"[BetterTeams] A file called `" + MISSINGMESSAGES_FILENAME + "` has been created within this plugins folder. To contribute to the community translations, translate the messages within it and submit it to the discord");
logger.info("[BetterTeams] ==================================================================");
}

}

private static void saveMissingMessages(List<String> missingMessages) {

File f = new File(Main.plugin.getDataFolder() + File.separator + "missingMessages.txt");
File f = new File(Main.plugin.getDataFolder() + File.separator + MISSINGMESSAGES_FILENAME);

List<String> existingKeys = new ArrayList<>();

if (f.exists()) {

try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#")) {
continue;
}
String[] split = line.split(": ", 2);
if (split.length == 2) {
existingKeys.add(split[0]);
}
}
} catch (IOException e) {
e.printStackTrace();
return;
}

if (!f.exists()) {
} else {
try {
f.createNewFile();
} catch (Exception e) {
Expand All @@ -133,12 +155,17 @@ private static void saveMissingMessages(List<String> missingMessages) {
}
}

try (PrintWriter writer = new PrintWriter(f)) {
writer.println(
"# Please translate these messages and then submit them to the Booksaw Development (https://discord.gg/JF9DNs3) in the #messages-submissions channel for a special rank");
writer.println("# Your translations will be included in the next update");
try (PrintWriter writer = new PrintWriter(new FileWriter(f, !existingKeys.isEmpty()))) {
if (existingKeys.isEmpty()) {
writer.println(
"# Please translate these messages and then submit them to the Booksaw Development (https://discord.gg/JF9DNs3) in the #messages-submissions channel for a special rank");
writer.println("# Your translations will be included in the next update");
writer.println("# When you are done translating, run '/teama importmessages' to include the translated messages in your main file");
}
for (String str : missingMessages) {
writer.println(str + ": " + messages.get(str));
if (!existingKeys.contains(str)) {
writer.println(str + ": " + messages.get(str));
}
}

} catch (Exception e) {
Expand All @@ -149,7 +176,6 @@ private static void saveMissingMessages(List<String> missingMessages) {

/**
* Used to send a (formatted) message to the specified user
*
* @param sender the commandSender which the message should be sent to
* @param reference the reference for the message
Expand Down Expand Up @@ -228,7 +254,7 @@ public static HashMap<String, String> getMessages() {

/**
* @return the prefix for all messages Defaults to [BetterTeams] unless it is
* changed by end user
* changed by end user
*/
public static String getPrefix() {
return prefix;
Expand All @@ -248,7 +274,7 @@ public static void sendFullMessage(CommandSender sender, String message) {
/**
* Used when you are sending a user a message instead of a message loaded from a
* file
*
*
* @param sender the player who sent the command
* @param message The message to send to that user
* @param prefixMessage The prefix for that message
Expand All @@ -266,15 +292,19 @@ public static File getFile() {
}

public static FileConfiguration getDefaultMessages() {
return defaultMessages;
return defaultMessagesConfigManager.config;
}

public static ConfigManager getDefaultMessagesConfigManager() {
return defaultMessagesConfigManager;
}

/**
* Used to clear all messages from the cache
*/
public static void dumpMessages() {
messages = new HashMap<>();
defaultMessages = null;
defaultMessagesConfigManager = null;
}

/**
Expand All @@ -289,7 +319,9 @@ public static void sendTitle(Player player, String reference, Object... replacem
sendFullTitle(player, message, false);
}

public static void sendFullTitle(Player player, String message) { sendFullTitle(player, message, true); }
public static void sendFullTitle(Player player, String message) {
sendFullTitle(player, message, true);
}

public static void sendFullTitle(Player player, String message, boolean prefixMessage) {
if (prefixMessage) {
Expand Down
Loading

0 comments on commit 2dec71a

Please sign in to comment.