-
[1.19.2 SOLVED] NBT Data isn't staying on ItemStack
Ok I solved the problem, but I still don't know why the previous code doesn't work. (And bc i know how awful it is to find a forum post that solved the problem and gave no solution I'm going to give my solution) I had a problem in the previous code, where clicking an entity that had other clicking code (such as a horse or villager), the entities code would activate first, which isn't what I wanted. So instead, I used EntityInteract and subscribed the event to the event bus. For whatever reason, this allowed the tags to save. // Inside of MainItem Class // Subscribe to the player event for interacting with living entities and see if tags will save there @SubscribeEvent(priority = EventPriority.HIGHEST) public static void EntityInteract(PlayerInteractEvent.EntityInteract event) { var stack = event.getItemStack(); if (stack.getItem() instanceof MainItem && event.getTarget() instanceof LivingEntity){ var entityNameTag = new CompoundTag(); entityNameTag.putString("held_entity", event.getTarget().getEncodeID()); event.getItemStack().setTag(entityNameTag); if (event.hasResult()) { event.setResult(Result.ALLOW); // my theory is that THIS is what allows the tags to get set, but i have zero idea otherwise } } } //Inside of my Mod File public Mod(){ // ... MinecraftForge.EVENT_BUS.register(MainItem.class); // ... }
-
SlimedLeviathan started following [1.19.2 SOLVED] NBT Data isn't staying on ItemStack
- [1.19.2 SOLVED] NBT Data isn't staying on ItemStack
-
SlimedLeviathan joined the community
-
[1.19.2 SOLVED] NBT Data isn't staying on ItemStack
I've searched everywhere for someone to have the same problem as me with this, but I really can't seem to figure out why this is happening. Every time I try to set a tag to my itemstack, it doesn't save, but instead only saves when I use the onUse function. The weird thing is that it shows that it saves inside of interactLivingEntity, but after the function ends nothing changes about the ItemStack (as shown with the isFoil method, and with seeing advanced info on the itemstack in game) I've tried to change the way i save the data, by using ItemStack#addTagElement, changing the return value if that were to change anything, but I really don't know why it isn't working. If anyone could help me this documentation is super annoying and im probably missing something. I could post the main file if needed, but its basically just an exampleMod with this item in it Github Link: https://github.com/SlimedLeviathan/CaptureCraft Thank you for any help. package github.io.slimedleviathan; import com.mojang.logging.LogUtils; import java.util.List; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity.RemovalReason; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.MobSpawnType; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraftforge.registries.ForgeRegistries; // so this is the public class MainItem extends Item{ public static String HELD_ENTITY_KEY = "held_entity"; public static final Logger LOGGER = LogUtils.getLogger(); public MainItem(Item.Properties itemProperties) { super(itemProperties.stacksTo(1)); // always no matter what stack to only 1 } // function that allows the player to place a mob @Override public InteractionResult useOn(UseOnContext context) { if (context.getLevel().getClass() == ServerLevel.class) { var item = context.getItemInHand(); CompoundTag itemData = new CompoundTag(); // this will be placed onto the item at the end of this if statement itemData.putString(HELD_ENTITY_KEY, "minecraft:sheep"); item.setTag(itemData); // for whatever reason, this one will work } return InteractionResult.PASS; } // a function that allows the user to pick up living entities @Override public InteractionResult interactLivingEntity(ItemStack stack, Player player, LivingEntity entity, InteractionHand hand) { if (entity.getLevel().getClass() == ServerLevel.class) { CompoundTag entityNameTag = stack.getOrCreateTag(); entityNameTag.putString(HELD_ENTITY_KEY, entity.getEncodeId()); stack.setTag(entityNameTag); LOGGER.info(stack.getTag().getAsString()); // shows that the data is both inside of the itemstack and inside the tag we just put in } return InteractionResult.PASS; } // this just makes the item look like its enchanted when it has the nbt data we want in it @Override public boolean isFoil(ItemStack stack) { return stack.hasTag() && stack.getTag().contains(HELD_ENTITY_KEY) && !stack.getTag().get(HELD_ENTITY_KEY).getAsString().isBlank(); } }
IPS spam blocked by CleanTalk.