Jump to content

1.19 I need help getting my tags to work


JoachimTa

Recommended Posts

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

}

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Luis_ST
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by JoachimTa
Link to comment
Share on other sites

I've split your purpose in two parts:

  1. 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
  2. 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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by JoachimTa
Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

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.



×
×
  • Create New...

Important Information

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