chore [WIP]

This commit is contained in:
Discreater committed 2021-05-08 00:39:17 +08:00
1 parent ea0253a359
commit 1939619ed4
34 files changed
+265 -167

No files matched your search

@@ -1,6 +1,5 @@
package me.towdium.jecalculation.data.label;
import codechicken.nei.recipe.IRecipeHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.JustEnoughCalculation;
@@ -259,7 +258,7 @@ public interface ILabel {
return new LItemStack(itemStack);
} else {
JustEnoughCalculation.logger.warn("Unrecognized ingredient type: " + o.getClass());
return ILabel.EMPTY;
return LPlaceholder.Converter.from(o);
}
}
@@ -268,27 +267,29 @@ public interface ILabel {
}
// get most possible guess from labels
public ILabel first(List<ILabel> labels, @Nullable IRecipeHandler context) {
public ILabel first(List<ILabel> labels, @Nullable Class<?> context) {
List<ILabel> guess = guess(labels, context).one;
return guess.isEmpty() ? labels.get(0) : guess.get(0);
}
// to test if the labels can be converted to other labels (like oreDict)
public Pair<List<ILabel>, List<ILabel>> guess(List<ILabel> labels) {
return guess(labels, null);
}
public Pair<List<ILabel>, List<ILabel>> guess(List<ILabel> labels, @Nullable IRecipeHandler context) {
List<ILabel> suggest = new ReversedIterator<>(handlers.get(Priority.SUGGEST)).stream().flatMap(
h -> h.convert(labels, context).stream()).collect(Collectors.toList());
List<ILabel> fallback = new ReversedIterator<>(handlers.get(Priority.FALLBACK)).stream().flatMap(
h -> h.convert(labels, context).stream()).collect(Collectors.toList());
public Pair<List<ILabel>, List<ILabel>> guess(List<ILabel> labels, @Nullable Class<?> context) {
List<ILabel> suggest = new ReversedIterator<>(handlers.get(Priority.SUGGEST)).stream()
.flatMap(h -> h.convert(labels,
context)
.stream())
.collect(Collectors.toList());
List<ILabel> fallback = new ReversedIterator<>(handlers.get(Priority.FALLBACK)).stream()
.flatMap(h -> h.convert(
labels, context)
.stream())
.collect(
Collectors.toList());
return new Pair<>(suggest, fallback);
}
@FunctionalInterface
public interface ConverterFunction {
List<ILabel> convert(List<ILabel> a, @Nullable IRecipeHandler context);
List<ILabel> convert(List<ILabel> a, @Nullable Class<?> context);
}
}
@@ -498,15 +499,29 @@ public interface ILabel {
@Override
public ILabel multiply(float i) {
float amount = i * getAmount();
if (amount > Long.MAX_VALUE) throw new ArithmeticException("Multiply overflow");
if (amount > Long.MAX_VALUE)
throw new ArithmeticException("Multiply overflow");
return setAmount((long) amount);
}
@Override
@SideOnly(Side.CLIENT)
public void getToolTip(List<String> existing, boolean detailed) {
if (detailed)
existing.add(FORMAT_GREY + Utilities.I18n.get("label.common.amount", getAmountString(false)));
if (detailed) {
long amount = percent ? (getAmount() + 99) / 100 : getAmount();
long stack = amount / 64;
long remainder = amount % 64;
boolean showStack = stack != 0;
boolean showReminder = stack < 1000 && remainder != 0;
String tip = "";
if (showStack)
tip += Utilities.cutNumber(stack, 5) + "x64";
if (showStack && showReminder)
tip += "+";
if (showReminder)
tip += Long.toString(remainder);
existing.add(FORMAT_GREY + Utilities.I18n.get("label.common.amount", tip));
}
}
@Override
@@ -580,9 +595,5 @@ public interface ILabel {
public boolean matches(Object l) {
return l instanceof Impl && percent == ((Impl) l).percent;
}
//abstract protected void drawLabel(JecaGui gui);
}
}
@@ -1,11 +1,10 @@
package me.towdium.jecalculation.data.label.labels;
import codechicken.nei.recipe.IRecipeHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
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.utils.Utilities;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
@@ -15,7 +14,6 @@ import net.minecraftforge.fluids.FluidStack;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@@ -60,7 +58,8 @@ public class LFluidStack extends ILabel.Impl {
super(nbt);
String id = nbt.getString(KEY_FLUID);
Fluid f = FluidRegistry.getFluid(id);
if (f == null) throw new Serializer.SerializationException("Fluid " + id + " cannot be resolved, ignoring");
if (f == null)
throw new Serializer.SerializationException("Fluid " + id + " cannot be resolved, ignoring");
init(f, nbt.hasKey(KEY_NBT) ? nbt.getCompoundTag(KEY_NBT) : null);
}
@@ -84,8 +83,7 @@ public class LFluidStack extends ILabel.Impl {
@Override
public String getAmountString(boolean round) {
return amount >= 1000 ? Utilities.cutNumber(amount / 1000f, 4) + "B"
: amount + "mB";
return amount >= 1000 ? Utilities.cutNumber(amount / 1000f, 4) + "B" : amount + "mB";
}
@@ -105,7 +103,8 @@ public class LFluidStack extends ILabel.Impl {
if (l instanceof LFluidStack) {
LFluidStack lfs = (LFluidStack) l;
return (Objects.equals(nbt, lfs.nbt)) && fluid == lfs.fluid;
} else return false;
} else
return false;
}
@Override
@@ -117,15 +116,16 @@ public class LFluidStack extends ILabel.Impl {
public NBTTagCompound toNbt() {
NBTTagCompound ret = super.toNbt();
ret.setString(KEY_FLUID, FluidRegistry.getFluidName(fluid));
if (nbt != null) ret.setTag(KEY_NBT, nbt);
if (nbt != null)
ret.setTag(KEY_NBT, nbt);
return ret;
}
@Override
@SideOnly(Side.CLIENT)
public void getToolTip(List<String> existing, boolean detailed) {
if (detailed) existing.add(FORMAT_GREY +
Utilities.I18n.get("label.common.amount", Long.toString(getAmount())) + "mB");
if (detailed)
existing.add(FORMAT_GREY + Utilities.I18n.get("label.common.amount", Long.toString(getAmount())) + "mB");
existing.add(FORMAT_BLUE + FORMAT_ITALIC + Utilities.getModName(fluid));
}
@@ -142,12 +142,14 @@ public class LFluidStack extends ILabel.Impl {
return super.hashCode() ^ fluid.getUnlocalizedName().hashCode() ^ (nbt == null ? 0 : nbt.hashCode());
}
public static List<ILabel> suggest(List<ILabel> iss, @Nullable IRecipeHandler rl) {
public static List<ILabel> suggest(List<ILabel> iss, @Nullable Class<?> context) {
return new ArrayList<>();
}
public static boolean merge(ILabel a, ILabel b) {
if (a instanceof LFluidStack && b instanceof LFluidStack) return a.matches(b);
else return false;
if (a instanceof LFluidStack && b instanceof LFluidStack)
return a.matches(b);
else
return false;
}
}
@@ -1,6 +1,5 @@
package me.towdium.jecalculation.data.label.labels;
import codechicken.nei.recipe.IRecipeHandler;
import cpw.mods.fml.common.registry.GameData;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -105,7 +104,7 @@ public class LItemStack extends ILabel.Impl {
return false;
}
public static List<ILabel> suggest(List<ILabel> iss, @Nullable IRecipeHandler rl) {
public static List<ILabel> suggest(List<ILabel> iss, @Nullable Class<?> context) {
if (iss.size() == 0)
return new ArrayList<>();
for (ILabel i : iss)
@@ -130,7 +129,7 @@ public class LItemStack extends ILabel.Impl {
}
public static List<ILabel> fallback(List<ILabel> iss, @Nullable IRecipeHandler rl) {
public static List<ILabel> fallback(List<ILabel> iss, @Nullable Class<?> context) {
List<ILabel> ret = new ArrayList<>();
if (iss.size() == 1) {
ILabel label = iss.get(0);
@@ -1,12 +1,10 @@
package me.towdium.jecalculation.data.label.labels;
import codechicken.nei.recipe.IRecipeHandler;
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.polyfill.mc.util.NonNullList;
import me.towdium.jecalculation.utils.ItemStackHelper;
import me.towdium.jecalculation.utils.Utilities;
@@ -81,7 +79,7 @@ public class LOreDict extends ILabel.Impl {
return false;
}
public static List<ILabel> suggest(List<ILabel> iss, @Nullable IRecipeHandler rl) {
public static List<ILabel> suggest(List<ILabel> iss, @Nullable Class<?> context) {
ILabel l = iss.get(0);
if (!(l instanceof LItemStack))
return new ArrayList<>();
@@ -94,7 +92,7 @@ public class LOreDict extends ILabel.Impl {
return ids.stream().map(i -> new LOreDict(OreDictionary.getOreName(i), amount)).collect(Collectors.toList());
}
public static List<ILabel> fallback(List<ILabel> iss, @Nullable IRecipeHandler rl) {
public static List<ILabel> fallback(List<ILabel> iss, @Nullable Class<?> context) {
ILabel l = iss.get(0);
if (!(l instanceof LItemStack))
return new ArrayList<>();
@@ -7,11 +7,17 @@ import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Pair;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
@@ -45,7 +51,8 @@ public class LPlaceholder extends ILabel.Impl {
public LPlaceholder(String name, long amount, boolean silent) {
super(amount, false);
this.name = name;
if (!silent) getActive().push(new LPlaceholder(name, 1, true), false);
if (!silent)
getActive().push(new LPlaceholder(name, 1, true), false);
}
public LPlaceholder(LPlaceholder label) {
@@ -120,6 +127,33 @@ public class LPlaceholder extends ILabel.Impl {
LPlaceholder lpA = (LPlaceholder) a;
LPlaceholder lpB = (LPlaceholder) b;
return lpA.name.equals(lpB.name);
} else return false;
} else
return false;
}
public static class Converter {
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
static List<Pair<Pattern, Function<Matcher, LPlaceholder>>> converters = Arrays.asList(
new Pair<>(Pattern.compile("\\[\\[Gas: mekanism:(.+)], (\\d+)]"),
i -> new LPlaceholder("Gas - " + capitalize(i.group(1)), Integer.parseInt(i.group(2)))));
public static String capitalize(String s) {
String[] arr = s.replace('_', ' ').split(" ");
StringBuilder sb = new StringBuilder();
for (String value : arr) {
sb.append(Character.toUpperCase(value.charAt(0)));
sb.append(value.substring(1)).append(" ");
}
return sb.toString().trim();
}
public static LPlaceholder from(Object o) {
String s = o.toString();
return converters.stream().map(i -> {
Matcher m = i.one.matcher(s);
return m.matches() ? i.two.apply(m) : null;
}).filter(Objects::nonNull).findFirst().orElseGet(() -> new LPlaceholder(s, 1));
}
}
}
@@ -148,6 +148,10 @@ public class Recipe {
}
public enum IO {
INPUT, OUTPUT, CATALYST
INPUT, OUTPUT, CATALYST;
public static IO isInput(boolean b) {
return b ? INPUT : OUTPUT;
}
}
}
@@ -13,6 +13,7 @@ import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.nei.NEIPlugin;
import me.towdium.jecalculation.polyfill.MethodsReturnNonnullByDefault;
import me.towdium.jecalculation.utils.ItemStackHelper;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Pair;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.InventoryPlayer;
@@ -132,7 +133,7 @@ public class GuiCraft extends WContainer implements IGui {
}
List<ILabel> getInventory() {
InventoryPlayer inv = Minecraft.getMinecraft().thePlayer.inventory;
InventoryPlayer inv = Utilities.getPlayer().inventory;
ArrayList<ILabel> labels = new ArrayList<>();
Consumer<ItemStack[]> add = i -> Arrays.stream(i)
.filter(j -> !ItemStackHelper.isEmpty(j))
@@ -182,7 +183,7 @@ public class GuiCraft extends WContainer implements IGui {
match.addAll(fuzzy);
List<ILabel> list = new ArrayList<>(match);
if (!match.isEmpty())
add(new Suggest(list.size() > 3 ? list.subList(0, 3) : list, !dup));
setOverlay(new Suggest(list.size() > 3 ? list.subList(0, 3) : list, !dup));
}
}
@@ -212,7 +213,7 @@ public class GuiCraft extends WContainer implements IGui {
}
public void refresh(ILabel l) {
GuiCraft.this.remove(this);
GuiCraft.this.setOverlay(null);
refreshLabel(l, replace, false);
}
}
@@ -98,7 +98,7 @@ public class GuiImport extends WContainer implements IGui, ISearchable {
//WRectangle rect = new WRectangle(22, 49 + 16 * i, 146, 13, JecaGui.COLOR_GUI_GREY);
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
gui.drawRectangle(22, yPos - 2, 146, 13, JecaGui.COLOR_GUI_GREY);
super.onDraw(gui, xMouse, yMouse);
if (expand) {
@@ -112,6 +112,7 @@ public class GuiImport extends WContainer implements IGui, ISearchable {
gui.drawText(25, yPos, JecaGui.Font.SHADOW, key);
expand = true;
}
return false;
}
@Override
@@ -1,6 +1,5 @@
package me.towdium.jecalculation.gui.guis;
import codechicken.nei.recipe.IRecipeHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.data.Controller;
@@ -10,13 +9,10 @@ import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.data.structure.Recipe.IO;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.widgets.*;
import me.towdium.jecalculation.nei.Adapter;
import me.towdium.jecalculation.nei.NEIPlugin;
import me.towdium.jecalculation.polyfill.MethodsReturnNonnullByDefault;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Pair;
import me.towdium.jecalculation.utils.wrappers.Trio;
import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -38,7 +34,7 @@ import static me.towdium.jecalculation.gui.Resource.*;
public class GuiRecipe extends WContainer implements IGui {
Pair<String, Integer> dest;
WSwitcher group = new WSwitcher(7, 7, 162, Controller.getGroups()).setListener(i -> refresh());
WTextField text = new WTextField(49, 32, 119);
WTextField text = new WTextField(49, 25, 119);
WLabelScroll catalyst = new WLabelScroll(25, 101, 7, 1, true);
WLabelScroll input = new WLabelScroll(25, 123, 7, 2, true);
WLabelScroll output = new WLabelScroll(25, 61, 7, 2, true);
@@ -93,7 +89,7 @@ public class GuiRecipe extends WContainer implements IGui {
.setLsnrClick((i, v) -> {
ILabel l = i.get(v).getLabel();
if (l != ILabel.EMPTY)
add(new WAmount(j, v));
setOverlay(new WAmount(j, v));
})
.setLsnrUpdate((i, v) -> {
refresh();
@@ -163,68 +159,22 @@ public class GuiRecipe extends WContainer implements IGui {
refresh();
}
public void transfer(IRecipeHandler recipe, int recipeIndex) {
// item disamb raw
EnumMap<IO, List<Trio<ILabel, CostList, CostList>>> merged = new EnumMap<>(IO.class);
public void transfer(EnumMap<IO, List<Trio<ILabel, CostList, CostList>>> recipe, Class<?> context) {
disamb.clear();
// merge recipe input, output and catalysts
List<Object[]> recipeInputs = new ArrayList<>();
List<Object[]> recipeOutputs = new ArrayList<>();
Adapter.handleRecipe(recipe, recipeIndex, recipeInputs, recipeOutputs);
// input
recipeInputs.forEach(i -> merge(merged, Arrays.asList(i), recipe, IO.INPUT));
// output
recipeOutputs.forEach(o -> merge(merged, Arrays.asList(o), recipe, IO.OUTPUT));
// catalyst. Ignore multiple catalyst
NEIPlugin.getCatalyst(recipe).ifPresent(catalyst -> {
List<ItemStack> catalysts = Collections.singletonList(catalyst);
merge(merged, catalysts, recipe, IO.CATALYST);
});
// other. Unused. For example fuel in furnaces.
// recipe.getOtherStacks(recipeIndex).stream();
// generate disamb and fill slots
for (IO i : IO.values())
getWidget(i).setLabels(extract(merged, i, recipe));
getWidget(i).setLabels(extract(recipe, i, context));
refresh();
}
private void merge(EnumMap<IO, List<Trio<ILabel, CostList, CostList>>> dst,
List<?> objs,
IRecipeHandler context,
IO type) {
List<ILabel> list = objs.stream().map(ILabel.Converter::from).collect(Collectors.toList());
if (list.isEmpty())
return;
ILabel rep = list.get(0).copy();
if (type == IO.INPUT && list.size() != 1)
rep = ILabel.CONVERTER.first(list, context);
ILabel fin = rep;
dst.computeIfAbsent(type, i -> new ArrayList<>()).stream().filter(p -> {
CostList cl = new CostList(list);
if (p.three.equals(cl)) {
ILabel.MERGER.merge(p.one, fin).ifPresent(i -> p.one = i);
p.two = p.two.merge(cl, true, false);
return true;
} else
return false;
}).findAny().orElseGet(() -> {
Trio<ILabel, CostList, CostList> ret = new Trio<>(fin, new CostList(list), new CostList(list));
dst.get(type).add(ret);
return ret;
});
}
private ArrayList<ILabel> extract(EnumMap<IO, List<Trio<ILabel, CostList, CostList>>> src,
IO type,
IRecipeHandler context) {
Class<?> context) {
List<Trio<ILabel, CostList, CostList>> l = src.get(type);
ArrayList<ILabel> ret = new ArrayList<>();
if (l == null)
return ret;
for (int i = 0; i < l.size(); i++) {
Trio<ILabel, CostList, CostList> p = l.get(i);
ret.add(p.one);
@@ -309,10 +259,7 @@ public class GuiRecipe extends WContainer implements IGui {
disamb = new WButtonIcon(x + 40, y - 24, 20, 20, BTN_DISAMB, "recipe.disamb");
Map<Integer, List<ILabel>> entry = GuiRecipe.this.disamb.get(type);
if (entry != null && entry.containsKey(idx)) {
disamb.setListener(i -> {
GuiRecipe.this.remove(this);
GuiRecipe.this.add(new WDisamb(type, idx));
});
disamb.setListener(i -> GuiRecipe.this.setOverlay(new WDisamb(type, idx)));
} else
disamb.setDisabled(true);
add(temp, text, pick, yes, no, disamb);
@@ -339,7 +286,7 @@ public class GuiRecipe extends WContainer implements IGui {
private void set(ILabel l, IO type, int idx) {
ref.setLabel(l, true);
removeDisamb(type, idx);
GuiRecipe.this.remove(this);
GuiRecipe.this.setOverlay(null);
}
private void update() {
@@ -373,7 +320,7 @@ public class GuiRecipe extends WContainer implements IGui {
ref.setLabel(
i.get(v).getLabel().copy().multiply(-1),
false);
GuiRecipe.this.remove(this);
GuiRecipe.this.setOverlay(null);
refresh();
});
add(new WIcon(x + 22, y - 1, 20, 20, ICN_TEXT, "common.search"));
@@ -26,7 +26,7 @@ public class GuiSearch extends WContainer implements IGui {
WLabelScroll labels = new WLabelScroll(7, 51, 8, 6, false)
.setLsnrClick((i, v) -> {
ILabel l = i.get(v).getLabel();
if (l != ILabel.EMPTY) add(new Overlay(i.get(v)));
if (l != ILabel.EMPTY) setOverlay(new Overlay(i.get(v)));
});
WSwitcher group;
WTextField text = new WTextField(7, 25, 119);
@@ -148,11 +148,11 @@ public class GuiSearch extends WContainer implements IGui {
add(new WLabel(x, y, 20, 20, false).setLabel(l.getLabel()));
add(new WButtonIcon(x + 23, y, 20, 20, BTN_EDIT, "search.edit").setListener(i -> {
JecaGui.displayGui(true, true, new GuiRecipe(recipe.two, recipe.three));
GuiSearch.this.remove(this);
GuiSearch.this.setOverlay(null);
}));
add(new WButtonIcon(x + 42, y, 20, 20, BTN_NO, "search.delete").setListener(i -> {
Controller.removeRecipe(recipe.two, recipe.three);
GuiSearch.this.remove(this);
GuiSearch.this.setOverlay(null);
refreshGroups();
refreshDisplay();
}));
@@ -10,4 +10,6 @@ public interface IContainer extends IWidget {
void remove(IWidget... w);
void clear();
void setOverlay(IWidget w);
}
@@ -12,7 +12,7 @@ import java.util.List;
*/
@ParametersAreNonnullByDefault
public interface IWidget {
void onDraw(JecaGui gui, int xMouse, int yMouse);
boolean onDraw(JecaGui gui, int xMouse, int yMouse);
default boolean onTooltip(JecaGui gui, int xMouse, int yMouse, List<String> tooltip) {
return false;
@@ -41,7 +41,7 @@ public abstract class WButton extends WTooltip {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
super.onDraw(gui, xMouse, yMouse);
boolean hovered = JecaGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse);
if (keys != null)
@@ -50,6 +50,7 @@ public abstract class WButton extends WTooltip {
hovered = true;
gui.drawResourceContinuous(disabled ? WGT_BUTTON_D : (hovered ? WGT_BUTTON_F : WGT_BUTTON_N), xPos, yPos, xSize,
ySize, 5, 5, 5, 5);
return hovered;
}
@Override
@@ -40,10 +40,11 @@ public class WButtonIcon extends WButton {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
super.onDraw(gui, xMouse, yMouse);
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
boolean ret = super.onDraw(gui, xMouse, yMouse);
Resource r = disabled ? rDisabled : (mouseIn(xMouse, yMouse) ? rFocused : rNormal);
if (r != null)
gui.drawResource(r, xPos + (xSize - r.getXSize() + 1) / 2, yPos + (ySize - r.getYSize() + 1) / 2);
return ret;
}
}
@@ -37,10 +37,11 @@ public class WButtonText extends WButton {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
super.onDraw(gui, xMouse, yMouse);
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
boolean ret = super.onDraw(gui, xMouse, yMouse);
JecaGui.Font font = mouseIn(xMouse, yMouse) ? focused : normal;
float x = xPos + Math.max(3, xSize / 2.0f - font.getTextWidth(text) / 2.0f);
gui.drawText(x, yPos + ySize / 2.0f - font.getTextHeight() / 2.0f, xSize - 6, font, text);
return ret;
}
}
@@ -22,6 +22,7 @@ import java.util.Objects;
@SideOnly(Side.CLIENT)
public class WContainer implements IContainer {
protected List<IWidget> widgets = new ArrayList<>();
protected IWidget overlay = null;
public void add(IWidget w) {
widgets.add(w);
@@ -41,6 +42,10 @@ public class WContainer implements IContainer {
widgets.removeAll(Arrays.asList(w));
}
public void setOverlay(@Nullable IWidget overlay) {
this.overlay = overlay;
}
public void clear() {
widgets.clear();
}
@@ -50,36 +55,46 @@ public class WContainer implements IContainer {
}
@Override
public void onDraw(JecaGui gui, int mouseX, int mouseY) {
widgets.forEach(widget -> widget.onDraw(gui, mouseX, mouseY));
public boolean onDraw(JecaGui gui, int mouseX, int mouseY) {
IWidget[] w = new IWidget[1];
widgets.forEach(widget -> {
if(widget.onDraw(gui, mouseX, mouseY)) w[0] = widget;
});
if(w[0] != null) w[0].onDraw(gui, mouseX, mouseY);
return false;
}
@Override
public boolean onMouseClicked(JecaGui gui, int xMouse, int yMouse, int button) {
return new Utilities.ReversedIterator<>(widgets).stream()
boolean b = overlay != null && overlay.onMouseClicked(gui, xMouse, yMouse, button);
return b || new Utilities.ReversedIterator<>(widgets).stream()
.anyMatch(i -> i.onMouseClicked(gui, xMouse, yMouse, button));
}
@Override
public boolean onKeyPressed(JecaGui gui, char ch, int code) {
return new Utilities.ReversedIterator<>(widgets).stream().anyMatch(i -> i.onKeyPressed(gui, ch, code));
boolean b = overlay != null && overlay.onKeyPressed(gui, ch, code);
return b || new Utilities.ReversedIterator<>(widgets).stream().anyMatch(i -> i.onKeyPressed(gui, ch, code));
}
@Override
public boolean onMouseReleased(JecaGui gui, int xMouse, int yMouse, int button) {
return new Utilities.ReversedIterator<>(widgets).stream()
boolean b = overlay != null && overlay.onMouseReleased(gui, xMouse, yMouse, button);
return b || new Utilities.ReversedIterator<>(widgets).stream()
.anyMatch(i -> i.onMouseReleased(gui, xMouse, yMouse, button));
}
@Override
public boolean onMouseScroll(JecaGui gui, int xMouse, int yMouse, int diff) {
return new Utilities.ReversedIterator<>(widgets).stream()
boolean b = overlay != null && overlay.onMouseScroll(gui, xMouse, yMouse, diff);
return b || new Utilities.ReversedIterator<>(widgets).stream()
.anyMatch(i -> i.onMouseScroll(gui, xMouse, yMouse, diff));
}
@Override
public boolean onTooltip(JecaGui gui, int xMouse, int yMouse, List<String> tooltip) {
return new Utilities.ReversedIterator<>(widgets).stream()
boolean b = overlay != null && overlay.onTooltip(gui, xMouse, yMouse, tooltip);
return b || new Utilities.ReversedIterator<>(widgets).stream()
.anyMatch(i -> i.onTooltip(gui, xMouse, yMouse, tooltip));
}
@@ -36,10 +36,11 @@ public class WHelp extends WContainer {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
super.onDraw(gui, xMouse, yMouse);
gui.drawResourceContinuous(WGT_PANEL_N, -21, 0, 25, 24, 4);
gui.drawResource(WGT_HELP_N, -19, 2);
return false;
}
@Override
@@ -108,8 +109,9 @@ public class WHelp extends WContainer {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
gui.drawSplitText(7, 21, PLAIN, pages.get(page));
return false;
}
public int amount() {
@@ -127,10 +129,11 @@ public class WHelp extends WContainer {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
super.onDraw(gui, xMouse, yMouse);
gui.drawResourceContinuous(WGT_HELP_B, -21, 0, 25, 24, 4);
gui.drawResource(WGT_HELP_F, -19, 2);
return false;
}
@Override
@@ -42,11 +42,12 @@ public class WIcon extends WTooltip {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
super.onDraw(gui, xMouse, yMouse);
gui.drawRectangle(xPos, yPos, xSize, ySize, COLOR_GUI_GREY);
Resource r = mouseIn(xMouse, yMouse) ? focused : normal;
gui.drawResource(r, (xSize - r.getXSize()) / 2 + xPos, (ySize - r.getYSize()) / 2 + yPos);
return false;
}
@Override
@@ -58,7 +58,7 @@ public class WLabel implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
gui.drawResourceContinuous(WGT_SLOT, xPos, yPos, xSize, ySize, 3, 3, 3, 3);
label.drawLabel(gui, xPos + xSize / 2, yPos + ySize / 2, true);
String s = fmtAmount.apply(label);
@@ -71,6 +71,7 @@ public class WLabel implements IWidget {
}
if (mouseIn(xMouse, yMouse))
GuiDraw.drawRect(xPos + 1, yPos + 1, xSize - 2, ySize - 2, 0x80FFFFFF);
return false;
}
@Override
@@ -55,12 +55,13 @@ public class WLcd implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
drawBackground(gui);
drawStr(gui, text);
if (dot != DOT_NONE)
drawDot(gui, 6 - dot);
drawOperator(gui, operator);
return false;
}
protected void drawBackground(JecaGui gui) {
@@ -35,8 +35,9 @@ public class WLine implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
gui.drawResourceContinuous(Resource.WGT_SLOT, xPos, yPos, xSize, ySize, 1);
return false;
}
public static class Joint implements IWidget {
@@ -61,7 +62,7 @@ public class WLine implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
if (!up) gui.drawResource(u, xPos, yPos);
if (!down) gui.drawResource(d, xPos, yPos + 1);
if (!left) gui.drawResource(l, xPos, yPos);
@@ -70,6 +71,7 @@ public class WLine implements IWidget {
if (right == up) gui.drawResource(ur, xPos + 1, yPos);
if (left == down) gui.drawResource(ll, xPos, 1 + yPos);
if (right == down) gui.drawResource(lr, xPos + 1, yPos + 1);
return false;
}
}
}
@@ -18,28 +18,29 @@ public class WOverlay extends WContainer {
public boolean onKeyPressed(JecaGui gui, char ch, int code) {
if (super.onKeyPressed(gui, ch, code)) return true;
if (code == Keyboard.KEY_ESCAPE) {
gui.root.remove(this);
gui.root.setOverlay(null);
return true;
} else return false;
}
@Override
public boolean onMouseClicked(JecaGui gui, int xMouse, int yMouse, int button) {
if (!super.onMouseClicked(gui, xMouse, yMouse, button)) gui.root.remove(this);
if (!super.onMouseClicked(gui, xMouse, yMouse, button)) gui.root.setOverlay(null);
return true;
}
@Override
public boolean onMouseScroll(JecaGui gui, int xMouse, int yMouse, int diff) {
if (!super.onMouseScroll(gui, xMouse, yMouse, diff)) gui.root.remove(this);
if (!super.onMouseScroll(gui, xMouse, yMouse, diff)) gui.root.setOverlay(null);
return true;
}
@Override
public void onDraw(JecaGui gui, int mouseX, int mouseY) {
public boolean onDraw(JecaGui gui, int mouseX, int mouseY) {
GlStateManager.pushMatrix();
GlStateManager.translate(0, 0, 40);
GlStateManager.translate(0, 0, 100);
super.onDraw(gui, mouseX, mouseY);
GlStateManager.popMatrix();
return false;
}
}
@@ -34,11 +34,12 @@ public class WPage extends WTooltip {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
Resource resource = focused ? WGT_PAGER_F : WGT_PANEL_N;
gui.drawResourceContinuous(resource, index * 24 + 3, -21, 24, 25, 4, 4, 4, 4);
record.representation.drawLabel(gui, index * 24 + 7, -17, false);
super.onDraw(gui, xMouse, yMouse);
return false;
}
@Override
@@ -34,8 +34,9 @@ public class WPanel implements IWidget {
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
gui.drawResourceContinuous(Resource.WGT_PANEL_F, xPos, yPos, xSize, ySize, 5, 5, 5, 5);
return false;
}
@Override
@@ -26,7 +26,8 @@ public class WRectangle implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
gui.drawRectangle(xPos, yPos, xSize, ySize, color);
return false;
}
}
@@ -36,7 +36,7 @@ public class WScroll implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
if (Mouse.isButtonDown(0) && active)
setCurrent(yMouse - yPos - height / 2, true);
else
@@ -48,6 +48,7 @@ public class WScroll implements IWidget {
gui.drawResourceContinuous(WGT_SLOT, xPos, yPos, 14, ySize, 3, 3, 3, 3);
gui.drawResourceContinuous(r, xPos, yPos + offset, 14, height, 3);
return false;
}
@Override
@@ -36,11 +36,12 @@ public class WText implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
int x = xPos + (centred ? xSize / 2 - font.getTextWidth(key) / 2 : 0);
if (xSize == UNDEFINED)
gui.drawText(x, yPos, font, key);
else
gui.drawText(x, yPos, xSize, font, key);
return false;
}
}
@@ -40,8 +40,9 @@ public class WTextField implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
textField.drawTextBox();
return false;
}
@Override
@@ -92,11 +92,13 @@ public class WTick extends WContainer {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
super.onDraw(gui, xMouse, yMouse);
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
boolean ret = super.onDraw(gui, xMouse, yMouse);
boolean hovered = JecaGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse);
gui.drawResourceContinuous(disabled ? WGT_BUTTON_S_D : (hovered ? WGT_BUTTON_S_F : WGT_BUTTON_S_N), xPos,
yPos, xSize, ySize, 5, 5, 5, 5);
return ret;
}
}
}
@@ -32,9 +32,10 @@ public abstract class WTooltip implements IWidget {
}
@Override
public void onDraw(JecaGui gui, int xMouse, int yMouse) {
public boolean onDraw(JecaGui gui, int xMouse, int yMouse) {
if (name != null)
timer.setState(mouseIn(xMouse, yMouse));
return false;
}
@Override
@@ -4,24 +4,84 @@ import codechicken.nei.api.IOverlayHandler;
import codechicken.nei.recipe.IRecipeHandler;
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.data.structure.CostList;
import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.guis.GuiRecipe;
import me.towdium.jecalculation.utils.wrappers.Trio;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import java.util.*;
import java.util.stream.Collectors;
@SideOnly(Side.CLIENT)
public class JecaOverlayHandler implements IOverlayHandler {
@Override
public void overlayRecipe(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex, boolean shift) {
if (firstGui instanceof JecaGui) {
Class<?> context = recipe.getClass();
JecaGui gui = (JecaGui) firstGui;
if (gui.root instanceof GuiRecipe) {
((GuiRecipe) gui.root).transfer(recipe, recipeIndex);
((GuiRecipe) gui.root).transfer(convertRecipe(recipe, recipeIndex, context), context);
} else {
GuiRecipe guiRecipe = new GuiRecipe();
JecaGui.displayGui(true, true, guiRecipe);
guiRecipe.transfer(recipe, recipeIndex);
guiRecipe.transfer(convertRecipe(recipe, recipeIndex, context), context);
}
}
}
private static EnumMap<Recipe.IO, List<Trio<ILabel, CostList, CostList>>> convertRecipe(IRecipeHandler recipe,
int recipeIndex,
Class<?> context) {
EnumMap<Recipe.IO, List<Trio<ILabel, CostList, CostList>>> merged = new EnumMap<>(
Recipe.IO.class); // item disamb raw
// merge recipe input, output and catalysts
List<Object[]> recipeInputs = new ArrayList<>();
List<Object[]> recipeOutputs = new ArrayList<>();
Adapter.handleRecipe(recipe, recipeIndex, recipeInputs, recipeOutputs);
// input
recipeInputs.forEach(i -> merge(merged, Arrays.asList(i), context, Recipe.IO.INPUT));
// output
recipeOutputs.forEach(o -> merge(merged, Arrays.asList(o), context, Recipe.IO.OUTPUT));
NEIPlugin.getCatalyst(recipe).ifPresent(catalyst -> {
List<ItemStack> catalysts = Collections.singletonList(catalyst);
merge(merged, catalysts, context, Recipe.IO.CATALYST);
});
return merged;
}
private static void merge(EnumMap<Recipe.IO, List<Trio<ILabel, CostList, CostList>>> dst,
List<?> objs,
Class<?> context,
Recipe.IO type) {
List<ILabel> list = objs.stream().map(ILabel.Converter::from).collect(Collectors.toList());
if (list.isEmpty())
return;
ILabel rep = list.get(0).copy();
if (type == Recipe.IO.INPUT && list.size() != 1)
rep = ILabel.CONVERTER.first(list, context);
ILabel fin = rep;
dst.computeIfAbsent(type, i -> new ArrayList<>()).stream().filter(p -> {
CostList cl = new CostList(list);
if (p.three.equals(cl)) {
ILabel.MERGER.merge(p.one, fin).ifPresent(i -> p.one = i);
p.two = p.two.merge(cl, true, false);
return true;
} else
return false;
}).findAny().orElseGet(() -> {
Trio<ILabel, CostList, CostList> ret = new Trio<>(fin, new CostList(list), new CostList(list));
dst.get(type).add(ret);
return ret;
});
}
}
@@ -4,12 +4,19 @@ import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.recipe.*;
import me.towdium.jecalculation.JustEnoughCalculation;
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.utils.wrappers.Trio;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import scala.tools.cmd.Opt;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class NEIPlugin {
@@ -60,4 +67,8 @@ public class NEIPlugin {
JustEnoughCalculation.logger.warn("unknown label representation " + rep);
}
}
}
@@ -12,6 +12,7 @@ import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.polyfill.NBTHelper;
import me.towdium.jecalculation.utils.wrappers.Pair;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@@ -142,6 +143,11 @@ public class Utilities {
return NBTHelper.getOrCreateSubCompound(is, JustEnoughCalculation.Reference.MODID);
}
public static EntityClientPlayerMP getPlayer() {
return Objects.requireNonNull(Minecraft.getMinecraft().thePlayer);
}
public static class Relation<K, V> {
public HashMap<Pair<K, K>, V> data = new HashMap<>();
@@ -367,8 +373,7 @@ public class Utilities {
}
public List<T> toList() {
//noinspection unchecked
return (List<T>) data.clone();
return new ArrayList<>(data);
}
public void clear() {
@@ -27,19 +27,6 @@ public class Pair<K, V> {
return false;
}
public boolean swap(Class<? extends K> ke, Class<? extends V> ve) {
if (ke.isInstance(one) && ve.isInstance(two)) return true;
else if (!(ke.isInstance(two) && ve.isInstance(one))) return false;
else {
//noinspection unchecked
K o = (K) two;
//noinspection unchecked
two = (V) one;
one = o;
return true;
}
}
public Pair<K, V> setOne(K one) {
this.one = one;
return this;