Skip to content
Permalink
Browse files

Added a proper gui/container impl for the 3x3 inventory crafting grid.

…Fixes #79
  • Loading branch information
alcatrazEscapee committed Jun 15, 2019
1 parent b3f545b commit 0670b894e5bb436919615d5c5463fd18b8d9290e
@@ -121,6 +121,7 @@ public void preInit(FMLPreInitializationEvent event)
network.registerMessage(new PacketGuiButton.Handler(), PacketGuiButton.class, ++id, Side.SERVER);
network.registerMessage(new PacketPlaceBlockSpecial.Handler(), PacketPlaceBlockSpecial.class, ++id, Side.SERVER);
network.registerMessage(new PacketSwitchPlayerInventoryTab.Handler(), PacketSwitchPlayerInventoryTab.class, ++id, Side.SERVER);
network.registerMessage(new PacketOpenCraftingGui.Handler(), PacketOpenCraftingGui.class, ++id, Side.SERVER);
// Received on client
network.registerMessage(new PacketAnvilUpdate.Handler(), PacketAnvilUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketCrucibleUpdate.Handler(), PacketCrucibleUpdate.class, ++id, Side.CLIENT);
@@ -93,6 +93,8 @@ public Container getServerGuiElement(int ID, EntityPlayer player, World world, i
case SKILLS:
case NUTRITION:
return new ContainerSimple(player.inventory);
case CRAFTING:
return new ContainerInventoryCrafting(player.inventory, player.world);
default:
return null;
}
@@ -145,6 +147,8 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int
return new GuiNutrition(container, player.inventory);
case SKILLS:
return new GuiSkills(container, player.inventory);
case CRAFTING:
return new GuiInventoryCrafting(container);
default:
return null;
}
@@ -170,6 +174,7 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int
NUTRITION,
SKILLS,
INVENTORY, // This is special, it is used by GuiButtonPlayerInventoryTab to signal to open the vanilla inventory
CRAFTING, // In-inventory 3x3 crafting grid
NULL; // This is special, it is a non-null null.

private static Type[] values = values();
@@ -7,7 +7,6 @@

import org.lwjgl.input.Keyboard;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.fml.client.registry.ClientRegistry;
@@ -18,6 +17,7 @@
import net.minecraftforge.fml.relauncher.SideOnly;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.network.PacketOpenCraftingGui;
import net.dries007.tfc.network.PacketPlaceBlockSpecial;

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;
@@ -44,7 +44,7 @@ public static void onKeyEvent(InputEvent.KeyInputEvent event)
final Minecraft mc = Minecraft.getMinecraft();
if (OPEN_CRAFTING_TABLE.isPressed())
{
mc.displayGuiScreen(new GuiCrafting(mc.player.inventory, mc.world));
TerraFirmaCraft.getNetwork().sendToServer(new PacketOpenCraftingGui());
}
if (PLACE_BLOCK.isPressed())
{
@@ -0,0 +1,191 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.client.gui;

import java.io.IOException;
import javax.annotation.Nonnull;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiButtonImage;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.client.gui.recipebook.GuiRecipeBook;
import net.minecraft.client.gui.recipebook.IRecipeShownListener;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ResourceLocation;

/**
* This is copied from {@link GuiCrafting} since it needs to use a different container implementation
*/
public class GuiInventoryCrafting extends GuiContainer implements IRecipeShownListener
{
private static final ResourceLocation CRAFTING_TABLE_GUI_TEXTURES = new ResourceLocation("textures/gui/container/crafting_table.png");
private final GuiRecipeBook recipeBookGui;
private GuiButtonImage recipeButton;
private boolean widthTooNarrow;

public GuiInventoryCrafting(Container container)
{
super(container);
this.recipeBookGui = new GuiRecipeBook();
}

/**
* Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
* window resizes, the buttonList is cleared beforehand.
*/
public void initGui()
{
super.initGui();
this.widthTooNarrow = this.width < 379;
this.recipeBookGui.func_194303_a(this.width, this.height, this.mc, this.widthTooNarrow, ((ContainerWorkbench) this.inventorySlots).craftMatrix);
this.guiLeft = this.recipeBookGui.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
this.recipeButton = new GuiButtonImage(10, this.guiLeft + 5, this.height / 2 - 49, 20, 18, 0, 168, 19, CRAFTING_TABLE_GUI_TEXTURES);
this.buttonList.add(this.recipeButton);
}

/**
* Draws the screen and all the components in it.
*/
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
this.drawDefaultBackground();

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.drawScreen(mouseX, mouseY, partialTicks);
this.recipeBookGui.renderGhostRecipe(this.guiLeft, this.guiTop, true, partialTicks);
}

this.renderHoveredToolTip(mouseX, mouseY);
this.recipeBookGui.renderTooltip(this.guiLeft, this.guiTop, mouseX, mouseY);
}

