Jump to content

Recommended Posts

Posted

right at first this item want a metadata and it looked like this, it worked fine....

http://pastebin.com/uX0AeCjd

 

then i made it a metadata and i couldn't get the cool-down to stay because after it would change the metadata to -1 then it would automatically reset the cool down.. i tried to accomplish saving the metadata by using stacktagcompound but i ended up breaking it where it wouldn't kill anything at all. i would enjoy some help on this please....

this is the code i made so far(warning it is messy for right now)

http://pastebin.com/J10X0wHb

Posted

You can't do that...

 

Item is a singleton. Think of it as "description of what you are holding".

 

The thing you are actually holding is ItemStack, thus - ANYTHING that will be put into Item.class will be shared between all items.

 

You need to save cooldown inside ItemStack's NBT. To learn how to use NBT- google it.

A bit of background: http://www.minecraftforge.net/wiki/Creating_NBT_for_items

1.7.10 is no longer supported by forge, you are on your own.

Posted

Well, you failed :D

 

Remove "int cooldown" field from your item class. Operate only on NBT.

1.7.10 is no longer supported by forge, you are on your own.

Posted

@Override
public void onUpdate(ItemStack itemStack, World world, Entity entity, int i, boolean flag)
{
super.onUpdate(itemStack, world, entity, i, flag);

if (!world.isRemote)
{
	NBTTagCompound data = itemStack.stackTagCompound;
	if (data == null)
	{
		itemStack.stackTagCompound = new NBTTagCompound();
		data = itemStack.stackTagCompound;
	}

	int cd = data.getInteger("cd");
	if (cd > 0)
	{
		--cd;
		data.setInteger("cd", cd);
	}
}
}

 

This code will try to decrement cooldown to zero whenever it's positive (positive = item has cooldown).

It also has lazy initialization and will work like charm (mind I wrote it here and am on 1.8, so might made mistake).

 

Btw. That is one way to do it. There are others - unless you need to have visible (e.g displayed on gui/item description) cooldown you can also use timestamps of last item use. That way you don't have to operate every tick on NBT unless you actually use it.

1.7.10 is no longer supported by forge, you are on your own.

Posted

What other ways could i do this ? You sead if i wanted to make it visable to use in my addinformation i would have to do something else then what  you put on it ?

No, the way he did it will allow you to display how much time is remaining before the next use. Anything you store in ItemStack NBT is generally synchronized to the client side as well, so even if you used a non-ticking cooldown such as storing the next world time at which it can be used, you could technically still calculate the time remaining for display purposes using e.g. nextAvailableUseTime - currentWorldTime.

Posted

I hate to say it, but you seem to just be spamming code without understanding one bit what it is doing. I mean, this:

NBTTagCompound nbtTagCompound = new NBTTagCompound();
int coolDown = nbtTagCompound.getInteger("coolDown");
// what do you expect this value to be? I certainly hope you don't expect anything other than zero...
// how could it be otherwise with a NEW tag compound???

or this:

NBTTagCompound nbtTagCompound = new NBTTagCompound();
if(nbtTagCompound == null) {
  // dead code - how can it be null when you just initialized it??? it can't.
}

or this:

if (entity instanceof EntityMob){
    hasEffect(itemStack);
    break;
}

I mean come on... calling #hasEffect, which is a CLIENT-side only method that simply tells the renderer whether to render the enchanted glint or not... what the heck do you expect that code to even do?

 

Please, take some time and read your code. Really read it.

 

Then, understand that you don't just make new NBT tag compounds for the hell of it - you only do it when the ItemStack doesn't yet have one, and then you attach it to the ItemStack. From then on, you only get the tag from the ItemStack, you never make a new one. Look again at Ernio's code example and you will see what I'm talking about.

Posted

For

NBTTagCompound nbtTagCompound = new NBTTagCompound();

if(nbtTagCompound == null) {

  // dead code - how can it be null when you just initialized it??? it can't.

}

 

i was telling him that i is dead code that his version for 1.8 doesn't work for 1.7.10, that's why i said ernio.....

 

and i realize the nbt portion and im fiddling with it right now

Posted

ernio posted this

 

 

 

 

 

 

i am telling him that there is a dead line in it... as he said

"" This code will try to decrement cooldown to zero whenever it's positive (positive = item has cooldown).

It also has lazy initialization and will work like charm (mind I wrote it here and am on 1.8, so might made mistake).  ""

 

seeing that he just wrote it on here and not cheeking if it works via forge i know that there is something wrong so i am telling him about it.....

for the mysterious stone i wrote that maybe maybe 7 or 8 months ago i have no clue what i was thinking but it worked before i changed it to a metadata class so i was seeing if there was a reason it stopped working on me

Posted

i looked up who helped me and it was you who helped me with this.. http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/modification-development/1437156-i-need-help-with-a-iteminteractionforentity

