Jump to content

[1.7.2]GUI not updating


slugslug

Recommended Posts

So i made a simple upgrade system.  I wanted to display how many upgrades were in the machine on the gui.  (i already have saved it to nbt)When when i reload the world it resets to 0 on the gui.  If i do a println in has the right number. How do i make it update on the GUI?

Link to comment
Share on other sites

Tileentity:

 

 

package com.professorvennie.tileEntity;

 

import java.util.Random;

 

import com.professorvennie.block.EFBlock;

import com.professorvennie.block.Grinder;

import com.professorvennie.block.SaltFurnace;

import com.professorvennie.item.EFitem;

 

import cpw.mods.fml.common.registry.GameRegistry;

import net.minecraft.block.Block;

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

import net.minecraft.item.crafting.FurnaceRecipes;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.tileentity.TileEntity;

import net.minecraftforge.common.util.Constants;

 

public class TileEntityGrinder extends TileEntity implements ISidedInventory {

 

private String localizedName;

 

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[4];

 

public int GrinderSpeed;

 

public int power;

 

public final int maxPower = 10000;

 

public int cookTime;

 

private int smeltItem;

 

private int powerusage;

 

public int numOfSpeedUpgrades = 0;

 

public Random rand = new Random();

 

public int getsizeInventory() {

return this.slots.length;

}

 

public void upgrade() {

ItemStack itemstack1;

ItemStack itemstack2;

ItemStack itemstack3;

ItemStack itemstack4;

itemstack1 = this.slots[3];

itemstack2 = this.slots[4];

itemstack3 = this.slots[5];

itemstack4 = this.slots[6];

 

boolean notspeedslot1 = false;

boolean notspeedslot2 = false;

boolean notspeedslot3 = false;

boolean notspeedslot4 = false;

 

if (notspeedslot1 && notspeedslot2 && notspeedslot3 && notspeedslot4) {

this.GrinderSpeed = 70;

}

if (this.GrinderSpeed <= 0) {

this.GrinderSpeed = 10;

}

 

// if(this.slots[3] != null){

// int slot1 = itemstack1.getItem().getIdFromItem(itemstack1.getItem());

// if(slot1 == EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack1.stackSize == 1 && this.GrinderSpeed > 10){

// this.GrinderSpeed -=5;

// this.powerusage +=.25;

// }else if(slot1 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack1.stackSize == 2 && this.GrinderSpeed > 10){

// this.GrinderSpeed -=10;

// this.powerusage+=.5;

// }else if(slot1 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade)){

// notspeedslot1 = true;

// }

// }

// if(this.slots[4] != null){

// int slot2 = itemstack2.getItem().getIdFromItem(itemstack2.getItem());

// if(slot2 == EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack2.stackSize == 1 && this.GrinderSpeed >10){

// this.GrinderSpeed -=5;

// this.powerusage+=.25;

// }else if(slot2 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack2.stackSize == 2 && this.GrinderSpeed >10){

// this.GrinderSpeed -=10;

// this.powerusage+=.5;

// }else if(slot2 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade)){

// notspeedslot2 = true;

// }

// if(this.slots[5] != null){

// int slot3 = itemstack3.getItem().getIdFromItem(itemstack3.getItem());

// if(slot3 == EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack3.stackSize == 1 && this.GrinderSpeed >10){

// this.GrinderSpeed -=5;

// this.powerusage+=.25;

// }else if(slot3 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack3.stackSize == 2 && this.GrinderSpeed >10){

// this.GrinderSpeed -=10;

// this.powerusage+=.5;

// }else if(slot3 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade)){

// notspeedslot3 = true;

// }

// }

// if(this.slots[6] != null){

// int slot4 = itemstack4.getItem().getIdFromItem(itemstack4.getItem());

// if(slot4 == EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack4.stackSize == 1 && this.GrinderSpeed >10){

// this.GrinderSpeed -=5;

// this.powerusage+=.25;

// }else if(slot4 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) &&

// itemstack4.stackSize == 2 && this.GrinderSpeed >10){

// this.GrinderSpeed -=10;

// this.powerusage+=.5;

// }else if(slot4 ==

// EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade)){

// notspeedslot4 = true;

// }

 

// }

// }

 

}

 

public String getInvName() {

return this.isInvNameLocalized() ? this.localizedName

: "container.Grinder";

}

 

public boolean isInvNameLocalized() {

return this.localizedName != null && this.localizedName.length() > 0;

}

 

public void setGuiDisplayName(String displayName) {

 

this.localizedName = displayName;

}

 

@Override

public int getSizeInventory() {

return 0;

}

 

@Override

public ItemStack getStackInSlot(int var1) {

return this.slots[var1];

}

 

@Override

public ItemStack decrStackSize(int var1, int var2) {

if (this.slots[var1] != null) {

ItemStack itemstack;

if (this.slots[var1].stackSize <= var2) {

itemstack = this.slots[var1];

this.slots[var1] = null;

return itemstack;

} else {

itemstack = this.slots[var1].splitStack(var2);

if (this.slots[var1].stackSize == 0) {

this.slots[var1] = null;

}

return itemstack;

}

}

return null;

}

 

@Override

public ItemStack getStackInSlotOnClosing(int var1) {

if (this.slots[var1] != null) {

ItemStack itemstack = this.slots[var1];

this.slots[var1] = null;

return itemstack;

}

 

return null;

}

 

@Override

public void setInventorySlotContents(int var1, ItemStack var2) {

this.slots[var1] = var2;

 

if (var2 != null && var2.stackSize > this.getInventoryStackLimit()) {

var2.stackSize = this.getInventoryStackLimit();

}

}

 

@Override

public String getInventoryName() {

// TODO Auto-generated method stub

return null;

}

 

@Override

public boolean hasCustomInventoryName() {

// TODO Auto-generated method stub

return false;

}

 

@Override

public int getInventoryStackLimit() {

return 64;

}

 

public void readFromNBT(NBTTagCompound nbt) {

super.readFromNBT(nbt);

 

NBTTagList list = nbt.getTagList("items", Constants.NBT.TAG_COMPOUND);

this.slots = new ItemStack[this.getsizeInventory()];

 

for (int i = 0; i < list.tagCount(); i++) {

NBTTagCompound compound = list.getCompoundTagAt(i);

int j = compound.getByte("slot") & 0xff;

 

if (j >= 0 && j < this.slots.length) {

this.slots[j] = ItemStack.loadItemStackFromNBT(compound);

 

}

}

this.power = (int) nbt.getShort("burntime");

this.cookTime = (int) nbt.getShort("cooktime");

this.GrinderSpeed = (int) nbt.getShort("grindspeed");

this.numOfSpeedUpgrades = nbt.getShort("upgrades");

 

if (nbt.hasKey("customname")) {

this.localizedName = nbt.getString("customname");

}

 

}

 

public void writeToNBT(NBTTagCompound nbt) {

super.writeToNBT(nbt);

 

nbt.setShort("burntime", (short) this.power);

nbt.setShort("cooktime", (short) this.cookTime);

nbt.setShort("grindspeed", (short) this.GrinderSpeed);

nbt.setShort("upgrades", (short) this.numOfSpeedUpgrades);

 

NBTTagList list = new NBTTagList();

 

for (int i = 0; i < this.slots.length; i++) {

if (this.slots != null) {

NBTTagCompound compound = new NBTTagCompound();

compound.setByte("slot", (byte) i);

this.slots.writeToNBT(compound);

list.appendTag(compound);

}

}

nbt.setTag("items", list);

if (this.isInvNameLocalized()) {

nbt.setString("customname", this.localizedName);

}

}

 

@Override

public boolean isUseableByPlayer(EntityPlayer var1) {

return this.worldObj.getTileEntity(this.xCoord, this.yCoord,

this.zCoord) != this ? false : var1.getDistanceSq(

(double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D,

(double) this.zCoord + 0.5D) <= 64D;

}

 

@Override

public void openInventory() {

 

}

 

@Override

public void closeInventory() {

 

}

 

public boolean hasPower() {

return this.power > 0;

}

 

public void updateEntity() {

if(this.GrinderSpeed == 0)

this.GrinderSpeed = 70;

 

if(this.powerusage == 0)

this.powerusage = 1;

 

if(this.GrinderSpeed <=5)

this.GrinderSpeed = 5;

 

// this.upgrade();

boolean flag = this.power > 0;

boolean flag1 = false;

 

if (slots[3] != null && this.GrinderSpeed > 5) {

ItemStack itemstack1 = this.slots[3];

int slot1 = itemstack1.getItem().getIdFromItem(itemstack1.getItem());

if (slot1 == EFitem.speedupgrade.getIdFromItem(EFitem.speedupgrade) && this.GrinderSpeed > 5) {

for (int i = 0; i < slots[3].stackSize+1; i++) {

System.out.println(this.GrinderSpeed);

this.GrinderSpeed -=5;

this.powerusage += 0.25;

this.slots[3].stackSize--;

this.numOfSpeedUpgrades++;

}

if(this.slots[3].stackSize == 0)

this.slots[3] = null;

}else

this.GrinderSpeed = 6;

}

 

if (!this.worldObj.isRemote) {

if (this.isItemPower(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 (this.hasPower() && this.canSmelt()) {

this.cookTime++;

this.power -= this.powerusage;

if (this.cookTime == this.GrinderSpeed) {

this.cookTime = 0;

this.GrindeItem();

flag1 = true;

}

} else {

this.cookTime = 0;

}

 

if (flag != this.isgrinding()) {

flag1 = true;

Grinder.updateGrinderBlockState(this.power > 0, this.worldObj,

this.xCoord, this.yCoord, this.zCoord);

}

 

}

 

if (flag1) {

this.markDirty();

}

}

 

private boolean isgrinding() {

if (this.hasPower() && this.cookTime > 0) {

return true;

}

 

return false;

}

 

private boolean canSmelt() {

if (this.slots[0] == null) {

return false;

} else {

ItemStack itemstack = getresult(this.slots[0]);

 

if (itemstack == null)

return false;

if (this.slots[2] == null)

return true;

if (!this.slots[2].isItemEqual(itemstack))

return false;

 

int result = this.slots[2].stackSize + itemstack.stackSize;

 

return (result <= getInventoryStackLimit() && result <= itemstack

.getMaxStackSize());

 

}

}

 

public ItemStack getresult(ItemStack itemstack) {

int i = itemstack.getItem().getIdFromItem(itemstack.getItem());

if (i == Blocks.iron_ore.getIdFromBlock(Blocks.iron_ore))

return new ItemStack(Items.iron_ingot, 2);

if (i == Blocks.gold_ore.getIdFromBlock(Blocks.gold_ore))

return new ItemStack(Items.gold_ingot, 2);

if (i == Blocks.coal_ore.getIdFromBlock(Blocks.coal_ore))

return new ItemStack(Items.coal, 2);

if (i == Blocks.diamond_ore.getIdFromBlock(Blocks.diamond_ore))

return new ItemStack(Items.diamond, 2);

if (i == EFBlock.Saltore.getIdFromBlock(EFBlock.Saltore))

return new ItemStack(EFitem.saltcyrstals, 6);

if (i == Items.chicken.getIdFromItem(Items.chicken))

return new ItemStack(EFitem.rawGroundchicken, 3);

if (i == Items.porkchop.getIdFromItem(Items.porkchop))

return new ItemStack(EFitem.rawGroundpork, 3);

if (i == Items.beef.getIdFromItem(Items.beef))

return new ItemStack(EFitem.rawGroundbeef, 3);

if (i == Blocks.redstone_ore.getIdFromBlock(Blocks.redstone_ore))

return new ItemStack(Items.redstone, 4 + rand.nextInt(3));

if (i == EFBlock.Saltore.getIdFromBlock(EFBlock.Saltore))

return new ItemStack(EFitem.saltcyrstals, 4 + rand.nextInt(4));

return null;

 

}

 

public void GrindeItem() {

if (this.canSmelt()) {

ItemStack itemstack = this.getresult(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 {

int i = itemstack.getItem().getIdFromItem(itemstack.getItem());

if (i == Items.redstone.getIdFromItem(Items.redstone))

return 10;

if (i == Blocks.redstone_block

.getIdFromBlock(Blocks.redstone_block))

return 90;

if (i == EFitem.saltcyrstals.getIdFromItem(EFitem.saltcyrstals))

return 25;

if (i == EFBlock.saltblock.getIdFromBlock(EFBlock.saltblock))

return (25 * 9);

if (i == EFitem.saltingot.getIdFromItem(EFitem.saltingot))

return 50;

if (i == EFBlock.saltblock.getIdFromBlock(EFBlock.saltblock))

return 50 * 9;

if (i == Blocks.redstone_block

.getIdFromBlock(Blocks.redstone_block))

return 90;

return 0;

}

}

 

public static boolean isItemPower(ItemStack itemstack) {

return getItemPower(itemstack) > 0;

}

 

@Override

public boolean isItemValidForSlot(int var1, ItemStack var2) {

 

return var1 == 2 ? false : (var1 == 1 ? isItemPower(var2) : true);

}

 

@Override

public int[] getAccessibleSlotsFromSide(int var1) {

return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_sides);

}

 

@Override

public boolean canInsertItem(int var1, ItemStack var2, int var3) {

return this.isItemValidForSlot(var1, var2);

}

 

@Override

public boolean canExtractItem(int var1, ItemStack var2, int var3) {

return var3 != 0 || var1 != 1 || var2.getItem() == Items.bucket;

}

 

public int getPowerReamingScaled(int i) {

return this.power * i / this.maxPower;

}

 

public int getGrinderSacled(int i) {

return this.cookTime * i / this.GrinderSpeed;

}

 

}

 

 

Gui:

 

 

package com.professorvennie.main.gui;

 

import org.lwjgl.opengl.GL11;

 

import com.professorvennie.lib.Strings;

import com.professorvennie.main.mainRegistry;

import com.professorvennie.tileEntity.TileEntityGrinder;

import com.professorvennie.tileEntity.TileEntitySaltFurnace;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.inventory.GuiContainer;

import net.minecraft.client.resources.I18n;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.util.ResourceLocation;

 

public class GuiGrinder  extends GuiContainer{

 

public static final ResourceLocation texture = new ResourceLocation(Strings.MODID, "textures/gui/Grinder.png");

 

public TileEntityGrinder Grinder;

 

public GuiGrinder(InventoryPlayer inventoryPlayer, TileEntityGrinder entity) {

super(new ContainerGrinder(inventoryPlayer, entity));

 

this.Grinder = entity;

 

this.xSize = 176;

this.ySize = 166;

}

 

public void drawGuiContainerForegroundLayer(int par1, int par2){

String name = this.Grinder.isInvNameLocalized() ? this.Grinder.getInvName() : I18n.format(this.Grinder.getInvName(), mainRegistry.instance);

 

this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 6, 4210752);

this.fontRendererObj.drawString(I18n.format("contaniner.inventory", mainRegistry.instance), 8, this.ySize - 96 + 2, 4210752);

this.fontRendererObj.drawString(I18n.format("Speed Upgrades: "+ Grinder.numOfSpeedUpgrades, mainRegistry.instance), 40, this.ySize - 96-10, 4210752);

}

 

@Override

public void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {

GL11.glColor4f(1F, 1F, 1F, 1F);

 

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

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

 

if(this.Grinder.hasPower()){

int k = this.Grinder.getPowerReamingScaled(41);

drawTexturedModalRect(guiLeft + 11, guiTop + 50  - k, 176, 59 - k, 16, k);

}

 

int k = this.Grinder.getGrinderSacled(24);

drawTexturedModalRect(guiLeft + 79, guiTop + 34, 176, 0, k + 1, 16);

}

 

 

}

 

 

Link to comment
Share on other sites

Well first it's good that you put your code inside spoilers but put the code inside the

 [code] Code here 

[/code]

 

ok now to the problem. Well I would suggest that you use NBT and load from it like this

 

if (!nbt.getShort("upgrades") == null){
Set the upgrade counter to X ammount of upgrades
}

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.