Skip to content

Commit

Permalink
Started working on knapping infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
alcatrazEscapee committed Oct 7, 2018
1 parent 6def05a commit 1288272
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 13 deletions.
Expand Up @@ -26,6 +26,7 @@ public class TFCRegistries
public static final IForgeRegistry<Metal> METALS = GameRegistry.findRegistry(Metal.class);

public static final IForgeRegistry<AlloyRecipe> ALLOYS = GameRegistry.findRegistry(AlloyRecipe.class);
public static final IForgeRegistry<KnappingRecipe> KNAPPING = GameRegistry.findRegistry(KnappingRecipe.class);

static
{
Expand Down
Expand Up @@ -12,7 +12,7 @@
/**
* The names are separate from the instances TFCRegistries so they can be used without loading the class prematurely.
*/
public class TFCRegistryNames
public final class TFCRegistryNames
{
public static final ResourceLocation ROCK_TYPE = new ResourceLocation(MOD_ID, "rock_type");
public static final ResourceLocation ROCK = new ResourceLocation(MOD_ID, "rock");
Expand All @@ -21,4 +21,5 @@ public class TFCRegistryNames
public static final ResourceLocation METAL = new ResourceLocation(MOD_ID, "metal");

public static final ResourceLocation ALLOY_RECIPE = new ResourceLocation(MOD_ID, "alloy_recipe");
public static final ResourceLocation KNAPPING_RECIPE = new ResourceLocation(MOD_ID, "knapping_recipe");
}
73 changes: 73 additions & 0 deletions src/main/java/net/dries007/tfc/api/types/KnappingRecipe.java
@@ -0,0 +1,73 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.api.types;

import net.minecraft.item.ItemStack;
import net.minecraftforge.registries.IForgeRegistryEntry;

public class KnappingRecipe extends IForgeRegistryEntry.Impl<KnappingRecipe>
{
private final int width;
private final int height;
private final boolean[] pattern;
private final ItemStack output;

public KnappingRecipe(ItemStack output, String... pattern)
{
if (pattern.length == 0 || pattern.length > 5)
throw new IllegalArgumentException("Pattern length is invalid");

this.width = pattern[0].length();
this.height = pattern.length;
this.output = output;
this.pattern = new boolean[width * height];

for (int i = 0; i < height; i++)
{
String line = pattern[i];
if (line.length() != width)
throw new IllegalArgumentException("Line " + i + " in the pattern has the incorrect length");
for (int c = 0; c < width; c++)
this.pattern[i * height + c] = (line.charAt(c) == ' ');
}
}

// This will check if it matches a 5x5 boolean matrix, from a GuiContainerKnapping
public boolean matches(boolean[] matrix)
{
// Check all possible shifted positions
for (int xShift = 0; xShift <= 5 - width; xShift++)
{
for (int yShift = 0; yShift <= 5 - height; yShift++)
{
boolean flag = true;
// check if the matrix matches this orientation
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
// Check the individual position
int matrixIdx = (yShift + y) * 5 + xShift + x;
int patternIdx = (yShift + y) * width + xShift + x;
if (matrix[matrixIdx] != pattern[patternIdx])
{
flag = false;
break;
}
}
}
if (flag)
return true;
}
}
return false;
}

