Jump to content

[1.6.4]Some problems with a custom "furnace"


Joe_bob

Recommended Posts

Hello. Total newbie to modding minecraft here, so just try to help me with any boneheaded mistakes I've made. I have a custom gui all set up for a block, complete with TileEntity and all that jazz, but the progress bar(s) are acting wonky. There is a bar for how "full" the block is in addition to the normal progress bar. The normal progress bar works normally, except it moves extremely slow and is delayed by a large amount of time. I looked in the the vanilla furnace to see what might be wrong, but everything SEEMS to be set up properly. Also, the "fullness" bar never updates and stays at the bottom no matter what.

 

GuiCrucible.java:

 

package joebob.joecraft.common;

import org.lwjgl.opengl.GL11;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
import net.minecraft.client.resources.I18n;

public class GuiCrucible extends GuiContainer {

public static final ResourceLocation guiTexture = new ResourceLocation("joecraft", "textures/gui/crucibleGUI.png");

public TileEntityCrucible crucible;

public GuiCrucible(InventoryPlayer inventoryPlayer, TileEntityCrucible entity) {
	super(new ContainerCrucible(inventoryPlayer, entity));

	this.crucible = entity;

	this.xSize = 176;
	this.ySize = 166;
}

public void drawGuiContainerForegroundLayer(int par1, int par2){
	String name = this.crucible.isInvNameLocalized() ? this.crucible.getInvName() : I18n.getString(this.crucible.getInvName());

	this.fontRenderer.drawString(name, this.xSize / 2 - this.fontRenderer.getStringWidth(name) / 2, 6, 421075);
	this.fontRenderer.drawString(I18n.getString("container.inventory"), 8, this.ySize - 96, 4210752);
}

public void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	GL11.glColor4f(1F, 1F, 1F, 1F);


	Minecraft.getMinecraft().getTextureManager().bindTexture(guiTexture);

	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);


	if(this.crucible.isBurning()){
		int k = this.crucible.getBurnTimeRemainingScaled(12);
		drawTexturedModalRect(guiLeft+26, guiTop+36+12-k, 176, 12-k, 14, k+2);
	}

	int k = this.crucible.getCookProgressScaled(24);
	this.drawTexturedModalRect(guiLeft+46, guiTop+34, 176, 14, 24-k, 16);

	int l = this.crucible.getAmountFilledScaled(54);
	drawTexturedModalRect(guiLeft+72, guiTop+16+54-l, 176, 52-l, 6, l+2);
}

}

 

 

ContainerCrucible.java:

 

package joebob.joecraft.common;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotFurnace;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.tileentity.TileEntityFurnace;

public class ContainerCrucible extends Container {

private TileEntityCrucible crucible;

public int lastBurnTime;
public int lastItemBurnTime;
public int lastCookTime;

public int amountFilled = 0;
public int maxFill = 500;

public ContainerCrucible(InventoryPlayer inventory, TileEntityCrucible tileentity){
	this.crucible = tileentity;

	this.addSlotToContainer(new Slot(tileentity, 0, 26, 17));
	this.addSlotToContainer(new Slot(tileentity, 1, 26, 53));
	this.addSlotToContainer(new Slot(tileentity, 2, 84, 36));
	this.addSlotToContainer(new SlotFurnace(inventory.player, tileentity, 3, 139, 35));

	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 9; j++){
			this.addSlotToContainer(new Slot(inventory, j + i*9 + 9, 8 + j*18, 84 + i*18));
		}
	}

	for(int i = 0; i<9; i++){
		this.addSlotToContainer(new Slot(inventory, i, 8 + i*18, 142));
	}
}

public void addCraftingToCrafters(ICrafting icrafting){
	super.addCraftingToCrafters(icrafting);
	icrafting.sendProgressBarUpdate(this, 0, this.crucible.cookTime);
	icrafting.sendProgressBarUpdate(this, 1, this.crucible.burnTime);
	icrafting.sendProgressBarUpdate(this, 2, this.crucible.currentItemBurnTime);

}

