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,19 +236,24 @@ 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) {
return reference;
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
@ -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

@ -65,17 +65,17 @@ public class EnterExitListener extends FlagListener {
islandFrom.ifPresent(i -> {
// Fire the IslandExitEvent
new IslandEvent.IslandEventBuilder()
.island(i)
.involvedPlayer(user.getUniqueId())
.reason(IslandEvent.Reason.EXIT)
.admin(false)
.location(user.getLocation())
.build();
.island(i)
.involvedPlayer(user.getUniqueId())
.reason(IslandEvent.Reason.EXIT)
.admin(false)
.location(user.getLocation())
.build();
// Send message if island is owned by someone
if (i.getOwner() != null) {
user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, (i.getName() != null) ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner())));
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner())));
}
// Send message if island is unowned, but has a name
else if (i.getName() != null) {
@ -86,17 +86,17 @@ public class EnterExitListener extends FlagListener {
islandTo.ifPresent(i -> {
// Fire the IslandEnterEvent
new IslandEvent.IslandEventBuilder()
.island(i)
.involvedPlayer(user.getUniqueId())
.reason(IslandEvent.Reason.ENTER)
.admin(false)
.location(user.getLocation())
.build();
.island(i)
.involvedPlayer(user.getUniqueId())
.reason(IslandEvent.Reason.ENTER)
.admin(false)
.location(user.getLocation())
.build();
// Send message if island is owned by someone
if (i.getOwner() != null) {
user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, (i.getName() != null) ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner())));
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner())));
}
// Send message if island is unowned, but has a name
else if (i.getName() != null) {

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)));
}
/**