Skip to content

Commit

Permalink
refactor recipe book to dedicated implementation, closes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIllusiveC4 committed Apr 19, 2020
1 parent 33667dd commit 0742ba5
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 53 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod_icon=curios_icon.png

# Dependencies
version_mc=1.15.2
version_forge=1.15.2-31.0.0
version_forge=1.15.2-31.1.0
version_mcp=20200301-1.15.1
version_jei=6.0.0.2

Expand Down
166 changes: 115 additions & 51 deletions src/main/java/top/theillusivec4/curios/client/gui/CuriosScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
package top.theillusivec4.curios.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import javax.annotation.Nonnull;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.recipebook.IRecipeShownListener;
import net.minecraft.client.gui.recipebook.RecipeBookGui;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.screen.inventory.InventoryScreen;
import net.minecraft.client.gui.widget.button.ImageButton;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.InputMappings;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.network.PacketDistributor;
import top.theillusivec4.curios.Curios;
import top.theillusivec4.curios.api.CuriosAPI;
import top.theillusivec4.curios.api.capability.ICurioItemHandler;
Expand All @@ -45,10 +45,8 @@
import top.theillusivec4.curios.common.CuriosConfig.Client;
import top.theillusivec4.curios.common.CuriosConfig.Client.ButtonCorner;
import top.theillusivec4.curios.common.inventory.CuriosContainer;
import top.theillusivec4.curios.common.network.NetworkHandler;
import top.theillusivec4.curios.common.network.client.CPacketOpenVanilla;

