Jump to content

How to spend my coal in campfire without having items?


MikeDark

Recommended Posts

I'm doing a mod called Scoutcraft, and I am finishing my campfire, but is missing just one detail that I am not able to do, do spend the fuel without having items to be processed, please help me ...

 

My TileEntity:

 

package mike.scoutcraft.tileentity;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import mike.scoutcraft.blocks.Fogueira;
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.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityFogueira extends TileEntity implements ISidedInventory {

private String localizeName;

private static final int[] slots_top = new int[] {0,};
private static final int[] slots_bottom = new int[] {2, 1};
private static final int[] slots_sides = new int[] {1};


private ItemStack[] slots = new ItemStack[3];


public int fogueiraSpeed = 300;	
public int power;	
public int maxPower = 51200;	
public int cookTime;

@SideOnly(Side.CLIENT)
public int getFogueiraProgressScaled(int i) {
	return this.cookTime * i / this.fogueiraSpeed;
}

@SideOnly(Side.CLIENT)
public int getpowerRemainingScaled(int i) {		
	return this.power * i / this.maxPower;
}


public int getSizeInventory(){
	return this.slots.length;
}


public String getInventoryName(){
	return this.hasCustomInventoryName() ? this.localizeName : "container.Fogueira";
}

public boolean hasCustomInventoryName(){
	return this.localizeName != null && this.localizeName.length() > 0;
}


public void setGuiDisplayName(String displayName) {
	this.localizeName = displayName;
}


@Override
public void closeInventory() {

}


@Override
public ItemStack decrStackSize(int i, int j) {
	if(this.slots[i] != null){
		ItemStack itemstack;

		if(this.slots[i].stackSize <= j){
			itemstack = this.slots[i];				
			this.slots[i] = null;				
			return itemstack;
			}else{
				itemstack = this.slots[i].splitStack(j);

				if(this.slots[i].stackSize == 0){
					this.slots[i] = null;
				}

				return itemstack;
			}

		}	

		return null;			
	}


@Override
public int getInventoryStackLimit() {
	return 64;
}


@Override
public ItemStack getStackInSlot(int i) {
	return this.slots[i];
}


@Override
public ItemStack getStackInSlotOnClosing(int i) {
	if(this.slots[i] != null){
		ItemStack itemstack = this.slots[i];
		this.slots[i] = null;
		return itemstack;
	}

	return null;
}

public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);
    	NBTTagList nbttaglist = nbt.getTagList("Items", 10);
    	this.slots = new ItemStack[this.getSizeInventory()];

    	for (int i = 0; i < nbttaglist.tagCount(); ++i)
    	{
        		NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.getCompoundTagAt(i);
        		byte b0 = nbttagcompound1.getByte("Slot");

        		if (b0 >= 0 && b0 < this.slots.length)
        		{
            		this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
        		}
    	}

    	this.power = nbt.getShort("power");
    	this.cookTime = nbt.getShort("CookTime");

    	if (nbt.hasKey("CustomName"))
    	{
        	this.localizeName = nbt.getString("CustomName");
    	}
}

public void writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);
    	nbt.setShort("power", (short)this.power);
    	nbt.setShort("CookTime", (short)this.cookTime);
    	NBTTagList nbttaglist = new NBTTagList();

    	for (int i = 0; i < this.slots.length; ++i)
    	{	
        		if (this.slots[i] != null)
        		{
            		NBTTagCompound nbttagcompound1 = new NBTTagCompound();
            		nbttagcompound1.setByte("Slot", (byte)i);
            		this.slots[i].writeToNBT(nbttagcompound1);
            		nbttaglist.appendTag(nbttagcompound1);
        		}
    	}

    	nbt.setTag("Items", nbttaglist);

    	if (this.hasCustomInventoryName())
    	{
        		nbt.setString("CustomName", this.localizeName);
    	}
}


public boolean hasPower(){
	return this.power > 0;
}

public boolean isFogueira(){
	return this.cookTime > 0;
}

