Skip to content

Commit

Permalink
Created ShapedRecipes + IRecipe
Browse files Browse the repository at this point in the history
  • Loading branch information
ResaloliPT committed Oct 1, 2017
1 parent b98e6b8 commit d9372e8
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 10 deletions.
Expand Up @@ -2,12 +2,13 @@

import com.resaloli.eim.content.crafting.CraftingManagerDCT;
import com.resaloli.eim.content.crafting.DCTCrafting;
import com.resaloli.eim.interfaces.IRecipe;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import com.resaloli.eim.content.crafting.InventoryCraftingResult;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.network.play.server.SPacketSetSlot;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand All @@ -17,7 +18,7 @@ public class ContainerDCT extends Container
{
/** The crafting matrix inventory (5x5). */
private InventoryCrafting craftMatrix;
private InventoryCraftResult craftResult;
private InventoryCraftingResult craftResult;
private final World world;
/** Position of the workbench */
private final BlockPos pos;
Expand All @@ -29,7 +30,7 @@ public ContainerDCT(InventoryPlayer playerInventory, World worldIn, BlockPos pos
this.pos = posIn;
this.player = playerInventory.player;
this.craftMatrix = new DCTCrafting(this, 5, 5);
this.craftResult = new InventoryCraftResult();
this.craftResult = new InventoryCraftingResult();

this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 25, 123, 68));

Expand Down Expand Up @@ -65,9 +66,9 @@ public void onCraftMatrixChanged(IInventory inventoryIn)
{
EntityPlayerMP entityplayermp = (EntityPlayerMP)this.player;
ItemStack itemstack = ItemStack.EMPTY;
IRecipe irecipe = CraftingManagerDCT.findMatchingRecipe(this.craftMatrix, this.world);
IRecipe irecipe = CraftingManagerDCT.findMatchingRecipes(this.craftMatrix, this.world);

if (irecipe != null && (irecipe.isHidden() || !this.world.getGameRules().getBoolean("doLimitedCrafting") || entityplayermp.getRecipeBook().containsRecipe(irecipe)))
if (irecipe != null && (irecipe.isHidden() || !this.world.getGameRules().getBoolean("doLimitedCrafting") || entityplayermp.getRecipeBook().containsRecipe((net.minecraft.item.crafting.IRecipe) irecipe)))
{
this.craftResult.setRecipeUsed(irecipe);
itemstack = irecipe.getCraftingResult(this.craftMatrix);
Expand Down
@@ -1,8 +1,9 @@
package com.resaloli.eim.content.crafting;

import com.resaloli.eim.interfaces.IRecipe;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.*;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.RegistryNamespaced;
Expand All @@ -29,7 +30,7 @@ public static ItemStack findMatchingResult(InventoryCrafting p_82787_0_, World c
}

@Nullable
public static IRecipe findMatchingRecipe(InventoryCrafting craftMatrix, World worldIn) {
public static IRecipe findMatchingRecipes(InventoryCrafting craftMatrix, World worldIn) {
for (IRecipe irecipe : REGISTRY) {
if (irecipe.matches(craftMatrix, worldIn)) {
return irecipe;
Expand All @@ -56,7 +57,7 @@ public static NonNullList<ItemStack> getRemainingItems(InventoryCrafting p_18030
}

@Nullable
public static IRecipe getRecipe(ResourceLocation name) {
public static IRecipe getRecipes(ResourceLocation name) {
return REGISTRY.getObject(name);
}
}
@@ -0,0 +1,167 @@
package com.resaloli.eim.content.crafting;

import com.resaloli.eim.interfaces.IRecipe;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;

import javax.annotation.Nullable;

public class InventoryCraftingResult implements IInventory {
/** A list of one item containing the result of the crafting formula */
private final NonNullList<ItemStack> stackResult = NonNullList.<ItemStack>withSize(1, ItemStack.EMPTY);
private IRecipe recipeUsed;

/**
* Returns the number of slots in the inventory.
*/
public int getSizeInventory()
{
return 1;
}

public boolean isEmpty()
{
for (ItemStack itemstack : this.stackResult)
{
if (!itemstack.isEmpty())
{
return false;
}
}

return true;
}

/**
* Returns the stack in the given slot.
*/
public ItemStack getStackInSlot(int index)
{
return this.stackResult.get(0);
}

/**
* Get the name of this object. For players this returns their username
*/
public String getName()
{
return "Result";
}

/**
* Returns true if this thing is named
*/
public boolean hasCustomName()
{
return false;
}

/**
* Get the formatted ChatComponent that will be used for the sender's username in chat
*/
public ITextComponent getDisplayName()
{
return (ITextComponent)(this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName(), new Object[0]));
}

/**
* Removes up to a specified number of items from an inventory slot and returns them in a new stack.
*/
public ItemStack decrStackSize(int index, int count)
{
return ItemStackHelper.getAndRemove(this.stackResult, 0);
}

/**
* Removes a stack from the given slot and returns it.
*/
public ItemStack removeStackFromSlot(int index)
{
return ItemStackHelper.getAndRemove(this.stackResult, 0);
}

/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
public void setInventorySlotContents(int index, ItemStack stack)
{
this.stackResult.set(0, stack);
}

/**
* Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
*/
public int getInventoryStackLimit()
{
return 64;
}

/**
* For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
* hasn't changed and skip it.
*/
public void markDirty()
{
}

/**
* Don't rename this method to canInteractWith due to conflicts with Container
*/
public boolean isUsableByPlayer(EntityPlayer player)
{
return true;
}

public void openInventory(EntityPlayer player)
{
}

public void closeInventory(EntityPlayer player)
{
}

/**
* Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot. For
* guis use Slot.isItemValid
*/
public boolean isItemValidForSlot(int index, ItemStack stack)
{
return true;
}

public int getField(int id)
{
return 0;
}

public void setField(int id, int value)
{
}

public int getFieldCount()
{
return 0;
}

public void clear()
{
this.stackResult.clear();
}

public void setRecipeUsed(@Nullable IRecipe p_193056_1_)
{
this.recipeUsed = p_193056_1_;
}

@Nullable
public IRecipe getRecipeUsed()
{
return this.recipeUsed;
}
}

0 comments on commit d9372e8

Please sign in to comment.