Skip to content

Commit

Permalink
Better formatting for purchase cost #143
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 19, 2025
1 parent 6e51f1e commit 5374994
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package world.bentobox.magiccobblestonegenerator.managers;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -1142,6 +1144,7 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
@NotNull GeneratorDataObject generatorData, @NotNull GeneratorTierObject generatorTier) {

final User owner = island.isSpawn() || island.getOwner() == null ? null : User.getInstance(island.getOwner());
NumberFormat numberFormat = NumberFormat.getNumberInstance(owner.getLocale());

if (generatorData.getPurchasedTiers().contains(generatorTier.getUniqueId())) {
// Generator is not unlocked. Return false.
Expand All @@ -1157,7 +1160,7 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "island-level-not-reached", Constants.GENERATOR,
generatorTier.getFriendlyName(), TextVariables.NUMBER,
String.valueOf(generatorTier.getRequiredMinIslandLevel())));
numberFormat.format(generatorTier.getRequiredMinIslandLevel())));
return false;
} else if (!generatorTier.getRequiredPermissions().isEmpty() && (owner == null || !owner.isPlayer()
|| !generatorTier.getRequiredPermissions().stream().allMatch(owner::hasPermission))) {
Expand All @@ -1178,8 +1181,9 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
// Return true only if user has enough money.
return true;
} else {

Utils.sendMessage(user, user.getTranslation(Constants.MESSAGES + "no-credits-buy-bank",
TextVariables.NUMBER, String.valueOf(generatorTier.getGeneratorTierCost())));
TextVariables.NUMBER, numberFormat.format(generatorTier.getGeneratorTierCost())));
return false;
}
} else {
Expand All @@ -1188,7 +1192,7 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
return true;
} else {
Utils.sendMessage(user, user.getTranslation(Constants.MESSAGES + "no-credits-buy",
TextVariables.NUMBER, String.valueOf(generatorTier.getGeneratorTierCost())));
TextVariables.NUMBER, numberFormat.format(generatorTier.getGeneratorTierCost())));
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,32 +554,33 @@ private String generateStatusDescription(GeneratorTierObject generator,
final String reference = Constants.DESCRIPTIONS + "generator.status.";

StringBuilder status = new StringBuilder();
NumberFormat numberFormat = NumberFormat.getNumberInstance(user.getLocale());

if (!isPurchased && this.addon.isVaultProvided() && generator.getGeneratorTierCost() > 0)
{
status.append(this.user.getTranslationOrNothing(reference + "purchase-cost",
Constants.NUMBER, String.valueOf(generator.getGeneratorTierCost())));
Constants.NUMBER, numberFormat.format(generator.getGeneratorTierCost())));
}

if (!isActive && this.addon.isVaultProvided() && generator.getActivationCost() > 0)
{
status.append("\n");
status.append(this.user.getTranslationOrNothing(reference + "activation-cost",
Constants.NUMBER, String.valueOf(generator.getActivationCost())));
Constants.NUMBER, numberFormat.format(generator.getActivationCost())));
}

if (isActive)
{
status.append("\n");
status.append(this.user.getTranslationOrNothing(reference + "active",
Constants.NUMBER, String.valueOf(generator.getGeneratorTierCost())));
Constants.NUMBER, numberFormat.format(generator.getGeneratorTierCost())));
}

if (!isUnlocked)
{
status.append("\n");
status.append(this.user.getTranslationOrNothing(reference + "locked",
Constants.NUMBER, String.valueOf(generator.getGeneratorTierCost())));
Constants.NUMBER, numberFormat.format(generator.getGeneratorTierCost())));
}

if (!generator.isDeployed())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package world.bentobox.magiccobblestonegenerator.panels.admin;


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Consumer;
Expand All @@ -25,8 +27,8 @@
import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorTierObject;
import world.bentobox.magiccobblestonegenerator.panels.CommonPanel;
import world.bentobox.magiccobblestonegenerator.panels.ConversationUtils;
import world.bentobox.magiccobblestonegenerator.panels.utils.MultiBiomeSelector;
import world.bentobox.magiccobblestonegenerator.panels.utils.GeneratorTypeSelector;
import world.bentobox.magiccobblestonegenerator.panels.utils.MultiBiomeSelector;
import world.bentobox.magiccobblestonegenerator.panels.utils.SingleBlockSelector;
import world.bentobox.magiccobblestonegenerator.utils.Constants;
import world.bentobox.magiccobblestonegenerator.utils.Pair;
Expand Down Expand Up @@ -90,7 +92,7 @@ public void build()

