Jump to content

[1.7.10] Get Access To NBT From A Different Class


Bugzoo

Recommended Posts

I want to use the NBT I have made in one class, in another, how would I do this and here is my code

 

NBT

 

package com.bugzoo.FinancialMod;

import java.util.List;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

public class ItemDebitCard extends Item {
	public ItemDebitCard(){
		setMaxStackSize(1);
		setUnlocalizedName("DebitCard");
		setCreativeTab(FinancialMod.financialTab);
	}

	int UniquePin = 1000 + (int)(Math.random()*9999); 
	public static double balance;

	    @Override
	    public void onCreated(ItemStack itemStack, World world, EntityPlayer player) {
	    	 itemStack.stackTagCompound = new NBTTagCompound();
	    	 itemStack.stackTagCompound.setString("owner", player.getDisplayName());
	    	 itemStack.stackTagCompound.setInteger("pin", UniquePin);
	    	 itemStack.stackTagCompound.setDouble("balance", balance);
	    	 if(!world.isRemote){
	    	 player.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.RED + "Your Unique Pin is " + UniquePin));
	    	 }
	    }

	    public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4) {
	    	 if (itemStack.stackTagCompound != null) {
                    String owner = itemStack.stackTagCompound.getString("owner");
                    String pin = itemStack.stackTagCompound.getString("pin");
                     list.add("Owner: " + owner);
                     list.add("Balance: " + balance);
                     
                     
                     if (!owner.equals(player.getDisplayName())) {
                    	  list.add("Pin: " + EnumChatFormatting.OBFUSCATED + pin);
    		    	 } else {
    		    		 list.add("Pin: " + pin);
    		    	 }
	    	 }
	    	 
             }
	    
	    
	    }

 

 

GUI

 

 

package com.bugzoo.FinancialMod;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;



