Skip to content

Commit

Permalink
Fixes TranslatableManager
Browse files Browse the repository at this point in the history
Adds TranslatablePH (%bloblibtranslatable_snippet_X% where X is the key of the TranslatableSnippet)
Currency now supports localization on display names
  • Loading branch information
anjoismysign committed Oct 8, 2023
1 parent 30e20de commit bae3c4c
Show file tree
Hide file tree
Showing 25 changed files with 296 additions and 75 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.0</version>
<version>1.697.3</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.0</version>
<version>1.697.3</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.0</version>
<version>1.697.3</version>
<packaging>pom</packaging>

<properties>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/us/mytheria/bloblib/BlobLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import us.mytheria.bloblib.hologram.HologramManager;
import us.mytheria.bloblib.managers.*;
import us.mytheria.bloblib.managers.fillermanager.FillerManager;
import us.mytheria.bloblib.placeholderapi.TranslatablePH;
import us.mytheria.bloblib.utilities.SerializationLib;
import us.mytheria.bloblib.vault.VaultManager;

Expand Down Expand Up @@ -44,6 +45,7 @@ public class BlobLib extends JavaPlugin {
private SerializationLib serializationLib;
private InventoryTrackerManager inventoryTrackerManager;
private TranslatableManager translatableManager;
private TranslatablePH translatablePH;

private static BlobLib instance;

Expand Down Expand Up @@ -104,8 +106,10 @@ public void onEnable() {
reload();
new BlobLibCmd();

Bukkit.getScheduler().runTask(this,
() -> disguiseManager.load());
Bukkit.getScheduler().runTask(this, () -> {
TranslatablePH.getInstance(this);
disguiseManager.load();
});
}

@Override
Expand All @@ -120,6 +124,7 @@ public void reload() {
configManager.reload();
listenerManager.reload();
soundManager.reload();
translatableManager.reload();
messageManager.reload();
actionManager.reload();
inventoryManager.reload();
Expand Down
42 changes: 38 additions & 4 deletions src/main/java/us/mytheria/bloblib/api/BlobLibTranslatableAPI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package us.mytheria.bloblib.api;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.BlobLib;
import us.mytheria.bloblib.entities.translatable.TranslatableBlock;
import us.mytheria.bloblib.entities.translatable.TranslatableSnippet;
import us.mytheria.bloblib.managers.TranslatableManager;

import java.util.Objects;


public class BlobLibTranslatableAPI {
private static BlobLibTranslatableAPI instance;
Expand Down Expand Up @@ -44,7 +48,8 @@ public TranslatableManager getTranslatableManager() {
* @return The TranslatableBlock
*/
@Nullable
public TranslatableBlock getTranslatableBlock(String key, String locale) {
public TranslatableBlock getTranslatableBlock(@NotNull String key,
@NotNull String locale) {
return getTranslatableManager().getBlock(key, locale);
}

Expand All @@ -55,10 +60,24 @@ public TranslatableBlock getTranslatableBlock(String key, String locale) {
* @return The TranslatableBlock
*/
@Nullable
public TranslatableBlock getTranslatableBlock(String key) {
public TranslatableBlock getTranslatableBlock(@NotNull String key) {
return getTranslatableManager().getBlock(key);
}

/**
* Will get a TranslatableBlock by its key and the player's locale.
*
* @param key The key of the translatable
* @param player The player to get the locale from
* @return The TranslatableBlock
*/
@Nullable
public TranslatableBlock getTranslatableBlock(@NotNull String key,
@NotNull Player player) {
Objects.requireNonNull(player);
return getTranslatableManager().getBlock(key, player.getLocale());
}

/**
* Will get a TranslatableSnippet by its key and locale.
* If locale is not available, it will return the default locale.
Expand All @@ -67,7 +86,8 @@ public TranslatableBlock getTranslatableBlock(String key) {
* @return The TranslatableSnippet
*/
@Nullable
public TranslatableSnippet getTranslatableSnippet(String key, String locale) {
public TranslatableSnippet getTranslatableSnippet(@NotNull String key,
@NotNull String locale) {
return getTranslatableManager().getSnippet(key, locale);
}

Expand All @@ -78,7 +98,21 @@ public TranslatableSnippet getTranslatableSnippet(String key, String locale) {
* @return The TranslatableSnippet
*/
@Nullable
public TranslatableSnippet getTranslatableSnippet(String key) {
public TranslatableSnippet getTranslatableSnippet(@NotNull String key) {
return getTranslatableManager().getSnippet(key);
}

/**
* Will get a TranslatableSnippet by its key and the player's locale.
*
* @param key The key of the translatable
* @param player The player to get the locale from
* @return The TranslatableSnippet
*/
@Nullable
public TranslatableSnippet getTranslatableSnippet(@NotNull String key,
@NotNull Player player) {
Objects.requireNonNull(player);
return getTranslatableManager().getSnippet(key, player.getLocale());
}
}
26 changes: 18 additions & 8 deletions src/main/java/us/mytheria/bloblib/entities/BlobPHExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

public class BlobPHExpansion extends PlaceholderExpansion {
private final JavaPlugin plugin;
private final String identifier;

private final Map<String, Function<OfflinePlayer, String>> simple;
private final Map<String, Function<OfflinePlayer, String>> startsWith;
private final Map<String, BiFunction<OfflinePlayer, String, String>> startsWith;

public BlobPHExpansion(JavaPlugin plugin, String identifier) {
this.plugin = plugin;
Expand All @@ -37,7 +38,7 @@ public Function<OfflinePlayer, String> putSimple(String key, Function<OfflinePla
/**
* @return the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)
*/
public Function<OfflinePlayer, String> putStartsWith(String key, Function<OfflinePlayer, String> function) {
public BiFunction<OfflinePlayer, String, String> putStartsWith(String key, BiFunction<OfflinePlayer, String, String> function) {
return startsWith.put(key, function);
}

Expand Down Expand Up @@ -69,11 +70,20 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
Function<OfflinePlayer, String> simpleFunction = simple.get(identifier);
if (simpleFunction != null)
return simpleFunction.apply(player);
return startsWith.entrySet().stream()
.filter(entry -> identifier.startsWith(entry.getKey()))
.filter(entry -> entry.getValue().apply(player) != null)
.findFirst()
.map(entry -> entry.getValue().apply(player))
.orElse(null);
String dynamicKey;
for (Map.Entry<String, BiFunction<OfflinePlayer, String, String>> entry : startsWith.entrySet()) {
if (identifier.startsWith(entry.getKey())) {
dynamicKey = dynamicKey(entry.getKey(), identifier);
if (entry.getValue().apply(player, dynamicKey) != null) {
return entry.getValue().apply(player, dynamicKey);
}
}
}
return null;
}

private String dynamicKey(String key, String identifier) {
int keyLength = key.length();
return identifier.substring(keyLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public boolean command(ExecutorData data) {
Player player = context.player();
walletOwner.deposit(currency, amount);
BlobLibMessageAPI.getInstance().getMessage("Economy.Deposit").modify(s -> s.replace("%display%", currency.display(amount))
.replace("%currency%", currency.getDisplayName())
.replace("%currency%", currency.getDisplayName(player))
.replace("%player%", player.getName())).toCommandSender(sender);
return true;
}
Expand All @@ -157,13 +157,13 @@ public boolean command(ExecutorData data) {
if (!walletOwner.has(currency, amount)) {
double missing = amount - walletOwner.getBalance(currency);
BlobLibMessageAPI.getInstance().getMessage("Economy.Cannot-Bankrupt-Others").modify(s -> s.replace("%display%", currency.display(missing))
.replace("%currency%", currency.getDisplayName())
.replace("%currency%", currency.getDisplayName(player))
.replace("%player%", player.getName())).toCommandSender(sender);
return true;
}
walletOwner.withdraw(currency, amount);
BlobLibMessageAPI.getInstance().getMessage("Economy.Withdraw").modify(s -> s.replace("%display%", currency.display(amount))
.replace("%currency%", currency.getDisplayName())
.replace("%currency%", currency.getDisplayName(player))
.replace("%player%", player.getName())).toCommandSender(sender);
return true;
}
Expand All @@ -178,7 +178,7 @@ public boolean command(ExecutorData data) {
Player player = context.player();
context.walletOwner().setBalance(currency, amount);
BlobLibMessageAPI.getInstance().getMessage("Economy.Set").modify(s -> s.replace("%display%", currency.display(amount))
.replace("%currency%", currency.getDisplayName())
.replace("%currency%", currency.getDisplayName(player))
.replace("%player%", player.getName())).toCommandSender(sender);
return true;
}
Expand All @@ -192,7 +192,7 @@ public boolean command(ExecutorData data) {
Player player = context.player();
context.walletOwner().reset(currency);
BlobLibMessageAPI.getInstance().getMessage("Economy.Reset").modify(s -> s
.replace("%currency%", currency.getDisplayName())
.replace("%currency%", currency.getDisplayName(player))
.replace("%player%", player.getName())).toCommandSender(sender);
return true;
}
Expand Down
58 changes: 50 additions & 8 deletions src/main/java/us/mytheria/bloblib/entities/currency/Currency.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
Expand All @@ -25,7 +26,8 @@
import java.util.*;

public class Currency implements BlobObject {
private final String display, key, displayName;
private final String display, key;
private final Map<String, String> displayNames;
private final double initialBalance;
private final boolean isPersistent;
private final DecimalFormat decimalFormat;
Expand All @@ -45,11 +47,11 @@ public class Currency implements BlobObject {
public Currency(String display, double initialBalance, boolean isPersistent,
String pattern, String key, boolean isTangible,
@Nullable Map<String, ItemStack> tangibleShapes,
BlobPlugin plugin, String displayName) {
BlobPlugin plugin, Map<String, String> displayNames) {
this.plugin = plugin;
this.display = display;
this.key = key;
this.displayName = displayName;
this.displayNames = displayNames;
this.initialBalance = initialBalance;
this.isPersistent = isPersistent;
this.decimalFormat = new DecimalFormat(pattern);
Expand Down Expand Up @@ -180,11 +182,40 @@ private String getDisplay() {

/**
* Returns how its name is displayed in the game.
* Will use the locale to get the name.
*
* @param locale the to get the locale from
* @return the display name
*/
@NotNull
public String getDisplayName(@NotNull String locale) {
String result = displayNames.get(locale);
if (result == null)
result = displayNames.get("en_us");
Objects.requireNonNull(result, "Default Display-Name for locale '" + locale + "' is missing");
return result;
}

/**
* Returns how its name is displayed in the game.
* Will use the player's locale to get the name.
*
* @param player the to get the locale from
* @return the display name
*/
@NotNull
public String getDisplayName(@NotNull Player player) {
return getDisplayName(player.getLocale());
}

/**
* Returns how its name is displayed in the game.
* Will use the default locale to get the name.
*
* @return the display name
*/
public String getDisplayName() {
return displayName;
return displayNames.get("en_us");
}

/**
Expand Down Expand Up @@ -254,9 +285,20 @@ public static Currency fromFile(File file, ManagerDirector director) {
boolean isPersistent = yamlConfiguration.getBoolean("Persistent");
String pattern = yamlConfiguration.getString("Decimal-Format");
String key = FilenameUtils.removeExtension(fileName);
String displayName = key;
if (yamlConfiguration.isString("Display-Name"))
displayName = TextColor.PARSE(yamlConfiguration.getString("Display-Name"));
Map<String, String> displayNames = new HashMap<>();
if (!yamlConfiguration.isConfigurationSection("Display-Name"))
displayNames.put("en_us", key);
else {
ConfigurationSection namesSection = yamlConfiguration.getConfigurationSection("Display-Name");
namesSection.getKeys(false).forEach(locale -> {
if (!namesSection.isString(locale))
return;
String value = namesSection.getString(locale);
displayNames.put(locale, value);
});
if (!displayNames.containsKey("en_us"))
displayNames.put("en_us", key);
}
boolean isTangible = yamlConfiguration.getBoolean("Is-Tangible", false);
final Map<String, ItemStack> tangibleShape = new HashMap<>();
if (isTangible) {
Expand Down Expand Up @@ -284,6 +326,6 @@ public static Currency fromFile(File file, ManagerDirector director) {
});
}
return new Currency(display, initialBalance, isPersistent, pattern, key,
isTangible, tangibleShape, director.getPlugin(), displayName);
isTangible, tangibleShape, director.getPlugin(), displayNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import us.mytheria.bloblib.entities.inventory.ObjectBuilderButtonBuilder;
import us.mytheria.bloblib.entities.message.BlobSound;

import java.util.Map;
import java.util.UUID;

public class CurrencyBuilder extends ObjectBuilder<Currency> {
Expand Down Expand Up @@ -76,6 +77,6 @@ public Currency construct() {
return new Currency(display, initialBalanceValue,
true, decimalFormatPattern, key,
false, null, getObjectDirector().getPlugin(),
key);
Map.of("en_us", key));
}
}
Loading

0 comments on commit bae3c4c

Please sign in to comment.