feat: float

This commit is contained in:
Discreater committed 2021-05-01 20:18:12 +08:00
1 parent 0e7c1b4f73
commit 6c80a421fe
18 files changed
+128 -109

No files matched your search

@@ -3,6 +3,7 @@ package me.towdium.jecalculation.data;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.JecaConfig;
import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.structure.Recents;
import me.towdium.jecalculation.data.structure.Recipe;
@@ -14,17 +15,20 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Author: towdium
* Date: 17-10-15.
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class Controller {
public static final String KEY_RECIPES = "recipes";
@@ -47,8 +51,14 @@ public class Controller {
File dir = JecaConfig.dataDir;
File[] fs = dir.listFiles();
if (fs == null) return new ArrayList<>();
Function<File, Recipes> read = f -> {
NBTTagCompound nbt = Utilities.Json.read(f);
JustEnoughCalculation.logger.warn("File " + f.getAbsolutePath() + " contains invalid records.");
return nbt == null ? null : new Recipes(nbt);
};
return Arrays.stream(fs)
.map(i -> new Pair<>(i.getName(), new Recipes(Utilities.Json.read(i))))
.map(i -> new Pair<>(i.getName(), read.apply(i)))
.filter(i -> i.two != null)
.collect(Collectors.toList());
}
@@ -70,9 +70,9 @@ public interface ILabel {
MERGER.register("fluidStack", "fluidStack", Impl.form(LFluidStack.class, LFluidStack.class, LFluidStack::merge));
}
int getAmount();
long getAmount();
ILabel multiply(int i);
ILabel multiply(float i);
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean acceptPercent();
@@ -83,6 +83,7 @@ public interface ILabel {
boolean isPercent();
static void initServer() {
// TODO handle invalid items
SERIALIZER.register(LFluidStack.IDENTIFIER, LFluidStack::new);
SERIALIZER.register(LItemStack.IDENTIFIER, LItemStack::new);
SERIALIZER.register(LOreDict.IDENTIFIER, LOreDict::new);
@@ -98,11 +99,11 @@ public interface ILabel {
ILabel copy();
NBTTagCompound toNBT();
NBTTagCompound toNbt();
String getIdentifier();
ILabel setAmount(int amount);
ILabel setAmount(long amount);
// test two labels are exactly same except amount
boolean matches(Object l);
@@ -215,7 +216,7 @@ public interface ILabel {
public NBTTagCompound serialize(ILabel label) {
NBTTagCompound ret = new NBTTagCompound();
ret.setString(KEY_IDENTIFIER, label.getIdentifier());
ret.setTag(KEY_CONTENT, label.toNBT());
ret.setTag(KEY_CONTENT, label.toNbt());
return ret;
}
}
@@ -313,7 +314,7 @@ public interface ILabel {
}
@Override
public ILabel multiply(int i) {
public ILabel multiply(float i) {
return this;
}
@@ -333,12 +334,12 @@ public interface ILabel {
}
@Override
public int getAmount() {
public long getAmount() {
return 0;
}
@Override
public ILabel setAmount(int amount) {
public ILabel setAmount(long amount) {
return this;
}
@@ -362,7 +363,7 @@ public interface ILabel {
}
@Override
public NBTTagCompound toNBT() {
public NBTTagCompound toNbt() {
return new NBTTagCompound();
}
@@ -379,7 +380,7 @@ public interface ILabel {
abstract class Impl implements ILabel {
public static final String KEY_AMOUNT = "amount";
public static final String KEY_PERCENT = "percent";
protected int amount;
protected long amount;
protected boolean percent;
@Override
@@ -387,7 +388,7 @@ public interface ILabel {
return getDisplayName() + 'x' + getAmount();
}
public Impl(int amount, boolean percent) {
public Impl(long amount, boolean percent) {
this.amount = amount;
this.percent = percent;
}
@@ -398,7 +399,7 @@ public interface ILabel {
}
public Impl(NBTTagCompound nbt) {
amount = nbt.getInteger(KEY_AMOUNT);
amount = nbt.getLong(KEY_AMOUNT);
percent = nbt.getBoolean(KEY_PERCENT);
}
@@ -430,20 +431,14 @@ public interface ILabel {
}
}
@Override
public ILabel multiply(int i) {
setAmount(i * getAmount());
return this;
}
protected static Merger.MergerFunction form(Class a, Class b, BiPredicate<ILabel, ILabel> p) {
return (c, d) -> {
if (c == EMPTY || d == EMPTY) return null;
if (a.isInstance(c) && b.isInstance(d) && p.test(c, d)) {
int amountC = c.isPercent() ? c.getAmount() : c.getAmount() * 100;
int amountD = d.isPercent() ? d.getAmount() : d.getAmount() * 100;
int amount = amountC + amountD;
int amountI = (amount > 0 ? amount + 99 : amount - 99) / 100;
long amountC = c.isPercent() ? c.getAmount() : Math.multiplyExact(c.getAmount(), 100);
long amountD = d.isPercent() ? d.getAmount() : Math.multiplyExact(d.getAmount(), 100);
long amount = Math.addExact(amountC, amountD);
long amountI = (amount > 0 ? Math.addExact(amount, 99) : Math.subtractExact(amount, 99)) / 100;
if (amount == 0) return EMPTY;
else if (amount > 0) return d.copy().setAmount(d.isPercent() ? amount : amountI);
else return c.copy().setAmount(c.isPercent() ? amount : amountI);
@@ -451,6 +446,15 @@ public interface ILabel {
};
}
@Override
public ILabel multiply(float i) {
float amount = i * getAmount();
if (amount > Long.MAX_VALUE) throw new ArithmeticException("Multiply overflow");
setAmount((long) amount);
return this;
}
@Override
@SideOnly(Side.CLIENT)
public void getToolTip(List<String> existing, boolean detailed) {
@@ -460,12 +464,12 @@ public interface ILabel {
}
@Override
public int getAmount() {
public long getAmount() {
return amount;
}
@Override
public ILabel setAmount(int amount) {
public ILabel setAmount(long amount) {
if (amount == 0)
return ILabel.EMPTY;
this.amount = amount;
@@ -473,10 +477,10 @@ public interface ILabel {
}
@Override
public NBTTagCompound toNBT() {
public NBTTagCompound toNbt() {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setInteger(KEY_AMOUNT, amount);
nbt.setBoolean(KEY_PERCENT, true);
nbt.setLong(KEY_AMOUNT, amount);
if (percent) nbt.setBoolean(KEY_PERCENT, true);
return nbt;
}
@@ -44,11 +44,11 @@ public class LFluidStack extends ILabel.Impl {
this(fs.amount, fs.getFluid(), fs.tag);
}
public LFluidStack(int amount, Fluid fluid) {
public LFluidStack(long amount, Fluid fluid) {
this(amount, fluid, null);
}
public LFluidStack(int amount, Fluid fluid, @Nullable NBTTagCompound nbt) {
public LFluidStack(long amount, Fluid fluid, @Nullable NBTTagCompound nbt) {
super(amount, false);
init(fluid, nbt);
}
@@ -62,7 +62,7 @@ public class LFluidStack extends ILabel.Impl {
private void init(Fluid fluid, @Nullable NBTTagCompound nbt) {
this.fluid = fluid;
this.nbt = nbt;
temp = new FluidStack(fluid, amount, nbt);
temp = new FluidStack(fluid, 1, nbt);
}
public LFluidStack(LFluidStack lfs) {
@@ -109,8 +109,8 @@ public class LFluidStack extends ILabel.Impl {
}
@Override
public NBTTagCompound toNBT() {
NBTTagCompound ret = super.toNBT();
public NBTTagCompound toNbt() {
NBTTagCompound ret = super.toNbt();
ret.setString(KEY_FLUID, FluidRegistry.getFluidName(fluid));
if (nbt != null) ret.setTag(KEY_NBT, nbt);
return ret;
@@ -120,7 +120,7 @@ public class LFluidStack extends ILabel.Impl {
@SideOnly(Side.CLIENT)
public void getToolTip(List<String> existing, boolean detailed) {
if (detailed) existing.add(FORMAT_GREY +
Utilities.I18n.format("label.common.tooltip.amount", Integer.toString(getAmount())) + "mB");
Utilities.I18n.format("label.common.tooltip.amount", Long.toString(getAmount())) + "mB");
existing.add(FORMAT_BLUE + FORMAT_ITALIC + Utilities.getModName(fluid));
}
@@ -134,7 +134,7 @@ public class LFluidStack extends ILabel.Impl {
@Override
public int hashCode() {
return fluid.getUnlocalizedName().hashCode() ^ amount ^ (nbt == null ? 0 : nbt.hashCode());
return fluid.getUnlocalizedName().hashCode() ^ (int) amount ^ (nbt == null ? 0 : nbt.hashCode());
}
public static boolean merge(ILabel a, ILabel b) {
@@ -39,11 +39,6 @@ public class LItemStack extends ILabel.Impl {
boolean fCap;
transient ItemStack temp;
public LItemStack(int amount, Item item, int meta, @Nullable NBTTagCompound nbt, boolean percent) {
super(amount, percent);
init(item, meta, nbt, false, false, false);
}
// Convert from itemStack
public LItemStack(ItemStack is) {
super(is.stackSize, false);
@@ -168,11 +163,11 @@ public class LItemStack extends ILabel.Impl {
}
@Override
public NBTTagCompound toNBT() {
public NBTTagCompound toNbt() {
if (item == null)
return ILabel.EMPTY.toNBT();
return ILabel.EMPTY.toNbt();
int id = Item.getIdFromItem(item);
NBTTagCompound ret = super.toNBT();
NBTTagCompound ret = super.toNbt();
if (meta != 0)
ret.setInteger(KEY_META, meta);
ret.setInteger(KEY_ITEM, id);
@@ -202,7 +197,7 @@ public class LItemStack extends ILabel.Impl {
}
@Override
public int hashCode() {
return (nbt == null ? 0 : nbt.hashCode()) ^ meta ^ item.getUnlocalizedName().hashCode() ^ amount;
public int hashCode() { // TODO all labels use super hashcode
return (nbt == null ? 0 : nbt.hashCode()) ^ meta ^ item.getUnlocalizedName().hashCode() ^ (int) amount;
}
}
@@ -19,7 +19,6 @@ import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@@ -39,7 +38,7 @@ public class LOreDict extends ILabel.Impl {
this(name, 1);
}
public LOreDict(String name, int amount) {
public LOreDict(String name, long amount) {
super(amount, false);
this.name = name;
}
@@ -103,7 +102,7 @@ public class LOreDict extends ILabel.Impl {
public static List<ILabel> guess(List<ILabel> iss) {
ILabel l = iss.get(0);
HashSet<Integer> ids = new HashSet<>();
int amount = l.getAmount();
long amount = l.getAmount();
for (int i : OreDictionary.getOreIDs(((LItemStack) l).getRep()))
if (check(i, iss))
ids.add(i);
@@ -186,8 +185,8 @@ public class LOreDict extends ILabel.Impl {
}
@Override
public NBTTagCompound toNBT() {
NBTTagCompound ret = super.toNBT();
public NBTTagCompound toNbt() {
NBTTagCompound ret = super.toNbt();
ret.setString(KEY_NAME, name);
return ret;
}
@@ -201,7 +200,7 @@ public class LOreDict extends ILabel.Impl {
@Override
public int hashCode() {
return name.hashCode() ^ amount;
return name.hashCode() ^ (int) amount;
}
public String getName() {
@@ -32,11 +32,11 @@ public class LPlaceholder extends ILabel.Impl {
name = tag.getString(KEY_NAME);
}
public LPlaceholder(String name, int amount) {
public LPlaceholder(String name, long amount) {
this(name, amount, false);
}
public LPlaceholder(String name, int amount, boolean silent) {
public LPlaceholder(String name, long amount, boolean silent) {
super(amount, false);
this.name = name;
if (!silent) getRecord().push(new LPlaceholder(name, 1, true));
@@ -65,8 +65,8 @@ public class LPlaceholder extends ILabel.Impl {
}
@Override
public NBTTagCompound toNBT() {
NBTTagCompound nbt = super.toNBT();
public NBTTagCompound toNbt() {
NBTTagCompound nbt = super.toNbt();
nbt.setString(KEY_NAME, name);
return nbt;
}
@@ -80,7 +80,7 @@ public class LPlaceholder extends ILabel.Impl {
@Override
public int hashCode() {
return name.hashCode() ^ amount;
return name.hashCode() ^ (int) amount;
}
@Nullable
@@ -6,6 +6,8 @@ import me.towdium.jecalculation.utils.wrappers.Pair;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ChatComponentTranslation;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.*;
import java.util.stream.Collectors;
@@ -13,6 +15,7 @@ import static me.towdium.jecalculation.data.structure.Recipe.enumIoType.INPUT;
import static me.towdium.jecalculation.data.structure.Recipe.enumIoType.OUTPUT;
// positive => generate; negative => require
@ParametersAreNonnullByDefault
public class CostList {
List<ILabel> labels;
@@ -53,7 +56,7 @@ public class CostList {
ILabel a = ret.labels.get(i);
ILabel b = ret.labels.get(j);
if (a.matches(b)) {
ret.labels.set(i, a.setAmount(a.getAmount() + b.getAmount()));
ret.labels.set(i, a.setAmount(Math.addExact(a.getAmount(), b.getAmount())));
ret.labels.set(j, ILabel.EMPTY);
}
} else {
@@ -69,7 +72,7 @@ public class CostList {
return ret;
}
public CostList multiply(int i) {
public CostList multiply(long i) {
labels = labels.stream().map(j -> j.multiply(i)).collect(Collectors.toList());
return this;
}
@@ -117,10 +120,10 @@ public class CostList {
ArrayList<ILabel> catalysts = new ArrayList<>();
private int index;
public Calculator() {
public Calculator() throws ArithmeticException {
HashSet<CostList> set = new HashSet<>();
set.add(CostList.this);
Pair<Recipe, Integer> next = find(true);
Pair<Recipe, Long> next = find(true);
int count = 0;
while (next != null) {
CostList original = getCurrent();
@@ -143,7 +146,8 @@ public class CostList {
}
}
private Pair<Recipe, Integer> find(boolean reset) {
@Nullable
private Pair<Recipe, Long> find(boolean reset) {
if (reset)
index = 0;
List<ILabel> labels = getCurrent().labels;
@@ -6,10 +6,12 @@ import me.towdium.jecalculation.utils.Utilities;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
@ParametersAreNonnullByDefault
public class Recents {
Utilities.Recent<ILabel> record = new Utilities.Recent<>((a, b) -> a == ILabel.EMPTY || a.equals(b), 9);
@@ -8,6 +8,7 @@ import me.towdium.jecalculation.utils.wrappers.Wrapper;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -23,6 +24,7 @@ import java.util.stream.StreamSupport;
* Author: towdium
* Date: 17-10-6.
*/
@ParametersAreNonnullByDefault
public class Recipe {
public static final String KEY_INPUT = "input";
public static final String KEY_CATALYST = "catalyst";
@@ -135,16 +137,17 @@ public class Recipe {
return Arrays.stream(output).anyMatch(i -> ILabel.MERGER.merge(label, i).isPresent());
}
public int multiplier(ILabel label) {
public long multiplier(ILabel label) {
return Arrays.stream(output).filter(i -> ILabel.MERGER.merge(label, i).isPresent()).findAny()
.map(i -> {
int amountA = label.getAmount();
if (!label.isPercent()) amountA *= 100;
int amountB = i.getAmount();
if (!i.isPercent()) amountB *= 100;
long amountA = label.getAmount();
if (!label.isPercent()) amountA = Math.multiplyExact(amountA, 100L);
long amountB = i.getAmount();
if (!i.isPercent()) amountB = Math.multiplyExact(amountB, 100L);
return (amountB + Math.abs(amountA) - 1) / amountB;
}).orElse(0);
}).orElse(0L);
}
public enum enumIoType {
INPUT, OUTPUT, CATALYST;
@@ -7,6 +7,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
@@ -14,6 +15,7 @@ import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@ParametersAreNonnullByDefault
public class Recipes {
LinkedHashMap<String, List<Recipe>> records = new LinkedHashMap<>();
@@ -45,7 +47,7 @@ public class Recipes {
}
public void modify(String group, int index, @Nullable Recipe recipe) {
if (index == -1) add(group, recipe);
if (index == -1 && recipe != null) add(group, recipe);
else if (recipe == null) remove(group, index);
else set(group, index, recipe);
}
@@ -97,11 +97,11 @@ public class GuiCalculator extends WContainer implements IGui {
void refreshCalculator() {
try {
String s = amount.getText();
int i = s.isEmpty() ? 1 : Integer.parseInt(amount.getText());
long i = s.isEmpty() ? 1 : Long.parseLong(amount.getText());
amount.setColor(JecaGui.COLOR_TEXT_WHITE);
List<ILabel> dest = Collections.singletonList(label.getLabel().copy().setAmount(i));
calculator = new CostList(getInventory(), dest).calculate();
} catch (NumberFormatException e) {
} catch (NumberFormatException | ArithmeticException e) {
amount.setColor(JecaGui.COLOR_TEXT_RED);
calculator = null;
}
@@ -1,9 +1,9 @@
package me.towdium.jecalculation.gui.guis;
import codechicken.nei.recipe.IRecipeHandler;
import me.towdium.jecalculation.data.structure.CostList;
import me.towdium.jecalculation.data.Controller;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.structure.CostList;
import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
@@ -34,12 +34,13 @@ public class GuiRecipe extends WContainer implements IGui {
WLabelGroup groupCatalyst = new WLabelGroup(28, 87, 7, 1, 20, 20, WLabel.enumMode.EDITOR);
WButton buttonDisamb = new WButtonIcon(121, 33, 20, 20, Resource.BTN_DISAMB, "recipe.disamb").setLsnrLeft(() -> {
if (disambiguation != null) JecaGui.displayGui(new GuiDisambiguation(new ArrayList<>(disambiguation.values()))
.setCallback(l -> {
JecaGui.displayParent();
JecaGui.getCurrent().hand = l;
}));
}); WLabelGroup groupInput = new WLabelGroup(28, 111, 7, 2, 20, 20, WLabel.enumMode.EDITOR).setLsnrUpdate(i -> {
if (disambiguation != null)
JecaGui.displayGui(new GuiDisambiguation(new ArrayList<>(disambiguation.values())).setCallback(l -> {
JecaGui.displayParent();
JecaGui.getCurrent().hand = l;
}));
});
WLabelGroup groupInput = new WLabelGroup(28, 111, 7, 2, 20, 20, WLabel.enumMode.EDITOR).setLsnrUpdate(i -> {
disambiguation.remove(i);
refresh();
});
@@ -73,11 +74,14 @@ public class GuiRecipe extends WContainer implements IGui {
});
WButton buttonNew = new WButtonIcon(7, 33, 20, 20, Resource.BTN_NEW, "recipe.new")
.setLsnrLeft(() -> setModeNewGroup(true));
WButton buttonYes = new WButtonIcon(7, 33, 20, 20, Resource.BTN_YES, "recipe.confirm").setLsnrLeft(() -> {
switcherGroup.setTemp(textField.getText());
textField.setText("");
setModeNewGroup(false);
});
WButton buttonYes = new WButtonIcon(7, 33, 20, 20, Resource.BTN_YES, "recipe.confirm").setDisabled(true)
.setLsnrLeft(() -> {
switcherGroup.setTemp(
textField
.getText());
textField.setText("");
setModeNewGroup(false);
});
WButton buttonNo = new WButtonIcon(26, 33, 20, 20, Resource.BTN_NO, "recipe.cancel")
.setLsnrLeft(() -> setModeNewGroup(false));
@@ -99,11 +103,13 @@ public class GuiRecipe extends WContainer implements IGui {
add(new WIcon(7, 111, 21, 40, Resource.ICN_INPUT, "recipe.input"));
add(new WLine(57));
addAll(groupInput, groupCatalyst, groupOutput, switcherGroup);
if (switcherGroup.getTexts().isEmpty()) switcherGroup.setTemp(Utilities.I18n.format("common.default"));
if (switcherGroup.getTexts().isEmpty())
switcherGroup.setTemp(Utilities.I18n.format("common.default"));
setModeNewGroup(false);
buttonCopy.setDisabled(true);
buttonDel.setDisabled(true);
buttonDisamb.setDisabled(true);
textField.setLsnrText(s -> buttonYes.setDisabled(s.isEmpty()));
}
public void setModeNewGroup(boolean b) {
@@ -27,5 +27,9 @@ public interface IPicker extends IGui {
this.callback = callback;
return this;
}
protected void notifyLsnr(ILabel l) {
if (callback != null) callback.accept(l);
}
}
}
@@ -19,7 +19,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
public class PickerPlaceholder extends IPicker.Impl implements IGui {
public PickerPlaceholder() {
WLabelScroll scroll = new WLabelScroll(7, 69, 8, 5, WLabel.enumMode.PICKER, true)
.setLabels(LPlaceholder.getRecent()).setLsnrUpdate(callback);
.setLabels(LPlaceholder.getRecent()).setLsnrUpdate(this::notifyLsnr);
WTextField create = new WTextField(26, 7, 69);
create.setLsnrText(s -> create.setColor(s.equals("") ? JecaGui.COLOR_TEXT_RED : JecaGui.COLOR_TEXT_WHITE));
add(new WSearch(26, 45, 90, scroll));
@@ -31,7 +31,7 @@ public class PickerSimple extends IPicker.Impl implements IGui {
*/
public PickerSimple(List<ILabel> labels, String l18nKey) {
WLabelScroll ls = new WLabelScroll(7, 33, 8, 7, WLabel.enumMode.PICKER, true)
.setLabels(labels).setLsnrUpdate(callback);
.setLabels(labels).setLsnrUpdate(this::notifyLsnr);
add(new WSearch(26, 7, 90, ls));
add(new WIcon(7, 7, 20, 20, Resource.ICN_TEXT, l18nKey + ".text"));
add(ls);
@@ -2,9 +2,11 @@ package me.towdium.jecalculation.gui.widgets;
import me.towdium.jecalculation.gui.JecaGui;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
public interface IWidget {
@ParametersAreNonnullByDefault
public interface IWidget { // TODO unify listener behavior when manually set and pass instance to listener
void onDraw(JecaGui gui, int xMouse, int yMouse);
default boolean onTooltip(JecaGui gui, int xMouse, int yMouse, List<String> tooltip) {
@@ -208,7 +208,7 @@ public class WLabel implements IWidget {
add(bAmount);
}
if (label.acceptPercent()) label.setPercent(b);
wtf.setText(Integer.toString(label.getAmount()));
wtf.setText(Long.toString(label.getAmount()));
}
@Override
@@ -27,6 +27,7 @@ import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.BreakIterator;
import java.text.DecimalFormat;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
@@ -42,30 +43,17 @@ import java.util.stream.StreamSupport;
@ParametersAreNonnullByDefault
public class Utilities {
// FLOAT FORMATTING
public static char[] suffix = new char[]{'K', 'M', 'B', 'G', 'T'};
public static DecimalFormat[] format = new DecimalFormat[]{
new DecimalFormat("#."), new DecimalFormat("#.#"), new DecimalFormat("#.##"),
new DecimalFormat("#.###"), new DecimalFormat("#.####")
};
public static String cutNumber(float f, int size) {
BiFunction<Float, Integer, String> form = (fl, len) -> {
String ret = Float.toString(fl);
if (ret.endsWith(".0"))
ret = ret.substring(0, ret.length() - 2);
if (ret.length() > len)
ret = ret.substring(0, len);
return ret;
};
BiFunction<Float, Integer, String> form = (fl, len) -> format[len - 1 - (int) Math.log10(fl)].format(fl);
int scale = (int) Math.log10(f) / 3;
switch (scale) {
case 0:
return form.apply(f, size);
case 1:
return form.apply(f / 1000.0f, size - 1) + 'K';
case 2:
return form.apply(f / 1000000.0f, size - 1) + 'M';
case 3:
return form.apply(f / 1000000000.0f, size - 1) + 'B';
case 4:
return form.apply(f / 1000000000000.0f, size - 1) + 'G';
default:
return form.apply(f / 1000000000000000.0f, size - 1) + 'T';
}
if (scale == 0) return form.apply(f, size);
else return form.apply(f / (float) Math.pow(1000, scale), size - 1) + suffix[scale - 1];
}