Skip to content

Commit

Permalink
Added support to craft item directly from recipe book
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCrayfish committed Feb 9, 2021
1 parent b7fd171 commit f29bfce
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 66 deletions.
141 changes: 98 additions & 43 deletions src/main/java/com/mrcrayfish/controllable/client/ControllerInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@
import com.mrcrayfish.controllable.integration.JustEnoughItems;
import com.mrcrayfish.controllable.mixin.client.CreativeScreenMixin;
import com.mrcrayfish.controllable.mixin.client.RecipeBookGuiMixin;
import com.mrcrayfish.controllable.mixin.client.RecipeBookPageMixin;
import com.mrcrayfish.controllable.mixin.client.RecipeBookPageAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.SimpleSound;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.IGuiEventListener;
import net.minecraft.client.gui.advancements.AdvancementsScreen;
import net.minecraft.client.gui.recipebook.IRecipeShownListener;
import net.minecraft.client.gui.recipebook.RecipeBookGui;
import net.minecraft.client.gui.recipebook.RecipeBookPage;
import net.minecraft.client.gui.recipebook.RecipeTabToggleWidget;
import net.minecraft.client.gui.recipebook.*;
import net.minecraft.client.gui.screen.IngameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
Expand All @@ -35,6 +31,7 @@
import net.minecraft.client.util.NativeUtil;
import net.minecraft.entity.item.BoatEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.RecipeBookContainer;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemGroup;
import net.minecraft.network.play.client.CPlayerDiggingPacket;
Expand Down Expand Up @@ -111,6 +108,11 @@ private void setControllerInUse()
this.lastUse = 100;
}

public boolean isControllerInUse()
{
return this.lastUse > 0;
}

