mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-10-06 10:37:29 +02:00
Adds ability for the start sign to use a locale file set by the addon
https://github.com/BentoBoxWorld/bentobox/issues/372 Signs must have [start] as their first line to be converted.
This commit is contained in:
parent
a7365ef805
commit
d907506fda
@ -1,9 +1,5 @@
|
|||||||
package world.bentobox.bentobox.api.addons.exceptions;
|
package world.bentobox.bentobox.api.addons.exceptions;
|
||||||
|
|
||||||
import world.bentobox.bentobox.api.addons.request.AddonRequestBuilder;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class AddonRequestException extends AddonException
|
public class AddonRequestException extends AddonException
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = -5698456013070166174L;
|
private static final long serialVersionUID = -5698456013070166174L;
|
||||||
|
@ -16,4 +16,5 @@ public class TextVariables {
|
|||||||
public static final String PERMISSION = "[permission]";
|
public static final String PERMISSION = "[permission]";
|
||||||
public static final String SPAWN_HERE = "[spawn_here]";
|
public static final String SPAWN_HERE = "[spawn_here]";
|
||||||
public static final String VERSION = "[version]";
|
public static final String VERSION = "[version]";
|
||||||
|
public static final String START_TEXT = "[start]";
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,25 @@ public class LocalesManager {
|
|||||||
return get(reference);
|
return get(reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the translated String corresponding to the reference from the locale file for this user.
|
||||||
|
* @param user the User
|
||||||
|
* @param reference a reference that can be found in a locale file
|
||||||
|
* @param default to return if the reference cannot be found anywhere
|
||||||
|
* @return the translated String from the User's locale or from the server's locale or from the en-US locale, or null.
|
||||||
|
*/
|
||||||
|
public String getOrDefault(User user, String reference, String defaultText) {
|
||||||
|
// Make sure the user is not null
|
||||||
|
if (user != null) {
|
||||||
|
BentoBoxLocale locale = languages.get(user.getLocale());
|
||||||
|
if (locale != null && locale.contains(reference)) {
|
||||||
|
return locale.get(reference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// No translation could be gotten from the player's locale, trying more generic solutions
|
||||||
|
return getOrDefault(reference, defaultText);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the translated String corresponding to the reference from the server's or the en-US locale file.
|
* Gets the translated String corresponding to the reference from the server's or the en-US locale file.
|
||||||
* @param reference a reference that can be found in a locale file
|
* @param reference a reference that can be found in a locale file
|
||||||
@ -70,6 +89,18 @@ public class LocalesManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the translated String corresponding to the reference from the server's or the en-US locale file
|
||||||
|
* or if it cannot be found anywhere, use the default text supplied.
|
||||||
|
* @param reference a reference that can be found in a locale file
|
||||||
|
* @param default text to return if the reference cannot be found anywhere
|
||||||
|
* @return the translated String from the server's locale or from the en-US locale, or default.
|
||||||
|
*/
|
||||||
|
public String getOrDefault(String reference, String defaultText) {
|
||||||
|
String result = get(reference);
|
||||||
|
return result == null ? defaultText : result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies all the locale files from the plugin jar to the filesystem.
|
* Copies all the locale files from the plugin jar to the filesystem.
|
||||||
* Only done if the locale folder does not already exist.
|
* Only done if the locale folder does not already exist.
|
||||||
@ -183,4 +214,6 @@ public class LocalesManager {
|
|||||||
loadLocalesFromFile(BENTOBOX);
|
loadLocalesFromFile(BENTOBOX);
|
||||||
plugin.getAddonsManager().getAddons().forEach(addon -> loadLocalesFromFile(addon.getDescription().getName()));
|
plugin.getAddonsManager().getAddons().forEach(addon -> loadLocalesFromFile(addon.getDescription().getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import java.util.zip.ZipInputStream;
|
|||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.DyeColor;
|
import org.bukkit.DyeColor;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -221,6 +222,19 @@ public class Clipboard {
|
|||||||
island.setSpawnPoint(block.getWorld().getEnvironment(), spawnPoint);
|
island.setSpawnPoint(block.getWorld().getEnvironment(), spawnPoint);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Handle locale text for starting sign
|
||||||
|
// Sign text must be stored under the addon's name.sign.line0,1,2,3 in the yaml file
|
||||||
|
if (island != null && !lines.isEmpty() && lines.get(0).equalsIgnoreCase(TextVariables.START_TEXT)) {
|
||||||
|
// Get the addon that is operating in this world
|
||||||
|
plugin.getIWM().getAddon(island.getWorld()).ifPresent(addon -> {
|
||||||
|
lines.clear();
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
lines.add(ChatColor.translateAlternateColorCodes('&', plugin.getLocalesManager().getOrDefault(User.getInstance(island.getOwner()),
|
||||||
|
addon.getDescription().getName().toLowerCase() + ".sign.line" + i,"")));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Get the name of the player
|
||||||
String name = TextVariables.NAME;
|
String name = TextVariables.NAME;
|
||||||
if (island != null) {
|
if (island != null) {
|
||||||
name = plugin.getPlayers().getName(island.getOwner());
|
name = plugin.getPlayers().getName(island.getOwner());
|
||||||
@ -229,6 +243,7 @@ public class Clipboard {
|
|||||||
for (int i = 0 ; i < lines.size(); i++) {
|
for (int i = 0 ; i < lines.size(); i++) {
|
||||||
sign.setLine(i, lines.get(i).replace(TextVariables.NAME, name));
|
sign.setLine(i, lines.get(i).replace(TextVariables.NAME, name));
|
||||||
}
|
}
|
||||||
|
// Update the sign
|
||||||
sign.update();
|
sign.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package world.bentobox.bentobox.api.localization;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class just to check that these constants don't accidentally change
|
||||||
|
* @author tastybento
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TextVariablesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
assertEquals("[name]", TextVariables.NAME);
|
||||||
|
assertEquals("[description]", TextVariables.DESCRIPTION);
|
||||||
|
assertEquals("[number]", TextVariables.NUMBER);
|
||||||
|
assertEquals("[rank]", TextVariables.RANK);
|
||||||
|
assertEquals("[label]", TextVariables.LABEL);
|
||||||
|
assertEquals("[permission]", TextVariables.PERMISSION);
|
||||||
|
assertEquals("[spawn_here]", TextVariables.SPAWN_HERE);
|
||||||
|
assertEquals("[version]", TextVariables.VERSION);
|
||||||
|
assertEquals("[start]", TextVariables.START_TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -124,6 +124,28 @@ public class LocalesManagerTest {
|
|||||||
assertNull(lm.get("test.test.test"));
|
assertNull(lm.get("test.test.test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#getOrDefault(java.lang.String, java.lang.String)}.
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetOrDefaultStringString() throws IOException {
|
||||||
|
makeFakeLocaleFile();
|
||||||
|
LocalesManager lm = new LocalesManager(plugin);
|
||||||
|
assertEquals("test string", lm.getOrDefault("test.test", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#getOrDefault(java.lang.String, java.lang.String)}.
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetOrDefaultStringStringFail() throws IOException {
|
||||||
|
makeFakeLocaleFile();
|
||||||
|
LocalesManager lm = new LocalesManager(plugin);
|
||||||
|
assertEquals("", lm.getOrDefault("test.test.test",""));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#get(world.bentobox.bentobox.api.user.User, java.lang.String)}.
|
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#get(world.bentobox.bentobox.api.user.User, java.lang.String)}.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
@ -149,6 +171,19 @@ public class LocalesManagerTest {
|
|||||||
assertEquals("test string", lm.get(user, "test.test"));
|
assertEquals("test string", lm.get(user, "test.test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#getOrDefault(world.bentobox.bentobox.api.user.User, java.lang.String, java.lang.String)}.
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetOrDefaultUserStringString() throws IOException {
|
||||||
|
makeFakeLocaleFile();
|
||||||
|
LocalesManager lm = new LocalesManager(plugin);
|
||||||
|
User user = mock(User.class);
|
||||||
|
when(user.getLocale()).thenReturn(Locale.US);
|
||||||
|
assertEquals("test string", lm.getOrDefault(user, "test.test", ""));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#get(world.bentobox.bentobox.api.user.User, java.lang.String)}.
|
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#get(world.bentobox.bentobox.api.user.User, java.lang.String)}.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
@ -175,6 +210,19 @@ public class LocalesManagerTest {
|
|||||||
assertNull(lm.get(user, "test.test.test"));
|
assertNull(lm.get(user, "test.test.test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#getOrDefault(world.bentobox.bentobox.api.user.User, java.lang.String, java.lang.String)}.
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetOrDefaultUserStringStringFail() throws IOException {
|
||||||
|
makeFakeLocaleFile();
|
||||||
|
LocalesManager lm = new LocalesManager(plugin);
|
||||||
|
User user = mock(User.class);
|
||||||
|
when(user.getLocale()).thenReturn(Locale.US);
|
||||||
|
assertEquals("", lm.getOrDefault(user, "test.test.test", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#getAvailableLocales(boolean)}.
|
* Test method for {@link world.bentobox.bentobox.managers.LocalesManager#getAvailableLocales(boolean)}.
|
||||||
|
Loading…
Reference in New Issue
Block a user