switch (this.activeTab) {
case INFO -> {
this.populateInfo(panelBuilder);
this.populateInfo(panelBuilder, user.getLocale());

// Add listener that allows to change icons
panelBuilder.listener(new IconChanger());
Expand Down Expand Up @@ -147,54 +149,55 @@ private void populateHeader(PanelBuilder panelBuilder)
* This method populates panel body with info blocks.
*
* @param panelBuilder PanelBuilder that must be created.
* @param locale
*/
private void populateInfo(PanelBuilder panelBuilder)
private void populateInfo(PanelBuilder panelBuilder, Locale locale)
{
panelBuilder.item(10, this.createButton(Button.NAME));
panelBuilder.item(11, this.createButton(Button.ID));
panelBuilder.item(19, this.createButton(Button.ICON));
panelBuilder.item(28, this.createButton(Button.DESCRIPTION));
panelBuilder.item(10, this.createButton(Button.NAME, locale));
panelBuilder.item(11, this.createButton(Button.ID, locale));
panelBuilder.item(19, this.createButton(Button.ICON, locale));
panelBuilder.item(28, this.createButton(Button.DESCRIPTION, locale));

// Add locked icon
panelBuilder.item(20, this.createButton(Button.LOCKED_ICON));
panelBuilder.item(20, this.createButton(Button.LOCKED_ICON, locale));

// Usefull information to know about generators.
panelBuilder.item(12, this.createButton(Button.DEFAULT));
panelBuilder.item(21, this.createButton(Button.PRIORITY));
panelBuilder.item(30, this.createButton(Button.TYPE));
panelBuilder.item(12, this.createButton(Button.DEFAULT, locale));
panelBuilder.item(21, this.createButton(Button.PRIORITY, locale));
panelBuilder.item(30, this.createButton(Button.TYPE, locale));

// Default genertator do not have requirements.
if (!this.generatorTier.isDefaultGenerator())
{
if (this.addon.isLevelProvided())
{
panelBuilder.item(13, this.createButton(Button.REQUIRED_MIN_LEVEL));
panelBuilder.item(13, this.createButton(Button.REQUIRED_MIN_LEVEL, locale));
}

// Display only permissions if they are required.
panelBuilder.item(22, this.createButton(Button.REQUIRED_PERMISSIONS));
panelBuilder.item(22, this.createButton(Button.REQUIRED_PERMISSIONS, locale));

if (this.addon.isVaultProvided())
{
// Display cost only if there exist vault.
panelBuilder.item(31, this.createButton(Button.PURCHASE_COST));
panelBuilder.item(31, this.createButton(Button.PURCHASE_COST, locale));
}
}

// If vault is disabled.
if (this.addon.isVaultProvided())
{
panelBuilder.item(15, this.createButton(Button.ACTIVATION_COST));
panelBuilder.item(15, this.createButton(Button.ACTIVATION_COST, locale));
}

panelBuilder.item(24, this.createButton(Button.BIOMES));
panelBuilder.item(24, this.createButton(Button.BIOMES, locale));

// deployed button.
panelBuilder.item(33, this.createButton(Button.DEPLOYED));
panelBuilder.item(33, this.createButton(Button.DEPLOYED, locale));

// display treasures.
panelBuilder.item(25, this.createButton(Button.TREASURE_CHANCE));
panelBuilder.item(34, this.createButton(Button.TREASURE_AMOUNT));
panelBuilder.item(25, this.createButton(Button.TREASURE_CHANCE, locale));
panelBuilder.item(34, this.createButton(Button.TREASURE_AMOUNT, locale));
}


Expand Down Expand Up @@ -323,9 +326,10 @@ else if (this.pageIndex > (this.treasureChanceList.size() / MAX_ELEMENTS))
* This method creates panel item for given button type.
*
* @param button Button type.
* @param locale locale of user requesting the button
* @return Clickable PanelItem button.
*/
private PanelItem createButton(Button button)
private PanelItem createButton(Button button, Locale locale)
{
final String reference = Constants.BUTTON + button.name().toLowerCase();
String name = this.user.getTranslation(reference + ".name");
Expand Down Expand Up @@ -623,10 +627,12 @@ private PanelItem createButton(Button button)
}
}
case PURCHASE_COST -> {
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);

itemStack = new ItemStack(Material.GOLD_BLOCK);

description.add(this.user.getTranslation(reference + ".value",
Constants.NUMBER, String.valueOf(this.generatorTier.getGeneratorTierCost())));
Constants.NUMBER, numberFormat.format(this.generatorTier.getGeneratorTierCost())));

