Jump to content

Recommended Posts

Posted (edited)

So the problem is very simple, i want the item i'm holding to change, and not the other ones of the same item in my inventory. Here is the class with the method. Any ideas?

	EntityLightningBolt lightning;
	
	private int lightning_index;
	
	public ItemZeusLightningCharged(String unlocalizedName) 
	{
		super(unlocalizedName);
		this.setCreativeTab(CreativeTabs.COMBAT);
		this.lightning_index = 0;
		
		this.addPropertyOverride(new ResourceLocation(Reference.MODID, "zeus_mode"), new IItemPropertyGetter() 
		{
			@Override
			public float apply(ItemStack stack, World worldIn, EntityLivingBase entityIn) 
			{
				Utils.getLogger().info("Itemstack: " + stack);
				if (entityIn != null && entityIn.getHeldItem(EnumHand.MAIN_HAND).getItem() == ModItems.ZEUS_LIGHTNING_CHARGED)
				{
					Utils.getLogger().info("Player has Item.");
					return (float)((ItemZeusLightningCharged)entityIn.getHeldItem(EnumHand.MAIN_HAND).getItem()).lightning_index;
				}
				else 
				{
					return 0.0F;
				}
			}
		});
	}

To clarify, the lightning index is an int that resembles the different modes. 

 

Edit: This doesn't actually work serverside either.

Edited by Triphion
Posted

Thank you for the advice. I have tried to make something like that. However, the problem is that the player doesn't register the active itemstack, and instead has it as simple air. And i don't really know how to fix that. 

			((ItemZeusLightningCharged)player.getHeldItemMainhand().getItem()).setLightningIndex(player.getActiveItemStack(), +1);

This is the function that i'm calling when the player presses a button to change the firing mode. After that, the lightning index is going up, and then the item property is supposed to override and give the item a new texture. 

	public static int getLightningIndex(ItemStack item) 
	{
	    NBTTagCompound nbtTagCompound = item.getTagCompound();
	    
	    if(nbtTagCompound != null)
		    return nbtTagCompound.getInteger("lIndex");
	   
	   return 0;
    }
   
    public static void setLightningIndex(ItemStack item, int lightningValue) 
    {
	    NBTTagCompound nbtTagCompound = item.getTagCompound();
	    
	    if (lightningValue < 2) 
	    {
	    }
	    else if (lightningValue == 2) 
	    {
	    	lightningValue = 0;
	    }
	    
	    nbtTagCompound.setInteger("lIndex", lightningValue);
	   
	    ItemZeusLightningCharged.lightningIndex = lightningValue;
	    
	    Utils.getLogger().info(lightningValue);
	}

This is the methods i'm using to get the index number. 

	public static int lightningIndex;

This is the int/float i'm using to check if the item should override. 

Posted

I do in-fact know how static works, this was actually me being frustrated and just trying everything(which is never a good thing). Static is a declaration that has the same value for the class, including all individual objects that stem from the class has the same value. I have now removed those. I realized now aswell, but i don't know how to get that specific itemstack and then use the method since i cannot cast directly to an itemstack. This is what i have for now, and it is not valid/or working method, just starting the method. 

		if (player.getHeldItem(EnumHand.MAIN_HAND).getItem() == ModItems.ZEUS_LIGHTNING_CHARGED) 
		{
			System.out.println("Succesfully retrieved Item.");
			
			ItemStack weapon = player.getHeldItem(EnumHand.MAIN_HAND);
			((ItemZeusLightningCharged)weapon).setLightningIndex(weapon, + 1);
		}

How do i cast directly to the itemstack and not the item class as a whole? Because i can get the itemstack, but not cast it directly to the itemstack and change that specific itemstack. 