public ItemStack getOutput()
{
return output.copy();
}
}
12 changes: 8 additions & 4 deletions src/main/java/net/dries007/tfc/client/TFCGuiHandler.java
Expand Up @@ -17,16 +17,15 @@
import net.minecraftforge.fml.common.network.IGuiHandler;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.client.gui.GuiContainerKnapping;
import net.dries007.tfc.client.gui.GuiContainerTFC;
import net.dries007.tfc.client.gui.GuiLiquidTransfer;
import net.dries007.tfc.objects.blocks.BlocksTFC;
import net.dries007.tfc.objects.container.ContainerFirePit;
import net.dries007.tfc.objects.container.ContainerLiquidTransfer;
import net.dries007.tfc.objects.container.ContainerLogPile;
import net.dries007.tfc.objects.container.ContainerSmallVessel;
import net.dries007.tfc.objects.container.*;
import net.dries007.tfc.objects.items.ItemsTFC;
import net.dries007.tfc.objects.items.ceramics.ItemMold;
import net.dries007.tfc.objects.items.ceramics.ItemSmallVessel;
import net.dries007.tfc.objects.items.rock.ItemRock;
import net.dries007.tfc.objects.te.TEFirePit;
import net.dries007.tfc.objects.te.TELogPile;
import net.dries007.tfc.util.Helpers;
Expand Down Expand Up @@ -70,6 +69,8 @@ public Container getServerGuiElement(int ID, EntityPlayer player, World world, i
case FIRE_PIT:
TEFirePit teFirePit = Helpers.getTE(world, pos, TEFirePit.class);
return new ContainerFirePit(player.inventory, teFirePit);
case KNAPPING:
return new ContainerKnapping(player.inventory, stack.getItem() instanceof ItemRock ? stack : player.getHeldItemOffhand());
default:
return null;
}
Expand All @@ -94,6 +95,8 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int
return new GuiLiquidTransfer(container, player, "", player.getHeldItemMainhand().getItem() instanceof ItemMold);
case FIRE_PIT:
return new GuiContainerTFC(container, player.inventory, FIRE_PIT_BACKGROUND, BlocksTFC.FIREPIT.getTranslationKey());
case KNAPPING:
return new GuiContainerKnapping(container, player);
default:
return null;
}
Expand All @@ -106,6 +109,7 @@ public enum Type
SMALL_VESSEL_LIQUID,
MOLD,
FIRE_PIT,
KNAPPING,
NULL;

private static Type[] values = values();
Expand Down
@@ -0,0 +1,59 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.client.button;

import javax.annotation.Nonnull;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.dries007.tfc.api.types.Rock;

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;

