Created
May 20, 2015 16:08
-
-
Save anonymous/ca9162f4661f4a62575a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.ah26.futura.common; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.nbt.NBTTagList; | |
import net.minecraft.tileentity.TileEntity; | |
public class TileEntityXFurnace extends TileEntity | |
{ | |
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) | |
{ | |
if (this.contents[i] != null) | |
{ | |
NBTTagCompound nbttagcompound1 = new NBTTagCompound(); | |
nbttagcompound1.setByte("Slot", (byte)i); | |
this.contents[i].writeToNBT(nbttagcompound1); | |
nbttaglist.appendTag(nbttagcompound1); | |
} | |
} | |
compound.setTag("Items", nbttaglist); | |
compound.setShort("workingTime", (short)this.workingTime); | |
compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded); | |
} | |
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"); | |
} | |
public int getSizeInventory() | |
{ | |
return this.contents.length; | |
} | |
public ItemStack getStackInSlot(int slotIndex) | |
{ | |
return this.contents[slotIndex]; | |
} | |
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; | |
} | |
} | |
public ItemStack getStackInSlotOnClosing(int slotIndex) { | |
if (this.contents[slotIndex] != null) | |
{ | |
ItemStack itemstack = this.contents[slotIndex]; | |
this.contents[slotIndex] = null; | |
return itemstack; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
public void setInventorySlotContents(int slotIndex, ItemStack stack) { | |
this.contents[slotIndex] = stack; | |
if (stack != null && stack.stackSize > this.getInventoryStackLimit()) | |
{ | |
stack.stackSize = this.getInventoryStackLimit(); | |
} | |
this.markDirty(); | |
} | |
public String getInventoryName() { //J'ai décider qu'on ne pouvait pas mettre de nom custom | |
return "tile.XFurnace"; | |
} | |
public boolean hasCustomInventoryName() { | |
return false; | |
} | |
public int getInventoryStackLimit() { | |
return 64; | |
} | |
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; | |
} | |
public void openInventory() { | |
} | |
public void closeInventory() { | |
} | |
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) //Si les trois premiers slots sont vides | |
{ | |
return false; //On ne peut pas lancer le processus | |
} | |
else | |
{ | |
ItemStack itemstack = XFurnaceRecipes.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; //rapport avec les recettes | |
if (this.contents[3] == null) return true; //vérifications du slot d'output | |
if (!this.contents[3].isItemEqual(itemstack)) return false; //ici aussi | |
int result = contents[3].stackSize + itemstack.stackSize; | |
return result <= getInventoryStackLimit() && result <= this.contents[3].getMaxStackSize(); //Et là aussi décidément | |
} | |
} | |
public void updateEntity() //Méthode exécutée à chaque tick | |
{ | |
if(this.isBurning() && this.canSmelt()) //Si on "cuit" et que notre recette et toujours bonne, on continue | |
{ | |
++this.workingTime; //incrémentation | |
} | |
if(this.canSmelt() && !this.isBurning()) //Si la recette est bonne mais qu'elle n'est toujours pas lancée, on la lance | |
{ | |
this.workingTime = 1; //La méthode isBurning() renverra true maintenant (1>0) | |
} | |
if(this.canSmelt() && this.workingTime == this.workingTimeNeeded) //Si on est arrivé au bout du temps de cuisson et que la recette est toujours bonne | |
{ | |
this.smeltItem(); //on "cuit" les items | |
this.workingTime = 0; //et on réinitialise le temps de cuisson | |
} | |
if(!this.canSmelt()) //Si la recette la recette n'est plus bonne | |
{ | |
this.workingTime= 0; //le temps de cuisson est de 0 | |
} | |
} | |
public void smeltItem() | |
{ | |
if (this.canSmelt()) | |
{ | |
ItemStack itemstack = XFurnaceRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1], this.contents[2]}); //On récupère l'output de la recette | |
if (this.contents[3] == null) //Si il y a rien dans le slot d'output | |
{ | |
this.contents[3] = itemstack.copy(); //On met directement l'ItemStack | |
} | |
else if (this.contents[3].getItem() == itemstack.getItem()) //Et si l'item que l'on veut est le même que celui qu'il y a déjà | |
{ | |
this.contents[3].stackSize += itemstack.stackSize; // Alors ont incrémente l'ItemStack | |
} | |
--this.contents[0].stackSize; //On décrémente les slots d'input | |
--this.contents[1].stackSize; | |
--this.contents[2].stackSize; | |
if (this.contents[0].stackSize <= 0) //Si les slots sont vides, on remet à null le slot | |
{ | |
this.contents[0] = null; | |
} | |
if (this.contents[1].stackSize <= 0) | |
{ | |
this.contents[1] = null; | |
} | |
if (this.contents[2].stackSize <= 0) | |
{ | |
this.contents[2] = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment