Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tr 11 - Added language translation for Materials #14

Merged
merged 10 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.townyadvanced</groupId>
<artifactId>TownyResources</artifactId>
<version>0.0.6</version>
<version>0.0.7</version>
<name>townyresources</name> <!-- Leave lower-cased -->

<properties>
Expand All @@ -20,6 +20,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>lang-utils-repo</id>
<url>https://ci.nyaacat.com/maven/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -32,15 +36,21 @@
<dependency>
<groupId>com.github.TownyAdvanced</groupId>
<artifactId>Towny</artifactId>
<version>0.97.0.0</version>
<version>0.97.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TownyAdvanced</groupId>
<artifactId>SiegeWar</artifactId>
<version>0.4.7</version>
<version>0.5.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.meowj</groupId>
<artifactId>LangUtils</artifactId>
<version>2.3-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TownyResources extends JavaPlugin {
private static Version requiredTownyVersion = Version.fromString("0.97.1.0");
private static boolean siegeWarInstalled;
private static boolean dynmapTownyInstalled;
private static boolean languageUtilsInstalled;

@Override
public void onEnable() {
Expand Down Expand Up @@ -165,6 +166,10 @@ public boolean isSiegeWarInstalled() {
return siegeWarInstalled;
}

public boolean isLanguageUtilsInstalled() {
return languageUtilsInstalled;
}

private String getTownyVersion() {
return Bukkit.getPluginManager().getPlugin("Towny").getDescription().getVersion();
}
Expand All @@ -185,5 +190,7 @@ private void setupIntegrationsWithOtherPlugins() {
siegeWarInstalled = siegeWar != null;
Plugin dynmapTowny = Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
dynmapTownyInstalled = dynmapTowny!= null;
Plugin languageUtils = Bukkit.getPluginManager().getPlugin("LanguageUtils");
languageUtilsInstalled = languageUtils!= null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void discoverNewResource(Resident resident, Town town, List<Materi
double productivityModifierNormalized = (double)TownyResourcesSettings.getProductionPercentagesPerResourceLevel().get(levelOfNewResource-1) / 100;
int preTaxProduction = (int)((winningCategory.getBaseAmountItems() * productivityModifierNormalized) + 0.5);
String categoryName = winningCategory.getName();
String translatedCategoryName = TownyResourcesTranslation.of("resource_category_" + categoryName).split(",")[1];
String translatedCategoryName = TownyResourcesTranslation.of("resource_category_" + categoryName).split(",")[1].trim();
String materialName = TownyResourcesMessagingUtil.formatMaterialForDisplay(winningMaterial);
TownyResourcesMessagingUtil.sendGlobalMessage(TownyResourcesTranslation.of("discovery.success", resident.getName(), translatedCategoryName, town.getName(), preTaxProduction, materialName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,17 @@ public enum TownyResourcesConfigNodes {
"# 1 - The name of the category (used for messaging)",
"# 2 - The discovery weight of the category (used during discovery)",
"# 3- The base amount of the offer (in stacks)",
"# 4- The list of materials in the category");

"# 4- The list of materials in the category"),
TOWN_RESOURCES_LANGUAGE(
"town_resources.language",
"",
"",
""),
TOWN_RESOURCES_LANGUAGE_MATERIALS_DISPLAY_LANGUAGE(
"town_resources.language.materials_display_language",
"zh_cn",
"",
"# If you have the LanguageUtils plugin installed, materials will be automatically translated into this locale/language.");
private final String Root;
private final String Default;
private String[] comments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,8 @@ public static boolean areResourceExtractionLimitsEnabled() {
public static int getCooldownAfterDailyLimitWarningMessageMillis() {
return getInt(TownyResourcesConfigNodes.RESOURCE_EXTRACTION_LIMITS_COOLDOWN_AFTER_DAILY_LIMIT_WARNING_MESSAGE_MILLIS);
}

public static String getMaterialsDisplayLanguage() {
return getString(TownyResourcesConfigNodes.TOWN_RESOURCES_LANGUAGE_MATERIALS_DISPLAY_LANGUAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.townyadvanced.townyresources.util;

import com.meowj.langutils.lang.LanguageHelper;
import io.github.townyadvanced.townyresources.settings.TownyResourcesSettings;
import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand All @@ -11,8 +13,11 @@

import io.github.townyadvanced.townyresources.TownyResources;
import io.github.townyadvanced.townyresources.settings.TownyResourcesTranslation;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TownyResourcesMessagingUtil {

Expand All @@ -38,37 +43,106 @@ public static void sendGlobalMessage(String message) {
}
}

/**
* Format resource string to something we can send to chat utils
*
* @param resourcesAsString resource string
* @return an array we can use on the town/nation screen
*/
public static String[] formatResourcesStringForGovernmentScreenDisplay(String resourcesAsString) {
if(resourcesAsString.length() == 0) {
if(resourcesAsString.isEmpty()) {
return new String[0];
} else {
//Convert given string to array
String[] resourcesAsFormattedArray =
WordUtils.capitalizeFully(
resourcesAsString
.replaceAll("-", " ")
.replaceAll("_", " "))
.split(",");

//Shorter result if it is too long
String[] resourcesAsFormattedArray = convertResourceAmountsStringToFormattedArray(resourcesAsString);
if(resourcesAsFormattedArray.length > 20) {
resourcesAsFormattedArray = Arrays.copyOf(resourcesAsFormattedArray, 21);
resourcesAsFormattedArray[20] = "...";
}
//Return result
return resourcesAsFormattedArray;
}
}

public static String formatProductionStringForDynmapTownyDisplay(String productionAsString) {
if(productionAsString.length() == 0) {
/**
* Format resource string to something we can send to the dynmap
*
* @param resourcesAsString resource string
* @return a string we can show on the dynmap e.g. "Wheat, Coal, Iron"
*/
public static String formatProductionStringForDynmapTownyDisplay(String resourcesAsString) {
if(resourcesAsString.isEmpty()) {
return "";
} else {
return WordUtils.capitalizeFully(productionAsString.replaceAll("_", " ").replaceAll("\\d+-", ""));
if(TownyResources.getPlugin().isLanguageUtilsInstalled()) {
List<String> resourcesAsFormattedList = new ArrayList<>();
String[] resourcesAsArray = resourcesAsString.replaceAll("\\d+-", "").split(",");
Material material;
String translatedMaterialName;
for(String resourceAsString: resourcesAsArray) {
material = Material.getMaterial(resourceAsString);
translatedMaterialName = getTranslatedMaterialName(material);
resourcesAsFormattedList.add(translatedMaterialName);
}
return Arrays.toString(resourcesAsFormattedList.toArray()).replace("[","").replace("]","");
} else {
return WordUtils.capitalizeFully(resourcesAsString.replaceAll("_", " ").replaceAll("\\d+-", ""));
}
}
}

/**
* Format one material for display.
*
* @param material the material
* @return the formatted material name
*/
public static String formatMaterialForDisplay(Material material) {
return WordUtils.capitalizeFully(material.toString().replaceAll("_", " "));
if(TownyResources.getPlugin().isLanguageUtilsInstalled()) {
return getTranslatedMaterialName(material);
} else {
return WordUtils.capitalizeFully(material.toString().replaceAll("_", " "));
}
}

/**
* Convert a resource amount string formatted array
*
* NOTE: Do not pass in an empty string
*
* @param resourcesAmountsString e.g. "64-WHEAT,64-COAL"
* @return e.g. ["64 Wheat","64 Coal"]
*/
private static String[] convertResourceAmountsStringToFormattedArray(String resourcesAmountsString) {
if(TownyResources.getPlugin().isLanguageUtilsInstalled()) {
//Return translated materials array
List<String> resourcesAsFormattedList = new ArrayList<>();
String[] resourcesAsArray = resourcesAmountsString.split(",");
String[] amountAndMaterialName;
String amount;
String materialName;
String translatedMaterialName;
Material material;
for(String resourceAsString: resourcesAsArray) {
amountAndMaterialName = resourceAsString.split("-");
amount = amountAndMaterialName[0];
materialName = amountAndMaterialName[1];
material = Material.getMaterial(materialName);
translatedMaterialName = getTranslatedMaterialName(material);
resourcesAsFormattedList.add(amount + " " + translatedMaterialName);
}
return resourcesAsFormattedList.toArray(new String[0]);
} else {
//Return english materials array
return WordUtils.capitalizeFully(
resourcesAmountsString
.replaceAll("_", " ")
.replaceAll("-", " "))
.split(",");
}
}

private static String getTranslatedMaterialName(Material material) {
ItemStack fakeItemStack = new ItemStack(material);
String translatedMaterialName = LanguageHelper.getItemDisplayName(fakeItemStack, TownyResourcesSettings.getMaterialsDisplayLanguage());
return translatedMaterialName;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ author: Goosius
website: 'https://github.com/TownyAdvanced/TownyResources'
prefix: ${project.artifactId}
depend: [Towny]
softdepend: [SiegeWar,Dynmap-Towny]
softdepend: [SiegeWar,Dynmap-Towny,LanguageUtils]
loadbefore: [SiegeWar]

description: This is an add-on plugin for Towny, which gives each town a unique set of automatically-extracted resources, and then protects the economic value of those resources with moderate limits to player resource extraction.
Expand Down