Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JEI request search to crafting teaching window #10471

Open
wants to merge 2 commits into
base: version/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.minecolonies.api.util.constant;

import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.common.util.Size2i;

import static com.minecolonies.api.util.constant.Constants.MOD_ID;
import static com.minecolonies.api.util.constant.TranslationConstants.*;

/**
Expand Down Expand Up @@ -1278,6 +1280,16 @@ public final class WindowConstants
*/
public static final String TITLE_LABEL = "title";

/**
* Crafting switch buttons texture.
*/
public static final ResourceLocation CRAFTING_SWITCH_TEXTURE = new ResourceLocation(MOD_ID, "textures/gui/craftingswitch.png");

/**
* Switch button size.
*/
public static final Size2i CRAFTING_SWITCH_SIZE = new Size2i(20, 18);

/**
* Private constructor to hide implicit public one.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.minecolonies.core.client.gui.containers;

import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.requestsystem.request.IRequest;
import com.minecolonies.api.colony.requestsystem.requestable.IConcreteDeliverable;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.crafting.ModCraftingTypes;
import com.minecolonies.api.inventory.container.ContainerCrafting;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.core.Network;
import com.minecolonies.core.client.gui.modules.WindowSelectRequest;
import com.minecolonies.core.colony.buildings.moduleviews.CraftingModuleView;
import com.minecolonies.core.colony.buildings.views.AbstractBuildingView;
import com.minecolonies.core.network.messages.server.SwitchRecipeCraftingTeachingMessage;
import com.minecolonies.core.network.messages.server.colony.building.worker.AddRemoveRecipeMessage;
import com.minecolonies.core.util.DomumOrnamentumUtils;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ImageButton;
Expand All @@ -19,12 +23,17 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import static com.minecolonies.api.util.constant.Constants.MOD_ID;
import static com.minecolonies.api.util.constant.TranslationConstants.WARNING_MAXIMUM_NUMBER_RECIPES;
import static com.minecolonies.api.util.constant.WindowConstants.*;
import static com.minecolonies.api.util.constant.translation.BaseGameTranslationConstants.BASE_GUI_DONE;

/**
Expand All @@ -36,8 +45,6 @@ public class WindowCrafting extends AbstractContainerScreen<ContainerCrafting>

private static final ResourceLocation CRAFTING_TABLE_GUI_TEXTURES3X3 = new ResourceLocation(MOD_ID, "textures/gui/crafting3x3.png");

private static final ResourceLocation SWITCH_TEXTURE = new ResourceLocation(MOD_ID, "textures/gui/craftingswitch.png");

/**
* X offset of the button.
*/
Expand All @@ -58,16 +65,6 @@ public class WindowCrafting extends AbstractContainerScreen<ContainerCrafting>
*/
private static final int BUTTON_HEIGHT = 20;

/**
* Switch button width.
*/
private static final int SWITCH_WIDTH = 20;

/**
* Switch button height.
*/
private static final int SWITCH_HEIGHT = 18;

/**
* Switch button horizontal location.
*/
Expand All @@ -76,7 +73,13 @@ public class WindowCrafting extends AbstractContainerScreen<ContainerCrafting>
/**
* Switch button vertical location.
*/
private static final int SWITCH_Y_OFFSET = 43 - (SWITCH_HEIGHT / 2);
private static final int SWITCH_Y_OFFSET = 43 - (CRAFTING_SWITCH_SIZE.height / 2);

/**
* Request list button location.
*/
private static final int REQUEST_X_OFFSET = 100 - (CRAFTING_SWITCH_SIZE.width / 2);
private static final int REQUEST_Y_OFFSET = 70 - CRAFTING_SWITCH_SIZE.height;

/**
* Color of the gui description.
Expand Down Expand Up @@ -123,6 +126,9 @@ public class WindowCrafting extends AbstractContainerScreen<ContainerCrafting>
*/
private ImageButton switchButton;

@Nullable public static Consumer<ItemStack> JEI_REQUEST_HOOK;
private final Map<IRequest<?>, ItemStack> requestables = new HashMap<>();

