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