Résolu Slots StackSize–
-
Yop!
J’ai fais mon generator mais quand le fuel est dans les slot il ne se delete pas pourtant j’ai bien mis ```java
if(this.slots[0] != null && this.getItemPower(this.slots[0]) > 0)
{
–this.slots[0].stackSize;
}Full class: ```java package tileEntity; import block.ElectricFurnace; import block.Generator; import block.GeneratorRecipe; import main.TAqmod2; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class TileEntityGenerator extends TileEntity implements ISidedInventory { public TileEntityGenerator() { } private static final int[] slots_fuel = new int[] {0}; private static final int[] slots_result = new int[] {1}; /** * The ItemStacks that hold the items currently being used in the furnace */ private ItemStack[] slots = new ItemStack[2]; public int processTime = 125; // 30 = 1 secondes /** power */ public int power = 0; /** Max Power */ public int maxPower = 5000; /** The number of ticks that the current item has been cooking for */ public int cookTime; public int burnTime; private String GeneratorName; public void GeneratorName(String string) { this.GeneratorName = string; } @Override public int getSizeInventory() { return this.slots.length; } @Override public ItemStack getStackInSlot(int par1) { return this.slots[par1]; } @Override public ItemStack decrStackSize(int par1, int par2) { if(this.slots[par1] != null) { ItemStack itemstack; if(this.slots[par1].stackSize <= par2) { itemstack = this.slots[par1]; this.slots[par1] = null; return itemstack; } else { itemstack = this.slots[par1].splitStack(par2); if(this.slots[par1].stackSize == 0) { this.slots[par1] = null; } return itemstack; } } else { return null; } } public boolean hasPower() { return this.power > 0; } @Override public ItemStack getStackInSlotOnClosing(int par1) { if(this.slots[par1] != null) { ItemStack itemstack = this.slots[par1]; this.slots[par1] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int par1, ItemStack itemStack) { this.slots[par1] = itemStack; if(itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) { itemStack.stackSize = this.getInventoryStackLimit(); } } @Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.GeneratorName : "container.generator"; } @Override public boolean hasCustomInventoryName() { return this.GeneratorName != null && this.GeneratorName.length() > 0; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer par1) { return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1.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 par1, ItemStack par2) { return par1 == 1 ? false : true; } @Override public int[] getAccessibleSlotsFromSide(int par1) { return par1 == 0 ? slots_fuel : slots_result; } @Override public boolean canInsertItem(int par1, ItemStack par2, int par3) { return this.isItemValidForSlot(par1, par2); } @Override public boolean canExtractItem(int par1, ItemStack par2, int par3) { return par3 != 0 || par1 != 1 || par2.getItem() == Items.bucket; } @Override public void readFromNBT(NBTTagCompound par1) { super.readFromNBT(par1); if(par1.hasKey("power")) { this.power = par1.getInteger("power"); } NBTTagList nbttaglist = par1.getTagList("Items", 10); this.slots = new ItemStack[this.getSizeInventory()]; for(int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbttacompound1 = (NBTTagCompound)nbttaglist.getCompoundTagAt(i); byte b0 = nbttacompound1.getByte("Slot"); if(b0 >= 0 && b0 < this.slots.length) { this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttacompound1); } } this.cookTime = par1.getShort("CookTime"); if(par1.hasKey("container.generator")) { this.GeneratorName = par1.getString("container.generator"); } } @Override public void writeToNBT(NBTTagCompound par1) { super.writeToNBT(par1); par1.setInteger("power", this.power); par1.setShort("CookTime", (short)this.cookTime); NBTTagList taglist = new NBTTagList(); for(int i = 0; i < this.slots.length; i++) { if(this.slots* != null) { NBTTagCompound tagcompound1 = new NBTTagCompound(); } } par1.setTag("Items", taglist); if(this.hasCustomInventoryName()) { par1.setString("container.generator", this.GeneratorName); } } public boolean isCharging() { return this.cookTime > 0; } public boolean isPowering() { return this.burnTime > 0; } /* * @SideOnly(Side.CLIENT) * public int getCookProgressScaled(int par1) * { * return this.cookTime * par1 / this.processTime; * } */ @SideOnly(Side.CLIENT) public int getPowerRemainingScaled(int par1) { return this.power * par1 / this.maxPower; } @Override public void updateEntity() { boolean flag = this.cookTime > 0; boolean flag1 = false; if(hasPower() && isCharging()) { this.power–; } if(!this.worldObj.isRemote) { if(this.power < this.maxPower)// && this.getItemPower(this.slots[0]) > 0) { this.burnTime += getItemPower(this.slots[0]); flag1 = true; if(isPowering()) { this.power++; this.burnTime–; System.out.println("Burn Time:" + this.burnTime + "Power:" + this.power); if(this.slots[0] != null && this.getItemPower(this.slots[0]) > 0) { –this.slots[0].stackSize; /* * if(this.slots[0].stackSize == 0) * { * this.slots[0] = slots[1].getItem().getContainerItem(slots[0]); * } */ /* * Useless parce que non machine. * else{ * if(this.slots[0].getItemDamage() < this.slots[0].getMaxDamage()){ * this.power += getItemPower(this.slots[1]); * this.slots[0] = new ItemStack(this.slots[0].getItem(), this.slots[0].stackSize, this.slots[0].getItemDamage() + 1); * } * if(this.slots[0].getItemDamage() == this.slots[0].getMaxDamage()); * this.slots[0].stackSize–; * } */ /** * Slots Chargement de Battery */ if(this.slots[1] != null) { if(this.slots[1].getItemDamage() <= this.slots[1].getMaxDamage() && hasPower()) { if(this.slots[1].getItemDamage() == this.slots[1].getMaxItemUseDuration()) { } else { this.power–; this.slots[1] = new ItemStack(this.slots[1].getItem(), this.slots[1].stackSize, this.slots[1].getItemDamage() - 1); } } } } if(this.canSmelt() && this.hasPower()) { this.cookTime++; if(this.cookTime == this.processTime) { this.cookTime = 0; flag1 = true; } } else { this.cookTime = 0; } } if(flag != cookTime > 0) { flag1 = true; ElectricFurnace.updateBlockState(this.cookTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord); } if(flag1) { this.markDirty(); } } } } private boolean canSmelt() { if(this.power > 0) { if(this.power == maxPower) { return false; } /* * if(itemstack == null) * return false; * if(this.slots[1] == null) * return true; * if(!this.slots[1].isItemEqual(itemstack)) * return false; */ // return slots[0].stackSize < itemstack.getMaxStackSize(); } return tileEntityInvalid; } protected static int getItemPower(ItemStack itemstack) { if(itemstack == null) { return 0; } else { Item item = itemstack.getItem(); if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) { Block block = Block.getBlockFromItem(item); if(block == Blocks.wooden_slab) { return 150; } if(block.getMaterial() == Material.wood) { return 300; } if(block == Blocks.coal_block) { return 16000; } } } return 0; } public static boolean isItemFuel(ItemStack itemstack) { return getItemPower(itemstack) > 0; } }
-
Il ne se delete pas ? L’item ne disparaît pas quand la stack size est à 0 ou la stack size ne diminue pas du tout ?
Si c’est le premier problème, il faut mettre :if(slots[0].stackSize <= 0) { this.slots[0] = null; }
-
Effectivement,Merci
Tain parfois je passe devant des chose vraiment stupide XD -
Ça arrive à tout le monde ^^
-
Bon je continue parce que je vais pas créer 2048 sujet par jour xD
package tileEntity; import scala.reflect.internal.Trees.This; import block.ElectricFurnace; import block.Generator; import block.GeneratorRecipe; import main.TAqmod2; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class TileEntityGenerator extends TileEntity implements ISidedInventory { public TileEntityGenerator() { } private static final int[] slots_fuel = new int[] {0}; private static final int[] slots_result = new int[] {1}; /** * The ItemStacks that hold the items currently being used in the furnace */ private ItemStack[] slots = new ItemStack[2]; public int processTime = 32000; // 30 = 1 secondes /** power */ public int power = 0; /** Max Power */ public int maxPower = 5000; /** The number of ticks that the current item has been cooking for */ public int cookTime; public int burnTime; private String GeneratorName; public void GeneratorName(String string) { this.GeneratorName = string; } @Override public int getSizeInventory() { return this.slots.length; } @Override public ItemStack getStackInSlot(int par1) { return this.slots[par1]; } @Override public ItemStack decrStackSize(int par1, int par2) { if(this.slots[par1] != null) { ItemStack itemstack; if(this.slots[par1].stackSize <= par2) { itemstack = this.slots[par1]; this.slots[par1] = null; return itemstack; } else { itemstack = this.slots[par1].splitStack(par2); if(this.slots[par1].stackSize == 0) { this.slots[par1] = null; } return itemstack; } } else { return null; } } public boolean hasPower() { return this.power > 0; } @Override public ItemStack getStackInSlotOnClosing(int par1) { if(this.slots[par1] != null) { ItemStack itemstack = this.slots[par1]; this.slots[par1] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int par1, ItemStack itemStack) { this.slots[par1] = itemStack; if(itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) { itemStack.stackSize = this.getInventoryStackLimit(); } } @Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.GeneratorName : "container.generator"; } @Override public boolean hasCustomInventoryName() { return this.GeneratorName != null && this.GeneratorName.length() > 0; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer par1) { return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1.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 par1, ItemStack par2) { return par1 == 1 ? false : true; } @Override public int[] getAccessibleSlotsFromSide(int par1) { return par1 == 0 ? slots_fuel : slots_result; } @Override public boolean canInsertItem(int par1, ItemStack par2, int par3) { return this.isItemValidForSlot(par1, par2); } @Override public boolean canExtractItem(int par1, ItemStack par2, int par3) { return par3 != 0 || par1 != 1 || par2.getItem() == Items.bucket; } @Override public void readFromNBT(NBTTagCompound par1) { super.readFromNBT(par1); if(par1.hasKey("power")) { this.power = par1.getInteger("power"); } NBTTagList nbttaglist = par1.getTagList("Items", 10); this.slots = new ItemStack[this.getSizeInventory()]; for(int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbttacompound1 = (NBTTagCompound)nbttaglist.getCompoundTagAt(i); byte b0 = nbttacompound1.getByte("Slot"); if(b0 >= 0 && b0 < this.slots.length) { this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttacompound1); } } this.cookTime = par1.getShort("CookTime"); if(par1.hasKey("container.generator")) { this.GeneratorName = par1.getString("container.generator"); } } @Override public void writeToNBT(NBTTagCompound par1) { super.writeToNBT(par1); par1.setInteger("power", this.power); par1.setShort("CookTime", (short)this.cookTime); NBTTagList taglist = new NBTTagList(); for(int i = 0; i < this.slots.length; i++) { if(this.slots* != null) { NBTTagCompound tagcompound1 = new NBTTagCompound(); } } par1.setTag("Items", taglist); if(this.hasCustomInventoryName()) { par1.setString("container.generator", this.GeneratorName); } } public boolean isCharging() { return this.cookTime > 0; } public boolean isPowering() { return this.burnTime > 0; } /* * @SideOnly(Side.CLIENT) * public int getCookProgressScaled(int par1) * { * return this.cookTime * par1 / this.processTime; * } */ @SideOnly(Side.CLIENT) public int getPowerRemainingScaled(int par1) { return this.power * par1 / this.maxPower; } @Override public void updateEntity() { boolean flag = this.cookTime > 0; boolean flag1 = false; if(hasPower() && isCharging()) { this.power–; } if(!this.worldObj.isRemote) { if(this.power < this.maxPower)// && this.getItemPower(this.slots[0]) > 0) { this.burnTime += getItemPower(this.slots[0]); flag1 = true; if(isPowering() && this.getItemPower(this.slots[0]) > 0) { this.power++; this.burnTime–; System.out.println("Burn Time:" + this.burnTime + "Power:" + this.power); } if(this.slots[0] != null && this.getItemPower(this.slots[0]) > 0) { –this.slots[0].stackSize; } if(slots[0].stackSize <= 0) { this.slots[0] = null; } /** * Slots Chargement de Battery */ if(this.slots[1] != null) { if(this.slots[1].getItemDamage() <= this.slots[1].getMaxDamage() && hasPower()) { if(this.slots[1].getItemDamage() == this.slots[1].getMaxItemUseDuration()) { } else { this.power–; this.slots[1] = new ItemStack(this.slots[1].getItem(), this.slots[1].stackSize, this.slots[1].getItemDamage() - 1); } } } } if(this.canSmelt() && this.hasPower()) { this.burnTime = this.getItemPower(this.slots[0]); if(this.burnTime == this.processTime) { this.burnTime = 0; flag1 = true; } } else { this.burnTime = 0; } if(flag != burnTime > 0) { flag1 = true; ElectricFurnace.updateBlockState(this.cookTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord); } } if(flag1) { this.markDirty(); } } private boolean canSmelt() { if(this.power > 0) { if(this.power == maxPower) { return false; } /* * if(itemstack == null) * return false; * if(this.slots[1] == null) * return true; * if(!this.slots[1].isItemEqual(itemstack)) * return false; */ // return slots[0].stackSize < itemstack.getMaxStackSize(); } return tileEntityInvalid; } protected static int getItemPower(ItemStack itemstack) { if(itemstack == null) { return 0; } else { Item item = itemstack.getItem(); if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) { Block block = Block.getBlockFromItem(item); if(block == Blocks.wooden_slab) { return 150; } if(block.getMaterial() == Material.wood) { return 300; } if(block == Blocks.coal_block) { return 16000; } } } return 0; } public static boolean isItemFuel(ItemStack itemstack) { return getItemPower(itemstack) > 0; } }
et ça Crash!
Crash Report:
http://pastebin.com/pgmxPbcX
J’ai changé tout mes cookTime par burnTime parce que j’avais pas besoin de cookTime dans un generator… -
if(slots[0].stackSize <= 0)
->
if(slots[0] != null && slots[0].stackSize <= 0) -
this.slot[0] est null d’où le NPE qui pointe sur cette ligne là (si je me fie au crash report l.301) :
if(slots[0].stackSize <= 0)Je te suggère donc de mettre cette condition :
if(slots[0].stackSize <= 0)
{
this.slots[0] = null;
}Dans la condition juste au-dessus, de manière à avoir ceci :
if(this.slots[0] != null && this.getItemPower(this.slots[0]) > 0) { –this.slots[0].stackSize; if(slots[0].stackSize <= 0) { this.slots[0] = null; } }
EDIT = Ouais non pas bonne idée, car on a pas besoin de savoir ceci this.getItemPower(this.slots[0]) > 0 pour mettre null slots[0]. Suis le message de robin au-dessus
-
Euh wat ```java
if(isPowering())
{
this.power++;
this.burnTime–;
System.out.println(“Burn Time:” + this.burnTime + “Power:” + this.power);
}il me sort burnTime = 299 Power = 2 Mais les valeurs se fixe alors qu'elles ne devraient pas... vu que isPowering() return burnTime > 0 Donc BurnTime devrait descendre alors que Power est censé monté...