Jump to content

[1.18.2] save entities in item nbt


KaboomRoads

Recommended Posts

ok so i use this to save the entity:

ListTag listtag = compoundtag.getList("thingsandstuff.entities", 0);
        listtag.add(entity.saveWithoutId(compoundtag));
        compoundtag.put("thingsandstuff.entities", listtag);
        stack.setTag(compoundtag);
        entity.remove(Entity.RemovalReason.DISCARDED);

but how do i get the entity from the tag?

Edited by KaboomRoads
Link to comment
Share on other sites

this is how i save the entities now:

CompoundTag compoundtag = stack.getOrCreateTag();
        if (!compoundtag.contains("thingsandstuff.entities")) {
            compoundtag.put("thingsandstuff.entities", new ListTag());
        }
        ListTag listtag = compoundtag.getList("thingsandstuff.entities", 10);
        listtag.add(entity.saveWithoutId(compoundtag));
        compoundtag.put("thingsandstuff.entities", listtag);
        stack.setTag(compoundtag);
        entity.remove(Entity.RemovalReason.DISCARDED);

this is how i spawn them:

 CompoundTag compoundtag = stack.getOrCreateTag();
        if (!compoundtag.contains("thingsandstuff.entities"))
            return;
        ListTag listtag = compoundtag.getList("thingsandstuff.entities", 10);
        Stream<Entity> entities = EntityType.loadEntitiesRecursive(listtag, level);
        entities.forEach((e) -> e.setPos(pos));
        compoundtag.put("thingsandstuff.entities", new ListTag());
        stack.setTag(compoundtag);

it still doesnt seem to work.

Link to comment
Share on other sites

so i just changed some things like you said, and this is what i have right now:

public static void saveEntity(ItemStack stack, Entity entity) {
        if (stack == null)
            return;
        CompoundTag compoundtag = stack.getOrCreateTag();
        if (!compoundtag.contains("thingsandstuff.entities")) {
            compoundtag.put("thingsandstuff.entities", new ListTag());
        }
        ListTag listtag = compoundtag.getList("thingsandstuff.entities", 10);
        listtag.add(entity.saveWithoutId(compoundtag));
        compoundtag.put("thingsandstuff.entities", listtag);
        stack.setTag(compoundtag);
        entity.remove(Entity.RemovalReason.DISCARDED);
    }

    public static void releaseEntities(ItemStack stack, Vec3 pos, Level level) {
        if (stack == null)
            return;
        CompoundTag compoundtag = stack.getOrCreateTag();
        if (!compoundtag.contains("thingsandstuff.entities"))
            return;
        ListTag listtag = compoundtag.getList("thingsandstuff.entities", Tag.TAG_COMPOUND);
        Stream<Entity> entities = EntityType.loadEntitiesRecursive(listtag, level);
        entities.forEach((e) -> {
            e.teleportTo(pos.x, pos.y, pos.z);
            level.addFreshEntity(e);
        });
        compoundtag.put("thingsandstuff.entities", new ListTag());
        stack.setTag(compoundtag);
    }

it still doesnt spawn the entities though.

i dont really understand what you meant by it not compiling, it works fine for me.

Link to comment
Share on other sites

i found what seems to be the problem, but cant find the thing causing it. its this check right here that stops the code after from running;

if (!compoundtag.contains("thingsandstuff.entities"))
            return;

 so the compound tag doesnt contain "thingsandstuff.entities" event though im setting the tag in the method saveEntity.

Link to comment
Share on other sites

Is your saveEntity function called?

If you enable the debug data in game, can you see the tag on the itemstack (F3 + H I think allows you to view NBT by holding ctrl maybe? If not, there is always the data command)

Edited by Alpvax
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.