mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-19 19:25:48 +00:00
Merge pull request #3954 from iTwins/refactor_ternary
refactored ternary operators
This commit is contained in:
commit
5d3f1ac360
@ -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());
|
||||
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user