public void detectAndSendChanges(){
	super.detectAndSendChanges();

	for(int i = 0; i < this.crafters.size(); i++){
		ICrafting icrafting = (ICrafting) this.crafters.get(i);

		if(this.lastCookTime != this.crucible.cookTime){
			icrafting.sendProgressBarUpdate(this, 0, this.crucible.cookTime);
		}

		if(this.lastBurnTime != this.crucible.burnTime){
			icrafting.sendProgressBarUpdate(this, 0, this.crucible.burnTime);
		}

		if(this.lastItemBurnTime != this.crucible.currentItemBurnTime){
			icrafting.sendProgressBarUpdate(this, 0, this.crucible.currentItemBurnTime);
		}
	}

	this.lastCookTime = this.crucible.cookTime;
	this.lastBurnTime = this.crucible.burnTime;
	this.lastItemBurnTime = this.crucible.currentItemBurnTime;
}

@SideOnly(Side.CLIENT)
public void updateProgressBar(int slot, int newValue){
	if(slot == 0) this.crucible.cookTime = newValue;
	if(slot == 1) this.crucible.burnTime = newValue;
	if(slot == 2) this.crucible.currentItemBurnTime = newValue;
	if(slot == 3) this.crucible.amountFilled = newValue;
}


public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (par2 == 2)
            {
                if (!this.mergeItemStack(itemstack1, 4, 40, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 != 1 && par2 != 0)
            {
                if (FurnaceRecipes.smelting().getSmeltingResult(itemstack1) != null)
                {
                    if (!this.mergeItemStack(itemstack1, 0, 1, false))
                    {
                        return null;
                    }
                }
                else if (TileEntityFurnace.isItemFuel(itemstack1))
                {
                    if (!this.mergeItemStack(itemstack1, 1, 2, false))
                    {
                        return null;
                    }
                }
                else if (par2 >= 4 && par2 < 31)
                {
                    if (!this.mergeItemStack(itemstack1, 31, 40, false))
                    {
                        return null;
                    }
                }
                else if (par2 >= 31 && par2 < 40 && !this.mergeItemStack(itemstack1, 4, 31, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 4, 40, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }

        return itemstack;
    }

public boolean canInteractWith(EntityPlayer entityplayer) {
	return this.crucible.isUseableByPlayer(entityplayer);
}

}

 

 

TileEntityCrucible.java:

 

package joebob.joecraft.common;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
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 TileEntityCrucible extends TileEntity implements ISidedInventory {

private String localizedName;

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

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


public int meltSpeed = 25;

////IMPORTANT VARS////
public int burnTime;
public int currentItemBurnTime;
public int cookTime;

public int amountFilled = 0;
public int maxFill = 500;

////METAL VARIABLES////
public int copper = 0;
public int tin = 0;


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

public String getInvName(){
	return this.isInvNameLocalized() ? this.localizedName : "container.crucible";
}

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

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

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

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;
}

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 setInventorySlotContents(int i, ItemStack itemstack) {
	this.slots[i] = itemstack;

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

public int getInventoryStackLimit() {
	return 64;
}

public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);

	NBTTagList list = nbt.getTagList("Items");
	this.slots = new ItemStack[this.getSizeInventory()];

	for(int i = 0; i < list.tagCount(); i++){
		NBTTagCompound compound = (NBTTagCompound) list.tagAt(i);
		byte b = compound.getByte("Slot");

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

	this.burnTime = nbt.getShort("burnTime");
	this.cookTime = nbt.getShort("cookTime");
	this.currentItemBurnTime = getItemBurnTime(this.slots[1]);
	this.amountFilled = nbt.getShort("amountFilled");
	this.copper = nbt.getShort("copper");
	this.tin = nbt.getShort("tin");

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

public void writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);

	nbt.setShort("burnTime", (short)this.burnTime);
	nbt.setShort("cookTime", (short)this.cookTime);
	nbt.setShort("amountFilled", (short)this.amountFilled);
	nbt.setShort("copper", (short)this.copper);
	nbt.setShort("tin", (short)this.tin);

	NBTTagList list = new NBTTagList();

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

	nbt.setTag("Items", list);

	if(this.isInvNameLocalized()){
		nbt.setString("CustomName", this.localizedName);
	}

}

public boolean isUseableByPlayer(EntityPlayer entityplayer) {
	return this.worldObj.getBlockTileEntity(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;
}

public void openChest() {}

public void closeChest() {}


public boolean isBurning(){
	return this.burnTime > 0;
}

public void updateEntity(){
	boolean flag = this.burnTime > 0;
	boolean flag1 = false;

	if(this.burnTime > 0){
		this.burnTime--;
	}

	if(!this.worldObj.isRemote){
		if(this.burnTime == 0 && this.canSmelt()){
			this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]);

			if(this.burnTime >0){
				flag1 = true;

				if(this.slots[1] != null){
					this.slots[1].stackSize--;

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

		if(this.isBurning() && this.canSmelt()){
			this.cookTime++;

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

		}else{
			this.cookTime = 0;
		}


		/*if(flag != this.burnTime > 0){
			blockCrucible.updateBlockState();
		}*/

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


private boolean canSmelt(){
	if(this.slots[0] == null){
		return false;
	}else if(this.amountFilled >= this.maxFill){
		return false;
	}else if(slots[0].getItem() == JoeCraft.copperNugget || slots[0].getItem() == JoeCraft.tinNugget){
			return true;
	}else{
		return false;
	}
	}

public void smeltItem(){
	if(this.canSmelt()){
		if(this.slots[0] == new ItemStack(JoeCraft.copperNugget)){
			this.copper += 20;
			this.amountFilled += 20;
		}
		if(this.slots[0] == new ItemStack(JoeCraft.tinNugget)){
			this.tin += 20;
			this.amountFilled += 20;
		}


		this.slots[0].stackSize--;

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

public static int getItemBurnTime(ItemStack itemstack){
	if(itemstack == null){
		return 0;
	}else{
		int i = itemstack.getItem().itemID;
		Item item = itemstack.getItem();

		if(item instanceof ItemBlock && Block.blocksList[i] != null){
			Block block = Block.blocksList[i];

			if(block.blockMaterial == Material.wood){
				return 300;
			}

			if(block == Block.coalBlock){
				return 16000;
			}
		}

		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).getMaterialName().equals("WOOD")) return 200;
		if(i == Item.stick.itemID) return 100;
		if(i == Item.coal.itemID) return 1600;
		if(i == Item.bucketLava.itemID) return 20000;
		if(i == Block.sapling.blockID) return 100;
		if(i == Item.blazeRod.itemID) return 2400;

		return GameRegistry.getFuelValue(itemstack);
	}
}

public static boolean isItemFuel(ItemStack itemstack){
	return getItemBurnTime(itemstack) > 0;
}

public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return i == 3 ? false : (i == 1 ? isItemFuel(itemstack) : true);
}

public int[] getAccessibleSlotsFromSide(int var1) {
	return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_side);
}

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

public boolean canExtractItem(int i, ItemStack itemstack, int j) {
	return j != 0 || i != 1 || itemstack.itemID == Item.bucketEmpty.itemID;
}

public int getBurnTimeRemainingScaled(int i){
	if(this.currentItemBurnTime == 0){
		this.currentItemBurnTime = this.meltSpeed;
	}

	return this.burnTime * i / this.currentItemBurnTime;
}

public int getCookProgressScaled(int i){

	 return this.cookTime * i / 1600;

}

public int getAmountFilledScaled(int i) {

	return this.amountFilled / this.maxFill * i;

}

}

 

Link to comment
Share on other sites

Whoops. I feel dumb now.

 

Now I just need to figure out why the progress bar is being wonky.

 

EDIT:

I'm also having some problems with the "copper" and "tin" values in TileEntityCrucible. They don't seem to be updating

I did the code for the "fullness" bar and that works now, but only when I exit the GUI and re-enter.

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.