feat: label fluid stack editor

This commit is contained in:
Discreater committed 2021-04-29 02:26:22 +08:00
1 parent 542ebbe83b
commit f4b0d527aa
11 files changed
+106 -41

No files matched your search

@@ -11,6 +11,7 @@ import me.towdium.jecalculation.utils.wrappers.Single;
import me.towdium.jecalculation.utils.wrappers.Triple;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
@@ -29,6 +30,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
/**
@@ -40,7 +42,6 @@ public class JecGui extends GuiContainer {
public static final int COLOR_GUI_GREY = 0xFFA1A1A1;
public static final int COLOR_TEXT_RED = 0xFF0000;
public static final int COLOR_TEXT_WHITE = 0xFFFFFF;
public static JecGui current;
public static final boolean ALWAYS_TOOLTIP = false;
protected JecGui parent;
@@ -73,26 +74,34 @@ public class JecGui extends GuiContainer {
displayGuiUnsafe(updateParent, acceptsTransfer, root);
}
/**
* @return The currently displayed {@link JecGui}
* Make sure the method is called when a {@link JecGui} is displayed!
* Otherwise it will throw a {@link NullPointerException}
*/
public static JecGui getCurrent() {
return current;
GuiScreen gui = Minecraft.getMinecraft().currentScreen;
JecGui ret = gui instanceof JecGui ? (JecGui) gui : null;
Objects.requireNonNull(ret);
return ret;
}
private static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, IDrawable root) {
Minecraft mc = Minecraft.getMinecraft();
JecGui parent;
if (mc.currentScreen == null) parent = null;
else if (!(mc.currentScreen instanceof JecGui)) parent = current;
else if (!(mc.currentScreen instanceof JecGui)) parent = getCurrent();
else if (updateParent) parent = (JecGui) mc.currentScreen;
else parent = ((JecGui) mc.currentScreen).parent;
JecGui toShow = new JecGui(parent, acceptsTransfer, root);
mc.displayGuiScreen(toShow);
current = toShow;
}
@Override
public void onGuiClosed() {
super.onGuiClosed();
current = null;
public static void displayParent() {
// isCallingFromMinecraftThread
if (Minecraft.getMinecraft().func_152345_ab()) {
Minecraft.getMinecraft().displayGuiScreen(getCurrent().parent);
}
}
@Override
@@ -60,7 +60,7 @@ public class DLabelScroll extends DContainer {
public boolean setFilter(String str) {
filter = str;
filtered = labels.stream().filter(l -> Utilities.contains(l.getDisplayName(), str))
filtered = labels.stream().filter(l -> Utilities.contains(l.getDisplayName().toLowerCase(), str.toLowerCase()))
.collect(Collectors.toList());
scroll.setCurrent(0);
return filtered.size() != 0;
@@ -68,6 +68,6 @@ public class DLabelScroll extends DContainer {
private int getStepAmount() {
int line = (labels.size() + column - 1) / column;
return line - row + 1;
return Math.max(line - row + 1, 1);
}
}
@@ -31,7 +31,7 @@ public class DScroll implements IDrawable {
@Override
public void onDraw(JecGui gui, int xMouse, int yMouse) {
if (Mouse.isButtonDown(0) && drag) setCurrent(yMouse);
if (Mouse.isButtonDown(0) && drag) setCurrent(yMouse - yPos - 9);
else drag = false;
gui.drawResourceContinuous(Resource.WGT_SLOT, xPos, yPos, 14, ySize, 3, 3, 3, 3);
@@ -0,0 +1,38 @@
package me.towdium.jecalculation.client.gui.drawables;
import me.towdium.jecalculation.client.gui.JecGui;
import me.towdium.jecalculation.core.labels.ILabel;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
/**
* Author: towdium
* Date: 17-9-28.
*/
@ParametersAreNonnullByDefault
public class DSearch extends DContainer {
DTextField tf;
List<DLabelScroll> lss;
Consumer<ILabel> callback;
public DSearch(Consumer<ILabel> callback, DTextField tf, DLabelScroll... lss) {
List<DLabelScroll> lst = Arrays.asList(lss);
tf.setLsnrText(s -> tf.setColor(lst.stream().anyMatch(ls -> ls.setFilter(s)) ?
JecGui.COLOR_TEXT_WHITE : JecGui.COLOR_TEXT_RED));
add(tf);
addAll(lss);
this.tf = tf;
this.lss = lst;
this.callback = callback;
}
@Override
public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) {
boolean ret = super.onClicked(gui, xMouse, yMouse, button);
if (gui.hand != ILabel.EMPTY) callback.accept(gui.hand);
return ret;
}
}
@@ -19,7 +19,7 @@ public class GuiCalculator extends DContainer {
add(new DTextField(61, 7, 64));
add(new DButtonIcon(7, 7, 20, 20, Resource.BTN_LABEL_N, Resource.BTN_LABEL_F, "calculator.label")
.setListenerLeft(() -> JecGui.displayGui(new GuiLabel(l -> {
JecGui.displayGui(this);
JecGui.displayParent();
JecGui.getCurrent().hand = l;
}))));
add(new DButtonIcon(130, 7, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "calculator.recipe")
@@ -22,14 +22,13 @@ public class GuiDisambiguation extends DContainer {
DLabelScroll lsUp = new DLabelScroll(25, 48, 7, 3, DLabel.enumMode.RESULT, true).setLabels(labels);
DLabelScroll lsDown = new DLabelScroll(25, 105, 7, 3, DLabel.enumMode.RESULT, true).setLabels(labels);
DTextField tf = new DTextField(25, 24, 90);
tf.setLsnrText(s -> tf.setColor(lsUp.setFilter(s) | lsDown.setFilter(s) ?
JecGui.COLOR_TEXT_WHITE : JecGui.COLOR_TEXT_RED));
add(new DPanel());
add(new DSwitcher(7, 7, 162, 5));
add(new DIcon(149, 24, 20, 20, Resource.ICN_HELP_N, Resource.ICN_HELP_F, "disambiguation.help"));
add(new DIcon(7, 48, 18, 54, Resource.ICN_LIST_N, Resource.ICN_LIST_F, "disambiguation.list"));
add(new DIcon(7, 105, 18, 54, Resource.ICN_LABEL_N, Resource.ICN_LABEL_F, "disambiguation.label"));
addAll(tf, lsUp, lsDown);
add(new DSearch(i -> {
}, tf, lsUp, lsDown));
}
}
@@ -0,0 +1,27 @@
package me.towdium.jecalculation.client.gui.guis;
import me.towdium.jecalculation.client.gui.drawables.DLabel;
import me.towdium.jecalculation.client.gui.drawables.DLabelScroll;
import me.towdium.jecalculation.client.gui.drawables.DSearch;
import me.towdium.jecalculation.client.gui.drawables.DTextField;
import me.towdium.jecalculation.core.labels.ILabel;
import me.towdium.jecalculation.core.labels.labels.LabelFluidStack;
import net.minecraftforge.fluids.FluidRegistry;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.stream.Collectors;
/**
* Author: towdium
* Date: 17-9-28.
*/
@ParametersAreNonnullByDefault
public class GuiEditorFluidStack extends ILabel.RegistryEditor.Editor {
public GuiEditorFluidStack() {
DLabelScroll ls = new DLabelScroll(7, 33, 8, 7, DLabel.enumMode.PICKER, true).setLabels(
FluidRegistry.getRegisteredFluids().entrySet().stream()
.map(e -> new LabelFluidStack(e.getValue(), 1000)).collect(Collectors.toList()));
DTextField tf = new DTextField(25, 7, 90);
add(new DSearch(l -> callback.value.accept(l), tf, ls));
}
}
@@ -28,7 +28,7 @@ public class GuiLabel extends DContainer {
@Override
public IDrawable load(Integer i) {
ILabel.RegistryEditor.Record record = ILabel.EDITOR.getRecords().get(i);
return record.editor.setCallback(callback);
return record.editor.get().setCallback(callback);
}
});
@@ -4,6 +4,7 @@ import com.google.common.base.CaseFormat;
import me.towdium.jecalculation.client.gui.IDrawable;
import me.towdium.jecalculation.client.gui.JecGui;
import me.towdium.jecalculation.client.gui.drawables.DContainer;
import me.towdium.jecalculation.client.gui.guis.GuiEditorFluidStack;
import me.towdium.jecalculation.core.labels.labels.LabelFluidStack;
import me.towdium.jecalculation.core.labels.labels.LabelItemStack;
import me.towdium.jecalculation.core.labels.labels.LabelOreDict;
@@ -12,6 +13,7 @@ import me.towdium.jecalculation.utils.ItemStackHelper;
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.Single;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidRegistry;
@@ -20,6 +22,7 @@ import net.minecraftforge.fluids.FluidStack;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
@@ -242,18 +245,18 @@ public interface ILabel {
static {
INSTANCE = new RegistryEditor();
INSTANCE.register(LabelOreDict.getEditor(), "common.label.ore_dict", new LabelOreDict("ingotIron"));
INSTANCE.register(LabelFluidStack.getEditor(), "common.label.fluid_stack",
INSTANCE.register(LabelOreDict::getEditor, "common.label.ore_dict",
new LabelOreDict("ingotIron"));
INSTANCE.register(GuiEditorFluidStack::new, "common.label.fluid_stack",
new LabelFluidStack(new FluidStack(FluidRegistry.WATER, 1000), 1000));
}
private ArrayList<Record> records = new ArrayList<>();
private RegistryEditor() {
}
public void register(IEditor editor, String unlocalizedName, ILabel representation) {
public void register(Supplier<IEditor> editor, String unlocalizedName, ILabel representation) {
records.add(new Record(editor, unlocalizedName, representation));
}
@@ -266,21 +269,21 @@ public interface ILabel {
}
public static class Editor extends DContainer implements RegistryEditor.IEditor {
protected Consumer<ILabel> callback;
protected Single<Consumer<ILabel>> callback = new Single<>(null);
@Override
public RegistryEditor.IEditor setCallback(Consumer<ILabel> callback) {
this.callback = callback;
this.callback.value = callback;
return this;
}
}
public static class Record {
public IEditor editor;
public Supplier<IEditor> editor;
public String localizeKey;
public ILabel representation;
public Record(IEditor editor, String localizeKey, ILabel representation) {
public Record(Supplier<IEditor> editor, String localizeKey, ILabel representation) {
this.editor = editor;
this.localizeKey = localizeKey;
this.representation = representation;
@@ -6,6 +6,7 @@ import me.towdium.jecalculation.client.gui.drawables.DText;
import me.towdium.jecalculation.core.labels.ILabel;
import me.towdium.jecalculation.utils.Utilities;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
@@ -29,15 +30,15 @@ public class LabelFluidStack implements ILabel {
this.amount = amount;
}
public LabelFluidStack(Fluid fluid, int amount) {
this(new FluidStack(fluid, 1000), amount);
}
public LabelFluidStack(NBTTagCompound nbt) {
fluid = FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag(KEY_FLUID));
amount = nbt.getInteger(KEY_AMOUNT);
}
public static RegistryEditor.Editor getEditor() {
return new Editor();
}
@Override
public ILabel increaseAmount() {
return ILabel.EMPTY;
@@ -92,16 +93,4 @@ public class LabelFluidStack implements ILabel {
gui.drawResource(Resource.LBL_FLUID, 0, 0);
gui.drawFluid(fluid.getFluid(), 2, 2, 12, 12);
}
public static class Editor extends RegistryEditor.Editor {
public Editor() {
add(new DText(5, 5, JecGui.Font.DEFAULT_NO_SHADOW, "fluid"));
}
@Override
public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) {
callback.accept(new LabelFluidStack(new FluidStack(FluidRegistry.WATER, 1000), 1000));
return super.onClicked(gui, xMouse, yMouse, button);
}
}
}
@@ -115,7 +115,7 @@ public class LabelOreDict extends LabelSimpleAmount {
@Override
public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) {
callback.accept(new LabelOreDict("plankWood"));
callback.value.accept(new LabelOreDict("plankWood"));
return super.onClicked(gui, xMouse, yMouse, button);
}
}