/**
* Create a crafting gui window.
*
Expand Down Expand Up @@ -167,13 +173,21 @@ protected void init()
doneButton.active = false;
}

this.switchButton = new ImageButton(leftPos + SWITCH_X_OFFSET, topPos + SWITCH_Y_OFFSET, SWITCH_WIDTH, SWITCH_HEIGHT,
0, 0, SWITCH_HEIGHT + 1, SWITCH_TEXTURE, btn ->
this.switchButton = new ImageButton(leftPos + SWITCH_X_OFFSET, topPos + SWITCH_Y_OFFSET, CRAFTING_SWITCH_SIZE.width, CRAFTING_SWITCH_SIZE.height,
0, 0, CRAFTING_SWITCH_SIZE.height + 1, CRAFTING_SWITCH_TEXTURE, btn ->
{
Network.getNetwork().sendToServer(new SwitchRecipeCraftingTeachingMessage());
});
this.switchButton.visible = false;
this.addRenderableWidget(this.switchButton);

final ImageButton requestsButton = new ImageButton(leftPos + REQUEST_X_OFFSET, topPos + REQUEST_Y_OFFSET, CRAFTING_SWITCH_SIZE.width, CRAFTING_SWITCH_SIZE.height,
CRAFTING_SWITCH_SIZE.width + 1, 0, CRAFTING_SWITCH_SIZE.height + 1, CRAFTING_SWITCH_TEXTURE, btn ->
{
requestables.clear();
new WindowSelectRequest(this.building, this::matchingRequest, this::reopenWithRequest).open();
});
this.addRenderableWidget(requestsButton);
}

@Override
Expand All @@ -184,6 +198,33 @@ protected void containerTick()
this.switchButton.visible = this.menu.canSwitchRecipes();
}

private boolean matchingRequest(@NotNull final IRequest<?> request)
{
if (!DomumOrnamentumUtils.getRequestedStack(request).isEmpty()) return false;

if (request.getRequest() instanceof IConcreteDeliverable deliverable)
{
for (final ItemStack stack : deliverable.getRequestedItems())
{
// todo filter?
requestables.put(request, stack);
return true;
}
}
return false;
}

private void reopenWithRequest(@Nullable final IRequest<?> request)
{
minecraft.setScreen(this);

final ItemStack stack = requestables.getOrDefault(request, ItemStack.EMPTY);
if (!stack.isEmpty() && JEI_REQUEST_HOOK != null)
{
JEI_REQUEST_HOOK.accept(stack);
}
}

private void onDoneClicked(final Button button)
{
if (module.canLearn(ModCraftingTypes.SMALL_CRAFTING.get()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
package com.minecolonies.core.client.gui.containers;

import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.requestsystem.request.IRequest;
import com.minecolonies.api.colony.requestsystem.requestable.IConcreteDeliverable;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.crafting.ModCraftingTypes;
import com.minecolonies.api.inventory.container.ContainerCraftingFurnace;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.core.Network;
import com.minecolonies.core.client.gui.modules.WindowSelectRequest;
import com.minecolonies.core.colony.buildings.moduleviews.CraftingModuleView;
import com.minecolonies.core.colony.buildings.views.AbstractBuildingView;
import com.minecolonies.core.network.messages.server.colony.building.worker.AddRemoveRecipeMessage;
import com.minecolonies.core.util.DomumOrnamentumUtils;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.minecolonies.api.util.constant.translation.BaseGameTranslationConstants.BASE_GUI_DONE;
import static com.minecolonies.api.util.constant.TranslationConstants.WARNING_MAXIMUM_NUMBER_RECIPES;
import static com.minecolonies.api.util.constant.WindowConstants.CRAFTING_SWITCH_SIZE;
import static com.minecolonies.api.util.constant.WindowConstants.CRAFTING_SWITCH_TEXTURE;
import static com.minecolonies.api.util.constant.translation.BaseGameTranslationConstants.BASE_GUI_DONE;

/**
* Furnace crafting gui.
Expand Down Expand Up @@ -53,6 +63,12 @@ public class WindowFurnaceCrafting extends AbstractContainerScreen<ContainerCraf
*/
private static final int BUTTON_HEIGHT = 20;

/**
* Request list button location.
*/
private static final int REQUEST_X_OFFSET = 90 - (CRAFTING_SWITCH_SIZE.width / 2);
private static final int REQUEST_Y_OFFSET = 70 - CRAFTING_SWITCH_SIZE.height;