public int getLastUse()
{
return this.lastUse;
Expand Down Expand Up @@ -218,9 +220,9 @@ public void onClientTick(TickEvent.ClientTickEvent event)
eventListeners.add(((RecipeBookGuiMixin) recipeBook).getToggleRecipesBtn());
eventListeners.addAll(((RecipeBookGuiMixin) recipeBook).getRecipeTabs());
RecipeBookPage recipeBookPage = ((RecipeBookGuiMixin) recipeBook).getRecipeBookPage();
eventListeners.addAll(((RecipeBookPageMixin) recipeBookPage).getButtons());
eventListeners.add(((RecipeBookPageMixin) recipeBookPage).getForwardButton());
eventListeners.add(((RecipeBookPageMixin) recipeBookPage).getBackButton());
eventListeners.addAll(((RecipeBookPageAccessor) recipeBookPage).getButtons());
eventListeners.add(((RecipeBookPageAccessor) recipeBookPage).getForwardButton());
eventListeners.add(((RecipeBookPageAccessor) recipeBookPage).getBackButton());
}
}
IGuiEventListener hoveredListener = eventListeners.stream().filter(o -> o != null && o.isMouseOver(mouseX, mouseY)).findFirst().orElse(null);
Expand Down Expand Up @@ -759,6 +761,7 @@ else if(ButtonBindings.NAVIGATE_RIGHT.isButtonPressed())
else if(button == ButtonBindings.PICKUP_ITEM.getButton())
{
invokeMouseClick(mc.currentScreen, 0);
this.craftRecipeBookItem();
}
else if(button == ButtonBindings.SPLIT_STACK.getButton())
{
Expand Down Expand Up @@ -859,7 +862,7 @@ private void scrollRecipeTab(RecipeBookGui recipeBook, int dir)

private void scrollRecipePage(RecipeBookGui recipeBook, int dir)
{
RecipeBookPageMixin page = (RecipeBookPageMixin)((RecipeBookGuiMixin) recipeBook).getRecipeBookPage();
RecipeBookPageAccessor page = (RecipeBookPageAccessor)((RecipeBookGuiMixin) recipeBook).getRecipeBookPage();
if(dir > 0 && page.getForwardButton().visible || dir < 0 && page.getBackButton().visible)
{
int currentPage = page.getCurrentPage();
Expand Down Expand Up @@ -943,9 +946,9 @@ private List<NavigationPoint> gatherNavigationPoints(Screen screen)
widgets.add(((RecipeBookGuiMixin) recipeBook).getToggleRecipesBtn());
widgets.addAll(((RecipeBookGuiMixin) recipeBook).getRecipeTabs());
RecipeBookPage recipeBookPage = ((RecipeBookGuiMixin) recipeBook).getRecipeBookPage();
widgets.addAll(((RecipeBookPageMixin) recipeBookPage).getButtons());
widgets.add(((RecipeBookPageMixin) recipeBookPage).getForwardButton());
widgets.add(((RecipeBookPageMixin) recipeBookPage).getBackButton());
widgets.addAll(((RecipeBookPageAccessor) recipeBookPage).getButtons());
widgets.add(((RecipeBookPageAccessor) recipeBookPage).getForwardButton());
widgets.add(((RecipeBookPageAccessor) recipeBookPage).getBackButton());
}
}

Expand Down Expand Up @@ -997,6 +1000,40 @@ private BasicNavigationPoint getCreativeTabPoint(ContainerScreen screen, ItemGro
return new BasicNavigationPoint(x + width / 2.0, y + height / 2.0);
}

private void craftRecipeBookItem()
{
Minecraft mc = Minecraft.getInstance();
if(mc.player == null)
return;

if(!(mc.currentScreen instanceof ContainerScreen) || !(mc.currentScreen instanceof IRecipeShownListener))
return;

IRecipeShownListener listener = (IRecipeShownListener) mc.currentScreen;
if(!listener.getRecipeGui().isVisible())
return;

ContainerScreen screen = (ContainerScreen) mc.currentScreen;
if(!(screen.getContainer() instanceof RecipeBookContainer))
return;

RecipeBookPage recipeBookPage = ((RecipeBookGuiMixin) listener.getRecipeGui()).getRecipeBookPage();
RecipeWidget recipe = ((RecipeBookPageAccessor) recipeBookPage).getButtons().stream().filter(Widget::isHovered).findFirst().orElse(null);
if(recipe != null)
{
RecipeBookContainer container = (RecipeBookContainer) screen.getContainer();
Slot slot = container.getSlot(container.getOutputSlot());
if(mc.player.inventory.getItemStack().isEmpty())
{
this.invokeMouseClick(screen, GLFW.GLFW_MOUSE_BUTTON_LEFT, screen.getGuiLeft() + slot.xPos + 8, screen.getGuiTop() + slot.yPos + 8);
}
else
{
this.invokeMouseReleased(screen, GLFW.GLFW_MOUSE_BUTTON_LEFT, screen.getGuiLeft() + slot.xPos + 8, screen.getGuiTop() + slot.yPos + 8);
}
}
}

private void moveMouseToClosestSlot(boolean moving, Screen screen)
{
this.nearSlot = false;
Expand Down Expand Up @@ -1129,6 +1166,28 @@ private void handleListScrolling(AbstractList list, Controller controller)
list.setScrollAmount(list.getScrollAmount() + dir * 10);
}

private double getMouseX()
{
Minecraft mc = Minecraft.getInstance();
double mouseX = mc.mouseHelper.getMouseX();
if(Controllable.getController() != null && Config.CLIENT.options.virtualMouse.get() && this.lastUse > 0)
{
mouseX = this.virtualMouseX;
}
return mouseX * (double) mc.getMainWindow().getScaledWidth() / (double) mc.getMainWindow().getWidth();
}

private double getMouseY()
{
Minecraft mc = Minecraft.getInstance();
double mouseY = mc.mouseHelper.getMouseY();
if(Controllable.getController() != null && Config.CLIENT.options.virtualMouse.get() && this.lastUse > 0)
{
mouseY = this.virtualMouseY;
}
return mouseY * (double) mc.getMainWindow().getScaledHeight() / (double) mc.getMainWindow().getHeight();
}

/**
* Invokes a mouse click in a GUI. This is modified version that is designed for controllers.
* Upon clicking, mouse released is called straight away to make sure dragging doesn't happen.
Expand All @@ -1138,34 +1197,32 @@ private void handleListScrolling(AbstractList list, Controller controller)
*/
private void invokeMouseClick(Screen screen, int button)
{
Minecraft mc = Minecraft.getInstance();
if(screen != null)
{
double mouseX = mc.mouseHelper.getMouseX();
double mouseY = mc.mouseHelper.getMouseY();
if(Controllable.getController() != null && Config.CLIENT.options.virtualMouse.get() && this.lastUse > 0)
{
mouseX = this.virtualMouseX;
mouseY = this.virtualMouseY;
}
mouseX = mouseX * (double) mc.getMainWindow().getScaledWidth() / (double) mc.getMainWindow().getWidth();
mouseY = mouseY * (double) mc.getMainWindow().getScaledHeight() / (double) mc.getMainWindow().getHeight();
double mouseX = this.getMouseX();
double mouseY = this.getMouseY();
this.invokeMouseClick(screen, button, mouseX, mouseY);
}
}

private void invokeMouseClick(Screen screen, int button, double mouseX, double mouseY)
{
Minecraft mc = Minecraft.getInstance();
if(screen != null)
{
mc.mouseHelper.activeButton = button;
mc.mouseHelper.eventTime = NativeUtil.getTime();

double finalMouseX = mouseX;
double finalMouseY = mouseY;
Screen.wrapScreenError(() ->
{
boolean cancelled = ForgeHooksClient.onGuiMouseClickedPre(screen, finalMouseX, finalMouseY, button);
boolean cancelled = ForgeHooksClient.onGuiMouseClickedPre(screen, mouseX, mouseY, button);
if(!cancelled)
{
cancelled = screen.mouseClicked(finalMouseX, finalMouseY, button);
cancelled = screen.mouseClicked(mouseX, mouseY, button);
}
if(!cancelled)
{
ForgeHooksClient.onGuiMouseClickedPost(screen, finalMouseX, finalMouseY, button);
ForgeHooksClient.onGuiMouseClickedPost(screen, mouseX, mouseY, button);
}
}, "mouseClicked event handler", screen.getClass().getCanonicalName());
}
Expand All @@ -1180,33 +1237,31 @@ private void invokeMouseClick(Screen screen, int button)
*/
private void invokeMouseReleased(Screen screen, int button)
{
Minecraft mc = Minecraft.getInstance();
if(screen != null)
{
double mouseX = mc.mouseHelper.getMouseX();
double mouseY = mc.mouseHelper.getMouseY();
if(Controllable.getController() != null && Config.CLIENT.options.virtualMouse.get() && lastUse > 0)
{
mouseX = this.virtualMouseX;
mouseY = this.virtualMouseY;
}
mouseX = mouseX * (double) mc.getMainWindow().getScaledWidth() / (double) mc.getMainWindow().getWidth();
mouseY = mouseY * (double) mc.getMainWindow().getScaledHeight() / (double) mc.getMainWindow().getHeight();
double mouseX = this.getMouseX();
double mouseY = this.getMouseY();
this.invokeMouseReleased(screen, button, mouseX, mouseY);
}
}

private void invokeMouseReleased(Screen screen, int button, double mouseX, double mouseY)
{
Minecraft mc = Minecraft.getInstance();
if(screen != null)
{
mc.mouseHelper.activeButton = -1;

double finalMouseX = mouseX;
double finalMouseY = mouseY;
Screen.wrapScreenError(() ->
{
boolean cancelled = ForgeHooksClient.onGuiMouseReleasedPre(screen, finalMouseX, finalMouseY, button);
boolean cancelled = ForgeHooksClient.onGuiMouseReleasedPre(screen, mouseX, mouseY, button);
if(!cancelled)
{
cancelled = screen.mouseReleased(finalMouseX, finalMouseY, button);
cancelled = screen.mouseReleased(mouseX, mouseY, button);
}
if(!cancelled)
{
ForgeHooksClient.onGuiMouseReleasedPost(screen, finalMouseX, finalMouseY, button);
ForgeHooksClient.onGuiMouseReleasedPost(screen, mouseX, mouseY, button);
}
}, "mouseReleased event handler", screen.getClass().getCanonicalName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mrcrayfish.controllable.mixin.client;

import net.minecraft.client.gui.recipebook.RecipeBookPage;
import net.minecraft.client.gui.recipebook.RecipeWidget;
import net.minecraft.client.gui.widget.ToggleWidget;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;

import java.util.List;

/**
* Author: MrCrayfish
*/
@Mixin(RecipeBookPage.class)
public interface RecipeBookPageAccessor
{
@Accessor("buttons")
List<RecipeWidget> getButtons();

@Accessor("forwardButton")
ToggleWidget getForwardButton();

@Accessor("backButton")
ToggleWidget getBackButton();

@Accessor("currentPage")
int getCurrentPage();

@Accessor("currentPage")
void setCurrentPage(int page);

@Invoker("updateButtonsForPage")
void invokeUpdateButtonsForPage();
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package com.mrcrayfish.controllable.mixin.client;

import com.mrcrayfish.controllable.Controllable;
import net.minecraft.client.gui.recipebook.RecipeBookPage;
import net.minecraft.client.gui.recipebook.RecipeWidget;
import net.minecraft.client.gui.widget.ToggleWidget;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

import java.util.List;

/**
* Author: MrCrayfish
*/
@Mixin(RecipeBookPage.class)
public interface RecipeBookPageMixin
public class RecipeBookPageMixin
{
@Accessor("buttons")
List<RecipeWidget> getButtons();

@Accessor("forwardButton")
ToggleWidget getForwardButton();

@Accessor("backButton")
ToggleWidget getBackButton();

@Accessor("currentPage")
int getCurrentPage();

@Accessor("currentPage")
void setCurrentPage(int page);

@Invoker("updateButtonsForPage")
void invokeUpdateButtonsForPage();
@ModifyArgs(method = "func_238926_a_", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;func_243308_b(Lcom/mojang/blaze3d/matrix/MatrixStack;Ljava/util/List;II)V"))
private void modifyRenderToolTip(Args args)
{
if(Controllable.getInput().isControllerInUse())
{
List<ITextComponent> components = args.get(1);
components.add(new TranslationTextComponent("controllable.tooltip.craft").mergeStyle(TextFormatting.BOLD).mergeStyle(TextFormatting.BLUE));
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/controllable/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@
"controllable.key.moveLeft": "Navigate Left",
"controllable.key.moveRight": "Navigate Right",
"controllable.key.moveUp": "Navigate Up",
"controllable.key.debugInfo": "Show Debug Info"
"controllable.key.debugInfo": "Show Debug Info",
"controllable.tooltip.craft": "Click to Craft Item"
}
1 change: 1 addition & 0 deletions src/main/resources/controllable.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"client.MinecraftMixin",
"client.MouseHelperMixin",
"client.RecipeBookGuiMixin",
"client.RecipeBookPageAccessor",
"client.RecipeBookPageMixin",
"client.jei.MouseUtilMixin",
"client.jei.IngredientListOverlayMixin",
Expand Down

0 comments on commit f29bfce

Please sign in to comment.