BlockFourRecipes :
package Package1.common.FourTotal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class BlockFourRecipes {
private static final BlockFourRecipes smeltingBase = new BlockFourRecipes();
private Map smeltingList = new HashMap();
public BlockFourRecipes()
{
this.addRecipe(Items.apple, Items.apple, Items.arrow, new ItemStack(Blocks.diamond_block));
}
public void addRecipe(ItemStack stack1, ItemStack stack2, ItemStack stack3, ItemStack stack4)
{
ItemStack[] stackList = new ItemStack[]{stack1, stack2, stack3};
this.smeltingList.put(stackList, stack4);
}
public void addRecipe(Item item1, Item item2, Item item3, ItemStack stack) //1er cas
{
this.addRecipe(new ItemStack(item1), new ItemStack(item2), new ItemStack(item3), stack);
}
public void addRecipe(Block block1, Item item2, Item item3, ItemStack stack) //2nd cas
{
this.addRecipe(Item.getItemFromBlock(block1), item2, item3, stack);
}
public void addRecipe(Block block1, Block block2, Item item3, ItemStack stack) //3ème cas
{
this.addRecipe(Item.getItemFromBlock(block1), Item.getItemFromBlock(block2), item3, stack);
}
public void addRecipe(Block block1, Block block2, Block block3, ItemStack stack) //4ème cas
{
this.addRecipe(Item.getItemFromBlock(block1), Item.getItemFromBlock(block2), Item.getItemFromBlock(block3), stack);
}
public ItemStack getSmeltingResult(ItemStack[] stack)
{
Iterator iterator = this.smeltingList.entrySet().iterator();
Entry entry;
do
{
if (!iterator.hasNext())
{
return null;
}
entry = (Entry)iterator.next();
}
while (!this.isSameKey(stack, (ItemStack[])entry.getKey()));
return (ItemStack)entry.getValue();
}
private boolean isSameKey(ItemStack[] stackList, ItemStack[] stackList2)
{
boolean isSame = false;
for(int i=0; i<=2; i++)
{
if(stackList*.getItem() == stackList2*.getItem())
{
isSame = true;
}
else
{
return false;
}
}
return isSame;
}
public Map getSmeltingList()
{
return this.smeltingList;
}
public static BlockFourRecipes smelting()
{
return smeltingBase;
}
}
TileEntityFour:
package Package1.common.FourTotal;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
public class TileEntityFour extends TileEntity implements IInventory{
private ItemStack[] contents = new ItemStack[4];
private int workingTime = 0;
private int workingTimeNeeded = 200;
@Override
public void writeToNBT(NBTTagCompound compound)
{
super.writeToNBT(compound);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.contents.length; ++i) //pour les slots
{
if (this.contents* != null)
{
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte)i);
this.contents*.writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
compound.setTag("Items", nbttaglist);
compound.setShort("workingTime",(short)this.workingTime); //On les enregistrent en short
compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded);
}
@Override
public void readFromNBT(NBTTagCompound compound)
{
super.readFromNBT(compound);
NBTTagList nbttaglist = compound.getTagList("Items", 10);
this.contents = new ItemStack[this.getSizeInventory()];
for (int i = 0; i < nbttaglist.tagCount(); ++i) //Encore une fois pour les slots
{
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("Slot") & 255;
if (j >= 0 && j < this.contents.length)
{
this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
this.workingTime = compound.getShort("workingTime"); //On lit nos valeurs
this.workingTimeNeeded = compound.getShort("workingTimeNeeded");
}
@Override
public int getSizeInventory() {
return this.contents.length;
}
@Override
public ItemStack getStackInSlot(int slotIndex) {
return this.contents[slotIndex];
}
@Override
public ItemStack decrStackSize(int slotIndex, int amount) {
if (this.contents[slotIndex] != null)
{
ItemStack itemstack;
if (this.contents[slotIndex].stackSize <= amount)
{
itemstack = this.contents[slotIndex];
this.contents[slotIndex] = null;
this.markDirty();
return itemstack;
}
else
{
itemstack = this.contents[slotIndex].splitStack(amount);
if (this.contents[slotIndex].stackSize == 0)
{
this.contents[slotIndex] = null;
}
this.markDirty();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int slotIndex) {
if (this.contents[slotIndex] != null)
{
ItemStack itemstack = this.contents[slotIndex];
this.contents[slotIndex] = null;
return itemstack;
}
else
{
return null;
}
}
@Override
public void setInventorySlotContents(int slotIndex, ItemStack stack) {
this.contents[slotIndex] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit())
{
stack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
@Override
public String getInventoryName() {
return "tile.BlockFour";
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return slot == 3 ? false : true;
}
public boolean isBurning()
{
return this.workingTime > 0;
}
private boolean canSmelt()
{
if (this.contents[0] == null || this.contents[1] == null || this.contents[2] == null)
{
return true;
}
else
{
ItemStack itemstack = BlockFourRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1], this.contents[2]}); //Il y a une erreur ici, c'est normal, on y vient après (c'est pour les recettes)
if (itemstack == null) return false;
if (this.contents[3] == null) return true;
if (!this.contents[3].isItemEqual(itemstack)) return false;
int result = contents[3].stackSize + itemstack.stackSize;
return result <= getInventoryStackLimit() && result <= this.contents[3].getMaxStackSize();
}
}
public void updateEntity()
{
if(this.isBurning() && this.canSmelt())
{
++this.workingTime;
}
if(this.canSmelt() && !this.isBurning())
{
this.workingTime = 1;
}
if(this.canSmelt() && this.workingTime == this.workingTimeNeeded)
{
this.smeltItem();
this.workingTime = 0;
}
if(!this.canSmelt())
{
this.workingTime= 0;
}
}
public void smeltItem()
{
if (this.canSmelt())
{
ItemStack itemstack = BlockFourRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1], this.contents[2]});
if (this.contents[3] == null)
{
this.contents[3] = itemstack.copy();
}
else if (this.contents[3].getItem() == itemstack.getItem())
{
this.contents[3].stackSize += itemstack.stackSize;
}
–this.contents[0].stackSize;
–this.contents[1].stackSize;
–this.contents[2].stackSize;
if (this.contents[0].stackSize <= 0)
{
this.contents[0] = null;
}
if (this.contents[1].stackSize <= 0)
{
this.contents[1] = null;
}
if (this.contents[2].stackSize <= 0)
{
this.contents[2] = null;
}
}
}
@SideOnly(Side.CLIENT)
public int getCookProgress()
{
return this.workingTime * 41 / this.workingTimeNeeded; //41 correspond à la hauteur de la barre de progression car notre barre de progression se déroule de haut en bas
}
}