Posted July 28, 20223 yr I am making a mod where when you kill a mob you get its hp into soulpower, and I am trying to get it to be stored in a nbt tag on the enchanted weapon, everything works except the nbt Tag. I have tried with setTags but that gets rid of the enchantment. Spoiler package com.pizzacat.soulmod.enchantment; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.EnchantmentCategory; import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @Mod.EventBusSubscriber public class SoulStealEnchantment extends Enchantment { protected SoulStealEnchantment(Rarity pRarity, EnchantmentCategory pCategory, EquipmentSlot... pSlots) { super(pRarity, pCategory, pSlots); } static float soulpower; static float sp; public static CompoundTag storeSoulPower() { soulpower += sp; if(soulpower > 1000){ soulpower = 1000; } CompoundTag nbtData = new CompoundTag(); nbtData.putString(String.valueOf(soulpower), "/1000"); return nbtData; } @SubscribeEvent public static void onPlayerKill(LivingDeathEvent entity){ entity.getEntity().getLevel(); if(entity.getEntity().level.isClientSide()){ boolean playerKill = SoulStealEvent.playerKill; int level = SoulStealEvent.level; if(level == 1 && playerKill == true) { sp = entity.getEntity().getMaxHealth(); storeSoulPower(); sp = 0; } } } @Override public int getMaxLevel() { return 1; } } Spoiler package com.pizzacat.soulmod.enchantment; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.event.entity.living.LivingEquipmentChangeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @Mod.EventBusSubscriber public class SoulStealEvent { static boolean playerKill; static int level; static ItemStack soulStealWeapon; @SubscribeEvent public static void onLivingDeath(LivingDeathEvent entity){ entity.getEntity().getLevel(); DamageSource source = entity.getSource(); if(source.getEntity() instanceof Player) { playerKill = true; } } @SubscribeEvent public static void onEquipChange(LivingEquipmentChangeEvent equipment){ ItemStack mainHandItem = equipment.getEntity().getMainHandItem(); level = EnchantmentHelper.getItemEnchantmentLevel(ModEnchantments.SOUL_STEAL.get(), mainHandItem); if(level == 1){ soulStealWeapon = mainHandItem; } } }
July 28, 20223 yr You can not store data like this in your Enchantment and Event class. If you want to store the data you need to use CompoundTag or a Capability (last one is recommended).
July 28, 20223 yr Author 4 hours ago, Luis_ST said: You can not store data like this in your Enchantment and Event class. If you want to store the data you need to use CompoundTag or a Capability (last one is recommended). How do I do this?, I am very new to modding in minecraft, and aren't I usign compoundTag or do I need to do it in another class?
July 28, 20223 yr 2 hours ago, JoachimTa said: How do I do this?, I am very new to modding in minecraft, and aren't I usign compoundTag or do I need to do it in another class? Yeah but not how you should do it, but i would highly recommend you to use a Capability. CompoundTag should be used to store and load data and not on runtime. Edited July 28, 20223 yr by Luis_ST
July 28, 20223 yr Author 9 minutes ago, Luis_ST said: Yeah but not how you should do it, but i would highly recommend you to use a Capability. CompoundTag should be used to store and load data and not on runtime. Yeah, but I am trying to make so that it saves the entitys hp on the sword, so you can see it, should I not use nbt tags for it or?
July 28, 20223 yr Author 1 minute ago, Luis_ST said: Where do you want to display the hp of the Entity? I want it displayed as a tooltip.. I dont need tags then right?
July 28, 20223 yr Author 8 minutes ago, Luis_ST said: Do you want to display the tooltip for all Items or only for your Mods Items? So when I kill a mob, its hp becomes a value i call soulpower, and that value is gonna be displayed on the weapon I killed it with that has the soul steal enchantment. Its gonna be displayed when I hover over my mouse on the sword in my inventory. All items with the soul steal enchantment is gonna have its own separate value based on how many mobs it has killed Edited July 28, 20223 yr by JoachimTa
July 28, 20223 yr I've split your purpose in two parts: Adding the CompoundTag First of all call ItemStack#getOrCreateTagElement with your Mod id, then add to the CompoundTag add data you want to add In your case CompoundTag#putInt the first parameter is the key the second the value Add the CompoundTag to the Tooltip of the Item Subscribe to ItemTooltipEvent, from the Event you can get the ItemStack, check if the ItemStack has your Enchantment and check if the ItemStack has your Tag using a non ull check on ItemStack#getTagElement with your Mod id. Then read the value from the CompoundTag via #getInt with the key you used in #putInt. After that create a new Component via Component#literal with your value and add this Component to the tooltip list of the Event
July 29, 20223 yr Author Sorry I dont understand, What does # mean, and how do I call ItemStack with getOrCreateTagElement. Edited July 29, 20223 yr by JoachimTa
July 29, 20223 yr Author Spoiler public static CompoundTag soulStealCompoundTag(){ ItemStack stack; new ItemStack().getOrCreateTag(soulmod.MODID); } Did you mean something like this?
July 29, 20223 yr Author Also should I make another class for this? Right now I have put it in soulStealEvent.java
July 29, 20223 yr Author 27 minutes ago, JoachimTa said: Reveal hidden contents public static CompoundTag soulStealCompoundTag(){ ItemStack stack; new ItemStack().getOrCreateTag(soulmod.MODID); } Did you mean something like this? Spoiler public static CompoundTag soulStealCompoundTag(){ CompoundTag tag = ItemStack.of(new CompoundTag()).getOrCreateTagElement(soulmod.MODID); tag.putFloat(soulmod.MODID, soulpower); return null; } This is what I have done, I don't know if this was what you meant
July 29, 20223 yr 4 hours ago, JoachimTa said: Sorry I dont understand, What does # mean, and how do I call ItemStack with getOrCreateTagElement. The # is a sign that the following "name" is a non-static method or non-static field in the class, which you need to call. 3 hours ago, JoachimTa said: CompoundTag tag = ItemStack.of(new CompoundTag()).getOrCreateTagElement(soulmod.MODID); Why did you create there a new ItemStack with an Empty CompoundTag? You should use the ItemStack on which you checked if your enchantment is preset (level > 0). Do you know basic java?
July 29, 20223 yr Author 2 minutes ago, Luis_ST said: The # is a sign that the following "name" is a non-static method or non-static field in the class, which you need to call. Why did you create there a new ItemStack with an Empty CompoundTag? You should use the ItemStack on which you checked if your enchantment is preset (level > 0). Do you know basic java? Yes, but I dont know how this ItemStack thing works, so i dont know what to put in the parantheses
July 29, 20223 yr 4 minutes ago, JoachimTa said: Yes, but I dont know how this ItemStack thing works, so i dont know what to put in the parantheses Do not create a new ItemStack add the Tag to the ItemStack which is used to damage the Entity
July 29, 20223 yr Author Yes I did that, Thank you! Whats the next step? Do I make a new compound tag, btw the soulpower is a float value. Edited July 29, 20223 yr by JoachimTa
July 29, 20223 yr 14 hours ago, Luis_ST said: Adding the CompoundTag First of all call ItemStack#getOrCreateTagElement with your Mod id, then add to the CompoundTag add data you want to add In your case CompoundTag#putFloat the first parameter is the key the second the value If you done step 1 you can continue with step 2: 14 hours ago, Luis_ST said: Add the CompoundTag to the Tooltip of the Item Subscribe to ItemTooltipEvent, from the Event you can get the ItemStack, check if the ItemStack has your Enchantment and check if the ItemStack has your Tag using a non ull check on ItemStack#getTagElement with your Mod id. Then read the value from the CompoundTag via #getFloat with the key you used in #putFloat. After that create a new Component via Component#literal with your value and add this Component to the tooltip list of the Event
July 30, 20223 yr Author I dont really understand, what do I add. Can you be a little bit more descriptive? Edited July 30, 20223 yr by JoachimTa
July 30, 20223 yr Author this is what I understood, the Item gets checked for my enchantment before arriving so it only needs to be checked for the tag here. I dont understand the component.literal part? Spoiler @SubscribeEvent public static ItemTooltipEvent tooltip(){ if (soulStealWeapon.getTagElement(soulmod.MODID) != null) { soulStealCompoundTag().getFloat(soulmod.MODID); Component.literal(soulpower?) } } Edited July 30, 20223 yr by JoachimTa
July 30, 20223 yr Author Update: is this what you wanted? Spoiler @SubscribeEvent public static ItemTooltipEvent tooltip(){ if (soulStealWeapon.getTagElement(soulmod.MODID) != null) { soulStealCompoundTag().getFloat(soulmod.MODID); Component component = new Component() { @Override public Style getStyle() { return null; } @Override public ComponentContents getContents() { return null; } @Override public List<Component> getSiblings() { return null; } @Override public FormattedCharSequence getVisualOrderText() { return null; } }; } return null; }
July 30, 20223 yr Author 1 minute ago, diesieben07 said: What does this method do? Do not implement Component manually, use the factory methods in the Component interface. That is my compound tag method
July 30, 20223 yr Quote @SubscribeEvent public static ItemTooltipEvent tooltip(){ This makes no sense. Event handlers are consumers of events (in their parameters), they don't create/supply them. Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
July 30, 20223 yr Author 1 minute ago, diesieben07 said: No shit... Show it. We have no idea what it does. Spoiler public static CompoundTag soulStealCompoundTag(){ CompoundTag tag = ItemStack.of(new CompoundTag()).getOrCreateTagElement(soulmod.MODID); tag.putFloat(soulmod.MODID, soulpower); return null; }
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.