Adds ability for gamemodes to override locale references

The default BentoBox locale text is geared towards island games. When
writing the SkyGrid addon I am using the standard protection flags, but
I need to reference a "protection area" instead of an "island". This
enables me to do that by having the same reference but prefixed in the
locale file by the name of the addon (in lower case), as written in the
addon's addon.yml.
This commit is contained in:
tastybento 2019-01-15 13:23:36 -08:00
parent 3a9e969a86
commit e56928bef1
3 changed files with 29 additions and 23 deletions

View File

@ -236,20 +236,25 @@ public class User {
}
/**
* Gets a translation of this reference for this user.
* Gets a translation of this reference for this user. Translations may be overridden by Addons
* by using the same reference prefixed by the addon name (from the Addon Description) in lower case.
* @param reference - reference found in a locale file
* @param variables - variables to insert into translated string. Variables go in pairs, for example
* "[name]", "tastybento"
* @return Translated string with colors converted, or the reference if nothing has been found
*/
public String getTranslation(String reference, String... variables) {
// Get translation
String translation = plugin.getLocalesManager().get(this, reference);
// Get translation.
String addonPrefix = plugin.getIWM().getAddon(getWorld()).map(a -> a.getDescription().getName().toLowerCase() + ".").orElse("");
String translation = plugin.getLocalesManager().get(addonPrefix + reference);
// If no translation has been found, return the reference for debug purposes.
if (translation == null) {
translation = plugin.getLocalesManager().get(reference);
if (translation == null) {
// If no translation has been found, return the reference for debug purposes.
return reference;
}
}
// Then replace variables
if (variables.length > 1) {
@ -327,10 +332,10 @@ public class User {
/**
* Gets the current world this entity resides in
* @return World
* @return World - world or null
*/
public World getWorld() {
return player.getWorld();
return player == null ? null : player.getWorld();
}
/**

View File

@ -15,6 +15,7 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
@ -549,8 +550,8 @@ public class IslandWorldManager {
* - world
* @return GameModeAddon, or empty
*/
public Optional<GameModeAddon> getAddon(World world) {
return Optional.ofNullable(gameModes.get(Util.getWorld(world)));
public Optional<GameModeAddon> getAddon(@Nullable World world) {
return world == null ? Optional.empty() : Optional.ofNullable(gameModes.get(Util.getWorld(world)));
}
/**