diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java index d15024440..3e93255b7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java @@ -28,6 +28,7 @@ import io.github.thebusybiscuit.slimefun4.api.events.GEOResourceGenerationEvent; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.items.geo.GEOMiner; import io.github.thebusybiscuit.slimefun4.implementation.items.geo.GEOScanner; +import io.github.thebusybiscuit.slimefun4.utils.ChatUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.HeadTexture; @@ -236,13 +237,13 @@ public class ResourceManager { for (int i = page * 28; i < resources.size() && i < (page + 1) * 28; i++) { GEOResource resource = resources.get(i); OptionalInt optional = getSupplies(resource, block.getWorld(), x, z); - int supplies = optional.isPresent() ? optional.getAsInt() : generate(resource, block.getWorld(), x, block.getY(), z); - String suffix = Slimefun.getLocalization().getResourceString(p, supplies == 1 ? "tooltips.unit" : "tooltips.units"); + int supplies = optional.orElse(generate(resource, block.getWorld(), x, block.getY(), z)); + String suffix = Slimefun.getLocalization().getResourceString(p, ChatUtils.checkPlurality("tooltips.unit", supplies)); ItemStack item = new CustomItemStack(resource.getItem(), "&f" + resource.getName(p), "&8\u21E8 &e" + supplies + ' ' + suffix); if (supplies > 1) { - item.setAmount(supplies > item.getMaxStackSize() ? item.getMaxStackSize() : supplies); + item.setAmount(Math.min(supplies, item.getMaxStackSize())); } menu.addItem(index, item, ChestMenuUtils.getEmptyClickHandler()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java index e3d7aac4b..829e5891c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java @@ -80,4 +80,26 @@ public final class ChatUtils { return builder.toString(); } + /** + * This method adds an s to a string if the supplied integer is not 1. + * + * @param string + * The string to potentially pluralize + * @param count + * The amount of things + * @return + * {@code string} if {@code count} is 1 else {@code string + "s"} + * @throws IllegalArgumentException + * if count is less than 0 + */ + public static @Nonnull String checkPlurality(@Nonnull String string, int count) { + if (count < 0) { + throw new IllegalArgumentException("Argument count cannot be negative."); + } + if (count == 1) { + return string; + } + return string + "s"; + } + } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/utils/TestChatUtils.java b/src/test/java/io/github/thebusybiscuit/slimefun4/utils/TestChatUtils.java index 80fa61aa8..794b02e4b 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/utils/TestChatUtils.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/utils/TestChatUtils.java @@ -33,4 +33,14 @@ class TestChatUtils { Assertions.assertEquals(expected, ChatUtils.removeColorCodes(ChatColor.GREEN + "Hello " + ChatColor.RED + "world")); } + @Test + @DisplayName("Test ChatUtils.checkPlurality(...)") + void testPluralization() { + String input = "Banana"; + Assertions.assertThrows(IllegalArgumentException.class, () -> ChatUtils.checkPlurality(input, -1)); + Assertions.assertEquals("Bananas", ChatUtils.checkPlurality(input, 0)); + Assertions.assertEquals("Banana", ChatUtils.checkPlurality(input, 1)); + Assertions.assertEquals("Bananas", ChatUtils.checkPlurality(input, 2)); + } + }