Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Translations class #14

Merged
merged 5 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 19 additions & 43 deletions fabric/src/main/java/stonks/fabric/StonksFabricHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@
*/
package stonks.fabric;

import java.util.Optional;

import nahara.common.tasks.Task;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import stonks.core.market.Offer;
import stonks.core.market.OfferType;
import stonks.core.product.Product;
import stonks.fabric.menu.MenuText;
import stonks.fabric.translation.Translations;

public class StonksFabricHelper {
public static Task<Void> instantOffer(ServerPlayerEntity player, Product product, OfferType type, int units, double balance) {
Expand All @@ -44,21 +40,19 @@ public static Task<Void> instantOffer(ServerPlayerEntity player, Product product
provider.getStonksAdapter().removeUnitsFrom(player, product, units);
}

player.sendMessage(MenuText.message$pleaseWait, true);
player.sendMessage(Translations.Messages.PleaseWait, true);
var task = provider.getStonksService().instantOffer(product, type, units, balance);

provider.getTasksHandler()
.handle(task, (result, error) -> {
if (error != null) {
player.sendMessage(MenuText.message$errorRefunding, true);
player.sendMessage(Translations.Messages.ErrorRefunding, true);
if (type == OfferType.BUY) provider.getStonksAdapter().accountDeposit(player, balance);
else provider.getStonksAdapter().addUnitsTo(player, product, units);
error.printStackTrace();
return;
}

var productNameText = Text.literal(product.getProductName()).styled(s -> s.withColor(Formatting.AQUA));

if (type == OfferType.BUY) {
var unitsLeft = result.units();
var unitsBought = units - unitsLeft;
Expand All @@ -68,16 +62,10 @@ public static Task<Void> instantOffer(ServerPlayerEntity player, Product product
provider.getStonksAdapter().accountDeposit(player, moneyLeft);
provider.getStonksAdapter().addUnitsTo(player, product, unitsBought);

var amountText = Text.literal(Integer.toString(unitsBought))
.styled(s -> s.withColor(Formatting.AQUA));
var moneySpentText = StonksFabricUtils.currencyText(Optional.of(moneySpent), true);
var unitsLeftText = Text.literal(Integer.toString(unitsLeft))
.styled(s -> s.withColor(Formatting.AQUA));
var text = unitsLeft == 0
? MenuText.messages$bought(amountText, productNameText, moneySpentText)
: MenuText.messages$boughtWithExtras(amountText, productNameText, moneySpentText,
unitsLeftText);
player.sendMessage(text, true);
player.sendMessage(unitsLeft == 0
? Translations.Messages.Bought(unitsBought, product, moneySpent)
: Translations.Messages.BoughtWithExtras(unitsBought, product, moneySpent, unitsLeft),
true);
} else {
var config = StonksFabric.getServiceProvider(player).getPlatformConfig();
var unitsLeft = result.units();
Expand All @@ -87,16 +75,10 @@ public static Task<Void> instantOffer(ServerPlayerEntity player, Product product
provider.getStonksAdapter().accountDeposit(player, earnings);
provider.getStonksAdapter().addUnitsTo(player, product, unitsLeft);

var amountText = Text.literal(Integer.toString(unitsSold))
.styled(s -> s.withColor(Formatting.AQUA));
var moneyReceivedText = StonksFabricUtils.currencyText(Optional.of(earnings), true);
var unitsLeftText = Text.literal(Integer.toString(unitsLeft))
.styled(s -> s.withColor(Formatting.AQUA));
var text = unitsLeft == 0
? MenuText.messages$sold(amountText, productNameText, moneyReceivedText)
: MenuText.messages$soldWithExtras(amountText, productNameText, moneyReceivedText,
unitsLeftText);
player.sendMessage(text, true);
player.sendMessage(unitsLeft == 0
? Translations.Messages.Sold(unitsSold, product, earnings)
: Translations.Messages.SoldWithExtras(unitsSold, product, earnings, unitsLeft),
true);
}
});

Expand All @@ -112,7 +94,7 @@ public static void placeOffer(ServerPlayerEntity player, Product product, OfferT
var balance = adapter.accountBalance(player);

if (balance < totalPrice) {
player.sendMessage(MenuText.messages$notEnoughMoney(balance, totalPrice), true);
player.sendMessage(Translations.Messages.NotEnoughMoney(balance, totalPrice), true);
return;
}

Expand All @@ -126,19 +108,19 @@ public static void placeOffer(ServerPlayerEntity player, Product product, OfferT
}

if (currentUnits < units) {
player.sendMessage(MenuText.messages$notEnoughItems(currentUnits, units));
player.sendMessage(Translations.Messages.NotEnoughItems(currentUnits, units));
return;
}

adapter.removeUnitsFrom(player, product, units);
}

player.sendMessage(MenuText.message$pleaseWait, true);
player.sendMessage(Translations.Messages.PleaseWait, true);
provider.getTasksHandler()
.handle(provider.getStonksService().listOffer(player.getUuid(), product, type, units, pricePerUnit),
(offer, error) -> {
if (error != null) {
player.sendMessage(MenuText.message$errorRefunding, true);
player.sendMessage(Translations.Messages.ErrorRefunding, true);

if (type == OfferType.BUY) {
adapter.accountDeposit(player, totalPrice);
Expand All @@ -150,15 +132,9 @@ public static void placeOffer(ServerPlayerEntity player, Product product, OfferT
return;
}

var unitsText = Text.literal(Integer.toString(units)).styled(s -> s.withColor(Formatting.AQUA));
var productNameText = Text.literal(product.getProductName())
.styled(s -> s.withColor(Formatting.AQUA));
var totalPriceText = StonksFabricUtils.currencyText(Optional.of(totalPrice), true);
var pricePerUnitText = StonksFabricUtils.currencyText(Optional.of(pricePerUnit), true);
player.sendMessage(offer.getType() == OfferType.BUY
? MenuText.messages$placedBuyOffer(unitsText, productNameText, totalPriceText, pricePerUnitText)
: MenuText.messages$placedSellOffer(unitsText, productNameText, totalPriceText,
pricePerUnitText),
? Translations.Messages.PlacedBuyOffer(units, product, totalPrice, pricePerUnit)
: Translations.Messages.PlacedSellOffer(units, product, totalPrice, pricePerUnit),
true);
});
}
Expand All @@ -167,7 +143,7 @@ public static void sendOfferFilledMessage(MinecraftServer server, Offer filledOf
var player = server.getPlayerManager().getPlayer(filledOffer.getOffererId());
if (player == null) return;
player.sendMessage(filledOffer.getType() == OfferType.BUY
? MenuText.messages$buyOfferFilled(filledOffer)
: MenuText.messages$sellOfferFilled(filledOffer));
? Translations.Messages.BuyOfferFilled(filledOffer)
: Translations.Messages.SellOfferFilled(filledOffer));
}
}
20 changes: 9 additions & 11 deletions fabric/src/main/java/stonks/fabric/StonksFabricUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import net.minecraft.util.Formatting;
import stonks.core.market.OfferType;
import stonks.core.market.OverviewOffer;
import stonks.fabric.menu.MenuText;
import stonks.fabric.translation.Translations;

public class StonksFabricUtils {
public static Text progressBar(int width, Formatting background, double[] progress, Formatting[] colors) {
Expand Down Expand Up @@ -76,8 +76,10 @@ public static String createStringFor(ItemStack stack, MinecraftServer server) {
public static final DecimalFormat TAX_FORMATTER = new DecimalFormat("#,##0.##%");

public static Text currencyText(Optional<Double> v, boolean fullNotAvailable) {
if (v.isEmpty()) return fullNotAvailable ? MenuText.messages$notAvailable : MenuText.messages$notAvailableShort;
return MenuText.messages$currency(v.get());
if (v.isEmpty()) return fullNotAvailable
? Translations.Messages.NotAvailable
: Translations.Messages.NotAvailableShort;
return Translations.Messages.Currency(v.get());
}

public static Optional<Text> taxText(double tax) {
Expand All @@ -87,14 +89,10 @@ public static Optional<Text> taxText(double tax) {

public static Text offerText(OfferType type, OverviewOffer offer) {
var typeText = type == OfferType.BUY
? MenuText.messages$offerInfoText$buy
: MenuText.messages$offerInfoText$sell;
var totalAvailableUnits = Text.literal(Integer.toString(offer.totalAvailableUnits()))
.styled(s -> s.withColor(Formatting.AQUA));
var offersCountText = Text.literal(Integer.toString(offer.offers())).styled(s -> s.withColor(Formatting.AQUA));
var ppuText = currencyText(Optional.of(offer.pricePerUnit()), false);

return MenuText.messages$offerInfoText(typeText, totalAvailableUnits, offersCountText, ppuText);
? Translations.Messages.OfferInfoText$Buy
: Translations.Messages.OfferInfoText$Sell;
return Translations.Messages.OfferInfoText(typeText, offer.totalAvailableUnits(), offer.offers(),
offer.pricePerUnit());
}

public static boolean compareStack(ItemStack a, ItemStack b) {
Expand Down
37 changes: 19 additions & 18 deletions fabric/src/main/java/stonks/fabric/menu/MarketMainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import stonks.fabric.StonksFabric;
import stonks.fabric.menu.handling.WaitableGuiElement;
import stonks.fabric.menu.product.ProductMenu;
import stonks.fabric.translation.Translations;

public class MarketMainMenu extends StackedMenu {
private static final int CATEGORIES_PER_PAGE = 5;
Expand All @@ -51,7 +52,7 @@ public class MarketMainMenu extends StackedMenu {

public MarketMainMenu(StackedMenu previous, ServerPlayerEntity player) {
super(previous, ScreenHandlerType.GENERIC_9X6, player, false);
setTitle(MenuText.menus$mainMenu);
setTitle(Translations.Menus.MainMenu.MainMenu);

for (int i = 0; i < getHeight() - 1; i++) {
var slot = (i + 1) * getWidth();
Expand All @@ -68,8 +69,8 @@ public MarketMainMenu(StackedMenu previous, ServerPlayerEntity player) {
(categories, error) -> {
if (error != null) {
var icon = new GuiElementBuilder(Items.BARRIER)
.setName(MenuText.errors)
.addLoreLine(MenuText.errors$categoriesList);
.setName(Translations.Errors.Errors)
.addLoreLine(Translations.Errors.CategoriesList);

for (int i = 0; i < getHeight() - 1; i++) {
var slot = (i + 1) * getWidth();
Expand Down Expand Up @@ -104,8 +105,8 @@ protected void placePagesNavigations(List<Category> categories) {
setSlot(10, categories == null || categoriesPage <= 0
? MenuIcons.BORDER
: new GuiElementBuilder(Items.RED_STAINED_GLASS_PANE, Math.max(Math.min(categoriesPage, 64), 1))
.setName(MenuText.icons$scrollUp)
.addLoreLine(MenuText.icons$scrollUp$0(categoriesPage, categoriesMaxPages))
.setName(Translations.Icons.ScrollUp)
.addLoreLine(Translations.Icons.ScrollUp$0(categoriesPage, categoriesMaxPages))
.setCallback((index, type, action, gui) -> {
if (categoriesPage <= 0 || categories == null) return;
categoriesPage--;
Expand All @@ -115,8 +116,8 @@ protected void placePagesNavigations(List<Category> categories) {
setSlot(46, categories == null || categoriesPage >= (categoriesMaxPages - 1)
? MenuIcons.BORDER
: new GuiElementBuilder(Items.YELLOW_STAINED_GLASS_PANE, Math.max(Math.min(categoriesPage + 2, 64), 1))
.setName(MenuText.icons$scrollDown)
.addLoreLine(MenuText.icons$scrollDown$0(categoriesPage, categoriesMaxPages))
.setName(Translations.Icons.ScrollDown)
.addLoreLine(Translations.Icons.ScrollDown$0(categoriesPage, categoriesMaxPages))
.setCallback((index, type, action, gui) -> {
if (categoriesPage >= (categoriesMaxPages - 1) || categories == null) return;
categoriesPage++;
Expand All @@ -128,8 +129,8 @@ protected void placePagesNavigations(List<Category> categories) {
setSlot(2, categories == null || productsPage <= 0
? MenuIcons.BORDER
: new GuiElementBuilder(Items.ARROW, Math.max(Math.min(productsPage, 64), 1))
.setName(MenuText.icons$previousPage)
.addLoreLine(MenuText.icons$previousPage$0(productsPage, productsMaxPage))
.setName(Translations.Icons.PreviousPage)
.addLoreLine(Translations.Icons.PreviousPage$0(productsPage, productsMaxPage))
.setCallback((index, type, action, gui) -> {
if (productsPage <= 0 || categories == null) return;
productsPage--;
Expand All @@ -139,8 +140,8 @@ protected void placePagesNavigations(List<Category> categories) {
setSlot(6, categories == null || productsPage >= (productsMaxPage - 1)
? MenuIcons.BORDER
: new GuiElementBuilder(Items.ARROW, Math.max(Math.min(productsPage + 2, 64), 1))
.setName(MenuText.icons$nextPage)
.addLoreLine(MenuText.icons$nextPage$0(productsPage, productsMaxPage))
.setName(Translations.Icons.NextPage)
.addLoreLine(Translations.Icons.NextPage$0(productsPage, productsMaxPage))
.setCallback((index, type, action, gui) -> {
if (productsPage >= (productsMaxPage - 1) || categories == null) return;
productsPage++;
Expand All @@ -167,8 +168,8 @@ private void placeCategories(List<Category> categories) {
.setName(Text.literal(category.getCategoryName())
.styled(s -> s.withColor(Formatting.AQUA)))
.addLoreLine(selected
? MenuText.menus$mainMenu$category$selected
: MenuText.menus$mainMenu$category$unselected)
? Translations.Menus.MainMenu.Category$Selected
: Translations.Menus.MainMenu.Category$Unselected)
.setCallback((index, type, action, gui) -> {
selectedCategoryIndex = currentCategoryIndex;
productsPage = 0;
Expand Down Expand Up @@ -213,8 +214,8 @@ private void placeProduct(int slot, StonksServiceCache cache, Category category,
public ItemStack createStackWhenLoaded(ProductMarketOverview overview, Throwable error) {
// TODO use message from UserException
if (error != null) return new GuiElementBuilder(Items.BARRIER)
.setName(MenuText.errors)
.addLoreLine(MenuText.errors$quickPriceDetails)
.setName(Translations.Errors.Errors)
.addLoreLine(Translations.Errors.QuickPriceDetails)
.asStack();

// TODO should we use avg price?
Expand All @@ -226,10 +227,10 @@ public ItemStack createStackWhenLoaded(ProductMarketOverview overview, Throwable
.addLoreLine(Text.literal(category.getCategoryName())
.styled(s -> s.withColor(Formatting.DARK_GRAY)))
.addLoreLine(Text.empty())
.addLoreLine(MenuText.menus$mainMenu$product$instantBuy(instantBuyPrice))
.addLoreLine(MenuText.menus$mainMenu$product$instantSell(instantSellPrice))
.addLoreLine(Translations.Menus.MainMenu.product$instantBuy(instantBuyPrice))
.addLoreLine(Translations.Menus.MainMenu.product$instantSell(instantSellPrice))
.addLoreLine(Text.empty())
.addLoreLine(MenuText.menus$mainMenu$product$clickToOpen)
.addLoreLine(Translations.Menus.MainMenu.Product$ClickToOpen)
.asStack();
}

Expand Down
13 changes: 7 additions & 6 deletions fabric/src/main/java/stonks/fabric/menu/MenuIcons.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import net.minecraft.item.Items;
import stonks.fabric.menu.player.ViewOffersMenu;
import stonks.fabric.translation.Translations;

public class MenuIcons {
public static final GuiElementBuilder BORDER = new GuiElementBuilder(Items.BLACK_STAINED_GLASS_PANE)
.setName(MenuText.icons$border);
.setName(Translations.Icons.Border);

public static final GuiElementBuilder BACK = new GuiElementBuilder(Items.ARROW)
.setName(MenuText.icons$previousMenu)
.setName(Translations.Icons.PreviousMenu)
.setCallback((index, type, action, gui) -> {
if (gui instanceof StackedMenu stacked) { stacked.getPrevious().open(); }
});

public static final GuiElementBuilder MAIN_MENU = new GuiElementBuilder(Items.GOLD_BLOCK)
.setName(MenuText.icons$mainMenu)
.addLoreLine(MenuText.icons$mainMenu$0)
.setName(Translations.Icons.MainMenu)
.addLoreLine(Translations.Icons.MainMenu$0)
.setCallback((index, type, action, gui) -> {
var previous = gui instanceof StackedMenu stacked ? stacked : null;
if (previous != null && previous.getPrevious() instanceof MarketMainMenu) {
Expand All @@ -50,8 +51,8 @@ public class MenuIcons {
});

public static final GuiElementBuilder VIEW_SELF_OFFERS = new GuiElementBuilder(Items.CHEST)
.setName(MenuText.icons$viewOffers)
.addLoreLine(MenuText.icons$viewOffer$0)
.setName(Translations.Icons.ViewOffers)
.addLoreLine(Translations.Icons.ViewOffers$0)
.setCallback((index, type, action, gui) -> {
var previous = gui instanceof StackedMenu stacked ? stacked : null;
if (previous != null && previous.getPrevious() instanceof ViewOffersMenu) {
Expand Down
Loading