clickHandler = (panel, user, clickType, i) ->
{
Expand Down Expand Up @@ -1317,7 +1323,7 @@ public void onInventoryClick(User user, InventoryClickEvent event)
GeneratorEditPanel.this.selectedButton = null;
// Rebuild icon
event.getInventory().setItem(19,
GeneratorEditPanel.this.createButton(Button.ICON).getItem());
GeneratorEditPanel.this.createButton(Button.ICON, user.getLocale()).getItem());
}
else
{
Expand All @@ -1327,7 +1333,7 @@ public void onInventoryClick(User user, InventoryClickEvent event)
GeneratorEditPanel.this.selectedButton = null;
// Rebuild icon
event.getInventory().setItem(20,
GeneratorEditPanel.this.createButton(Button.LOCKED_ICON).getItem());
GeneratorEditPanel.this.createButton(Button.LOCKED_ICON, user.getLocale()).getItem());
}

// save change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package world.bentobox.magiccobblestonegenerator.utils;


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -808,7 +809,7 @@ public static void sendUnlockMessage(UUID uuid,
boolean available)
{
User user = User.getInstance(uuid);

NumberFormat numberFormat = NumberFormat.getNumberInstance(user.getLocale());
WorldSettings settings = addon.getPlugin().getIWM().getWorldSettings(island.getWorld());

if (settings != null && user != null && user.isOnline())
Expand All @@ -826,7 +827,7 @@ public static void sendUnlockMessage(UUID uuid,
{
component = new TextComponent(user.getTranslation(Constants.CONVERSATIONS + "click-text-to-purchase",
Constants.GENERATOR, generator.getFriendlyName(),
Constants.NUMBER, String.valueOf(generator.getGeneratorTierCost())));
Constants.NUMBER, numberFormat.format(generator.getGeneratorTierCost())));

commandBuilder.append(addon.getSettings().getPlayerBuyCommand().split(" ")[0]);
}
Expand All @@ -837,7 +838,7 @@ public static void sendUnlockMessage(UUID uuid,
component =
new TextComponent(user.getTranslation(Constants.CONVERSATIONS + "click-text-to-activate-vault",
Constants.GENERATOR, generator.getFriendlyName(),
Constants.NUMBER, String.valueOf(generator.getActivationCost())));
Constants.NUMBER, numberFormat.format(generator.getActivationCost())));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -56,6 +57,7 @@
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon;
import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorBundleObject;
Expand Down Expand Up @@ -105,6 +107,8 @@ public class StoneGeneratorManagerTest {
private PluginManager pim;
@Mock
private RanksManager rm;
@Mock
private PlayersManager pm;

@SuppressWarnings("unchecked")
@BeforeClass
Expand Down Expand Up @@ -149,6 +153,10 @@ public void setUp() throws Exception {
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(addon.getPlugin()).thenReturn(plugin);

// Players manager & locale
when(pm.getLocale(any())).thenReturn("en-US");
when(plugin.getPlayers()).thenReturn(pm);

// The database type has to be created one line before the thenReturn() to work!
DatabaseType value = DatabaseType.JSON;
when(plugin.getSettings()).thenReturn(settings);
Expand All @@ -161,6 +169,7 @@ public void setUp() throws Exception {
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.getLocale()).thenReturn(Locale.ENGLISH);
User.setPlugin(plugin);

// Generator Tier
Expand Down

0 comments on commit 5374994

Please sign in to comment.