Jump to content

[1.7.10] Gui only drawing one string value when the real value is different


Recommended Posts

Posted

I have a string being drawn on a custom gui that is supposed to present useful information, but the only two values it displays are "Empty" and "Unknown", even when the actual string value in the TileEntity is different.

 

TileEntity:

 

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.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 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 ItemStack fillResult;

public String fillResultString;	

public int meltSpeed = 25;

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

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

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


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

public String getInventoryName(){
	return this.hasCustomInventoryName() ? 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", 10);
	this.slots = new ItemStack[this.getSizeInventory()];

	for(int i = 0; i < list.tagCount(); i++){
		NBTTagCompound compound = (NBTTagCompound) list.getCompoundTagAt(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");
	this.fillTime = nbt.getShort("fillTime");

	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);
	nbt.setShort("fillTime", (short)fillTime);

	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.hasCustomInventoryName()){
		nbt.setString("CustomName", this.localizedName);
	}

}

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

public void openInventory() {}

public void closeInventory() {}


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

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

	if(this.copper < 0) this.copper = 0;
	if(this.tin < 0) this.tin = 0;

	if(this.copper > 0 && this.tin <= 0){
		this.fillResult = new ItemStack(JoeCraft.copperMold);
		this.fillResultString = "Copper";
	}else
	if(this.tin > 0 && this.copper <= 0){
		this.fillResult = new ItemStack(JoeCraft.tinMold);
		this.fillResultString = "Tin";
	}else
	if(this.copper > 0 && this.tin > 0){
		if(this.copper - this.tin > 40 && this.copper - this.tin < 80){
			this.fillResult = new ItemStack(JoeCraft.bronzeMold);
			this.fillResultString = "Bronze";
		}
	}else
	if(this.amountFilled <= 0){
		this.fillResult = null;
		this.fillResultString = "Empty";
	}else
	{
		this.fillResult = new ItemStack(Blocks.dirt);
		this.fillResultString = "Unknown";
	}

	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().getContainerItem(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(this.canFill()){
			this.fillTime++;

			if(this.fillTime == this.meltSpeed){
				this.fillTime = 0;
				this.fillMold();
				flag1 = true;
			}

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

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

}


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

private boolean canFill(){

	if(this.slots[2] == null) return false;

	if(this.amountFilled < 100) return false;

	if(slots[2].getItem() == JoeCraft.clayMold) return true;

	if(this.slots[3] == null) return true;

	if(this.slots[3].isItemEqual(this.fillResult)) return false;

	return false;
}

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

		this.amountFilled += 20;
		this.slots[0].stackSize--;
		//System.out.println("crucible copper " + this.copper);
		//System.out.println("crucible tin " + this.tin);
		//System.out.println(this.fillResultString);

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

public void fillMold(){
	if(this.canFill()){

		if(this.copper > 0){
			this.copper -= 100;
		}
		if(this.tin > 0){
			this.tin -=100;
		}

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

		this.amountFilled -= 100;
		this.slots[2].stackSize--;
		//System.out.println("crucible copper " + this.copper);
		//System.out.println("crucible tin " + this.tin);

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

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

		if(item instanceof ItemBlock && Block.getBlockFromItem(item) != null){
			Block block = Block.getBlockFromItem(item);

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

			if(block == Blocks.coal_block){
				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).getToolMaterialName().equals("WOOD")) return 200;
		if(item == Items.stick) return 100;
		if(item == Items.coal) return 1600;
		if(item == Items.lava_bucket) return 20000;
		if(item == Item.getItemFromBlock(Blocks.sapling)) return 100;
		if(item == Items.blaze_rod) 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.getItem() == Items.bucket;
}

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 / this.meltSpeed;

}

public int getFillProgressScaled(int i){

	 return this.fillTime * i / this.meltSpeed;

}

public int getAmountFilledScaled(int i) {

	return this.amountFilled * i / this.maxFill;

}


}

 

 

Gui:

 

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.hasCustomInventoryName() ? this.crucible.getInventoryName() : I18n.format(this.crucible.getInventoryName(), new Object[0]);

	this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 6, 421075);
	this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 8, this.ySize - 96, 4210752);
	this.fontRendererObj.drawString(this.crucible.fillResultString, 32 + this.xSize / 2 - this.fontRendererObj.getStringWidth(this.crucible.fillResultString) / 2, 17, 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);
	drawTexturedModalRect(guiLeft+46, guiTop+34, 176, 14, k+1, 16);

	int l = this.crucible.getAmountFilledScaled(54);
	drawTexturedModalRect(guiLeft+72, guiTop+16+53-l, 176, 84-l, 6, l+1);

	int m = this.crucible.getFillProgressScaled(24);
	drawTexturedModalRect(guiLeft+106, guiTop+34, 176, 14, m+1, 16);
}

}

 

 

I'm probably overlooking something really simple, but I can't seem to figure out what's wrong.

Posted

Hi

 

My guess - perhaps your string is never being set correctly; or perhaps you are updating some of the components on the server side, but not on the client side.

 

Perhaps try adding a diagnostic at the very end of updateEntity, for example

 

System.out.println("TileEntityCrucible::updateEntity side= " +  FMLcommonHandler.instance().getEffectiveSide() + ", fillResultString= " +  fillResultString);

 

-TGG

Posted

Yep, it turned out that the server has the correct value and the client doesn't.

Pardon my stupidity, but how exactly would I go about fixing this? I'm very new to Minecraft modding.

Posted

Hi

 

You may not need packets if you can calculate the string using information available on the client.  Is there any way your client can calculate tin and copper itself, in parallel with the server?

 

Vanilla synchronises some aspects of Inventories / Containers automatically, although I'm not clear on the details because I've never used it.

 

If tin and copper are being altered by something else on the server that the client doesn't know about, then you will need to use Packets to tell the client what is happening on the server.  In 1.6.4 you could use  Packet132TileEntityData for this sort of thing (->TileEntity.onDataPacket(), TileEntity.getDescriptionPacket()); I don't know what it's called know but it's likely to be very similar.  Try looking in TileEntityBeacon or TileEntitySkull for clues.

 

-TGG

 

 

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.