@SideOnly(Side.CLIENT)
public class GuiButtonKnapping extends GuiButton
{
private final ResourceLocation BG_TEXTURE;

public GuiButtonKnapping(int id, int x, int y, int width, int height, Rock rockType)
{
super(id, x, y, width, height, "");

BG_TEXTURE = new ResourceLocation(MOD_ID, "textures/blocks/stonetypes/raw/" + rockType.getRegistryName().getPath() + ".png");
}

public void onClick()
{
this.visible = false;
}

@Override
public void drawButton(@Nonnull Minecraft mc, int mouseX, int mouseY, float partialTicks)
{
if (this.visible)
{
GlStateManager.color(1, 1, 1, 1);
mc.getTextureManager().bindTexture(BG_TEXTURE);

hovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height;

//int i = this.getHoverState(this.hovered);
//GlStateManager.enableBlend();
//GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
//GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);

drawTexturedModalRect(x, y, 0, 0, width, height);
//drawTexturedModalRect(this.x, this.y, 0, 46 + i * 20, this.width / 2, this.height);
//drawTexturedModalRect(this.x + this.width / 2, this.y, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);
mouseDragged(mc, mouseX, mouseY);
}
}
}
103 changes: 103 additions & 0 deletions src/main/java/net/dries007/tfc/client/gui/GuiContainerKnapping.java
@@ -0,0 +1,103 @@
/*
* 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 net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.types.Rock;
import net.dries007.tfc.api.util.TFCConstants;
import net.dries007.tfc.client.button.GuiButtonKnapping;
import net.dries007.tfc.objects.items.ItemsTFC;
import net.dries007.tfc.objects.items.rock.ItemRock;
import net.dries007.tfc.util.Helpers;

public class GuiContainerKnapping extends GuiContainerTFC
{
private static final ResourceLocation BG_TEXTURE = new ResourceLocation(TFCConstants.MOD_ID, "textures/gui/knapping.png");
private final int slotIdx;
private final Rock rock;

private final boolean[] matrix; // true = clicked away
private boolean hasBeenModified;

public GuiContainerKnapping(Container container, EntityPlayer player)
{
super(container, player.inventory, BG_TEXTURE, "");
ItemStack stack = player.getHeldItemMainhand();
if (stack.getItem() instanceof ItemRock)
{
slotIdx = playerInv.currentItem;
}
else
{
stack = player.getHeldItemOffhand();
slotIdx = 40;
}
rock = ((ItemRock) stack.getItem()).ore;
matrix = new boolean[25];
hasBeenModified = false;

ySize = 184; // Bigger than normal gui
}

@Override
public void initGui()
{
super.initGui();
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
int bx = (width - xSize) / 2 + 12 + 16 * x;
int by = (height - ySize) / 2 + 12 + 16 * y;
addButton(new GuiButtonKnapping(x + 5 * y, bx, by, 16, 16, rock));
}
}
}

@Override
protected void actionPerformed(GuiButton button)
{
// do something with the buttons
// todo: this needs to all happen on serverside: look up AbstractPacker or some gib
TerraFirmaCraft.getLog().debug("Pressed button of id: {}", button.id);
if (button instanceof GuiButtonKnapping)
{
((GuiButtonKnapping) button).onClick();
matrix[button.id] = true;

if (!hasBeenModified)
{
ItemStack stack = playerInv.getStackInSlot(slotIdx);
playerInv.setInventorySlotContents(slotIdx, Helpers.consumeItem(stack, 1));
hasBeenModified = true;
}

// check the pattern
Slot slot = inventorySlots.getSlot(0);
if (matches())
{
slot.putStack(new ItemStack(ItemsTFC.GOLDPAN)); // todo: make based on recipe
}
else
{
slot.putStack(ItemStack.EMPTY);
}
}
}

private boolean matches()
{
// todo: replace with an actual recipe type deal
return matrix[0] && !matrix[1];
}
}
Expand Up @@ -7,7 +7,6 @@

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -51,8 +50,10 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i

protected void drawSimpleForeground()
{
String name = I18n.format(translationKey + ".name");
fontRenderer.drawString(name, xSize / 2 - fontRenderer.getStringWidth(name) / 2, 6, 0x404040);
// in some guis this causes collisions: graphics need to be redesigned.
// 1.7 never had 'inventory' or the gui name printed here, so we don't either, until further notice
//String name = I18n.format(translationKey + ".name");
//fontRenderer.drawString(name, xSize / 2 - fontRenderer.getStringWidth(name) / 2, 6, 0x404040);
//fontRenderer.drawString(playerInv.getDisplayName().getUnformattedText(), 8, ySize - 94, 0x404040);
}

Expand Down
Expand Up @@ -17,8 +17,6 @@
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

import net.dries007.tfc.TerraFirmaCraft;

public abstract class ContainerItemStack extends Container
{
protected final ItemStack stack;
Expand Down Expand Up @@ -102,7 +100,6 @@ public ItemStack transferStackInSlot(EntityPlayer player, int index)
@Nonnull
public ItemStack slotClick(int slotID, int dragType, ClickType clickType, EntityPlayer player)
{
TerraFirmaCraft.getLog().debug("Clicked on slot " + slotID);
if ((clickType == ClickType.QUICK_MOVE || clickType == ClickType.PICKUP || clickType == ClickType.SWAP) && slotID == itemIndex)
{
return ItemStack.EMPTY;
Expand Down
@@ -0,0 +1,52 @@
/*
* 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 net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import net.dries007.tfc.objects.inventory.SlotOutput;

public class ContainerKnapping extends ContainerItemStack
{
public ContainerKnapping(InventoryPlayer playerInv, ItemStack stack)
{
super(playerInv, stack);
this.itemIndex += 1;
}

@Override
protected void addContainerSlots()
{
// todo: this needs to not come from an item capability; the item in question doesn't have that capability
IItemHandler inventory = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (inventory != null)
{
addSlotToContainer(new SlotOutput(inventory, 0, 128, 44));
}
}

@Override
protected void addPlayerInventorySlots(InventoryPlayer playerInv)
{
// Add Player Inventory Slots (lower down)
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 9; j++)
{
addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18 + 18));
}
}

for (int k = 0; k < 9; k++)
{
addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142 + 18));
}
}
}

0 comments on commit 1288272

Please sign in to comment.