[16:42:22] [Netty Server IO #1/INFO] [STDOUT]: [com.etauricstaff.etauricmod.network.MessageGetPlayer:handleServerSide:36]: Succesfully retrieved Item.
[16:42:22] [Netty Server IO #1/INFO] [STDOUT]: [com.etauricstaff.etauricmod.network.MessageGetPlayer:handleServerSide:39]: Itemstack: 1xitem.zeus_lightning_charged@0

 

Posted (edited)

EDIT: I realized that there is actually the setLightningIndex function that gets the server to crash. And this is the error i get everytime:

Quote

[19:52:05] [Netty Server IO #1/ERROR] [FML]: There was a critical exception handling a packet on channel etauric
java.lang.NullPointerException: null
    at com.etauricstaff.etauricmod.items.ItemZeusLightningCharged.setLightningIndex(ItemZeusLightningCharged.java:92) ~[ItemZeusLightningCharged.class:?]
    at com.etauricstaff.etauricmod.network.MessageGetPlayer.handleServerSide(MessageGetPlayer.java:43) ~[MessageGetPlayer.class:?]
    at com.etauricstaff.etauricmod.network.MessageBase.onMessage(MessageBase.java:17) ~[MessageBase.class:?]

And this is how my setLightningFunction looks right now:

Quote

    public void setLightningIndex(ItemStack item, int lightningValue) 
    {
        NBTTagCompound nbtTagCompound = item.getTagCompound();
        
        if (lightningValue < 2) 
        {
        }
        else if (lightningValue == 2) 
        {
            lightningValue = 0;
        }
        
        nbtTagCompound.setInteger("lIndex", lightningValue);
        
        Utils.getLogger().info(lightningValue);
    }

 

Edited by Triphion
Posted
3 minutes ago, diesieben07 said:

This can be null if the ItemStack does not have an NBT tag yet. Your code does not handle that case.

I SOLVED IT! Thank you so much diesieben! ❤️

This is how i did it: 

Quote

    public void setLightningIndex(ItemStack item, int lightningValue) 
    {
        NBTTagCompound nbtTagCompound = item.getTagCompound();
        System.out.println(nbtTagCompound);
        
        if (nbtTagCompound == null) 
        {
            nbtTagCompound = new NBTTagCompound();
            item.setTagCompound(nbtTagCompound);
        }
        
        if (lightningValue < 2) 
        {
        }
        else if (lightningValue == 2) 
        {
            lightningValue = 0;
        }
        
        nbtTagCompound.setInteger("lIndex", lightningValue);
        System.out.println("Lightning Value: " + lightningValue);
    }

Posted
8 hours ago, Triphion said:

System.out.println(nbtTagCompound);

1) remove this

2) use the logger the FMLPreInitializationEvent gives you

 

8 hours ago, Triphion said:

if (lightningValue < 2) 
        {
        }

Did you mean to leave this empty?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
2 hours ago, Draco18s said:

1) remove this

2) use the logger the FMLPreInitializationEvent gives you

 

Did you mean to leave this empty?

I have removed the printlns, and i usually use the logger, but for this i used the printlns because of a whim i guess? ?

And that check is actually not needed, so i removed it. I always add +1 as the lightning index, so it always goes up, then i check if it goes up to 2, and if it does, it resets the value. But thanks for pointing those out. ?

Posted
7 hours ago, Triphion said:

I always add +1 as the lightning index, so it always goes up, then i check if it goes up to 2, and if it does, it resets the value. But thanks for pointing those out. 

So, it will do this:

 

0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0...

 

Yes?

 

If so, why do you need an integer? Or an if statement at all?

 

1 - currentValue will do this just fine. So will (currentValue + 1) % maximum

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)
14 minutes ago, Draco18s said:

So, it will do this:

 

0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0...

 

Yes?

 

If so, why do you need an integer? Or an if statement at all?

 

1 - currentValue will do this just fine. So will (currentValue + 1) % maximum

I need the integer since i have added more than just 2 modes. This is how it looks now:

    public void setLightningIndex(ItemStack item, int lightningValue) 
    {
	    NBTTagCompound nbtTagCompound = item.getTagCompound();
	    
	    if (nbtTagCompound == null) 
	    {
	    	nbtTagCompound = new NBTTagCompound();
	    	item.setTagCompound(nbtTagCompound);
	    }
	    
	    if (lightningValue == 4) 
	    {
	    	lightningValue = 0;
	    }
	    
	    nbtTagCompound.setInteger("lIndex", lightningValue);
	}

And this works just fine for this case. 

But i am very appreciative of the advice however. ^^

 

EDIT: Actually, i do not need that integer parameter, my bad, thanks for pointing that out.

Quote

    public void setLightningIndex(ItemStack item) 
    {
        NBTTagCompound nbtTagCompound = item.getTagCompound();
        
        if (nbtTagCompound == null) 
        {
            nbtTagCompound = new NBTTagCompound();
            item.setTagCompound(nbtTagCompound);
        }
        
        nbtTagCompound.setInteger("lIndex", nbtTagCompound.getInteger("lIndex") + 1);
        
        if (nbtTagCompound.getInteger("lIndex") == 4) 
        {
            nbtTagCompound.setInteger("lIndex", 0);
        }
    }

This works easier than the above mentioned, thanks Draco. ^^

Edited by Triphion
Posted

I can replace this:

11 hours ago, Triphion said:

        nbtTagCompound.setInteger("lIndex", nbtTagCompound.getInteger("lIndex") + 1);
        
        if (nbtTagCompound.getInteger("lIndex") == 4) 
        {
            nbtTagCompound.setInteger("lIndex", 0);
        }

 

With this:

nbtTagCompound.setInteger("lIndex", (nbtTagCompound.getInteger("lIndex") + 1) % 4);

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
7 hours ago, Draco18s said:

I can replace this:

With this:

nbtTagCompound.setInteger("lIndex", (nbtTagCompound.getInteger("lIndex") + 1) % 4);

Oh wow! Worked wonders, didn't know you could actually do it like that. Thanks for tellinig me. ?

Posted

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.