Skip to content

Commit

Permalink
Work on indexing the crafting windows that popup in preparation of fi…
Browse files Browse the repository at this point in the history
…guring out how to get it working properly with JEI, JEI now shows a debug number when trying to fill the crafting grid of the QIO of which window is selected
  • Loading branch information
pupnewfster committed Dec 31, 2020
1 parent 2e16ce0 commit 7b9bb1e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 30 deletions.
7 changes: 7 additions & 0 deletions src/main/java/mekanism/client/gui/GuiMekanism.java
Expand Up @@ -242,7 +242,9 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
}
// this check prevents us from moving the window to the top of the stack if the clicked window opened up an additional window
if (top != focused) {
top.onFocusLost();
windows.moveUp(focused);
focused.onFocused();
}
return true;
}
Expand Down Expand Up @@ -416,7 +418,12 @@ protected static String formatInt(long l) {

@Override
public void addWindow(GuiWindow window) {
GuiWindow top = windows.isEmpty() ? null : windows.iterator().next();
if (top != null) {
top.onFocusLost();
}
windows.add(window);
window.onFocused();
}

@Override
Expand Down
@@ -1,6 +1,7 @@
package mekanism.client.gui.element.tab.window;

import com.mojang.blaze3d.matrix.MatrixStack;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import mekanism.client.SpecialColors;
Expand All @@ -15,10 +16,15 @@
public class GuiCraftingWindowTab<DATA_SOURCE> extends GuiWindowCreatorTab<DATA_SOURCE, GuiCraftingWindowTab<DATA_SOURCE>> {

private static final int MAX_WINDOWS = 3;
private final Consumer<GuiCraftingWindow<DATA_SOURCE>> onFocus;
//TODO: Evaluate a better way of doing this than this weird openWindows thing
private final boolean[] openWindows = new boolean[MAX_WINDOWS];
private int currentWindows;

public GuiCraftingWindowTab(IGuiWrapper gui, DATA_SOURCE dataSource, Supplier<GuiCraftingWindowTab<DATA_SOURCE>> elementSupplier) {
public GuiCraftingWindowTab(IGuiWrapper gui, DATA_SOURCE dataSource, Supplier<GuiCraftingWindowTab<DATA_SOURCE>> elementSupplier,
Consumer<GuiCraftingWindow<DATA_SOURCE>> onFocus) {
super(MekanismUtils.getResource(ResourceType.GUI_BUTTON, "crafting.png"), gui, dataSource, -26, 34, 26, 18, true, elementSupplier);
this.onFocus = onFocus;
}

@Override
Expand All @@ -34,9 +40,12 @@ protected void colorTab() {
}

@Override
protected Runnable getCloseListener() {
return () -> {
protected Consumer<GuiWindow> getCloseListener() {
return window -> {
GuiCraftingWindowTab<DATA_SOURCE> tab = getElementSupplier().get();
if (window instanceof GuiCraftingWindow) {
tab.openWindows[((GuiCraftingWindow<?>) window).getIndex()] = false;
}
tab.currentWindows--;
if (tab.currentWindows < MAX_WINDOWS) {
//If we have less than the max number of windows re-enable the tab
Expand All @@ -45,6 +54,16 @@ protected Runnable getCloseListener() {
};
}

@Override
protected Consumer<GuiWindow> getReAttachListener() {
return super.getReAttachListener().andThen(window -> {
if (window instanceof GuiCraftingWindow) {
GuiCraftingWindowTab<DATA_SOURCE> tab = getElementSupplier().get();
tab.openWindows[((GuiCraftingWindow<?>) window).getIndex()] = true;
}
});
}

@Override
protected void disableTab() {
currentWindows++;
Expand All @@ -56,6 +75,20 @@ protected void disableTab() {

@Override
protected GuiWindow createWindow() {
return new GuiCraftingWindow<>(guiObj, guiObj.getWidth() / 2 - 156 / 2, 15, dataSource);
int index = 0;
for (int i = 0; i < openWindows.length; i++) {
if (!openWindows[i]) {
index = i;
break;
}
}
openWindows[index] = true;
//TODO: Fix indexing after exiting JEI it gets screwed up and resets
// It does this because onClose is called, which means we then think the window is closed
// To fix this would mean we have to keep track of how things close and maybe just not fire
// the on close stuff if everything gets closed? Could lead to things that are supposed to
// save on close not happening
// TODO: Replace what calls onClose to call it with a "source"??? ^ may not even be true actually
return new GuiCraftingWindow<>(guiObj, guiObj.getWidth() / 2 - 156 / 2, 15, dataSource, onFocus, index);
}
}
@@ -1,5 +1,6 @@
package mekanism.client.gui.element.tab.window;

import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import mekanism.client.gui.IGuiWrapper;
Expand Down Expand Up @@ -35,11 +36,8 @@ public void adoptWindows(int prevLeft, int prevTop, GuiWindow... windows) {
int left = guiObj.getLeft();
int top = guiObj.getTop();
for (GuiWindow window : windows) {
Runnable reattachListener = getReAttachListener();
//TODO: Fix the windows after being adopted not being able to be closed??
window.setTabListeners(getCloseListener(), reattachListener);
//TODO: Fix positioning
//reattachListener.run();
window.setTabListeners(getCloseListener(), getReAttachListener());
window.resize(prevLeft, prevTop, left, top);
}
}
Expand All @@ -48,12 +46,12 @@ protected void disableTab() {
active = false;
}

protected Runnable getCloseListener() {
return () -> elementSupplier.get().active = true;
protected Consumer<GuiWindow> getCloseListener() {
return window -> elementSupplier.get().active = true;
}

protected Runnable getReAttachListener() {
return () -> elementSupplier.get().disableTab();
protected Consumer<GuiWindow> getReAttachListener() {
return window -> elementSupplier.get().disableTab();
}

protected abstract GuiWindow createWindow();
Expand Down
@@ -1,6 +1,7 @@
package mekanism.client.gui.element.window;

import com.mojang.blaze3d.matrix.MatrixStack;
import java.util.function.Consumer;
import mekanism.api.text.TextComponentUtil;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.gui.element.GuiRightArrow;
Expand All @@ -10,13 +11,15 @@

public class GuiCraftingWindow<DATA_SOURCE> extends GuiWindow {

private final Consumer<GuiCraftingWindow<DATA_SOURCE>> onFocus;
private final DATA_SOURCE dataSource;
//TODO: Implement calculating what this is
private int index;
private final int index;

public GuiCraftingWindow(IGuiWrapper gui, int x, int y, DATA_SOURCE dataSource) {
public GuiCraftingWindow(IGuiWrapper gui, int x, int y, DATA_SOURCE dataSource, Consumer<GuiCraftingWindow<DATA_SOURCE>> onFocus, int index) {
super(gui, x, y, 118, 80);
this.dataSource = dataSource;
this.onFocus = onFocus;
this.index = index;
interactionStrategy = InteractionStrategy.ALL;
//TODO: Have this stuff sync, the container will need to always be syncing the slots just in case
// the order of which one is open changes or stuff
Expand All @@ -30,8 +33,13 @@ public GuiCraftingWindow(IGuiWrapper gui, int x, int y, DATA_SOURCE dataSource)
}

@Override
public void close() {
super.close();
public void onFocused() {
super.onFocused();
onFocus.accept(this);
}

public int getIndex() {
return index;
}

@Override
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/mekanism/client/gui/element/window/GuiWindow.java
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import java.util.function.Consumer;
import java.util.function.Supplier;
import mekanism.client.gui.GuiMekanism;
import mekanism.client.gui.GuiUtils;
Expand All @@ -19,8 +20,8 @@ public class GuiWindow extends GuiTexturedElement {

private static final Color OVERLAY_COLOR = Color.rgbai(60, 60, 60, 128);

private Runnable closeListener;
private Runnable reattachListener;
private Consumer<GuiWindow> closeListener;
private Consumer<GuiWindow> reattachListener;

private boolean dragging = false;
private double dragX, dragY;
Expand All @@ -37,6 +38,13 @@ public GuiWindow(IGuiWrapper gui, int x, int y, int width, int height) {
}
}

public void onFocusLost() {
//TODO: Validate onFocusLost gets called properly
}

public void onFocused() {
}

protected void addCloseButton() {
addChild(new GuiCloseButton(getGuiObj(), this.x + 6, this.y + 6, this));
}
Expand Down Expand Up @@ -115,10 +123,10 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
}

public void setListenerTab(Supplier<? extends GuiElement> elementSupplier) {
setTabListeners(() -> elementSupplier.get().active = true, () -> elementSupplier.get().active = false);
setTabListeners(window -> elementSupplier.get().active = true, window -> elementSupplier.get().active = false);
}

public void setTabListeners(Runnable closeListener, Runnable reattachListener) {
public void setTabListeners(Consumer<GuiWindow> closeListener, Consumer<GuiWindow> reattachListener) {
this.closeListener = closeListener;
this.reattachListener = reattachListener;
}
Expand All @@ -127,7 +135,7 @@ public void setTabListeners(Runnable closeListener, Runnable reattachListener) {
public void resize(int prevLeft, int prevTop, int left, int top) {
super.resize(prevLeft, prevTop, left, top);
if (reattachListener != null) {
reattachListener.run();
reattachListener.accept(this);
}
}

Expand All @@ -146,7 +154,7 @@ public void close() {
((GuiMekanism<?>) guiObj).setListener(null);
}
if (closeListener != null) {
closeListener.run();
closeListener.accept(this);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/client/gui/qio/GuiQIODashboard.java
Expand Up @@ -32,7 +32,7 @@ public void init() {
addButton(new GuiQIOFrequencyTab(this, tile));
//TODO: Figure out how we want to implement this into the portable QIO dashboard, as it may still need a decent
// bit of refactoring to properly allow for updating the selected crafting grid window's index in the container
addButton(craftingWindowTab = new GuiCraftingWindowTab<>(this, tile, () -> craftingWindowTab));
addButton(craftingWindowTab = new GuiCraftingWindowTab<>(this, tile, () -> craftingWindowTab, window -> container.setSelectedCraftingGrid(window.getIndex())));
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/mekanism/client/jei/MekanismJEI.java
Expand Up @@ -49,6 +49,7 @@
import mekanism.common.capabilities.Capabilities;
import mekanism.common.inventory.container.entity.robit.CraftingRobitContainer;
import mekanism.common.inventory.container.tile.FormulaicAssemblicatorContainer;
import mekanism.common.inventory.container.tile.QIODashboardContainer;
import mekanism.common.recipe.MekanismRecipeType;
import mekanism.common.registries.MekanismBlocks;
import mekanism.common.registries.MekanismItems;
Expand All @@ -59,6 +60,7 @@
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.subtypes.ISubtypeInterpreter;
import mezz.jei.api.ingredients.subtypes.UidContext;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandlerHelper;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.IModIngredientRegistration;
import mezz.jei.api.registration.IRecipeCatalystRegistration;
Expand Down Expand Up @@ -340,9 +342,10 @@ public void registerRecipeCatalysts(IRecipeCatalystRegistration registry) {

@Override
public void registerRecipeTransferHandlers(IRecipeTransferRegistration registry) {
IRecipeTransferHandlerHelper transferHelper = registry.getTransferHelper();
registry.addRecipeTransferHandler(CraftingRobitContainer.class, VanillaRecipeCategoryUid.CRAFTING, 1, 9, 10, 36);
//TODO - 10.1: Validate we are properly allowing for searching all slots in the formulaic assemblicator and the player's inventory
registry.addRecipeTransferHandler(FormulaicAssemblicatorContainer.class, VanillaRecipeCategoryUid.CRAFTING, 19, 9, 35, 36);
registry.addRecipeTransferHandler(new QIOCraftingTransferHandler(registry.getTransferHelper()), VanillaRecipeCategoryUid.CRAFTING);
registry.addRecipeTransferHandler(new QIOCraftingTransferHandler<>(transferHelper, QIODashboardContainer.class), VanillaRecipeCategoryUid.CRAFTING);
}
}
Expand Up @@ -8,22 +8,24 @@
import mezz.jei.api.recipe.transfer.IRecipeTransferHandlerHelper;
import net.minecraft.entity.player.PlayerEntity;

public class QIOCraftingTransferHandler implements IRecipeTransferHandler<QIOItemViewerContainer> {
public class QIOCraftingTransferHandler<CONTAINER extends QIOItemViewerContainer> implements IRecipeTransferHandler<CONTAINER> {

private final IRecipeTransferHandlerHelper handlerHelper;
private final Class<CONTAINER> containerClass;

public QIOCraftingTransferHandler(IRecipeTransferHandlerHelper handlerHelper) {
public QIOCraftingTransferHandler(IRecipeTransferHandlerHelper handlerHelper, Class<CONTAINER> containerClass) {
this.handlerHelper = handlerHelper;
this.containerClass = containerClass;
}

@Override
public Class<QIOItemViewerContainer> getContainerClass() {
return QIOItemViewerContainer.class;
public Class<CONTAINER> getContainerClass() {
return containerClass;
}

@Nullable
@Override
public IRecipeTransferError transferRecipe(QIOItemViewerContainer container, Object recipe, IRecipeLayout recipeLayout, PlayerEntity player, boolean maxTransfer,
public IRecipeTransferError transferRecipe(CONTAINER container, Object recipe, IRecipeLayout recipeLayout, PlayerEntity player, boolean maxTransfer,
boolean doTransfer) {
if (container.getSelectedCraftingGrid() == -1) {
//Note: While the java docs recommend to log a message to the console when returning an internal error,
Expand Down

0 comments on commit 7b9bb1e

Please sign in to comment.