Jump to content

Recommended Posts

Posted

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_) {


}
}

 

Posted

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

Posted

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

Posted

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/

Posted

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_) {


}
}

Posted

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!");
		}

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

    • I tried do download the essential mod to my mod pack but i didnt work. I paly on 1.21 and it should work. I use neoforge for my modding. The weird things is my friend somehow added the mod to his modpack and many others that I somehow can´t. Is there anything i can do? 
    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
  • Topics

×
×
  • Create New...

Important Information

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