public class CuriosScreen extends ContainerScreen<CuriosContainer> {
public class CuriosScreen extends ContainerScreen<CuriosContainer> implements IRecipeShownListener {

static final ResourceLocation CURIO_INVENTORY = new ResourceLocation(Curios.MODID,
"textures/gui/inventory.png");
Expand All @@ -58,7 +56,12 @@ public class CuriosScreen extends ContainerScreen<CuriosContainer> {
private static final ResourceLocation CREATIVE_INVENTORY_TABS = new ResourceLocation(
"textures/gui/container/creative_inventory/tabs.png");

private boolean widthTooNarrow;
private final RecipeBookGui recipeBookGui = new RecipeBookGui();

public boolean hasScrollBar;
public boolean widthTooNarrow;

private GuiButtonCurios buttonCurios;
private float currentScroll;
private boolean isScrolling;
private boolean buttonClicked;
Expand All @@ -72,44 +75,53 @@ public CuriosScreen(CuriosContainer curiosContainer, PlayerInventory playerInven
@Override
public void init() {
super.init();
this.widthTooNarrow = this.width < 379;
this.guiLeft = (this.width - this.xSize) / 2;
Tuple<Integer, Integer> offsets = getButtonOffset(false);
this.addButton(new GuiButtonCurios(this, this.getGuiLeft() + offsets.getA(),
this.height / 2 + offsets.getB(), 14, 14, 50, 0, 14, CURIO_INVENTORY));

if (!this.playerInventory.player.isCreative() && this.minecraft != null) {
this.addButton(new ImageButton(this.guiLeft + 104, this.height / 2 - 22, 20, 18, 0, 0, 19,
RECIPE_BUTTON_TEXTURE, (button) -> {
Minecraft mc = this.minecraft;
InventoryScreen inventory = new InventoryScreen(mc.player);
ItemStack stack = mc.player.inventory.getItemStack();
mc.player.inventory.setItemStack(ItemStack.EMPTY);
mc.displayGuiScreen(inventory);
RecipeBookGui recipeBookGui = inventory.getRecipeGui();

if (!recipeBookGui.isVisible()) {
recipeBookGui.initSearchBar(inventory.width < 379);
recipeBookGui.toggleVisibility();
ObfuscationReflectionHelper.setPrivateValue(ContainerScreen.class, inventory,
recipeBookGui.updateScreenPosition(inventory.width < 379, inventory.width,
inventory.getXSize()), "field_147003_i");
inventory.children().forEach((listener) -> {
if (listener instanceof ImageButton) {
ImageButton imgButton = (ImageButton) listener;
ResourceLocation resourceLocation = ObfuscationReflectionHelper
.getPrivateValue(ImageButton.class, imgButton, "field_191750_o");

if (resourceLocation != null && resourceLocation.equals(RECIPE_BUTTON_TEXTURE)) {
imgButton.setPosition(inventory.getGuiLeft() + 104, inventory.height / 2 - 22);
}
}
});
}
mc.player.inventory.setItemStack(stack);
NetworkHandler.INSTANCE.send(PacketDistributor.SERVER.noArg(), new CPacketOpenVanilla());
}));

if (this.minecraft != null) {

if (this.minecraft.player != null) {
hasScrollBar = CuriosAPI.getCuriosHandler(this.minecraft.player)
.map(handler -> handler.getSlots() > 8).orElse(false);
}
this.widthTooNarrow = this.width < (hasScrollBar ? 461 : 491);
this.recipeBookGui
.init(this.width, this.height, this.minecraft, this.widthTooNarrow, this.container);
this.updateScreenPosition();
this.children.add(this.recipeBookGui);
this.setFocusedDefault(this.recipeBookGui);
Tuple<Integer, Integer> offsets = getButtonOffset(false);
this.buttonCurios = new GuiButtonCurios(this, this.getGuiLeft() + offsets.getA(),
this.height / 2 + offsets.getB(), 14, 14, 50, 0, 14, CURIO_INVENTORY);
this.addButton(this.buttonCurios);

if (!this.playerInventory.player.isCreative()) {
this.addButton(new ImageButton(this.guiLeft + 104, this.height / 2 - 22, 20, 18, 0, 0, 19,
RECIPE_BUTTON_TEXTURE, (button) -> {
this.recipeBookGui.initSearchBar(this.widthTooNarrow);
this.recipeBookGui.toggleVisibility();
this.updateScreenPosition();
((ImageButton) button).setPosition(this.guiLeft + 104, this.height / 2 - 22);
this.buttonCurios
.setPosition(this.guiLeft + offsets.getA(), this.height / 2 + offsets.getB());
}));
}
}
}

private void updateScreenPosition() {
int i;

if (this.recipeBookGui.isVisible() && !this.widthTooNarrow) {
i = 177 + (this.width - this.xSize - (hasScrollBar ? 118 : 148)) / 2;
} else {
i = (this.width - this.xSize) / 2;
}
this.guiLeft = i;
}

@Override
public void tick() {
super.tick();
this.recipeBookGui.tick();
}

public static Tuple<Integer, Integer> getButtonOffset(boolean isCreative) {
Expand Down Expand Up @@ -142,16 +154,32 @@ private boolean inScrollBar(double mouseX, double mouseY) {
@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.renderBackground();
super.render(mouseX, mouseY, partialTicks);

if (this.recipeBookGui.isVisible() && this.widthTooNarrow) {
this.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
this.recipeBookGui.render(mouseX, mouseY, partialTicks);
} else {
this.recipeBookGui.render(mouseX, mouseY, partialTicks);
super.render(mouseX, mouseY, partialTicks);
this.recipeBookGui.renderGhostRecipe(this.guiLeft, this.guiTop, true, partialTicks);
}
this.renderHoveredToolTip(mouseX, mouseY);
}

@Override
public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {

if (KeyRegistry.openCurios
if (this.recipeBookGui.isVisible() && this.widthTooNarrow) {
this.recipeBookGui.toggleVisibility();
this.updateScreenPosition();
return true;
} else if (KeyRegistry.openCurios
.isActiveAndMatches(InputMappings.getInputByCode(p_keyPressed_1_, p_keyPressed_2_))) {
this.getMinecraft().player.closeScreen();
ClientPlayerEntity playerEntity = this.getMinecraft().player;

if (playerEntity != null) {
playerEntity.closeScreen();
}
return true;
} else {
return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
Expand Down Expand Up @@ -214,7 +242,7 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i
@Override
protected boolean isPointInRegion(int rectX, int rectY, int rectWidth, int rectHeight,
double pointX, double pointY) {
return !this.widthTooNarrow && super
return (!this.widthTooNarrow || !this.recipeBookGui.isVisible()) && super
.isPointInRegion(rectX, rectY, rectWidth, rectHeight, pointX, pointY);
}

Expand All @@ -224,11 +252,14 @@ protected boolean isPointInRegion(int rectX, int rectY, int rectWidth, int rectH
@Override
public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {

if (this.inScrollBar(mouseX, mouseY)) {
if (this.recipeBookGui.mouseClicked(mouseX, mouseY, mouseButton)) {
return true;
} else if (this.inScrollBar(mouseX, mouseY)) {
this.isScrolling = this.needsScrollBars();
return true;
}
return !this.widthTooNarrow && super.mouseClicked(mouseX, mouseY, mouseButton);
return this.widthTooNarrow && this.recipeBookGui.isVisible() || super
.mouseClicked(mouseX, mouseY, mouseButton);
}

@Override
Expand Down Expand Up @@ -281,4 +312,37 @@ public boolean mouseScrolled(double pMouseScrolled1, double pMouseScrolled3,
private boolean needsScrollBars() {
return this.container.canScroll();
}

protected boolean hasClickedOutside(double mouseX, double mouseY, int guiLeftIn, int guiTopIn,
int mouseButton) {
boolean flag =
mouseX < (double) guiLeftIn || mouseY < (double) guiTopIn || mouseX >= (double) (guiLeftIn
+ this.xSize) || mouseY >= (double) (guiTopIn + this.ySize);
return this.recipeBookGui
.func_195604_a(mouseX, mouseY, this.guiLeft, this.guiTop, this.xSize, this.ySize,
mouseButton) && flag;
}

@Override
protected void handleMouseClick(Slot slotIn, int slotId, int mouseButton, ClickType type) {
super.handleMouseClick(slotIn, slotId, mouseButton, type);
this.recipeBookGui.slotClicked(slotIn);
}

@Override
public void recipesUpdated() {
this.recipeBookGui.recipesUpdated();
}

@Override
public void removed() {
this.recipeBookGui.removed();
super.removed();
}

@Nonnull
@Override
public RecipeBookGui getRecipeGui() {
return this.recipeBookGui;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

package top.theillusivec4.curios.common.inventory;

import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;
import java.util.List;
import java.util.Optional;
import java.util.SortedMap;
import javax.annotation.Nonnull;
import net.minecraft.client.util.RecipeBookCategories;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -35,10 +38,13 @@
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.CraftingResultSlot;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.inventory.container.RecipeBookContainer;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.RecipeItemHelper;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SSetSlotPacket;
import net.minecraft.server.MinecraftServer;
Expand All @@ -58,7 +64,7 @@
import top.theillusivec4.curios.common.network.client.CPacketScrollCurios;
import top.theillusivec4.curios.common.network.server.SPacketScrollCurios;

public class CuriosContainer extends Container {
public class CuriosContainer extends RecipeBookContainer<CraftingInventory> {

private static final ResourceLocation[] ARMOR_SLOT_TEXTURES = new ResourceLocation[]{
PlayerContainer.EMPTY_ARMOR_SLOT_BOOTS, PlayerContainer.EMPTY_ARMOR_SLOT_LEGGINGS,
Expand Down Expand Up @@ -359,4 +365,48 @@ public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {

return itemstack;
}

@Nonnull
@Override
public List<RecipeBookCategories> getRecipeBookCategories() {
return Lists.newArrayList(RecipeBookCategories.SEARCH, RecipeBookCategories.EQUIPMENT,
RecipeBookCategories.BUILDING_BLOCKS, RecipeBookCategories.MISC,
RecipeBookCategories.REDSTONE);
}

@Override
public void fillStackedContents(@Nonnull RecipeItemHelper itemHelperIn) {
this.craftMatrix.fillStackedContents(itemHelperIn);
}

@Override
public void clear() {
this.craftMatrix.clear();
this.craftResult.clear();
}

@Override
public boolean matches(IRecipe<? super CraftingInventory> recipeIn) {
return recipeIn.matches(this.craftMatrix, this.player.world);
}

@Override
public int getOutputSlot() {
return 0;
}

@Override
public int getWidth() {
return this.craftMatrix.getWidth();
}

@Override
public int getHeight() {
return this.craftMatrix.getHeight();
}

@Override
public int getSize() {
return 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,26 @@

package top.theillusivec4.curios.integration.jei;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import mezz.jei.api.gui.handlers.IGuiContainerHandler;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.recipebook.RecipeBookGui;
import net.minecraft.client.gui.recipebook.RecipeTabToggleWidget;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import top.theillusivec4.curios.Curios;
import top.theillusivec4.curios.api.CuriosAPI;
import top.theillusivec4.curios.client.gui.CuriosScreen;

public class CuriosContainerHandler implements IGuiContainerHandler<CuriosScreen> {

private static final Field RECIPE_TABS = ObfuscationReflectionHelper
.findField(RecipeBookGui.class, "field_193018_j");

@Override
@Nonnull
public List<Rectangle2d> getGuiExtraAreas(CuriosScreen containerScreen) {
Expand All @@ -45,6 +53,27 @@ public List<Rectangle2d> getGuiExtraAreas(CuriosScreen containerScreen) {
int left = containerScreen.getGuiLeft() - width;
int top = containerScreen.getGuiTop() + 4;
areas.add(new Rectangle2d(left, top, width, height));
RecipeBookGui guiRecipeBook = containerScreen.getRecipeGui();

if (guiRecipeBook.isVisible()) {
int i = (containerScreen.width - 147) / 2 - (containerScreen.widthTooNarrow ? 0 : 86);
int j = (containerScreen.height - 166) / 2;
areas.add(new Rectangle2d(i, j, 147, 166));
try {
List<RecipeTabToggleWidget> tabs = (List<RecipeTabToggleWidget>) RECIPE_TABS
.get(guiRecipeBook);

for (RecipeTabToggleWidget tab : tabs) {

if (tab.visible) {
areas.add(new Rectangle2d(tab.x, tab.y, tab.getWidth(), tab.getHeight()));
}
}
} catch (IllegalAccessException e) {
Curios.LOGGER.error("Error accessing recipe tabs!");
}
return areas;
}
return areas;
}).orElse(Collections.emptyList());
} else {
Expand Down

0 comments on commit 0742ba5

Please sign in to comment.