feat: clean up

This commit is contained in:
Discreater committed 2021-05-01 15:38:40 +08:00
1 parent 0f1f888299
commit fa3ba7542c
55 files changed
+611 -490

No files matched your search

@@ -7,16 +7,16 @@ import me.towdium.jecalculation.data.label.labels.LFluidStack;
import me.towdium.jecalculation.data.label.labels.LItemStack;
import me.towdium.jecalculation.data.label.labels.LOreDict;
import me.towdium.jecalculation.data.label.labels.LPlaceholder;
import me.towdium.jecalculation.gui.IWPicker;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.guis.pickers.IPicker;
import me.towdium.jecalculation.gui.guis.pickers.PickerItemStack;
import me.towdium.jecalculation.gui.guis.pickers.PickerPlaceholder;
import me.towdium.jecalculation.gui.guis.pickers.PickerSimple;
import me.towdium.jecalculation.polyfill.mc.client.renderer.GlStateManager;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.Utilities.Relation;
import me.towdium.jecalculation.utils.Utilities.ReversedIterator;
import me.towdium.jecalculation.utils.wrappers.Pair;
import me.towdium.jecalculation.utils.wrappers.Wrapper;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -29,6 +29,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@@ -49,22 +51,23 @@ public interface ILabel {
String FORMAT_GREY = "\u00A78";
String FORMAT_ITALIC = "\u00A7o";
@Nullable
Object getRepresentation();
ILabel increaseAmount();
ILabel decreaseAmount();
static void initClient() {
CONVERTER.register(LOreDict::guess);
EDITOR.register(PickerSimple.FluidStack::new, "fluid_stack", new LFluidStack(1000, FluidRegistry.WATER));
EDITOR.register(PickerSimple.OreDict::new, "ore_dict", new LOreDict("ingotIron"));
EDITOR.register(PickerSimple.FluidStack::new, "fluid", new LFluidStack(1000, FluidRegistry.WATER));
EDITOR.register(PickerSimple.OreDict::new, "ore", new LOreDict("ingotIron"));
EDITOR.register(PickerPlaceholder::new, "placeholder", new LPlaceholder("example", 1, true));
EDITOR.register(PickerItemStack::new, "item_stack",
new LItemStack(new ItemStack(Items.iron_pickaxe)).setFMeta(true));
MERGER.register("itemStack", "itemStack", LItemStack::merge);
MERGER.register("oreDict", "oreDict", LOreDict::merge);
MERGER.register("itemStack", "oreDict", LOreDict::merge);
MERGER.register("fluidStack", "fluidStack", Impl::merge);
MERGER.register("fluidStack", "fluidStack", Impl::merge);
EDITOR.register(PickerItemStack::new, "item", new LItemStack(new ItemStack(Items.iron_pickaxe)).setFMeta(true));
MERGER.register("itemStack", "itemStack", Impl.form(LItemStack.class, LItemStack.class, LItemStack::merge));
MERGER.register("oreDict", "oreDict", Impl.form(LOreDict.class, LOreDict.class, LOreDict::mergeSame));
MERGER.register("oreDict", "itemStack", Impl.form(LOreDict.class, LItemStack.class, LOreDict::mergeFuzzy));
MERGER.register("fluidStack", "fluidStack", Impl.form(LFluidStack.class, LFluidStack.class, LFluidStack::merge));
}
int getAmount();
@@ -77,6 +80,8 @@ public interface ILabel {
@SuppressWarnings("UnusedReturnValue")
ILabel setPercent(boolean p);
boolean isPercent();
static void initServer() {
SERIALIZER.register(LFluidStack.IDENTIFIER, LFluidStack::new);
SERIALIZER.register(LItemStack.IDENTIFIER, LItemStack::new);
@@ -85,7 +90,7 @@ public interface ILabel {
SERIALIZER.register(LEmpty.IDENTIFIER, i -> EMPTY);
}
String getAmountString();
String getAmountString(boolean round);
String getDisplayName();
@@ -110,40 +115,58 @@ public interface ILabel {
* Since {@link ILabel} merging is bidirectional, it is redundant to
* implement on both side. So this class is created for merging
* {@link ILabel label(s)}.
* It uses singleton mode. First registerGuess merge functions, then use
* {@link #merge(ILabel, ILabel, boolean)} to operate the {@link ILabel}.
* It uses singleton mode. First register merge functions, then use
* {@link #merge(ILabel, ILabel)} to operate the {@link ILabel}.
* For registering, see {@link Serializer}.
*/
class Merger {
private Relation<String, MergerFunction> functions = new Relation<>();
private HashMap<Pair<String, String>, MergerFunction> functions = new HashMap<>();
private Merger() {
}
public void register(String a, String b, MergerFunction func) {
functions.add(a, b, func);
functions.put(new Pair<>(a, b), func);
}
public Optional<ILabel> merge(ILabel a, ILabel b, boolean add) {
Optional<ILabel> ret = functions.get(a.getIdentifier(), b.getIdentifier())
.orElse((x, y, f) -> Optional.empty()).merge(a, b, add);
if (ret.isPresent() && ret.get() != ILabel.EMPTY && (ret.get() == a || ret.get() == b))
throw new RuntimeException("Merger should not modify the given label.");
return ret;
/**
* @param a one label
* @param b another label
* @return merge result or empty
* This function will try to merge two labels.
* If both label has same type, just sum up amount
* For different type, the framework will try reversing the order for MergeFunctions to work
* So generally speaking, a and b has no priority in this function
*/
public Optional<ILabel> merge(ILabel a, ILabel b) {
BiFunction<ILabel, ILabel, ILabel> get = (c, d) -> {
MergerFunction mf = functions.get(new Pair<>(c.getIdentifier(), d.getIdentifier()));
if (mf != null) return mf.merge(c, d);
else return null;
};
return new Wrapper<ILabel>(null)
.or(() -> get.apply(a, b))
.or(() -> get.apply(b, a))
.ifPresent(i -> {
if (i != ILabel.EMPTY && (i == a || i == b))
throw new RuntimeException("Merger should not modify the given label.");
})
.toOptional();
}
@FunctionalInterface
public interface MergerFunction {
/**
* @param a an {@link ILabel} to merge
* @param b another {@link ILabel} to merge
* @param add add together or cancel each other
* @return an optional {@link Pair pair} of {@link ILabel label(s)}.
* If empty, the labels cannot merge, otherwise returns difference and common elements
* @param a requested label
* @param b supplied label
* @return merge result or empty
* This function ensures to generate same type if a and b are from same type
* If a is fuzzy, b is explicit, generates a is amount is negative, else generates b
* Normally, explicit type cannot request fuzzy type
*/
Optional<ILabel> merge(ILabel a, ILabel b, boolean add);
@Nullable
ILabel merge(ILabel a, ILabel b);
}
}
@@ -181,7 +204,8 @@ public interface ILabel {
public ILabel deserialize(NBTTagCompound nbt) {
String s = nbt.getString(KEY_IDENTIFIER);
Function<NBTTagCompound, ILabel> func = idToData.get(s);
if (func != null) return func.apply(nbt.getCompoundTag(KEY_CONTENT));
if (func != null)
return func.apply(nbt.getCompoundTag(KEY_CONTENT));
else {
JustEnoughCalculation.logger.warn("Unrecognized type identifier \"" + s + "\", use empty instead.");
return ILabel.EMPTY;
@@ -219,11 +243,13 @@ public interface ILabel {
handlers.add(handler);
}
// get most possible guess from labels
public ILabel first(List<ILabel> labels) {
List<ILabel> guess = guess(labels);
return guess.isEmpty() ? labels.get(0) : guess.get(0);
}
// to test if the labels can be converted to other labels (like oreDict)
public List<ILabel> guess(List<ILabel> labels) {
return new ReversedIterator<>(handlers).stream().flatMap(h -> h.apply(labels).stream())
.collect(Collectors.toList());
@@ -237,7 +263,7 @@ public interface ILabel {
private RegistryEditor() {
}
public void register(Supplier<IWPicker> editor, String unlocalizedName, ILabel representation) {
public void register(Supplier<IPicker> editor, String unlocalizedName, ILabel representation) {
records.add(new Record(editor, "common.label." + unlocalizedName, representation));
}
@@ -247,11 +273,11 @@ public interface ILabel {
}
public static class Record {
public Supplier<IWPicker> editor;
public Supplier<IPicker> editor;
public String localizeKey;
public ILabel representation;
public Record(Supplier<IWPicker> editor, String localizeKey, ILabel representation) {
public Record(Supplier<IPicker> editor, String localizeKey, ILabel representation) {
this.editor = editor;
this.localizeKey = localizeKey;
this.representation = representation;
@@ -270,6 +296,12 @@ public interface ILabel {
private LEmpty() {
}
@Nullable
@Override
public Object getRepresentation() {
return null;
}
@Override
public ILabel increaseAmount() {
return this;
@@ -295,6 +327,11 @@ public interface ILabel {
throw new UnsupportedOperationException();
}
@Override
public boolean isPercent() {
return false;
}
@Override
public int getAmount() {
return 0;
@@ -306,7 +343,7 @@ public interface ILabel {
}
@Override
public String getAmountString() {
public String getAmountString(boolean round) {
return "";
}
@@ -350,9 +387,9 @@ public interface ILabel {
return getDisplayName() + 'x' + getAmount();
}
public Impl(int amount) {
public Impl(int amount, boolean percent) {
this.amount = amount;
percent = false;
this.percent = percent;
}
public Impl(Impl lsa) {
@@ -399,32 +436,27 @@ public interface ILabel {
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;
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);
} else return null;
};
}
@Override
@SideOnly(Side.CLIENT)
public void getToolTip(List<String> existing, boolean detailed) {
if (detailed)
existing.add(FORMAT_GREY + Utilities.I18n.format("label.common.tooltip.amount", getAmountString()));
}
public static Optional<ILabel> merge(ILabel a, ILabel b, boolean add) {
if (a instanceof Impl && b instanceof Impl && a.matches(b)) return mergeUnchecked((Impl) a, (Impl) b, add);
else return Optional.empty();
}
protected static Optional<ILabel> mergeUnchecked(Impl a, Impl b, boolean add) {
Impl ret = a.copy();
boolean p = a.percent || b.percent;
int amountA = a.amount;
int amountB = b.amount;
if (p) {
if (!a.percent)
amountA *= 100;
if (!b.percent)
amountB *= 100;
}
ret.amount = add ? amountA + amountB : amountA - amountB;
ret.percent = p;
return Optional.of(ret.amount == 0 ? ILabel.EMPTY : ret);
existing.add(
FORMAT_GREY + Utilities.I18n.format("label.common.tooltip.amount", getAmountString(false)));
}
@Override
@@ -454,7 +486,7 @@ public interface ILabel {
}
public ILabel setPercent(boolean p) {
if (!acceptPercent())
if (p && !acceptPercent())
throw new UnsupportedOperationException();
if (p && !percent) {
amount *= 100;
@@ -466,6 +498,11 @@ public interface ILabel {
return this;
}
@Override
public boolean isPercent() {
return percent;
}
@Override
public boolean equals(Object obj) {
return obj instanceof Impl && amount == ((Impl) obj).amount && matches(obj);
@@ -473,10 +510,16 @@ public interface ILabel {
@Override
@SideOnly(Side.CLIENT)
public String getAmountString() {
return getAmount() == 0 ?
"" :
(percent ? Utilities.cutNumber(getAmount(), 4) + "%" : Utilities.cutNumber(getAmount(), 5));
@SuppressWarnings("IntegerDivisionInFloatingPointContext")
public String getAmountString(boolean round) {
if (getAmount() == 0)
return "";
else if (!percent)
return Utilities.cutNumber(getAmount(), 5);
else if (round)
return Utilities.cutNumber((getAmount() + 99) / 100, 5);
else
return Utilities.cutNumber(getAmount(), 4) + "%";
}
@Override