Jump to content

Recommended Posts

Posted

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?

Posted

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

}

 

 

}

 

 

Posted

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
}

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Minecraft 1.21.4 requires a new model definition file for each item, which you don't have. These can be created through Data Generation, specifically the ModelProvider, BlockModelGenerators and ItemModelGenerators classes.
    • Hi,  I'm using Forge 47.3.0 for Minecraft 1.20.1 I apologise if this is obvious I am very new to modding for Minecraft. I sucessfully made a mod that launched without errors or crashes (without it doing anything) but in order to add the features I need, I need to add "Custom Portal API [Forge]" as a dependency. However no matter the way I've tried to acheive this, it crashes. I am pretty sure it's not the way I'm putting it in the repositories, the dependencies or the way I'm refrencing it, as I've a hundred diffrent combinations and multiple Maven methods. And on all those diffrent variations I still get this crash: pastebin.com/UhumzZCZ Any tips would be invaluable as I've been loosing my mind over this!
    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
  • Topics

×
×
  • Create New...

Important Information

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