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

6 minutes ago, KaboomRoads said:
compoundtag.getList("thingsandstuff.entities", 0);

This makes no sense. You need to pass in the type ID of the tag you want the list to contain. Use the constants in the Tag interface.

You can use EntityType.loadEntityRecursive to recreate the entity.

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

Just now, KaboomRoads said:
compoundtag.getList("thingsandstuff.entities", 10);

Use the constants as I mentioned, do not hardcode magic numbers like this.

1 minute ago, KaboomRoads said:
EntityType.loadEntitiesRecursive(listtag, level);

This won't even compile...?

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

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