public class ATMPinGUI extends GuiContainer{
public final int xSizeBackground1 = 232;
public final int ySizeBackground1 = 240;

private String enteredPin = "";
private String underScore = "";
private int tick = 0;




public final int guiX = (this.width - xSizeBackground1) / 2;
public final int guiY = (this.height - xSizeBackground1) / 2;

public ResourceLocation background1 = new ResourceLocation("financialmod", "textures/gui/ATM.png");


public ATMPinGUI(InventoryPlayer player, TileEntityATM atm){
	super(new ContainerATM(player, atm));
}


public void initGui(){
	buttonList.clear();
	buttonList.add(new ATMButton(1, guiLeft + 181, guiTop + 149, 24, 15, "1")); // 1
    	buttonList.add(new ATMButton(2, guiLeft + 213, guiTop + 149, 24, 15, "2")); // 2
    	buttonList.add(new ATMButton(3, guiLeft + 245, guiTop + 149, 24, 15, "3")); // 3
    	buttonList.add(new ATMRedButton(4, guiLeft + 281, guiTop + 149, 24, 15, "")); // Cancel
    	buttonList.add(new ATMButton(5, guiLeft + 181, guiTop + 173, 24, 15, "4")); // 4
    	buttonList.add(new ATMButton(6, guiLeft + 213, guiTop + 173, 24, 15, "5")); // 5
    	buttonList.add(new ATMButton(7, guiLeft + 245, guiTop + 173, 24, 15, "6")); // 6
    	buttonList.add(new ATMYellowButton(8, guiLeft + 281, guiTop + 173, 24, 15, "")); // Clear
    	buttonList.add(new ATMButton(9, guiLeft + 181, guiTop + 197, 24, 15, "7")); // 7
    	buttonList.add(new ATMButton(10, guiLeft + 213, guiTop + 197, 24, 15, "8")); // 8
    	buttonList.add(new ATMButton(11, guiLeft + 245, guiTop + 197, 24, 15, "9")); // 9
    	buttonList.add(new ATMConfirmButton(12, guiLeft + 281, guiTop + 197, 24, 15, "")); // Confirm
    	buttonList.add(new ATMButton(13, guiLeft + 213, guiTop + 221, 24, 15, "0")); // 0

    	buttonList.add(new ATMLeftButton(14, guiLeft + 130, guiTop + 33, 24, 15, "")); //Top-Left
    	buttonList.add(new ATMLeftButton(15, guiLeft + 130, guiTop + 60, 24, 15, "")); //Mid-Upper Left
    	buttonList.add(new ATMLeftButton(16, guiLeft + 130, guiTop + 87, 24, 15, "")); //Mid-Lower Left
    	buttonList.add(new ATMLeftButton(17, guiLeft + 130, guiTop + 114, 24, 15, "")); //Bottom Left
    	
    	buttonList.add(new ATMRightButton(18, guiLeft + 325, guiTop + 33, 24, 15, "")); //Top-Right
    	buttonList.add(new ATMRightButton(19, guiLeft + 325, guiTop + 60, 24, 15, "")); //Mid-Upper Right
    	buttonList.add(new ATMRightButton(20, guiLeft + 325, guiTop + 87, 24, 15, "")); //Mid-Lower Right
    	buttonList.add(new ATMRightButton(21, guiLeft + 325, guiTop + 114, 24, 15, "")); //Bottom Right

}

public void actionPerformed(GuiButton guibutton){
	if(guibutton.id==1){
		enteredPin = enteredPin + "1";
		System.out.println(enteredPin);
	}
	if(guibutton.id==2){
		enteredPin = enteredPin + "2";
		System.out.println(enteredPin);
	}
	if(guibutton.id==3){
		enteredPin = enteredPin + "3";
		System.out.println(enteredPin);
	}
	if(guibutton.id==4){
		this.enteredPin = "";
	}
	if(guibutton.id==5){
		enteredPin = enteredPin + "4";
		System.out.println(enteredPin);
	}
	if(guibutton.id==6){
		enteredPin = enteredPin + "5";
		System.out.println(enteredPin);
	}
	if(guibutton.id==7){
		enteredPin = enteredPin + "6";
		System.out.println(enteredPin);
	}
	if(guibutton.id=={

	}
	if(guibutton.id==9){
		enteredPin = enteredPin + "7";
		System.out.println(enteredPin);
	}
	if(guibutton.id==10){
		enteredPin = enteredPin + "8";
		System.out.println(enteredPin);
	}
	if(guibutton.id==11){
		enteredPin = enteredPin + "9";
		System.out.println(enteredPin);
	}
	if(guibutton.id==12){
		if(enteredPin.equals(ItemDebitCard.owner)){
			System.out.println("THEY HAVE THE SAME PIN!");
		}
	}
}


public boolean doesGuiPauseGame(){
	return false;
}

public void drawScreen(int i, int j, float f){



	//Background Texture
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(background1);
        int k1 = (this.width - this.xSizeBackground1) / 2;
        int l1 = (this.height - this.ySizeBackground1) / 2;
        this.drawTexturedModalRect(k1, l1, 0, 0, this.xSizeBackground1, this.ySizeBackground1);
        
        String underScore = "";
    	if (tick < 60) {
    		tick++;
    	} else if (tick >= 60) {
    		tick = 0;
    	}
    	
    	if (tick < 30) {
    		underScore = "_";
    	}
    	
    	if (enteredPin.length() == 1) {
		drawString(fontRendererObj, "*" + underScore, width - 295, 90, 0xff00);
	} else if (enteredPin.length() == 2) {
		drawString(fontRendererObj, "**" + underScore, width - 295, 90, 0xff00);
	} else if (enteredPin.length() == 3) {
		drawString(fontRendererObj, "***" + underScore, width - 295, 90, 0xff00);
	} else if (enteredPin.length() >= 4) {
		drawString(fontRendererObj, "****" + underScore, width - 295, 90, 0xff00);
	}
        
        this.drawCenteredString(fontRendererObj, "Welcome!", width/2, 45, 0xff00);
        this.drawString(fontRendererObj, "Please Enter Your Pin", width - 295, 60, 0xff00);
        if(enteredPin.length() == 0){
        this.drawString(fontRendererObj, underScore, width - 295, 90, 0xff00);
        }
        super.drawScreen(i, j, f); 
}


@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_,
		int p_146976_2_, int p_146976_3_) {


}
}

 