/**
* The building the window belongs to.
*/
Expand All @@ -68,6 +84,8 @@ public class WindowFurnaceCrafting extends AbstractContainerScreen<ContainerCraf
*/
private final CraftingModuleView module;

private final Map<IRequest<?>, ItemStack> requestables = new HashMap<>();

/**
* Create a crafting gui window.
*
Expand Down Expand Up @@ -103,6 +121,14 @@ protected void init()
{
doneButton.active = false;
}

final ImageButton requestsButton = new ImageButton(leftPos + REQUEST_X_OFFSET, topPos + REQUEST_Y_OFFSET, CRAFTING_SWITCH_SIZE.width, CRAFTING_SWITCH_SIZE.height,
CRAFTING_SWITCH_SIZE.width + 1, 0, CRAFTING_SWITCH_SIZE.height + 1, CRAFTING_SWITCH_TEXTURE, btn ->
{
requestables.clear();
new WindowSelectRequest(this.building, this::matchingRequest, this::reopenWithRequest).open();
});
this.addRenderableWidget(requestsButton);
}

public class OnButtonPress implements Button.OnPress
Expand All @@ -124,6 +150,33 @@ public void onPress(@NotNull final Button button)
}
}

private boolean matchingRequest(@NotNull final IRequest<?> request)
{
if (!DomumOrnamentumUtils.getRequestedStack(request).isEmpty()) return false;

if (request.getRequest() instanceof IConcreteDeliverable deliverable)
{
for (final ItemStack stack : deliverable.getRequestedItems())
{
// todo filter?
requestables.put(request, stack);
return true;
}
}
return false;
}

private void reopenWithRequest(@Nullable final IRequest<?> request)
{
minecraft.setScreen(this);

final ItemStack stack = requestables.getOrDefault(request, ItemStack.EMPTY);
if (!stack.isEmpty() && WindowCrafting.JEI_REQUEST_HOOK != null)
{
WindowCrafting.JEI_REQUEST_HOOK.accept(stack);
}
}

/**
* Draws the background layer of this container (behind the items).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.minecolonies.api.util.Log;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.api.util.constant.TranslationConstants;
import com.minecolonies.core.client.gui.containers.WindowCrafting;
import com.minecolonies.core.colony.buildings.modules.AnimalHerdingModule;
import com.minecolonies.core.colony.crafting.RecipeAnalyzer;
import com.minecolonies.core.compatibility.jei.transfer.*;
Expand All @@ -21,8 +22,11 @@
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.helpers.IJeiHelpers;
import mezz.jei.api.helpers.IModIdHelper;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.registration.*;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.network.chat.Component;
Expand All @@ -31,13 +35,19 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.BiConsumer;

@mezz.jei.api.JeiPlugin
public class JEIPlugin implements IModPlugin
{
public JEIPlugin()
{
WindowCrafting.JEI_REQUEST_HOOK = this::showOutputStack;
}

@NotNull
@Override
public ResourceLocation getPluginUid()
Expand All @@ -46,6 +56,8 @@ public ResourceLocation getPluginUid()
}

private final List<JobBasedRecipeCategory<?>> categories = new ArrayList<>();
@Nullable
private IJeiRuntime jei;

@Override
public void registerCategories(@NotNull final IRecipeCategoryRegistration registration)
Expand Down Expand Up @@ -187,4 +199,25 @@ public void registerGuiHandlers(@NotNull final IGuiHandlerRegistration registrat
new FurnaceCraftingGuiHandler(this.categories).register(registration);
new BrewingCraftingGuiHandler(this.categories).register(registration);
}

@Override
public void onRuntimeAvailable(@NotNull final IJeiRuntime jeiRuntime)
{
this.jei = jeiRuntime;
}

@Override
public void onRuntimeUnavailable()
{
this.jei = null;
}

private void showOutputStack(@NotNull final ItemStack stack)
{
if (this.jei != null)
{
final IFocus<?> focus = this.jei.getJeiHelpers().getFocusFactory().createFocus(RecipeIngredientRole.OUTPUT, VanillaTypes.ITEM_STACK, stack);
this.jei.getRecipesGui().show(focus);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.