/**
* Draw the foreground layer for the GuiContainer (everything in front of the items)
*/
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
{
this.fontRenderer.drawString(I18n.format("container.crafting"), 28, 6, 4210752);
this.fontRenderer.drawString(I18n.format("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
}

/**
* Draws the background layer of this container (behind the items).
*/
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
{
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(CRAFTING_TABLE_GUI_TEXTURES);
int i = this.guiLeft;
int j = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
}

/**
* Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
*/
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
{
if (!this.recipeBookGui.mouseClicked(mouseX, mouseY, mouseButton))
{
if (!this.widthTooNarrow || !this.recipeBookGui.isVisible())
{
super.mouseClicked(mouseX, mouseY, mouseButton);
}
}
}

protected boolean hasClickedOutside(int p_193983_1_, int p_193983_2_, int p_193983_3_, int p_193983_4_)
{
boolean flag = p_193983_1_ < p_193983_3_ || p_193983_2_ < p_193983_4_ || p_193983_1_ >= p_193983_3_ + this.xSize || p_193983_2_ >= p_193983_4_ + this.ySize;
return this.recipeBookGui.hasClickedOutside(p_193983_1_, p_193983_2_, this.guiLeft, this.guiTop, this.xSize, this.ySize) && flag;
}

/**
* Test if the 2D point is in a rectangle (relative to the GUI). Args : rectX, rectY, rectWidth, rectHeight, pointX,
* pointY
*/
protected boolean isPointInRegion(int rectX, int rectY, int rectWidth, int rectHeight, int pointX, int pointY)
{
return (!this.widthTooNarrow || !this.recipeBookGui.isVisible()) && super.isPointInRegion(rectX, rectY, rectWidth, rectHeight, pointX, pointY);
}

/**
* Called when the mouse is clicked over a slot or outside the gui.
*/
protected void handleMouseClick(Slot slotIn, int slotId, int mouseButton, @Nonnull ClickType type)
{
super.handleMouseClick(slotIn, slotId, mouseButton, type);
this.recipeBookGui.slotClicked(slotIn);
}

/**
* Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
* KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
*/
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
if (!this.recipeBookGui.keyPressed(typedChar, keyCode))
{
super.keyTyped(typedChar, keyCode);
}
}

/**
* Called when the screen is unloaded. Used to disable keyboard repeat events
*/
public void onGuiClosed()
{
this.recipeBookGui.removed();
super.onGuiClosed();
}

/**
* Called from the main game loop to update the screen.
*/
public void updateScreen()
{
super.updateScreen();
this.recipeBookGui.tick();
}

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

@Nonnull
public GuiRecipeBook func_194310_f()
{
return this.recipeBookGui;
}

/**
* Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
*/
protected void actionPerformed(GuiButton button)
{
if (button.id == 10)
{
this.recipeBookGui.initVisuals(this.widthTooNarrow, ((ContainerWorkbench) this.inventorySlots).craftMatrix);
this.recipeBookGui.toggleVisibility();
this.guiLeft = this.recipeBookGui.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
this.recipeButton.setPosition(this.guiLeft + 5, this.height / 2 - 49);
}
}
}
@@ -0,0 +1,40 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.network;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import io.netty.buffer.ByteBuf;
import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.client.TFCGuiHandler;

public class PacketOpenCraftingGui implements IMessage
{
@Override
public void fromBytes(ByteBuf buf) {}

@Override
public void toBytes(ByteBuf buf) {}

public static final class Handler implements IMessageHandler<PacketOpenCraftingGui, IMessage>
{
@Override
public IMessage onMessage(PacketOpenCraftingGui message, MessageContext ctx)
{
TerraFirmaCraft.getProxy().getThreadListener(ctx).addScheduledTask(() -> {
EntityPlayer player = TerraFirmaCraft.getProxy().getPlayer(ctx);
if (player != null)
{
TFCGuiHandler.openGui(player.world, player, TFCGuiHandler.Type.CRAFTING);
}
});
return null;
}
}
}
@@ -0,0 +1,29 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.objects.container;

import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

@ParametersAreNonnullByDefault
public class ContainerInventoryCrafting extends ContainerWorkbench
{
public ContainerInventoryCrafting(InventoryPlayer inventory, World world)
{
super(inventory, world, BlockPos.ORIGIN);
}

@Override
public boolean canInteractWith(EntityPlayer playerIn)
{
return true;
}
}

0 comments on commit 0670b89

Please sign in to comment.
You can’t perform that action at this time.