Jump to content

Item Nbt help


Mightydanp

Recommended Posts

i am having a bit of a issue. i made a nbt tag compound for my item. the problem is i tried to make it a cool down. The cooldown is always 0 for some reason even if i try to set it.

 

 

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

if (itemStack.hasTagCompound()) {

nbtDataCompund = itemStack.getTagCompound();

} else {

nbtDataCompund = new NBTTagCompound();

}

 

int coolDown = nbtDataCompund.getInteger("coolDown");

if (coolDown > 0) {

--coolDown;

nbtDataCompund.setInteger("coolDown", coolDown);

}

}

}

 

@Override

public boolean itemInteractionForEntity(ItemStack itemStack, EntityPlayer entityPlayer, EntityLivingBase entity, EnumHand hand) {

 

NBTTagCompound nbt;

if (itemStack.hasTagCompound()) {

nbt = itemStack.getTagCompound();

} else {

nbt = new NBTTagCompound();

}

int coolDown = nbt.getInteger("coolDown");

 

if (coolDown > 0) {

return false;

}

 

if (coolDown == 0) {

if (entity instanceof EntityMob) {

if (itemStack.getItemDamage() > 0) {

entity.setHealth(0.0F);

nbt.setInteger("coolDown", 180);

                                }

                          }

                }

        }

 

 

Link to comment
Share on other sites

i am having a bit of a issue. i made a nbt tag compound for my item. the problem is i tried to make it a cool down. The cooldown is always 0 for some reason even if i try to set it.

 

 

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

if (itemStack.hasTagCompound()) {

nbtDataCompund = itemStack.getTagCompound();

} else {

nbtDataCompund = new NBTTagCompound();

}

 

int coolDown = nbtDataCompund.getInteger("coolDown");

if (coolDown > 0) {

--coolDown;

nbtDataCompund.setInteger("coolDown", coolDown);

}

}

}

 

@Override

public boolean itemInteractionForEntity(ItemStack itemStack, EntityPlayer entityPlayer, EntityLivingBase entity, EnumHand hand) {

 

NBTTagCompound nbt;

if (itemStack.hasTagCompound()) {

nbt = itemStack.getTagCompound();

} else {

nbt = new NBTTagCompound();

}

int coolDown = nbt.getInteger("coolDown");

 

if (coolDown > 0) {

return false;

}

 

if (coolDown == 0) {

if (entity instanceof EntityMob) {

if (itemStack.getItemDamage() > 0) {

entity.setHealth(0.0F);

nbt.setInteger("coolDown", 180);

                                }

                          }

                }

        }

 

 

You never set it to be anything other than 0...

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

it is but here is the whole code after working on it a bit.

 

@Override

public boolean itemInteractionForEntity(ItemStack itemStack, EntityPlayer entityPlayer, EntityLivingBase entity, EnumHand hand) {

 

NBTTagCompound nbt;

if (itemStack.hasTagCompound()) {

nbt = itemStack.getTagCompound();

} else {

nbt = new NBTTagCompound();

}

int coolDown = nbt.getInteger("coolDown");

 

if (coolDown > 0) {

return false;

}

 

if (coolDown == 0) {

if (entity instanceof EntityMob) {

if (itemStack.getItemDamage() > 0) {

entity.setHealth(0.0F);

nbt.setInteger("coolDown", 180);

if (!entity.worldObj.isRemote) {

entityPlayer.addChatMessage(new TextComponentString(TextFormatting.GRAY + "Soul Absurbed!"));

}

itemStack.damageItem(-1, entityPlayer);

return true;

} else {

if (!entity.worldObj.isRemote) {

entityPlayer.addChatMessage(new TextComponentString(TextFormatting.GRAY + "The  Magical Stone seems to be full?"));

entityPlayer.addChatMessage(new TextComponentString(TextFormatting.GRAY + "It seems to be useless now."));

entityPlayer.addChatMessage(new TextComponentString(TextFormatting.GRAY + "I wonder whats inside?"));

}

return true;

}

} else if (!entity.worldObj.isRemote) {

entityPlayer.addChatMessage(new TextComponentString(TextFormatting.GRAY + "Why has this stoped working?"));

}

} else {

entityPlayer.addChatMessage(new TextComponentString(TextFormatting.GRAY + "Why has this stoped working?"));

}

return false;

}

 

 

ignore all the isRemote it is there so that the message doesn't get displayed twice

Link to comment
Share on other sites

Won't be the solution, but doing 'damageItem' with a value of -1 won't do anything, that function calls 'attemptDamageItem' which explicitly checks if the int passed in is above 0.

 

 

[edit] ...Ok wow I really overlooked something, you're not writing the altered NBT to the stack's NBT.  That...might be a problem :P

Link to comment
Share on other sites

not writing ?

 

This:

         if (itemStack.hasTagCompound()) {
            nbtDataCompund = itemStack.getTagCompound();
         } else {
            nbtDataCompund = new NBTTagCompound();
         }

 

If the stack doesn't have NBT, you create an NBT tag.  you never give the item stack this new NBT.  It exists only as a local variable and is discarded when the method returns.

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.

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.

Announcements



×
×
  • Create New...

Important Information

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