Link to comment
Share on other sites

Grmbl, you still have

UniquePin

and

balance

as fields in your Item class. This does not work! You have to use the ItemStack every time you want to access those values. You cannot store them in the Item class.

 

For accessing those values from your Gui: Use the ItemStack, just like you do in your Item class already. For writing values to it from a Gui you will need packets though, as a Gui is client-only and you cannot change things in the world/in inventories/etc. from the client.

 

So like this?

itemStack.stackTagCompound = new NBTTagCompound();
	    	 itemStack.stackTagCompound.setString("owner", player.getDisplayName());
	    	 itemStack.stackTagCompound.setInteger("pin", 1000 + (int)(Math.random()*9999));

Link to comment
Share on other sites

That would overwrite any previous data, yes. I suggest you only create a new NBTTagCompound if stackTagCompound is null, to avoid overwriting any previous data like e.g. Enchantments.

Also you should not store the display name. It potentially includes stuff like color codes and stuff. Use the UUID for the player, as Mojang will soon allow username changes.

 

So, how would I get the pin into my gui class, I used this but it gave me a null pointer

if(enteredPin.equals(itemstack.stackTagCompound.getString("pin")))

Link to comment
Share on other sites

Either

enteredPin

,

itemstack

or

stackTagCompound

null. Put a breakpoint on that line so you can see which was is null.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Either

enteredPin

,

itemstack

or

stackTagCompound

null. Put a breakpoint on that line so you can see which was is null.

 

Here is the whole class. I think that the itemstack might be null

package com.bugzoo.FinancialMod;

import java.io.IOException;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;



