Jump to content

How to set the LootModifier Set Item NBT?


Spring

Recommended Posts

public class AddItemModifier extends LootModifier {

    private final Item item;

    private final CompoundTag compoundTag;

    public static final Supplier<Codec<AddItemModifier>> CODEC =
            Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst)
                    .and(ForgeRegistries.ITEMS.getCodec().fieldOf("item").forGetter(m -> m.item))
                   //I didn't find anything about CompoundTag in ForgeRegistries?  ForgeRegistries.BIOMES be replaced with?                 
                    .and(ForgeRegistries.BIOMES.getCodec().fieldOf("nbt").forGetter(m -> m.compoundTag))
                    .apply(inst, AddItemModifier::new)));

    protected AddItemModifier(LootItemCondition[] conditionsIn, Item item, CompoundTag compoundTag) {
        super(conditionsIn);
        this.item = item;
        this.compoundTag = compoundTag;
    }

    @Override
    protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot,
                                                          LootContext context) {
        if (context.getRandom().nextFloat() >= 0.4f) {
            ItemStack out = new ItemStack(item);
            if (compoundTag != null) {
                out.setTag(compoundTag);
            }
            generatedLoot.add(new ItemStack(item));
        }
        return generatedLoot;
    }

    @Override
    public Codec<? extends IGlobalLootModifier> codec() {
        return CODEC.get();
    }
}

 

loot_modifiers folder

{
  "type": "chinacraft:add_item",
  "conditions": [
    {
      "condition": "forge:loot_table_id",
      "loot_table_id": "minecraft:chests/nether_bridge"
    }
  ],
  "item": "patchouli:guide_book",
  "nbt": "{\"patchouli:book\":\"chinacraft:mencius\"}"
}

thank you ! 

Link to comment
Share on other sites

hmm...

4 hours ago, Spring said:
            ItemStack out = new ItemStack(item);
            if (compoundTag != null) {
                out.setTag(compoundTag);
            }
            generatedLoot.add(new ItemStack(item));

so make a stack, okay, set the tag, okay (i'll just assume you tried that), then disregard the stack you made, make a new one without tag and put in into drops...

might work...

 

Link to comment
Share on other sites

On 12/13/2022 at 4:16 PM, MFMods said:

hmm...

so make a stack, okay, set the tag, okay (i'll just assume you tried that), then disregard the stack you made, make a new one without tag and put in into drops...

might work...

 

 

On 12/13/2022 at 11:50 AM, Spring said:
.and(ForgeRegistries.BIOMES.getCodec().fieldOf("nbt").forGetter(m -> m.compoundTag))

Maybe I didn't express my problem well.

But now it has been solved. This line of code is compiled incorrectly.

 

But I did。

public class AddItemTagModifier extends LootModifier {

    public static final Codec<CompoundTag> TAG_CODEC = Codec.PASSTHROUGH.comapFlatMap((convert) -> {
        Tag tag = convert.convert(NbtOps.INSTANCE).getValue();
        String replace = tag.toString().replace("'", "");
        CompoundTag compoundtag = null;
        try {
            compoundtag = TagParser.parseTag(replace);
        } catch (CommandSyntaxException e) {
            e.printStackTrace();
        }
        return DataResult.success(compoundtag);
    }, (value) -> new Dynamic<>(NbtOps.INSTANCE, value));

    private final Item item;

    private final CompoundTag compoundTag;

    public static final Supplier<Codec<AddItemTagModifier>> CODEC =
            Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst)
                    .and(ForgeRegistries.ITEMS.getCodec().fieldOf("item").forGetter(m -> m.item))
                    .and(TAG_CODEC.fieldOf("nbt").forGetter(m -> m.compoundTag))
                    .apply(inst, AddItemTagModifier::new)));

    protected AddItemTagModifier(LootItemCondition[] conditionsIn, Item item, CompoundTag compoundTag) {
        super(conditionsIn);
        this.item = item;
        this.compoundTag = compoundTag;
    }

    @Override
    protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot,
                                                          LootContext context) {
        if (context.getRandom().nextFloat() >= 0.4f) {
            ItemStack out = new ItemStack(item);
            if (compoundTag != null) {
                out.setTag(compoundTag);
            }
            generatedLoot.add(out);
        }
        return generatedLoot;
    }

    @Override
    public Codec<? extends IGlobalLootModifier> codec() {
        return CODEC.get();
    }

It works very well !

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.

Announcements



×
×
  • Create New...

Important Information

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