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

@@ -56,18 +56,10 @@ public class JustEnoughCalculation {
public static final String VERSION = "@VERSION@";
}
// Only to use at client side
public enum enumSide {
/**
* Running at client side and server not installed.
*/
CLIENT,
/**
* Running at client side and both installed.
*/
BOTH,
/**
* Default unknown.
*/
UNDEFINED
CLIENT, // Running at client side and server not installed
BOTH, // Running at client side and both installed
UNDEFINED // Default unknown
}
}
@@ -1,9 +1,9 @@
package me.towdium.jecalculation.command;
import me.towdium.jecalculation.command.commands.CommandHelp;
import me.towdium.jecalculation.command.commands.CommandOreDict;
import me.towdium.jecalculation.command.commands.CommandState;
import me.towdium.jecalculation.command.commands.CommandUuid;
import me.towdium.jecalculation.command.commands.CHelp;
import me.towdium.jecalculation.command.commands.COreDict;
import me.towdium.jecalculation.command.commands.CState;
import me.towdium.jecalculation.command.commands.CUuid;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
@@ -14,20 +14,13 @@ import java.util.HashMap;
*/
@ParametersAreNonnullByDefault
public class Commands {
public static final HashMap<String, ISubCommand> commands;
public static final CommandHelp commandHelp = new CommandHelp();
public static final CommandState commandState = new CommandState();
public static final CommandOreDict commandOreDict = new CommandOreDict();
public static final CommandUuid commandUuid = new CommandUuid();
public static final HashMap<String, ISubCommand> commands = new HashMap<>();
static {
commands = new HashMap<>();
add(commandHelp);
add(commandState);
add(commandOreDict);
add(commandUuid);
add(new CHelp());
add(new CState());
add(new COreDict());
add(new CUuid());
}
static void add(ISubCommand c) {
@@ -23,4 +23,8 @@ public interface ISubCommand {
default List<String> getTabCompletions(ICommandSender sender, String[] args) {
return Collections.emptyList();
}
default String getKey(String key) {
return "jecharacters.command." + getName() + '.' + key;
}
}
@@ -1,5 +1,6 @@
package me.towdium.jecalculation.command;
import me.towdium.jecalculation.command.commands.CHelp;
import me.towdium.jecalculation.utils.wrappers.Wrapper;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
@@ -18,19 +19,19 @@ import java.util.List;
public class JecaCommand extends CommandBase {
@Override
public String getCommandName() {
return "jec";
return "jeca";
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "/jec [help]";
return "/jeca [options]";
}
@Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
if (args.length == 0 || args[0].toLowerCase().equals("help")) {
Commands.commandHelp.execute(sender, cut(args));
if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
Commands.commands.get(CHelp.NAME).execute(sender, cut(args));
} else {
ISubCommand cmd = Commands.commands.get(args[0].toLowerCase());
if (cmd != null) {
@@ -71,4 +72,6 @@ public class JecaCommand extends CommandBase {
: Collections.emptyList();
}
}
}
@@ -15,25 +15,21 @@ import java.util.Comparator;
* Date: 8/10/17.
*/
@ParametersAreNonnullByDefault
public class CommandHelp implements ISubCommand {
public class CHelp implements ISubCommand {
public static final String NAME = "help";
@Override
public String getName() {
return "help";
return NAME;
}
@Override
public String getUsage(ICommandSender sender) {
return "/jec [help]";
return "/jeca [help]";
}
@Override
public void execute(ICommandSender sender, String[] args) {
if (args.length != 0) {
sender.addChatMessage(new ChatComponentTranslation("command.common.unexpected_arg", String.join(" ", args)));
return;
}
sender.addChatMessage(new ChatComponentTranslation("command.help.list"));
sender.addChatMessage(new ChatComponentTranslation(getKey("list")));
Commands.commands.values().stream().sorted(Comparator.comparing(ISubCommand::getName))
.forEachOrdered(c -> sender.addChatMessage(new ChatComponentText(c.getUsage(sender))));
}
@@ -14,7 +14,7 @@ import java.util.Arrays;
* Date: 17-9-11.
*/
@ParametersAreNonnullByDefault
public class CommandOreDict implements ISubCommand {
public class COreDict implements ISubCommand {
@Override
public String getName() {
return "ore";
@@ -22,13 +22,11 @@ public class CommandOreDict implements ISubCommand {
@Override
public String getUsage(ICommandSender sender) {
return "/jec ore";
return "/jeca ore";
}
@Override
public void execute(ICommandSender sender, String[] args) {
if (args.length == 0) {
Arrays.stream(OreDictionary.getOreNames()).forEach(s -> sender.addChatMessage(new ChatComponentText(s)));
} // TODO error handling
Arrays.stream(OreDictionary.getOreNames()).forEach(s -> sender.addChatMessage(new ChatComponentText(s)));
}
}
@@ -13,7 +13,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
* Date: 8/10/17.
*/
@ParametersAreNonnullByDefault
public class CommandState implements ISubCommand {
public class CState implements ISubCommand {
@Override
public String getName() {
return "state";
@@ -21,11 +21,11 @@ public class CommandState implements ISubCommand {
@Override
public String getUsage(ICommandSender sender) {
return "/jec state";
return "/jeca state";
}
@Override
public void execute(ICommandSender sender, String[] args) {
sender.addChatMessage(new ChatComponentTranslation("command.state.desc", JustEnoughCalculation.side.toString()));
sender.addChatMessage(new ChatComponentTranslation(getKey("desc"), JustEnoughCalculation.side.toString()));
}
}
@@ -13,7 +13,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
* Date: 17-10-15.
*/
@ParametersAreNonnullByDefault
public class CommandUuid implements ISubCommand {
public class CUuid implements ISubCommand {
@Override
public String getName() {
return "uuid";
@@ -21,7 +21,7 @@ public class CommandUuid implements ISubCommand {
@Override
public String getUsage(ICommandSender sender) {
return "/jec uuid";
return "/jeca uuid";
}
@@ -9,6 +9,9 @@ import me.towdium.jecalculation.data.structure.Recents;
import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.data.structure.Recipes;
import me.towdium.jecalculation.utils.wrappers.Triple;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
@@ -27,7 +30,7 @@ import java.util.stream.Collectors;
* Date: 17-10-15.
*/
@SideOnly(Side.CLIENT)
public class Controller { // TODO record calculate amount
public class Controller {
public static final String KEY_RECIPES = "recipes";
public static final String KEY_RECENTS = "recents";
static Recipes recipesClient;
@@ -37,6 +40,10 @@ public class Controller { // TODO record calculate amount
return recipesClient;
}
static Optional<ItemStack> getStack() {
return Optional.empty();
}
public static List<String> getGroups() {
Recipes user = getRecord();
if (user.size() != 0)
@@ -72,11 +79,11 @@ public class Controller { // TODO record calculate amount
return getRecord().getRecipes(group);
}
public static Optional<Recipe> getRecipe(ILabel label, Recipe.enumIoType type) {
return getRecord().getRecipe(label, type);
public static Optional<Recipe> getRecipe(ILabel label) {
return getRecord().getRecipe(label);
}
public static List<ILabel> getRecent() { // TODO reduce usage of it since it reads nbt again
public static List<ILabel> getRecent() {
return recentsClient.getRecords();
}
@@ -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
@@ -30,6 +30,12 @@ public class LFluidStack extends ILabel.Impl {
NBTTagCompound nbt;
FluidStack temp;
@Override
public FluidStack getRepresentation() {
return temp;
}
@Override
public boolean acceptPercent() {
return false;
@@ -44,7 +50,7 @@ public class LFluidStack extends ILabel.Impl {
}
public LFluidStack(int amount, Fluid fluid, @Nullable NBTTagCompound nbt) {
super(amount);
super(amount, false);
this.fluid = fluid;
this.nbt = nbt;
temp = new FluidStack(fluid, amount, nbt);
@@ -68,11 +74,12 @@ public class LFluidStack extends ILabel.Impl {
}
@Override
public String getAmountString() {
public String getAmountString(boolean round) {
return amount >= 1000 ? Utilities.cutNumber(amount / 1000f, 4) + "B"
: Integer.toString(amount) + "mB";
: amount + "mB";
}
@Override
@SideOnly(Side.CLIENT)
public String getDisplayName() {
@@ -88,7 +95,7 @@ public class LFluidStack extends ILabel.Impl {
public boolean matches(Object l) {
if (l instanceof LFluidStack) {
LFluidStack lfs = (LFluidStack) l;
return (nbt == null ? lfs.nbt == null : nbt.equals(lfs.nbt)) && fluid == lfs.fluid;
return (Objects.equals(nbt, lfs.nbt)) && fluid == lfs.fluid;
} else return false;
}
@@ -125,4 +132,9 @@ public class LFluidStack extends ILabel.Impl {
public int hashCode() {
return fluid.getUnlocalizedName().hashCode() ^ amount ^ (nbt == null ? 0 : nbt.hashCode());
}
public static boolean merge(ILabel a, ILabel b) {
if (a instanceof LFluidStack && b instanceof LFluidStack) return a.matches(b);
else return false;
}
}
@@ -41,8 +41,8 @@ public class LItemStack extends ILabel.Impl {
boolean fCap;
transient ItemStack temp;
public LItemStack(int amount, Item item, int meta, @Nullable NBTTagCompound nbt) {
super(amount);
public LItemStack(int amount, Item item, int meta, @Nullable NBTTagCompound nbt, boolean percent) {
super(amount, percent);
this.item = item;
this.meta = meta;
this.nbt = nbt == null ? null : (NBTTagCompound) nbt.copy();
@@ -52,13 +52,20 @@ public class LItemStack extends ILabel.Impl {
// Convert from itemStack
public LItemStack(ItemStack is) {
this(is.stackSize, is.getItem(), is.getItemDamage(), is.getTagCompound());
this(is.stackSize, is.getItem(), is.getItemDamage(), is.getTagCompound(), false);
}
public LItemStack(NBTTagCompound nbt) {
this(nbt.getInteger(KEY_AMOUNT), Objects.requireNonNull(Item.getItemById(nbt.getInteger(KEY_ITEM))),
nbt.getInteger(KEY_META), nbt.hasKey(KEY_NBT) ? nbt.getCompoundTag(KEY_NBT) : null);
fromFuzzy(nbt.getByte(KEY_FUZZY));
public LItemStack(NBTTagCompound tag) {
super(tag);
item = Item.getItemById(tag.getInteger(KEY_ITEM));
meta = tag.getInteger(KEY_META);
nbt = tag.hasKey(KEY_NBT) ? tag.getCompoundTag(KEY_NBT) : null;
byte fuzzy = tag.getByte(KEY_FUZZY);
fMeta = (fuzzy & FUZZY_META) != 0;
fCap = (fuzzy & FUZZY_CAP) != 0;
fNbt = (fuzzy & FUZZY_NBT) != 0;
temp = new ItemStack(item, 1, meta);
temp.setTagCompound(tag);
}
private LItemStack(LItemStack lis) {
@@ -66,36 +73,31 @@ public class LItemStack extends ILabel.Impl {
item = lis.item;
meta = lis.meta;
nbt = lis.nbt;
fMeta = lis.fMeta;
fNbt = lis.fNbt;
fCap = lis.fCap;
temp = new ItemStack(item, 1, meta);
temp.setTagCompound(nbt);
}
public static Optional<ILabel> merge(ILabel a, ILabel b, boolean add) {
public static boolean merge(ILabel a, ILabel b) {
if (a instanceof LItemStack && b instanceof LItemStack) {
LItemStack lisA = (LItemStack) a;
LItemStack lisB = (LItemStack) b;
// check meta
int resultMeta = 0;
boolean checkedMeta = true;
if (lisA.meta == WILDCARD_VALUE || lisB.meta == WILDCARD_VALUE)
resultMeta = WILDCARD_VALUE;
else if (lisA.fMeta || lisB.fMeta || lisA.meta == lisB.meta)
resultMeta = lisA.meta;
else
checkedMeta = false;
// check nbt and cap
boolean checkedNbt = (Objects.equals(lisA.nbt, lisB.nbt)) || lisA.fNbt || lisB.fNbt;
if (lisA.item == lisB.item && checkedMeta && checkedNbt) {
LItemStack is = lisA.copy();
is.meta = resultMeta;
return Impl.mergeUnchecked(is, lisB, add);
if (lisA.meta != WILDCARD_VALUE && lisB.meta == WILDCARD_VALUE) return false;
if (!lisA.fNbt) {
if (lisB.fNbt) return false;
else if (lisA.nbt == null) {
if (lisB.nbt != null) return false;
} else if (lisB.nbt == null || !lisA.nbt.equals(lisB.nbt)) return false;
}
if (!lisA.fCap) {
if (lisB.fCap) return false;
}
return lisA.item == lisB.item;
}
return Optional.empty();
return false;
}
public ILabel setFMeta(boolean f) {
@@ -124,6 +126,11 @@ public class LItemStack extends ILabel.Impl {
existing.add(FORMAT_BLUE + FORMAT_ITALIC + Utilities.getModName(item));
}
@Override
public ItemStack getRepresentation() {
return temp;
}
@Override
@SideOnly(Side.CLIENT)
public String getDisplayName() {
@@ -161,20 +168,11 @@ public class LItemStack extends ILabel.Impl {
ret.setInteger(KEY_ITEM, id);
if (nbt != null)
ret.setTag(KEY_NBT, nbt);
ret.setByte(KEY_FUZZY, toFuzzy());
int fuzzy = (fMeta ? FUZZY_META : 0) | (fCap ? FUZZY_CAP : 0) | (fNbt ? FUZZY_NBT : 0);
ret.setByte(KEY_FUZZY, (byte) fuzzy);
return ret;
}
private byte toFuzzy() {
return (byte) ((fMeta ? FUZZY_META : 0) | (fCap ? FUZZY_CAP : 0) | (fNbt ? FUZZY_NBT : 0));
}
private void fromFuzzy(byte i) {
fMeta = (i & FUZZY_META) != 0;
fCap = (i & FUZZY_CAP) != 0;
fNbt = (i & FUZZY_NBT) != 0;
}
@Override
@SideOnly(Side.CLIENT)
public void drawLabel(JecaGui gui) {
@@ -3,10 +3,9 @@ package me.towdium.jecalculation.data.label.labels;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.polyfill.mc.client.renderer.GlStateManager;
import me.towdium.jecalculation.polyfill.mc.util.NonNullList;
import me.towdium.jecalculation.utils.Utilities;
import net.minecraft.creativetab.CreativeTabs;
@@ -14,6 +13,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.oredict.OreDictionary;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.HashSet;
@@ -30,7 +30,7 @@ public class LOreDict extends ILabel.Impl {
public static final String IDENTIFIER = "oreDict";
public static final String KEY_NAME = "name";
public static final String KEY_AMOUNT = "amount";
public static final boolean MODE_FORCE = true;
public static final boolean MODE_FORCE = false;
protected String name;
@@ -39,7 +39,7 @@ public class LOreDict extends ILabel.Impl {
}
public LOreDict(String name, int amount) {
super(amount);
super(amount, false);
this.name = name;
}
@@ -53,35 +53,45 @@ public class LOreDict extends ILabel.Impl {
name = nbt.getString(KEY_NAME);
}
@Override
@Nonnull
public ItemStack getRepresentation() {
NonNullList<ItemStack> list = NonNullList.create();
OreDictionary.getOres(name).forEach(is -> {
if (is.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
is.getItem().getSubItems(is.getItem(), CreativeTabs.tabAllSearch, list);
} else
list.add(is);
});
long index = System.currentTimeMillis() / 1500;
return list.get((int) (index % list.size()));
}
@Override
@SideOnly(Side.CLIENT)
public String getDisplayName() {
return Utilities.I18n.format("label.ore_dict.name", name);
}
public static Optional<ILabel> merge(ILabel a, ILabel b, boolean add) {
public static boolean mergeSame(ILabel a, ILabel b) {
if (a instanceof LOreDict && b instanceof LOreDict) {
LOreDict lodA = (LOreDict) a;
LOreDict lodB = (LOreDict) b;
if (lodA.getName().equals(lodB.getName())) {
return Impl.mergeUnchecked(lodA, lodB, add);
}
} else if ((a instanceof LOreDict && b instanceof LItemStack)
|| a instanceof LItemStack && b instanceof LOreDict) {
LItemStack lis;
LOreDict lor;
if (a instanceof LOreDict) {
lis = (LItemStack) b;
lor = (LOreDict) a;
} else {
lis = (LItemStack) a;
lor = (LOreDict) b;
}
return OreDictionary.getOres(lor.name).stream().map(o -> Converter.from(o).multiply(lor.amount))
.map(i -> LItemStack.merge(i, lis, add)).filter(Optional::isPresent)
.findAny().flatMap(i -> i);
return lodA.getName().equals(lodB.getName());
} else return false;
}
public static boolean mergeFuzzy(ILabel a, ILabel b) {
if (a instanceof LOreDict && b instanceof LItemStack) {
LOreDict lod = (LOreDict) a;
LItemStack lis = (LItemStack) b;
if (lod.getAmount() > 0)
return false;
return OreDictionary.getOres(lod.name).stream()
.map(Converter::from)
.anyMatch(i -> LItemStack.merge(i, lis));
}
return Optional.empty();
return false;
}
public static List<ILabel> guess(List<ILabel> iss) {
@@ -89,24 +99,24 @@ public class LOreDict extends ILabel.Impl {
HashSet<Integer> ids = new HashSet<>();
int amount = l.getAmount();
for (int i : OreDictionary.getOreIDs(((LItemStack) l).getRep()))
if (check(i, iss)) ids.add(i);
return ids.stream().map(i -> new LOreDict(OreDictionary.getOreName(i), amount))
.collect(Collectors.toList());
if (check(i, iss))
ids.add(i);
return ids.stream().map(i -> new LOreDict(OreDictionary.getOreName(i), amount)).collect(Collectors.toList());
}
// check labels in the list suitable for the ore id
private static boolean check(int id, List<ILabel> labels) {
ArrayList<ItemStack> ores = OreDictionary.getOres(OreDictionary.getOreName(id));
if (labels.size() == 1 && ores.size() == 1
&& (ores.get(0).getItemDamage() == OreDictionary.WILDCARD_VALUE || !MODE_FORCE))
if (labels.size() == 1 && ores.size() == 1 &&
(ores.get(0).getItemDamage() == OreDictionary.WILDCARD_VALUE || !MODE_FORCE))
return false;
boolean acceptable = true;
for (ItemStack ore : ores) {
boolean found = false;
for (ILabel label : labels) {
if (label instanceof LItemStack && OreDictionary.itemMatches(
ore, ((LItemStack) label).getRep(), false)) {
if (label instanceof LItemStack &&
OreDictionary.itemMatches(ore, ((LItemStack) label).getRep(), false)) {
found = true;
break;
}
@@ -119,8 +129,8 @@ public class LOreDict extends ILabel.Impl {
for (ILabel label : labels) {
boolean found = false;
for (ItemStack ore : ores) {
if (label instanceof LItemStack && OreDictionary.itemMatches(
ore, ((LItemStack) label).getRep(), false)) {
if (label instanceof LItemStack &&
OreDictionary.itemMatches(ore, ((LItemStack) label).getRep(), false)) {
found = true;
break;
}
@@ -155,9 +165,11 @@ public class LOreDict extends ILabel.Impl {
OreDictionary.getOres(name).forEach(is -> {
if (is.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
is.getItem().getSubItems(is.getItem(), CreativeTabs.tabAllSearch, list);
} else list.add(is);
} else
list.add(is);
});
if(list.isEmpty()) return;
if (list.isEmpty())
return;
long index = System.currentTimeMillis() / 1500;
//GlStateManager.pushMatrix();
//GlStateManager.translate(1, 1, 0);
@@ -9,6 +9,7 @@ import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.utils.Utilities;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.stream.Collectors;
@@ -27,7 +28,8 @@ public class LPlaceholder extends ILabel.Impl {
String name;
public LPlaceholder(NBTTagCompound tag) {
this(tag.getString(KEY_NAME), tag.getInteger(KEY_AMOUNT));
super(tag);
name = tag.getString(KEY_NAME);
}
public LPlaceholder(String name, int amount) {
@@ -35,7 +37,7 @@ public class LPlaceholder extends ILabel.Impl {
}
public LPlaceholder(String name, int amount, boolean silent) {
super(amount);
super(amount, false);
this.name = name;
if (!silent) getRecord().push(new LPlaceholder(name, 1, true));
}
@@ -82,6 +84,12 @@ public class LPlaceholder extends ILabel.Impl {
return name.hashCode() ^ amount;
}
@Nullable
@Override
public Object getRepresentation() {
return null;
}
@Override
@SideOnly(Side.CLIENT)
public String getDisplayName() {
@@ -1,15 +1,16 @@
package me.towdium.jecalculation.algorithm;
package me.towdium.jecalculation.data.structure;
import me.towdium.jecalculation.data.Controller;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.utils.wrappers.Pair;
import java.util.*;
import java.util.stream.Collectors;
import static me.towdium.jecalculation.data.structure.Recipe.enumIoType.INPUT;
import static me.towdium.jecalculation.data.structure.Recipe.enumIoType.OUTPUT;
// positive => generate; negative => require
public class CostList {
List<ILabel> labels;
@@ -22,65 +23,61 @@ public class CostList {
}
public CostList(List<ILabel> labels) {
this.labels = labels.stream().map(i -> i.copy().multiply(-1)).collect(Collectors.toList());
this.labels = labels.stream()
.filter(i -> i != ILabel.EMPTY)
.map(i -> i.copy().multiply(-1))
.collect(Collectors.toList());
}
public CostList(List<ILabel> positive, List<ILabel> negative) {
labels = positive.stream().map(ILabel::copy).collect(Collectors.toList());
negative.forEach(i -> {
ILabel l = i.copy();
labels.add(l.multiply(-1));
});
this(positive);
multiply(-1);
merge(new CostList(negative), false, true);
}
public CostList(Recipe recipe) {
this(Arrays.stream(recipe.getLabel(OUTPUT)).filter(i -> i != ILabel.EMPTY).collect(Collectors.toList()),
Arrays.stream(recipe.getLabel(Recipe.enumIoType.INPUT)).filter(i -> i != ILabel.EMPTY)
.collect(Collectors.toList()));
Arrays.stream(recipe.getLabel(INPUT)).filter(i -> i != ILabel.EMPTY).collect(Collectors.toList()));
}
@SuppressWarnings("UnusedReturnValue")
public CostList merge(CostList costList, boolean add, boolean strict) {
CostList ret = copy();
costList.labels.forEach(i -> ret.labels.add(add ? i.copy() : i.copy().multiply(-1)));
ret.cancel(strict);
return ret;
}
public void multiply(int i) {
labels = labels.stream().map(j -> j.multiply(i)).collect(Collectors.toList());
}
private void cancel(boolean strict) {
for (int i = 0; i < labels.size(); i++) {
for (int j = 0; j < labels.size(); j++) {
if (i == j)
continue;
public CostList merge(CostList costList, boolean strict, boolean inplace) {
CostList ret = inplace ? this : copy();
costList.labels.forEach(i -> ret.labels.add(i.copy()));
for (int i = 0; i < ret.labels.size(); i++) {
for (int j = i + 1; j < ret.labels.size(); j++) {
if (strict) {
ILabel a = labels.get(i);
ILabel b = labels.get(j);
ILabel a = ret.labels.get(i);
ILabel b = ret.labels.get(j);
if (a.matches(b)) {
labels.set(i, a.setAmount(a.getAmount() + b.getAmount()));
labels.set(j, ILabel.EMPTY);
ret.labels.set(i, a.setAmount(a.getAmount() + b.getAmount()));
ret.labels.set(j, ILabel.EMPTY);
}
} else {
Optional<ILabel> l = ILabel.MERGER.merge(labels.get(i), labels.get(j), true);
Optional<ILabel> l = ILabel.MERGER.merge(ret.labels.get(i), ret.labels.get(j));
if (l.isPresent()) {
labels.set(i, l.get());
labels.set(j, ILabel.EMPTY);
ret.labels.set(i, l.get());
ret.labels.set(j, ILabel.EMPTY);
}
}
}
}
labels = labels.stream().filter(i -> i != ILabel.EMPTY).collect(Collectors.toList());
ret.labels = ret.labels.stream().filter(i -> i != ILabel.EMPTY).collect(Collectors.toList());
return ret;
}
public CostList multiply(int i) {
labels = labels.stream().map(j -> j.multiply(i)).collect(Collectors.toList());
return this;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof CostList) {
CostList c = (CostList) obj;
CostList m = c.copy();
return m.merge(this, false, true).labels.isEmpty();
CostList m = c.copy().multiply(-1);
return merge(m, true, false).labels.isEmpty();
} else
return false;
}
@@ -126,7 +123,7 @@ public class CostList {
CostList original = getCurrent();
CostList difference = new CostList(next.one);
difference.multiply(next.two);
CostList result = original.merge(difference, true, false);
CostList result = original.merge(difference, false, false);
if (set.contains(result))
next = find(false);
else {
@@ -146,9 +143,8 @@ public class CostList {
ILabel label = labels.get(index);
if (label.getAmount() >= 0)
continue;
Optional<Recipe> recipe = Controller.getRecipe(label, OUTPUT);
if (recipe.isPresent())
return new Pair<>(recipe.get(), recipe.get().multiplier(label, OUTPUT));
Optional<Recipe> recipe = Controller.getRecipe(label);
if (recipe.isPresent()) return new Pair<>(recipe.get(), recipe.get().multiplier(label));
}
return null;
}
@@ -178,5 +174,29 @@ public class CostList {
return getCurrent().labels.stream().filter(i -> i.getAmount() < 0).map(i -> i.copy().multiply(-1))
.collect(Collectors.toList());
}
public List<ILabel> getOutputs(List<ILabel> ignore) {
return getCurrent().labels.stream()
.map(i -> i.copy().multiply(-1))
.map(i -> {
for (ILabel j : ignore) {
Optional<ILabel> merged = ILabel.MERGER.merge(i, j);
if (merged.isPresent()) i = merged.get();
}
return i;
})
.filter(i -> i != ILabel.EMPTY && i.getAmount() < 0)
.map(i -> i.multiply(-1))
.collect(Collectors.toList());
}
public List<ILabel> getSteps() {
//noinspection OptionalGetWithoutIsPresent
List<ILabel> ret = procedure.stream()
.map(i -> i.two.labels.stream().filter(l -> l.getAmount() > 0).findFirst().get())
.collect(Collectors.toList());
Collections.reverse(ret);
return ret;
}
}
}
@@ -105,15 +105,20 @@ public class Recipe {
return ret;
}
public boolean matches(ILabel label, enumIoType type) {
return Arrays.stream(getLabel(type)).anyMatch(i -> ILabel.MERGER.merge(label, i, true).isPresent());
public boolean matches(ILabel label) {
return Arrays.stream(output).anyMatch(i -> ILabel.MERGER.merge(label, i).isPresent());
}
public int multiplier(ILabel label, enumIoType type) {
return Arrays.stream(getLabel(type)).filter(i -> ILabel.MERGER.merge(label, i, true).isPresent()).findAny()
.map(i -> (i.getAmount() + Math.abs(label.getAmount()) - 1) / i.getAmount()).orElse(0);
public int 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;
return (amountB + Math.abs(amountA) - 1) / amountB;
}).orElse(0);
}
public enum enumIoType {
INPUT, OUTPUT, CATALYST;
@@ -95,10 +95,9 @@ public class Recipes {
return flatStream(group).collect(Collectors.toCollection(ArrayList::new));
}
public Optional<Recipe> getRecipe(ILabel label, Recipe.enumIoType type) {
return flatStream().map(i -> i.one).filter(i -> i.matches(label, type)).findFirst();
public Optional<Recipe> getRecipe(ILabel label) {
return flatStream().map(i -> i.one).filter(i -> i.matches(label)).findFirst();
}
public void forEach(BiConsumer<String, List<Recipe>> consumer) {
records.forEach(consumer);
}
@@ -25,7 +25,7 @@ public class FMLBusEventHandler {
public void onKey(InputEvent.KeyInputEvent event) {
if (ClientHandler.keyOpenGui.isPressed()) {
if (JustEnoughCalculation.side == JustEnoughCalculation.enumSide.CLIENT)
JecaGui.displayGui(new GuiCalculator());
JecaGui.displayGui(true, true, new GuiCalculator());
else
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentTranslation("chat.server_mode"));
}
@@ -34,7 +34,7 @@ public class FMLBusEventHandler {
@SubscribeEvent
public void onLogout(PlayerEvent.PlayerLoggedOutEvent event) {
JustEnoughCalculation.logger.info("on log out event");
Controller.writeToLocal();;
Controller.writeToLocal();
LPlaceholder.onLogOut();
}
}
@@ -5,7 +5,8 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.gui.drawables.WContainer;
import me.towdium.jecalculation.gui.guis.IGui;
import me.towdium.jecalculation.gui.widgets.WContainer;
import me.towdium.jecalculation.nei.NEIPlugin;
import me.towdium.jecalculation.polyfill.mc.client.renderer.GlStateManager;
import me.towdium.jecalculation.utils.Utilities;
@@ -25,10 +26,7 @@ import org.lwjgl.input.Mouse;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
* Author: towdium
@@ -43,14 +41,15 @@ public class JecaGui extends GuiContainer {
public static final int COLOR_TEXT_WHITE = 0xFFFFFF;
public static final boolean ALWAYS_TOOLTIP = false;
public ILabel hand = ILabel.EMPTY;
public WContainer root;
protected static JecaGui last;
protected JecaGui parent;
public IGui root;
public JecaGui(@Nullable JecaGui parent, WContainer root) {
public JecaGui(@Nullable JecaGui parent, IGui root) {
this(parent, false, root);
}
public JecaGui(@Nullable JecaGui parent, boolean acceptsTransfer, WContainer root) {
public JecaGui(@Nullable JecaGui parent, boolean acceptsTransfer, IGui root) {
super(acceptsTransfer ? new ContainerTransfer() : new ContainerNonTransfer());
this.parent = parent;
this.root = root;
@@ -62,11 +61,11 @@ public class JecaGui extends GuiContainer {
return xMouse > xPos && yMouse > yPos && xMouse <= xPos + xSize && yMouse <= yPos + ySize;
}
public static void displayGui(WContainer root) {
public static void displayGui(IGui root) {
displayGui(true, false, root);
}
public static void displayGui(boolean updateParent, boolean acceptsTransfer, WContainer root) {
public static void displayGui(boolean updateParent, boolean acceptsTransfer, IGui root) {
// isCallingFromMinecraftThread
if (Minecraft.getMinecraft().func_152345_ab())
displayGuiUnsafe(updateParent, acceptsTransfer, root);
@@ -86,25 +85,30 @@ public class JecaGui extends GuiContainer {
private static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, WContainer root) {
private static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, IGui root) {
Minecraft mc = Minecraft.getMinecraft();
JecaGui parent;
if (mc.currentScreen == null)
parent = null;
else if (!(mc.currentScreen instanceof JecaGui))
parent = getCurrent();
parent = last;
else if (updateParent)
parent = (JecaGui) mc.currentScreen;
else
parent = ((JecaGui) mc.currentScreen).parent;
JecaGui toShow = new JecaGui(parent, acceptsTransfer, root);
root.onVisible(toShow);
last = toShow;
mc.displayGuiScreen(toShow);
}
public static void displayParent() {
// isCallingFromMinecraftThread
if (Minecraft.getMinecraft().func_152345_ab()) {
Minecraft.getMinecraft().displayGuiScreen(getCurrent().parent);
JecaGui gui = getCurrent().parent;
gui.root.onVisible(gui);
last = gui;
Minecraft.getMinecraft().displayGuiScreen(gui);
}
}
@@ -389,7 +393,7 @@ public class JecaGui extends GuiContainer {
}
public void drawHoveringText(String text, int x, int y) {
super.drawHoveringText(Arrays.asList(text), x, y, fontRendererObj);
super.drawHoveringText(Collections.singletonList(text), x, y, fontRendererObj);
}
public void drawHoveringText(List<String> textLines, int x, int y) {
@@ -141,10 +141,7 @@ public class Resource {
public static final ResourceGroup ICN_CATALYST = new ResourceGroup(ICN_CATALYST_N, ICN_CATALYST_F);
public static final ResourceGroup ICN_LIST = new ResourceGroup(ICN_LIST_N, ICN_LIST_F);
public static final ResourceGroup ICN_LABEL = new ResourceGroup(ICN_LABEL_N, ICN_LABEL_F);
public static final ResourceGroup ICN_HELP = new ResourceGroup(ICN_HELP_N, ICN_HELP_F);
public static final ResourceGroup ICN_STACK = new ResourceGroup(ICN_STACK_N, ICN_STACK_F);
public static final ResourceGroup ICN_TEXT = new ResourceGroup(ICN_TEXT_N, ICN_TEXT_F);
public static final ResourceGroup ICN_MULTI = new ResourceGroup(ICN_MULTI_N, ICN_MULTI_F);
protected int xPos, yPos, xSize, ySize;
protected ResourceLocation rl;
@@ -2,16 +2,24 @@ package me.towdium.jecalculation.gui.guis;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.algorithm.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.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.gui.drawables.*;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.Controller;
import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.utils.ItemStackHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* Author: towdium
@@ -19,30 +27,30 @@ import java.util.List;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class GuiCalculator extends WContainer {
public class GuiCalculator extends WContainer implements IGui {
enumMode mode = enumMode.INPUT;
CostList.Calculator calculator = null;
WLabelGroup lgRecent = new WLabelGroup(7, 31, 8, 1, WLabel.enumMode.PICKER);
WLabel lLabel = new WLabel(31, 7, 20, 20, WLabel.enumMode.SELECTOR);
WLabelScroll lsResult = new WLabelScroll(7, 87, 8, 4, WLabel.enumMode.RESULT, true);
WButton btnSteps = new WButtonIcon(64, 62, 20, 20, Resource.BTN_LIST, "calculator.step")
.setLsnrLeft(() -> setMode(enumMode.STEPS));
WButton btnCatalyst = new WButtonIcon(45, 62, 20, 20, Resource.BTN_CAT, "calculator.catalyst")
.setLsnrLeft(() -> setMode(enumMode.CATALYST));
WButton btnOutput = new WButtonIcon(26, 62, 20, 20, Resource.BTN_OUT, "calculator.output")
.setLsnrLeft(() -> setMode(enumMode.OUTPUT));
WButton btnInput = new WButtonIcon(7, 62, 20, 20, Resource.BTN_IN, "calculator.input")
WLabelGroup recent = new WLabelGroup(7, 31, 8, 1, WLabel.enumMode.PICKER);
WLabel label = new WLabel(31, 7, 20, 20, WLabel.enumMode.SELECTOR);
WLabelScroll result = new WLabelScroll(7, 87, 8, 4, WLabel.enumMode.RESULT, true);
WButton input = new WButtonIcon(7, 62, 20, 20, Resource.BTN_IN, "calculator.input")
.setLsnrLeft(() -> setMode(enumMode.INPUT));
WTextField tfAmount = new WTextField(60, 7, 65);
WButton output = new WButtonIcon(26, 62, 20, 20, Resource.BTN_OUT, "calculator.output")
.setLsnrLeft(() -> setMode(enumMode.OUTPUT));
WButton catalyst = new WButtonIcon(45, 62, 20, 20, Resource.BTN_CAT, "calculator.catalyst")
.setLsnrLeft(() -> setMode(enumMode.CATALYST));
WButton steps = new WButtonIcon(64, 62, 20, 20, Resource.BTN_LIST, "calculator.step")
.setLsnrLeft(() -> setMode(enumMode.STEPS));
WTextField amount = new WTextField(60, 7, 65);
public GuiCalculator() {
lLabel.setLsnrUpdate(() -> {
Controller.setRecent(lLabel.label);
label.setLsnrUpdate(() -> {
Controller.setRecent(label.label);
refreshRecent();
refreshCalculator();
});
lgRecent.setLsnrUpdate(l -> JecaGui.getCurrent().hand = lgRecent.getLabelAt(l));
tfAmount.setLsnrText(s -> refreshCalculator());
recent.setLsnrUpdate(l -> JecaGui.getCurrent().hand = recent.getLabelAt(l));
amount.setLsnrText(s -> refreshCalculator());
add(new WHelp("calculator"));
add(new WPanel());
add(new WButtonIcon(7, 7, 20, 20, Resource.BTN_LABEL, "calculator.label")
@@ -57,56 +65,73 @@ public class GuiCalculator extends WContainer {
add(new WText(53, 13, JecaGui.Font.PLAIN, "x"));
add(new WLine(55));
add(new WIcon(151, 31, 18, 18, Resource.ICN_RECENT, "calculator.history"));
addAll(lgRecent, lLabel, btnInput, btnOutput, btnCatalyst, btnSteps, lsResult, tfAmount);
addAll(recent, label, input, output, catalyst, steps, result, amount);
refreshRecent();
setMode(enumMode.INPUT);
}
@Override
public void onVisible(JecaGui gui) {
refreshCalculator();
}
void setMode(enumMode mode) {
this.mode = mode;
btnInput.setDisabled(mode == enumMode.INPUT);
btnOutput.setDisabled(mode == enumMode.OUTPUT);
btnCatalyst.setDisabled(mode == enumMode.CATALYST);
btnSteps.setDisabled(mode == enumMode.STEPS);
input.setDisabled(mode == enumMode.INPUT);
output.setDisabled(mode == enumMode.OUTPUT);
catalyst.setDisabled(mode == enumMode.CATALYST);
steps.setDisabled(mode == enumMode.STEPS);
refreshResult();
}
void refreshRecent() {
List<ILabel> recent = Controller.getRecent();
if (!recent.isEmpty()) lLabel.setLabel(recent.get(0));
if (recent.size() > 1) lgRecent.setLabel(recent.subList(1, recent.size()), 0);
if (!recent.isEmpty()) label.setLabel(recent.get(0));
if (recent.size() > 1) this.recent.setLabel(recent.subList(1, recent.size()), 0);
}
void refreshCalculator() {
try {
String s = tfAmount.getText();
int i = s.isEmpty() ? 1 : Integer.parseInt(tfAmount.getText());
tfAmount.setColor(JecaGui.COLOR_TEXT_WHITE);
calculator = new CostList(lLabel.getLabel().copy().setAmount(i)).calculate();
String s = amount.getText();
int i = s.isEmpty() ? 1 : Integer.parseInt(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) {
tfAmount.setColor(JecaGui.COLOR_TEXT_RED);
amount.setColor(JecaGui.COLOR_TEXT_RED);
calculator = null;
}
refreshResult();
}
List<ILabel> getInventory() {
InventoryPlayer inv = Minecraft.getMinecraft().thePlayer.inventory;
ArrayList<ILabel> labels = new ArrayList<>();
Consumer<Stream<ItemStack>> add = i -> i
.filter(j -> !ItemStackHelper.isEmpty(j))
.forEach(j -> labels.add(ILabel.Converter.from(j)));
add.accept(Arrays.stream(inv.armorInventory));
add.accept(Arrays.stream(inv.mainInventory));
return labels;
}
void refreshResult() {
if (calculator == null) {
lsResult.setLabels(new ArrayList<>());
result.setLabels(new ArrayList<>());
} else {
switch (mode) {
case INPUT:
lsResult.setLabels(calculator.getInputs());
result.setLabels(calculator.getInputs());
break;
case OUTPUT:
lsResult.setLabels(new ArrayList<>());
result.setLabels(calculator.getOutputs(getInventory()));
break;
case CATALYST:
lsResult.setLabels(calculator.getCatalysts());
result.setLabels(calculator.getCatalysts());
break;
case STEPS:
lsResult.setLabels(new ArrayList<>());
result.setLabels(calculator.getSteps());
break;
}
}
@@ -2,9 +2,9 @@ package me.towdium.jecalculation.gui.guis;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWPicker;
import me.towdium.jecalculation.gui.guis.pickers.IPicker;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.gui.drawables.*;
import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.data.label.ILabel;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -16,7 +16,7 @@ import java.util.List;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class GuiDisambiguation extends IWPicker.Impl {
public class GuiDisambiguation extends IPicker.Impl implements IGui {
protected WLabelScroll lsUp;
protected WLabelScroll lsDown;
protected WSwitcher switcher;
@@ -31,20 +31,17 @@ public class GuiDisambiguation extends IWPicker.Impl {
add(new WPanel());
add(switcher);
add(new WIcon(149, 24, 20, 20, Resource.ICN_HELP, "disambiguation.help"));
add(new WIcon(7, 48, 18, 54, Resource.ICN_LIST, "disambiguation.list"));
add(new WIcon(7, 105, 18, 54, Resource.ICN_LABEL, "disambiguation.label"));
add(new WSearch(i -> {
if (callback != null) callback.accept(i.copy().increaseAmount());
if (callback != null) callback.accept(i.copy().multiply(-1));
}, tf, lsUp, lsDown));
setPage(0);
}
protected void setPage(int n) { // TODO
protected void setPage(int n) {
lsUp.setLabels(record.get(n));
lsDown.setLabels(ILabel.CONVERTER.guess(record.get(n)));
//lsUp.setLabels(record.get(n).stream().map(ILabel.CONVERTER_ITEM::toLabel).collect(Collectors.toList()));
//lsDown.setLabels(ILabel.CONVERTER_ITEM.toLabel(record.get(n)));
}
}
@@ -6,10 +6,7 @@ import com.google.common.cache.LoadingCache;
import com.google.common.cache.Weigher;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.drawables.WContainer;
import me.towdium.jecalculation.gui.drawables.WPage;
import me.towdium.jecalculation.gui.drawables.WPanel;
import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.utils.wrappers.Wrapper;
@@ -22,7 +19,7 @@ import java.util.function.Consumer;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class GuiLabel extends WContainer {
public class GuiLabel extends WContainer implements IGui {
WContainer container = new WContainer();
Consumer<ILabel> callback;
LoadingCache<Integer, IWidget> cache = CacheBuilder.newBuilder().concurrencyLevel(1)
@@ -43,7 +40,7 @@ public class GuiLabel extends WContainer {
add(new WPage(i, r, false).setListener(() -> refresh(i)));
index.value += 1;
});
add(new WPanel());
addAll(new WHelp("label"), new WPanel());
add(container);
refresh(0);
}
@@ -1,13 +1,13 @@
package me.towdium.jecalculation.gui.guis;
import codechicken.nei.recipe.IRecipeHandler;
import me.towdium.jecalculation.algorithm.CostList;
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.Recipe;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.gui.drawables.*;
import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.nei.NEIPlugin;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Pair;
@@ -22,7 +22,7 @@ import java.util.stream.Stream;
* Author: towdium
* Date: 17-9-8.
*/
public class GuiRecipe extends WContainer {
public class GuiRecipe extends WContainer implements IGui {
Pair<String, Integer> dest;
HashMap<Integer, List<ILabel>> disambiguation = new HashMap<>();
WSwitcher switcherGroup = new WSwitcher(7, 7, 162, Controller.getGroups());
@@ -88,10 +88,12 @@ public class GuiRecipe extends WContainer {
Recipe r = Controller.getRecipe(group, index);
fromRecipe(r);
switcherGroup.setIndex(Controller.getGroups().indexOf(group));
buttonCopy.setDisabled(false);
buttonDel.setDisabled(false);
}
public GuiRecipe() {
add(new WPanel());
addAll(new WHelp("recipe"), new WPanel());
add(new WIcon(7, 63, 21, 20, Resource.ICN_OUTPUT, "recipe.output"));
add(new WIcon(7, 87, 21, 20, Resource.ICN_CATALYST, "recipe.catalyst"));
add(new WIcon(7, 111, 21, 40, Resource.ICN_INPUT, "recipe.input"));
@@ -151,8 +153,8 @@ public class GuiRecipe extends WContainer {
dst.stream().filter(p -> {
CostList cl = new CostList(list);
if (p.three.equals(cl)) {
ILabel.MERGER.merge(p.one, ILabel.CONVERTER.first(list), true).ifPresent(i -> p.one = i);
p.two = p.two.merge(cl, true, true);
ILabel.MERGER.merge(p.one, ILabel.CONVERTER.first(list)).ifPresent(i -> p.one = i);
p.two = p.two.merge(cl, true, false);
return true;
} else
return false;
@@ -1,10 +1,10 @@
package me.towdium.jecalculation.gui.guis;
import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.data.Controller;
import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.gui.drawables.*;
import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Triple;
@@ -12,29 +12,39 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GuiSearch extends WContainer {
public class GuiSearch extends WContainer implements IGui {
List<Triple<Recipe, String, Integer>> content;
WSwitcher switcherGroup;
WLabelScroll labelScroll;
public GuiSearch() {
ArrayList<String> groups = new ArrayList<>();
groups.add(Utilities.I18n.format("gui.search.all"));
groups.addAll(Controller.getGroups());
switcherGroup = new WSwitcher(7, 7, 162, groups).setListener(this::refresh);
labelScroll = new WLabelScroll(7, 51, 8, 6, WLabel.enumMode.PICKER, true);
add(new WPanel());
add(switcherGroup);
add(new WSearch(i -> content.stream().filter(j -> j.one.getRep() == i).findFirst().ifPresent(j ->
JecaGui.displayGui(false, true, new GuiRecipe(j.two, j.three))), new WTextField(25, 27, 90), labelScroll));
add(new WIcon(149, 27, 20, 20, Resource.ICN_HELP, "search.help"));
add(new WSearch(i -> content.stream().filter(j -> j.one.getRep() == i).findFirst()
.ifPresent(j -> JecaGui.displayGui(true, true, new GuiRecipe(j.two, j.three))),
new WTextField(25, 27, 90), labelScroll));
add(new WIcon(7, 27, 20, 20, Resource.ICN_TEXT, "search.text"));
refresh();
}
public void refresh() {
int iGroup = switcherGroup.getIndex();
content = iGroup == 0 ? Controller.getRecipes() : Controller.getRecipes(switcherGroup.getText());
labelScroll.setLabels(content.stream().map(i -> i.one.getRep()).collect(Collectors.toCollection(ArrayList::new)));
labelScroll
.setLabels(content.stream().map(i -> i.one.getRep()).collect(Collectors.toCollection(ArrayList::new)));
}
@Override
public void onVisible(JecaGui gui) {
remove(switcherGroup);
ArrayList<String> groups = new ArrayList<>();
groups.add(Utilities.I18n.format("gui.search.all"));
groups.addAll(Controller.getGroups());
int index = switcherGroup == null ? 0 : switcherGroup.getIndex();
if (index >= groups.size())
index = groups.size() - 1;
switcherGroup = new WSwitcher(7, 7, 162, groups).setListener(this::refresh);
switcherGroup.setIndex(index);
add(switcherGroup);
refresh();
}
}
@@ -0,0 +1,13 @@
package me.towdium.jecalculation.gui.guis;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.widgets.IContainer;
/**
* Author: Towdium
* Date: 18-9-25
*/
public interface IGui extends IContainer {
default void onVisible(JecaGui gui) {
}
}
@@ -1,10 +1,11 @@
package me.towdium.jecalculation.gui;
package me.towdium.jecalculation.gui.guis.pickers;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.drawables.WContainer;
import me.towdium.jecalculation.gui.guis.IGui;
import me.towdium.jecalculation.gui.widgets.IWidget;
import me.towdium.jecalculation.gui.widgets.WContainer;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.utils.wrappers.Wrapper;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.function.Consumer;
@@ -15,10 +16,10 @@ import java.util.function.Consumer;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public interface IWPicker extends IWidget {
IWPicker setCallback(Consumer<ILabel> callback);
public interface IPicker extends IGui {
IPicker setCallback(Consumer<ILabel> callback);
class Impl extends WContainer implements IWPicker {
class Impl extends WContainer implements IPicker {
protected Consumer<ILabel> callback;
@Override
@@ -4,11 +4,11 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.label.labels.LItemStack;
import me.towdium.jecalculation.gui.IWPicker;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.gui.drawables.WButton;
import me.towdium.jecalculation.gui.drawables.WButtonIcon;
import me.towdium.jecalculation.gui.drawables.WLabel;
import me.towdium.jecalculation.gui.guis.IGui;
import me.towdium.jecalculation.gui.widgets.WButton;
import me.towdium.jecalculation.gui.widgets.WButtonIcon;
import me.towdium.jecalculation.gui.widgets.WLabel;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.function.Consumer;
@@ -19,7 +19,7 @@ import java.util.function.Consumer;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class PickerItemStack extends IWPicker.Impl {
public class PickerItemStack extends IPicker.Impl implements IGui {
WLabel label = new WLabel(7, 7, 20, 20, WLabel.enumMode.SELECTOR).setLsnrUpdate(this::update);
WButton bConfirm = new WButtonIcon(149, 7, 20, 20, Resource.BTN_YES).setLsnrLeft(() -> callback.accept(label.getLabel()));
WButton bNbtF = new WButtonIcon(49, 7, 20, 20, Resource.BTN_NBT_F).setLsnrLeft(() -> setFNbt(false));
@@ -3,10 +3,10 @@ package me.towdium.jecalculation.gui.guis.pickers;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
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.Resource;
import me.towdium.jecalculation.gui.drawables.*;
import me.towdium.jecalculation.gui.guis.IGui;
import me.towdium.jecalculation.gui.widgets.*;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -16,7 +16,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class PickerPlaceholder extends IWPicker.Impl {
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());
@@ -24,8 +24,6 @@ public class PickerPlaceholder extends IWPicker.Impl {
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(l -> callback.accept(l), search, scroll));
add(new WIcon(149, 45, 20, 20, Resource.ICN_HELP, "picker_placeholder.help_search"));
add(new WIcon(149, 7, 20, 20, Resource.ICN_HELP, "picker_placeholder.help_create"));
add(new WIcon(7, 45, 20, 20, Resource.ICN_TEXT, "picker_placeholder.text_search"));
add(new WIcon(7, 7, 20, 20, Resource.ICN_TEXT, "picker_placeholder.text_create"));
add(new WLine(36));
@@ -2,9 +2,9 @@ package me.towdium.jecalculation.gui.guis.pickers;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWPicker;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.gui.drawables.*;
import me.towdium.jecalculation.gui.guis.IGui;
import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.label.labels.LFluidStack;
import me.towdium.jecalculation.data.label.labels.LOreDict;
@@ -22,7 +22,7 @@ import java.util.stream.Collectors;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class PickerSimple extends IWPicker.Impl {
public class PickerSimple extends IPicker.Impl implements IGui {
/**
* @param labels label to be displayed for selection
* @param l18nKey localization key for help string,
@@ -32,15 +32,14 @@ public class PickerSimple extends IWPicker.Impl {
WLabelScroll ls = new WLabelScroll(7, 33, 8, 7, WLabel.enumMode.PICKER, true).setLabels(labels);
WTextField tf = new WTextField(26, 7, 90);
add(new WSearch(l -> callback.accept(l), tf, ls));
add(new WIcon(149, 7, 20, 20, Resource.ICN_HELP, l18nKey + ".help"));
add(new WIcon(7, 7, 20, 20, Resource.ICN_TEXT, l18nKey + ".text"));
}
public static class FluidStack extends PickerSimple {
public FluidStack() {
super(FluidRegistry.getRegisteredFluids().entrySet().stream()
.map(e -> new LFluidStack(1000, e.getValue())).collect(Collectors.toList()),
super(FluidRegistry.getRegisteredFluids().values().stream()
.map(fluid -> new LFluidStack(1000, fluid)).collect(Collectors.toList()),
"picker_fluid_stack");
}
}
@@ -0,0 +1,17 @@
package me.towdium.jecalculation.gui.widgets;
/**
* Author: Towdium
* Date: 18-9-25
*/
public interface IContainer extends IWidget {
void add(IWidget w);
void addAll(IWidget... w);
void remove(IWidget w);
void removeAll(IWidget... w);
void clear();
}
@@ -1,4 +1,6 @@
package me.towdium.jecalculation.gui;
package me.towdium.jecalculation.gui.widgets;
import me.towdium.jecalculation.gui.JecaGui;
import java.util.List;
@@ -1,4 +1,4 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -1,4 +1,4 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -1,9 +1,8 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.utils.Utilities;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -1,9 +1,8 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.utils.Utilities;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -16,7 +15,7 @@ import java.util.List;
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class WContainer implements IWidget {
public class WContainer implements IContainer {
protected List<IWidget> widgets = new ArrayList<>();
public void add(IWidget w) {
@@ -1,6 +1,5 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.utils.Utilities.I18n;
@@ -31,7 +30,7 @@ public class WHelp extends WTooltip {
@Override
public boolean mouseIn(int xMouse, int yMouse) {
return JecaGui.mouseIn(-17, 0, 20, 20, xMouse, yMouse);
return JecaGui.mouseIn(-21, 0, 24, 24, xMouse, yMouse);
}
@Override
@@ -50,6 +49,12 @@ public class WHelp extends WTooltip {
addAll(new WPanel(), new Icon(), tTitle, tContent, sPage);
}
@Override
public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) {
if (!super.onClicked(gui, xMouse, yMouse, button)) gui.root.remove(this);
return true;
}
public class Text implements IWidget {
List<List<String>> pages = new ArrayList<>();
int page;
@@ -111,7 +116,7 @@ public class WHelp extends WTooltip {
@Override
public boolean mouseIn(int xMouse, int yMouse) {
return JecaGui.mouseIn(-17, 0, 20, 20, xMouse, yMouse);
return JecaGui.mouseIn(-21, 0, 24, 24, xMouse, yMouse);
}
@Override
@@ -1,4 +1,4 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -1,9 +1,10 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import codechicken.nei.recipe.GuiCraftingRecipe;
import codechicken.nei.recipe.GuiUsageRecipe;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.utils.IllegalPositionException;
@@ -51,7 +52,7 @@ public class WLabel implements IWidget {
gui.drawResourceContinuous(Resource.WGT_SLOT, xPos, yPos, xSize, ySize, 3, 3, 3, 3);
label.drawLabel(gui, xPos + xSize / 2, yPos + ySize / 2, true);
if (mode == enumMode.RESULT || mode == enumMode.EDITOR) {
String s = label.getAmountString();
String s = label.getAmountString(mode == enumMode.RESULT);
gui.drawText(xPos + xSize / 2.0f + 8 - HALF.getTextWidth(s),
yPos + ySize / 2.0f + 8.5f - HALF.getTextHeight(), HALF, s);
}
@@ -60,12 +61,14 @@ public class WLabel implements IWidget {
int color = 0xFFFFFF + (int) ((-Math.cos(timer.getTime() * Math.PI / 1500) + 1) * 0x40) * 0x1000000;
gui.drawRectangle(xPos + 1, yPos + 1, xSize - 2, ySize - 2, color);
}
if (mouseIn(xMouse, yMouse)) gui.drawRectangle(xPos + 1, yPos + 1, xSize - 2, ySize - 2, 0x80FFFFFF);
if (mouseIn(xMouse, yMouse))
gui.drawRectangle(xPos + 1, yPos + 1, xSize - 2, ySize - 2, 0x80FFFFFF);
}
@Override
public boolean onTooltip(JecaGui gui, int xMouse, int yMouse, List<String> tooltip) {
if (!mouseIn(xMouse, yMouse)) return false;
if (!mouseIn(xMouse, yMouse))
return false;
if (label != ILabel.EMPTY) {
tooltip.add(label.getDisplayName());
label.getToolTip(tooltip, mode == enumMode.EDITOR || mode == enumMode.RESULT);
@@ -87,7 +90,8 @@ public class WLabel implements IWidget {
@Override
public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) {
if (!mouseIn(xMouse, yMouse)) return false;
if (!mouseIn(xMouse, yMouse))
return false;
switch (mode) {
case EDITOR:
if (gui.hand != label.EMPTY) {
@@ -98,14 +102,27 @@ public class WLabel implements IWidget {
} else if (label != label.EMPTY) {
gui.root.add(new WAmount());
return true;
} else return false;
} else
return false;
case RESULT:
// open NEI
Object item = label.getRepresentation();
if (item != null) {
if (button == 0) {
GuiCraftingRecipe.openRecipeGui("item", item);
return true;
} else if (button == 1) {
GuiUsageRecipe.openRecipeGui("item", item);
return true;
}
}
return false;
case PICKER:
if (label != label.EMPTY) {
notifyLsnr();
return true;
} else return false;
} else
return false;
case SELECTOR:
label = gui.hand;
gui.hand = label.EMPTY;
@@ -134,24 +151,13 @@ public class WLabel implements IWidget {
public enum enumMode {
/**
* Slots in editor gui. Can use to edit amount. Exact amount displayed.
*/
EDITOR,
/**
* Slots to display calculate/getRecipes result. Rounded amount displayed.
*/
RESULT,
/**
* Slots that can pick items from. No amount displayed.
*/
PICKER,
/**
* Slots to put labels into. No amount displayed.
*/
SELECTOR
EDITOR, // Slots in editor gui. Can use to edit amount. Exact amount displayed.
RESULT, // Slots to display calculate/getRecipes result. Rounded amount displayed.
PICKER, // Slots that can pick items from. No amount displayed.
SELECTOR // Slots to put labels into. No amount displayed.
}
class WAmount extends WContainer {
WLabel wl = new WLabel(xPos, yPos, xSize, ySize, enumMode.SELECTOR).setLsnrUpdate(this::update);
WTextField wtf = new WTextField(xPos + xSize + 10, yPos + ySize / 2 - WTextField.HEIGHT / 2, 50);
@@ -160,8 +166,7 @@ public class WLabel implements IWidget {
WButton bAmount = new WButtonText(xPos + xSize + 60, yPos, 20, 20, "general.to_percent", "#")
.setLsnrLeft(() -> setPercent(true));
WButton bYes = new WButtonIcon(xPos + xSize + 83, yPos, 20, 20, Resource.BTN_YES).setLsnrLeft(() -> {
setLabel(wl.getLabel().setAmount(
wtf.getText().isEmpty() ? 0 : Integer.parseInt(wtf.getText())));
setLabel(wl.getLabel().setAmount(wtf.getText().isEmpty() ? 0 : Integer.parseInt(wtf.getText())));
JecaGui.getCurrent().root.remove(this);
});
WButton bNo = new WButtonIcon(xPos + xSize + 102, yPos, 20, 20, Resource.BTN_NO).setLsnrLeft(() -> {
@@ -170,8 +175,10 @@ public class WLabel implements IWidget {
});
public WAmount() {
wl.setLabel(getLabel());
wtf.setText(Integer.toString(getLabel().getAmount()));
wl.setLabel(label);
add(new WPanel(xPos - 5, yPos - 5, xSize + 133, ySize + 10));
add(new WText(xPos + xSize + 3, yPos + 5, JecaGui.Font.PLAIN, "x"));
addAll(wl, wtf, bYes, bNo);
wtf.setLsnrText(i -> {
try {
Integer.parseInt(wtf.getText());
@@ -183,15 +190,13 @@ public class WLabel implements IWidget {
bYes.setDisabled(!acceptable);
}
});
add(new WPanel(xPos - 5, yPos - 5, xSize + 133, ySize + 10));
add(new WText(xPos + xSize + 3, yPos + 5, JecaGui.Font.PLAIN, "x"));
addAll(wl, wtf, bYes, bNo);
update();
}
private void update() {
bYes.setDisabled(!wl.getLabel().acceptPercent());
setPercent(false);
label = wl.getLabel();
bAmount.setDisabled(!wl.label.acceptPercent());
setPercent(label.isPercent());
}
private void setPercent(boolean b) {
@@ -202,16 +207,19 @@ public class WLabel implements IWidget {
remove(bPercent);
add(bAmount);
}
wl.getLabel().setPercent(b);
wl.label.setPercent(b);
wtf.setText(Integer.toString(wl.label.getAmount()));
}
@Override
public boolean onKey(JecaGui gui, char ch, int code) {
if (super.onKey(gui, ch, code)) return true;
if (super.onKey(gui, ch, code))
return true;
if (code == Keyboard.KEY_ESCAPE) {
gui.root.remove(this);
return true;
} else return false;
} else
return false;
}
}
}
@@ -1,8 +1,7 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.utils.wrappers.Wrapper;
@@ -38,13 +37,6 @@ public class WLabelGroup extends WContainer {
}));
}
@Override
public void remove(IWidget w) {
super.remove(w);
if (w instanceof WLabel)
labels.remove(w);
}
public ILabel getLabelAt(int index) {
return labels.get(index).getLabel();
}
@@ -62,8 +54,7 @@ public class WLabelGroup extends WContainer {
}
public void setLabel(List<ILabel> labels, int start) {
Wrapper<Integer> i = new Wrapper<>(start); // TODO simplify
this.labels.forEach(l -> l.setLabel(i.value < labels.size() ? labels.get(i.value++) : ILabel.EMPTY));
for (WLabel label : this.labels) label.setLabel(start < labels.size() ? labels.get(start++) : ILabel.EMPTY);
}
public WLabelGroup setLsnrUpdate(Consumer<Integer> listener) {
@@ -1,4 +1,4 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -74,7 +74,7 @@ public class WLabelScroll extends WContainer {
public boolean setFilter(String str) {
filter = str;
filtered = labels.stream().filter(l -> Utilities.contains(l.getDisplayName().toLowerCase(), str.toLowerCase()))
filtered = labels.stream().filter(l -> Utilities.I18n.contains(l.getDisplayName().toLowerCase(), str.toLowerCase()))
.collect(Collectors.toList());
scroll.setCurrent(0);
update(0);
@@ -1,8 +1,7 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
@@ -1,10 +1,9 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.label.ILabel.RegistryEditor.Record;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.utils.Utilities;
@@ -1,8 +1,7 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
@@ -1,8 +1,7 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -1,8 +1,7 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import org.lwjgl.input.Mouse;
@@ -1,4 +1,4 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -6,8 +6,6 @@ import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.data.label.ILabel;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
/**
@@ -1,4 +1,4 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -9,7 +9,6 @@ import me.towdium.jecalculation.utils.Utilities.Circulator;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@@ -1,12 +1,10 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.function.Supplier;
/**
* Author: towdium
@@ -1,9 +1,8 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.IWidget;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiTextField;
@@ -1,8 +1,7 @@
package me.towdium.jecalculation.gui.drawables;
package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.IWidget;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.utils.Utilities;
@@ -127,6 +127,10 @@ public class ItemStackHelper {
}
public static boolean isEmpty(ItemStack stack) {
return stack == null || stack == EMPTY_ITEM_STACK || stack.getItem() == EMPTY_ITEM;
}
public static boolean isTypeEqual(@Nullable ItemStack one, @Nullable ItemStack two) {
return one != null && two != null && one.getItem() == two.getItem() && getMetadata(one) == getMetadata(two) &&
NBT.equalsIgnoreJEC(one.getTagCompound(), two.getTagCompound());
@@ -82,31 +82,6 @@ public class Utilities {
return NBTHelper.getOrCreateSubCompound(is, JustEnoughCalculation.Reference.MODID);
}
public static boolean contains(String s1, String s2) {
return s1.contains(s2);
}
// public static Recipes getRecipes(EntityPlayer player) {
// IExtendedEntityProperties properties = player
// .getExtendedProperties(JecaCapabilityProvider.PROPERTIES_NAME);
// if(properties instanceof JecaCapabilityProvider) {
// return ((JecaCapabilityProvider) properties).record;
// } else {
// JustEnoughCalculation.logger.info("getExtendedProperties error: " + properties.toString());
// return new Recipes();
// }
// }
// // get calculator item in player inventory
// public static Optional<ItemStack> getStack() {
// InventoryPlayer inv = Minecraft.getMinecraft().thePlayer.inventory;
// ItemStack is = inv.getCurrentItem();
// if (is.getItem() instanceof ItemCalculator)
// return Optional.of(is);
// return Optional.empty();
// }
public static class Timer {
long time = System.currentTimeMillis();
boolean running = false;
@@ -202,27 +177,12 @@ public class Utilities {
}
}
public static class Relation<T, R> {
HashMap<Pair<T, T>, R> data = new HashMap<>();
public void add(T a, T b, R relation) {
data.put(a.hashCode() < b.hashCode() ? new Pair<>(a, b) : new Pair<>(b, a), relation);
}
public Optional<R> get(T a, T b) {
int ah = a.hashCode();
int bh = b.hashCode();
Wrapper<R> ret = new Wrapper<>(null);
if (ah == bh)
ret.push(data.get(new Pair<>(a, b))).push(data.get(new Pair<>(b, a)));
else
ret.push(data.get(ah < bh ? new Pair<>(a, b) : new Pair<>(b, a)));
return Optional.ofNullable(ret.value);
}
}
@SideOnly(Side.CLIENT)
public static class I18n {
public static boolean contains(String s1, String s2) {
return s1.contains(s2);
}
public static Pair<String, Boolean> search(String translateKey, Object... parameters) {
Pair<String, Boolean> ret = new Pair<>(null, null);
translateKey = "jecharacters." + translateKey;
@@ -332,7 +292,7 @@ public class Utilities {
}
public static Locale getLocaleFromString(String languageCode) {
String parts[] = languageCode.split("_", -1);
String[] parts = languageCode.split("_", -1);
if (parts.length == 1) return new Locale(parts[0]);
else if (parts.length == 2
|| (parts.length == 3 && parts[2].startsWith("#")))
@@ -1,8 +1,10 @@
package me.towdium.jecalculation.utils.wrappers;
import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Author: Towdium
@@ -22,6 +24,15 @@ public class Wrapper<T> {
return this;
}
public Wrapper<T> or(Supplier<T> s) {
if (value == null) value = s.get();
return this;
}
public Optional<T> toOptional() {
return Optional.ofNullable(value);
}
public Wrapper<T> ifPresent(Consumer<T> c) {
if (value != null)
c.accept(value);