public class ATMPinGUI extends GuiContainer{
public final int xSizeBackground1 = 232;
public final int ySizeBackground1 = 240;

private String enteredPin = "";
private String underScore = "";
private int tick = 0;

public ItemStack itemstack;


public final int guiX = (this.width - xSizeBackground1) / 2;
public final int guiY = (this.height - xSizeBackground1) / 2;

public ResourceLocation background1 = new ResourceLocation("financialmod", "textures/gui/ATM.png");


public ATMPinGUI(InventoryPlayer player, TileEntityATM atm){
	super(new ContainerATM(player, atm));
}


public void initGui(){
	buttonList.clear();
	buttonList.add(new ATMButton(1, guiLeft + 181, guiTop + 149, 24, 15, "1")); // 1
    	buttonList.add(new ATMButton(2, guiLeft + 213, guiTop + 149, 24, 15, "2")); // 2
    	buttonList.add(new ATMButton(3, guiLeft + 245, guiTop + 149, 24, 15, "3")); // 3
    	buttonList.add(new ATMRedButton(4, guiLeft + 281, guiTop + 149, 24, 15, "")); // Cancel
    	buttonList.add(new ATMButton(5, guiLeft + 181, guiTop + 173, 24, 15, "4")); // 4
    	buttonList.add(new ATMButton(6, guiLeft + 213, guiTop + 173, 24, 15, "5")); // 5
    	buttonList.add(new ATMButton(7, guiLeft + 245, guiTop + 173, 24, 15, "6")); // 6
    	buttonList.add(new ATMYellowButton(8, guiLeft + 281, guiTop + 173, 24, 15, "")); // Clear
    	buttonList.add(new ATMButton(9, guiLeft + 181, guiTop + 197, 24, 15, "7")); // 7
    	buttonList.add(new ATMButton(10, guiLeft + 213, guiTop + 197, 24, 15, "8")); // 8
    	buttonList.add(new ATMButton(11, guiLeft + 245, guiTop + 197, 24, 15, "9")); // 9
    	buttonList.add(new ATMConfirmButton(12, guiLeft + 281, guiTop + 197, 24, 15, "")); // Confirm
    	buttonList.add(new ATMButton(13, guiLeft + 213, guiTop + 221, 24, 15, "0")); // 0

    	buttonList.add(new ATMLeftButton(14, guiLeft + 130, guiTop + 33, 24, 15, "")); //Top-Left
    	buttonList.add(new ATMLeftButton(15, guiLeft + 130, guiTop + 60, 24, 15, "")); //Mid-Upper Left
    	buttonList.add(new ATMLeftButton(16, guiLeft + 130, guiTop + 87, 24, 15, "")); //Mid-Lower Left
    	buttonList.add(new ATMLeftButton(17, guiLeft + 130, guiTop + 114, 24, 15, "")); //Bottom Left
    	
    	buttonList.add(new ATMRightButton(18, guiLeft + 325, guiTop + 33, 24, 15, "")); //Top-Right
    	buttonList.add(new ATMRightButton(19, guiLeft + 325, guiTop + 60, 24, 15, "")); //Mid-Upper Right
    	buttonList.add(new ATMRightButton(20, guiLeft + 325, guiTop + 87, 24, 15, "")); //Mid-Lower Right
    	buttonList.add(new ATMRightButton(21, guiLeft + 325, guiTop + 114, 24, 15, "")); //Bottom Right

}

public void actionPerformed(GuiButton guibutton){
	if(guibutton.id==1){
		enteredPin = enteredPin + "1";
		System.out.println(enteredPin);
	}
	if(guibutton.id==2){
		enteredPin = enteredPin + "2";
		System.out.println(enteredPin);
	}
	if(guibutton.id==3){
		enteredPin = enteredPin + "3";
		System.out.println(enteredPin);
	}
	if(guibutton.id==4){
		this.enteredPin = "";
	}
	if(guibutton.id==5){
		enteredPin = enteredPin + "4";
		System.out.println(enteredPin);
	}
	if(guibutton.id==6){
		enteredPin = enteredPin + "5";
		System.out.println(enteredPin);
	}
	if(guibutton.id==7){
		enteredPin = enteredPin + "6";
		System.out.println(enteredPin);
	}
	if(guibutton.id=={


		/*String URL = "https://www.google.co.uk/search?q=atm+screen&client=firefox-a&hs=KRv&rls=org.mozilla:en-GB:official&channel=np&tbm=isch&imgil=4qxgegDeEWCSaM%253A%253BMH8IbcHAhg9-3M%253Bhttps%25253A%25252F%25252Fsufiazamir.wordpress.com%25252Ftag%25252Fatm-screens%25252F&source=iu&pf=m&fir=4qxgegDeEWCSaM%253A%252CMH8IbcHAhg9-3M%252C_&usg=__0fIcaFRq-6p0shy1i8dW3VNWoa4%3D&biw=1536&bih=755&ved=0CC4Qyjc&ei=MRlWVPz-FOat7AaP04HwCw#facrc=_&imgdii=_&imgrc=Hxd6rXd8p_O0NM%253A%3B4r3sPIEVxepLfM%3Bhttp%253A%252F%252Fzesty.ca%252Flj%252Flusaka-atm-3.jpg%3Bhttp%253A%252F%252Fzestyping.livejournal.com%252F251102.html%3B400%3B300";
		try {
			java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
	}
	if(guibutton.id==9){
		enteredPin = enteredPin + "7";
		System.out.println(enteredPin);
	}
	if(guibutton.id==10){
		enteredPin = enteredPin + "8";
		System.out.println(enteredPin);
	}
	if(guibutton.id==11){
		enteredPin = enteredPin + "9";
		System.out.println(enteredPin);
	}
	if(guibutton.id==12){
		if(enteredPin.equals(itemstack.stackTagCompound.getString("pin"))){

		}
	}
}


public boolean doesGuiPauseGame(){
	return false;
}

public void drawScreen(int i, int j, float f){



	//Background Texture
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(background1);
        int k1 = (this.width - this.xSizeBackground1) / 2;
        int l1 = (this.height - this.ySizeBackground1) / 2;
        this.drawTexturedModalRect(k1, l1, 0, 0, this.xSizeBackground1, this.ySizeBackground1);
        
        String underScore = "";
    	if (tick < 60) {
    		tick++;
    	} else if (tick >= 60) {
    		tick = 0;
    	}
    	
    	if (tick < 30) {
    		underScore = "_";
    	}
    	
    	if (enteredPin.length() == 1) {
		drawString(fontRendererObj, "*" + underScore, width - 295, 90, 0xff00);
	} else if (enteredPin.length() == 2) {
		drawString(fontRendererObj, "**" + underScore, width - 295, 90, 0xff00);
	} else if (enteredPin.length() == 3) {
		drawString(fontRendererObj, "***" + underScore, width - 295, 90, 0xff00);
	} else if (enteredPin.length() >= 4) {
		drawString(fontRendererObj, "****" + underScore, width - 295, 90, 0xff00);
	}
        
        this.drawCenteredString(fontRendererObj, "Welcome!", width/2, 45, 0xff00);
        this.drawString(fontRendererObj, "Please Enter Your Pin", width - 295, 60, 0xff00);
        if(enteredPin.length() == 0){
        this.drawString(fontRendererObj, underScore, width - 295, 90, 0xff00);
        }
        super.drawScreen(i, j, f); 
}


@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_,
		int p_146976_2_, int p_146976_3_) {


}
}

Link to comment
Share on other sites

Yeah, you don't set the

itemstack

variable anywhere, so how would you expect it to not be null?

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Yeah, you don't set the

itemstack

variable anywhere, so how would you expect it to not be null?

 

Ok, so what should I set the itemstack equal to?

ItemStack itemstack = null;
itemstack.stackTagCompound = new NBTTagCompound();
		if(enteredPin.equals(itemstack.stackTagCompound.getString("pin"))){
			System.out.println("The Right Pin!");
		}

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • file:///C:/Users/Danie/Downloads/Capture.JPG I keep experiencing this error, does anyone know how to fix it here is the report: file:///C:/Users/Danie/AppData/Roaming/.minecraft/crash-reports/Bug_report1.txt
    • Hi, i am unable to join a multiplayer server my friend and i created. We started it with some mods then decided to add some others and this is when the error appeared. We are using Curseforge, the server is in 1.20.1 . The full error message is "Internal Exception: io.netty.handler.codec.DecoderException: java.lang.ArrayIndexOutOfBoundsException: Index 17196645 out of bounds for length 13" I am the only one with that issue, my friends can log in and out no problem. I have tried switching minecraft account but that doesn't help so i guess the bug is on my side. We use the same mod profile so i know the error doesn't come from there. Solo works fine tho Here is the latest.log file https://pastebin.com/X6J9Fnnf. If you need more informations to try and investigate, please let me know. Big thanks to anyone that will try to help
    • [00:02:07] [main/ERROR]: Missing or unsupported mandatory dependencies:     Mod ID: 'kotlinforforge', Requested by: 'flightlib', Expected range: '[4.3.0,)', Actual version: '[MISSING]'     Mod ID: 'terrablender', Requested by: 'biomesoplenty', Expected range: '[2.2.0.154,)', Actual version: '[MISSING]'     Mod ID: 'geckolib', Requested by: 'cakescosmetics', Expected range: '[4.4,)', Actual version: '[MISSING]'     Mod ID: 'balm', Requested by: 'waystones', Expected range: '[7.2.0,)', Actual version: '[MISSING]'     Mod ID: 'oculus', Requested by: 'irisflw', Expected range: '[1.6.15,)', Actual version: '[MISSING]'     Mod ID: 'cupboard', Requested by: 'betterfpsdist', Expected range: '[1.20.1-1.5,)', Actual version: '[MISSING]' [00:02:53] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\fmlcore\1.20.1-47.2.32\fmlcore-1.20.1-47.2.32.jar is missing mods.toml file [00:02:54] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\javafmllanguage\1.20.1-47.2.32\javafmllanguage-1.20.1-47.2.32.jar is missing mods.toml file [00:02:54] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\lowcodelanguage\1.20.1-47.2.32\lowcodelanguage-1.20.1-47.2.32.jar is missing mods.toml file [00:02:54] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\mclanguage\1.20.1-47.2.32\mclanguage-1.20.1-47.2.32.jar is missing mods.toml file [00:02:65] [main/ERROR]: Skipping early mod setup due to previous error
    • Guns and more cool stuff
    • nvm i did it, turn out i just needed to install a drive or smth
  • Topics

×
×
  • Create New...

Important Information

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