Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted
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 ! 

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...

 

  • Author
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 !

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.