public void updateEntity(){
	boolean flag = this.hasPower();
	boolean flag1= false;

	if(hasPower() && this.isFogueira()) {
		this.power--;
	}

	if(!worldObj.isRemote) {
		if (this.hasItemPower(this.slots[1]) && this.power < (this.maxPower - this.getItempower(this.slots[1]))) {
			this.power += getItempower(this.slots[1]);

			if(this.slots[1] != null) {
				flag1 = true;

				this.slots[1].stackSize--;

				if(this.slots[1].stackSize == 0) {
					this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
				}
			}
		}

		if (hasPower() && canSmelt()) {
			cookTime++;

			if (this.cookTime == this.fogueiraSpeed) {
				this.cookTime = 0;
				this.smeltItem();
				flag1 = true;
			}
		}else{
			cookTime = 0;
		}

		if (flag != this.isFogueira()) {
			flag1 = true;
			Fogueira.updateFogueiraBlockState(this.isFogueira(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}
		if(this.cookTime > 0){
			flag1 = true;
			Fogueira.updateFogueiraBlockState(this.isFogueira(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}else{
			flag1 = true;
			Fogueira.updateFogueiraBlockState(this.isFogueira(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}
	}

	if (flag1) {
		this.markDirty();
	}
    }

private boolean canSmelt(){

	if (slots[0] == null) {
		return false;
	}

	ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);

	if (itemstack == null) {
		return false;
	}

	if (slots[2] == null) {
		return true;
	}

	if (!slots[2].isItemEqual(itemstack)) {
		return false;
	}

	if (slots[2].stackSize < getInventoryStackLimit() && slots[2].stackSize < slots[2].getMaxStackSize()) {
		return true;
	}else{
		return slots[2].stackSize < itemstack.getMaxStackSize();
	}
}

public void smeltItem(){
	if(this.canSmelt()){
		ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);

		if(this.slots[2] == null){
			this.slots[2] = itemstack.copy();
		}else if(this.slots[2].isItemEqual(itemstack)){
			this.slots[2].stackSize += itemstack.stackSize;
		}

		this.slots[0].stackSize--;

		if(this.slots[0].stackSize <= 0){	
			this.slots[0] = null;
		}
	}
}


public 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.sapling) return 100;
				if(block == Blocks.coal_block) return 14400;
				if(block == Blocks.log) return 200;
				if(block == Blocks.log2) return 200;
				if(block == Blocks.planks) return 50;
				if(block == Blocks.wooden_slab)return 150;
				if(block == Blocks.wooden_pressure_plate)return 150;
				if(block == Blocks.wooden_door)return 150;
				if(block == Blocks.wooden_button)return 150;

		}

		if(item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD")) return 200;
		if(item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD")) return 200;
		if(item instanceof ItemHoe && ((ItemHoe) item).getToolMaterialName().equals("WOOD")) return 200;
		if(itemstack.getItem() == Items.coal) return 1600;
		if(item == Items.stick) return 100;
		if(item == Items.lava_bucket) return 20000;
		if(item == Items.blaze_rod) return 2400;

		return GameRegistry.getFuelValue(itemstack);
	}
}


public static boolean hasItemPower(ItemStack itemstack){
	return getItempower(itemstack) > 0;
}


@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return i == 2 ? false : (i == 1 ? hasItemPower(itemstack) : true);
}


@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
	return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityplayer.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 setInventorySlotContents(int i, ItemStack itemstack) {
	this.slots[i] = itemstack;

	if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
		itemstack.stackSize = this.getInventoryStackLimit();
	}
}


@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j) {
	return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
}


@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j) {
	return this.isItemValidForSlot(i, itemstack);
}


@Override
public int[] getAccessibleSlotsFromSide(int arg0) {
	return arg0 == 0 ? slots_bottom : (arg0 == 1 ? slots_top :  slots_sides);
}

}

 

Link to comment
Share on other sites

I tried some things but none worked, could you +/- tell  me about what to add or remove?

 

My updateEntity:

 

public void updateEntity(){
	boolean flag = this.hasPower();
	boolean flag1= false;

	if(hasPower() && this.isFogueira()) {
		this.power--;

	}

	if(!worldObj.isRemote) {
		if (this.hasItemPower(this.slots[1]) && this.power < (this.maxPower - this.getItempower(this.slots[1]))) {
			this.power += getItempower(this.slots[1]);

			if(this.slots[1] != null) {
				flag1 = true;

				this.slots[1].stackSize--;

				if(this.slots[1].stackSize == 0) {
					this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
				}
			}
		}

		if (hasPower() && canSmelt()) {
			cookTime++;

			if (this.cookTime == this.fogueiraSpeed) {
				this.cookTime = 0;
				this.smeltItem();
				flag1 = true;
			}
		}else{
			cookTime = 0;
		}

		if (flag != this.isFogueira()) {
			flag1 = true;
			Fogueira.updateFogueiraBlockState(this.isFogueira(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}
		if(this.cookTime > 0){
			flag1 = true;
			Fogueira.updateFogueiraBlockState(this.isFogueira(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}else{
			flag1 = true;
			Fogueira.updateFogueiraBlockState(this.isFogueira(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}
	}

	if (flag1) {
		this.markDirty();

	}
    }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.