From c8303a012b3a6ccc42e02ceaee57998e05508687 Mon Sep 17 00:00:00 2001 From: fzzyhmstrs Date: Thu, 26 Sep 2024 19:15:53 -0400 Subject: [PATCH] Add chance decimal place setting --- xplat/src/main/java/fzzyhmstrs/emi_loot/EMILoot.java | 8 +++++++- .../fzzyhmstrs/emi_loot/emi/ArchaeologyLootRecipe.java | 4 ++-- .../java/fzzyhmstrs/emi_loot/emi/BlockLootRecipe.java | 2 +- .../java/fzzyhmstrs/emi_loot/emi/ChestLootRecipe.java | 4 ++-- .../java/fzzyhmstrs/emi_loot/emi/GameplayLootRecipe.java | 2 +- .../main/java/fzzyhmstrs/emi_loot/emi/MobLootRecipe.java | 2 +- .../main/java/fzzyhmstrs/emi_loot/util/FloatTrimmer.java | 6 +++++- .../java/fzzyhmstrs/emi_loot/util/IconGroupEmiWidget.java | 3 ++- xplat/src/main/resources/assets/emi_loot/lang/en_us.json | 2 ++ 9 files changed, 23 insertions(+), 10 deletions(-) diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/EMILoot.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/EMILoot.java index 84323f90..f21d343e 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/EMILoot.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/EMILoot.java @@ -25,6 +25,8 @@ import me.fzzyhmstrs.fzzy_config.validation.misc.ValidatedAny; import me.fzzyhmstrs.fzzy_config.validation.misc.ValidatedChoice; import me.fzzyhmstrs.fzzy_config.validation.misc.ValidatedString; +import me.fzzyhmstrs.fzzy_config.validation.number.ValidatedInt; +import me.fzzyhmstrs.fzzy_config.validation.number.ValidatedNumber; import net.minecraft.enchantment.Enchantment; import net.minecraft.loot.condition.LootConditionType; import net.minecraft.loot.function.LootFunctionType; @@ -119,7 +121,10 @@ public static class EmiLootConfig extends Config { @NonSync public boolean mobLootIncludeDirectDrops = true; - @NonSync + @NonSync + public ValidatedInt chanceDecimalPlaces = new ValidatedInt(1, 10, 0, ValidatedNumber.WidgetType.SLIDER); + + @NonSync @SuppressWarnings("FieldMayBeFinal") private ValidatedChoice conditionStyle = EMILootAgnos.isModLoaded("symbols_n_stuff") ? @@ -146,6 +151,7 @@ public boolean isDebug(Type type) { public boolean isLogI18n(Type type) { return type.logUntranslatedTablesSupplier.getAsBoolean(); } + } @IgnoreVisibility diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ArchaeologyLootRecipe.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ArchaeologyLootRecipe.java index d7b1673a..b905582b 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ArchaeologyLootRecipe.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ArchaeologyLootRecipe.java @@ -156,7 +156,7 @@ public void addWidgets(WidgetHolder widgets) { int row = (int) Math.ceil(index.get() / columns) - 1; int column= (index.get() - 1) % (int) columns; index.getAndDecrement(); - String fTrim = trimFloatString(weight); + String fTrim = trimFloatString(weight, EMILoot.config.chanceDecimalPlaces.get()); SlotWidget slotWidget = new SlotWidget(stack, column * 18, titleSpace + row * finalRowHeight).recipeContext(this); widgets.add(slotWidget.appendTooltip(LText.translatable("emi_loot.percentage", fTrim))); } @@ -165,7 +165,7 @@ public void addWidgets(WidgetHolder widgets) { int column = (int)((index.get() - 1) % columns); index.getAndDecrement(); EmiIngredient ingredient = EmiIngredient.of(items.stream().toList()); - String fTrim = trimFloatString(Math.max(weight / 100f, 0.01f), 2); + String fTrim = trimFloatString(Math.max(weight / 100f, 0.01f), Math.max(EMILoot.config.chanceDecimalPlaces.get() + 1, 2)); SlotWidget slotWidget = new SlotWidget(ingredient, column * 18, titleSpace + row * finalRowHeight).recipeContext(this); widgets.add(slotWidget.appendTooltip(LText.translatable("emi_loot.rolls", fTrim).formatted(Formatting.ITALIC, Formatting.GOLD))); } diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/BlockLootRecipe.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/BlockLootRecipe.java index d6c4e0e4..f8b0ca34 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/BlockLootRecipe.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/BlockLootRecipe.java @@ -146,7 +146,7 @@ public void addWidgets(WidgetHolder widgets) { int j = 0; for (ConditionalStack stack: stacks) { SlotWidget widget = widgets.addSlot(stack.getIngredient(), i * 18, 18 * j); - String rounded = FloatTrimmer.trimFloatString(stack.weight()); + String rounded = FloatTrimmer.trimFloatString(stack.weight(), EMILoot.config.chanceDecimalPlaces.get()); widget.appendTooltip(FcText.INSTANCE.translatable("emi_loot.percent_chance", rounded)); if (EMILoot.config.isNotPlain()) { for (Pair pair : stack.conditions()) { diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ChestLootRecipe.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ChestLootRecipe.java index a0fdf5c5..c1dafb57 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ChestLootRecipe.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/ChestLootRecipe.java @@ -159,7 +159,7 @@ public void addWidgets(WidgetHolder widgets) { int row = (int) Math.ceil(index.get() / columns) - 1; int column = (index.get() - 1) % (int) columns; index.getAndDecrement(); - String fTrim = trimFloatString(weight); + String fTrim = trimFloatString(weight, EMILoot.config.chanceDecimalPlaces.get()); SlotWidget slotWidget = new SlotWidget(stack, column * 18, titleSpace + row * finalRowHeight).recipeContext(this); widgets.add(slotWidget.appendTooltip(LText.translatable("emi_loot.percentage", fTrim))); } @@ -168,7 +168,7 @@ public void addWidgets(WidgetHolder widgets) { int column = (int)((index.get() - 1) % columns); index.getAndDecrement(); EmiIngredient ingredient = EmiIngredient.of(items.stream().toList()); - String fTrim = trimFloatString(Math.max(weight/100f, 0.01f), 2); + String fTrim = trimFloatString(Math.max(weight/100f, 0.01f), Math.max(EMILoot.config.chanceDecimalPlaces.get() + 1, 2)); SlotWidget slotWidget = new SlotWidget(ingredient, column * 18, titleSpace + row * finalRowHeight).recipeContext(this); widgets.add(slotWidget.appendTooltip(LText.translatable("emi_loot.rolls", fTrim).formatted(Formatting.ITALIC, Formatting.GOLD))); } diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/GameplayLootRecipe.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/GameplayLootRecipe.java index 8da48d65..9f6540c0 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/GameplayLootRecipe.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/GameplayLootRecipe.java @@ -180,7 +180,7 @@ public void addWidgets(WidgetHolder widgets) { int j = 0; for (ConditionalStack stack: stacks) { SlotWidget widget = widgets.addSlot(stack.getIngredient(), i * 18, 11 + (18 * j)); - String rounded = FloatTrimmer.trimFloatString(stack.weight()); + String rounded = FloatTrimmer.trimFloatString(stack.weight(), EMILoot.config.chanceDecimalPlaces.get()); widget.appendTooltip(FcText.INSTANCE.translatable("emi_loot.percent_chance", rounded)); if (EMILoot.config.isNotPlain()) { for (Pair pair : stack.conditions()) { diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/MobLootRecipe.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/MobLootRecipe.java index f1a70d66..e508925e 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/MobLootRecipe.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/emi/MobLootRecipe.java @@ -233,7 +233,7 @@ public void addWidgets(WidgetHolder widgets) { int j = 0; for (ConditionalStack stack: stacks) { SlotWidget widget = widgets.addSlot(stack.getIngredient(), i * 18, 11 + (18 * j)); - String rounded = FloatTrimmer.trimFloatString(stack.weight()); + String rounded = FloatTrimmer.trimFloatString(stack.weight(), EMILoot.config.chanceDecimalPlaces.get()); widget.appendTooltip(FcText.INSTANCE.translatable("emi_loot.percent_chance", rounded)); if (EMILoot.config.isNotPlain()) { for (Pair pair : stack.conditions()) { diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/util/FloatTrimmer.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/util/FloatTrimmer.java index 3c08fe16..99ff09a1 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/util/FloatTrimmer.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/util/FloatTrimmer.java @@ -11,7 +11,11 @@ public static String trimFloatString(float weight, int places) { int fDot = f.indexOf("."); String fTrim; if (fDot > 0) { - fTrim = f.substring(0, Math.min(f.length(), fDot + 1 + places)); + if (places > 0) { + fTrim = f.substring(0, Math.min(f.length(), fDot + 1 + places)); + } else { + fTrim = f.substring(0, fDot); + } } else { if (weight < 10f) { fTrim = f + ".0"; diff --git a/xplat/src/main/java/fzzyhmstrs/emi_loot/util/IconGroupEmiWidget.java b/xplat/src/main/java/fzzyhmstrs/emi_loot/util/IconGroupEmiWidget.java index 25cfced4..26b15780 100644 --- a/xplat/src/main/java/fzzyhmstrs/emi_loot/util/IconGroupEmiWidget.java +++ b/xplat/src/main/java/fzzyhmstrs/emi_loot/util/IconGroupEmiWidget.java @@ -4,6 +4,7 @@ import dev.emi.emi.api.widget.Bounds; import dev.emi.emi.api.widget.SlotWidget; import dev.emi.emi.api.widget.Widget; +import fzzyhmstrs.emi_loot.EMILoot; import fzzyhmstrs.emi_loot.EMILootClientAgnos; import fzzyhmstrs.emi_loot.client.ClientBuiltPool; import net.minecraft.client.gui.DrawContext; @@ -40,7 +41,7 @@ public IconGroupEmiWidget(int x, int y, ClientBuiltPool pool) { List list2 = new LinkedList<>(); int itemXOffset = iconsWidth + 2; for(ConditionalStack entry: pool.stacks()) { - String rounded = FloatTrimmer.trimFloatString(entry.weight()); + String rounded = FloatTrimmer.trimFloatString(entry.weight(), EMILoot.config.chanceDecimalPlaces.get()); SlotWidget widget = new SlotWidget(entry.getIngredient(), itemXOffset + x, y + 3).appendTooltip(LText.translatable("emi_loot.percent_chance", rounded)); itemXOffset +=20; list2.add(widget); diff --git a/xplat/src/main/resources/assets/emi_loot/lang/en_us.json b/xplat/src/main/resources/assets/emi_loot/lang/en_us.json index e6175e51..1fed6817 100644 --- a/xplat/src/main/resources/assets/emi_loot/lang/en_us.json +++ b/xplat/src/main/resources/assets/emi_loot/lang/en_us.json @@ -42,6 +42,8 @@ "emi_loot.emi_loot_config.chestLootAlwaysStackSame.desc": "If true, chest and archaeology pages will always condense stacks that are equivalent in drop chance. If false, stacks will be spread out if there are fewer than 48 of them.", "emi_loot.emi_loot_config.mobLootIncludeDirectDrops": "Include Direct Drops", "emi_loot.emi_loot_config.mobLootIncludeDirectDrops.desc": "Whether mob loot tables include manually added 'direct drops', such as equipment drops", + "emi_loot.emi_loot_config.chanceDecimalPlaces": "Chance Decimal Places", + "emi_loot.emi_loot_config.chanceDecimalPlaces.desc": "How many decimal places show on chances or weights (5.123%, 5.1%, 5%, etc.)", "emi_loot.emi_loot_config.conditionStyle": "Condition Style", "emi_loot.emi_loot_config.conditionStyle.desc": "How loot conditions are visually presented in the recipe viewer", "emi_loot.emi_loot_config.conditionStyle.default": "Default",