@Override
    public boolean itemInteractionForEntity(ItemStack itemStack, EntityPlayer entityplayer, EntityLivingBase entity ){ 
        if(!entity.worldObj.isRemote){
            return false;
        }
        
        int coolDown = itemStack.stackTagCompound.getInteger("coolDown");
    	
        if(coolDown > 0){
        	return false;
        }
        if(coolDown == 0){
        	if (entity instanceof EntityMob){
                if(itemStack.getItemDamage() > 0){
                	entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY +"Soul Absurbed!"));
                	itemStack.damageItem(-1, entityplayer);
                    entity.setHealth(0.0F);
                    return true;
                }else{
                	entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "The  Magical Stone seems to be full?"));
                    entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "It seems to be useless now."));
                    entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "I wonder whats inside?"));
                    return true;
                }
            }else{
            	
                entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "Why has this stoped working?"));
            }
        }
        itemStack.stackTagCompound.setInteger("coolDown", 180);;
        return false;
    }

Posted

I doubt he is very concerned if the code doesn't compile as written - it was supposed to be an example to help you understand the basic logic behind ItemStack NBT-based cooldowns, not production-ready code.

 

Also, "dead code" in programming refers to code that will never be reached due to an impossible condition, not to a line with an error on it.

 

To fix your error, delete 'itemStack.stackTagCompound' then type in 'itemStack.' with the period and stop typing; you should get a list of options - scroll through those until you find the one that will give you the stack's tag compound, which should be pretty obvious when you see it.

Posted

your talking about this ?

 

if (!world.isRemote){

    NBTTagCompound nbtDataCompund = itemStack.stackTagCompound;

    if (nbtDataCompund == null)

    {

    itemStack.stackTagCompound = new NBTTagCompound();

    nbtDataCompund = itemStack.getTagCompound();

    }

Posted

your talking about this ?

if (!world.isRemote){
    	NBTTagCompound nbtDataCompund = itemStack.stackTagCompound; // is this not the line with the error?
    	if (nbtDataCompund == null)
    	{
    		itemStack.stackTagCompound = new NBTTagCompound();
    		nbtDataCompund = itemStack.getTagCompound();
    	}

You'll have to be more specific about what line you are getting an error on.

Posted

you told me to change itemStack.stackTagCompound to something that can go and get the stack compound

so i looked thow there was only stacktagcompound and

getstacktagcompound...

so on this line

nbtDataCompund = itemStack.stackTagCompound();

i changed it to

nbtDataCompund = itemStack.getTagCompound();

Posted

im still having the issue where the cooldown still isnt working

 

@Override
    public void onUpdate(ItemStack itemStack, World world, Entity entity, int i, boolean flag){
    	super.onUpdate(itemStack, world, entity, i, flag);
    	
    	if (!world.isRemote){
    		NBTTagCompound nbtDataCompund = itemStack.stackTagCompound;
    		if (nbtDataCompund == null)
    		{
    			itemStack.stackTagCompound = new NBTTagCompound();
    			nbtDataCompund = itemStack.getTagCompound();
    		}
    		
    		int coolDown = nbtDataCompund.getInteger("coolDown");
    		if(coolDown > 0){
    			--coolDown;
    			nbtDataCompund.setInteger("coolDown", coolDown);
        	}
    	}
    }

 

@Override
    public boolean itemInteractionForEntity(ItemStack itemStack, EntityPlayer entityplayer, EntityLivingBase entity ){ 
    	
        int coolDown = itemStack.stackTagCompound.getInteger("coolDown");
    	
        if(coolDown > 0){
        	return false;
        }
        if(coolDown == 0){
        	if (entity instanceof EntityMob){
                if(itemStack.getItemDamage() > 0){
                	entity.setHealth(0.0F);
                	if(!entity.worldObj.isRemote){
                		entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY +"Soul Absurbed!"));
                	}
                	itemStack.damageItem(-1, entityplayer);
                    return true;
                }else{
                	if(!entity.worldObj.isRemote){
                		entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "The  Magical Stone seems to be full?"));
                        entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "It seems to be useless now."));
                        entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "I wonder whats inside?"));
                    }
                    return true;
                }
            }else{
            	if(!entity.worldObj.isRemote){
            		entityplayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + "Why has this stoped working?"));
                }
            }
        }
        itemStack.stackTagCompound.setInteger("coolDown", 180);;
        return false;
    }

 

im not trying to copy past but for most people they know nothing about tagCompounds and id rather you yell as me and tell me what im doing wrong and learn it then trying to figure it out myself

Posted

Seriously? Do you think he is your personal mentor? Someone's gonna reply as soon as they know of a way to help you.

 

For the issue, please define 'isn't working'. Is it crashing? Can't you use the item at all? Please, be more specific.

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

Its not crashing as o sead  its  not working meaning its not changing anything after i changed

nbtDataCompund = itemStack.stackTagCompound();

to

nbtDataCompund = itemStack.getTagCompound();

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.