Jump to content

[1.19] How can I apply a LootItemFunctionType in a GlobalLootModifier?


nu11une

Recommended Posts

Specifically, I'm trying to add my custom enchantment to a loot table with the setup below.

public class AncientCityLootModifier extends LootModifier {
    private final Item addition;

    protected AncientCityLootModifier(LootItemCondition[] conditionsIn, Item addition) {
        super(conditionsIn);
        this.addition = addition;
    }

    @Nonnull
    @Override
    protected @NotNull ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
        int x = 0;
        int y = 0;
        while(x < 4){
            if(context.getRandom().nextFloat() < 0.08F){
                generatedLoot.add(new ItemStack(ModItems.SCULK_SOUL.get(), 1));
            }
            x += 1;
        }
        if(context.getRandom().nextFloat() < 0.06F){
            generatedLoot.add(new ItemStack(ModItems.SCULK_INGOT.get(), 1));
        }
        while(y < 3){
            if(context.getRandom().nextFloat() < 0.03F){
                generatedLoot.add(new ItemStack(addition, 1));
            }
            y += 1;
        }
        return generatedLoot;
    }

    public static class Serializer extends GlobalLootModifierSerializer<AncientCityLootModifier> {

        @Override
        public AncientCityLootModifier read(ResourceLocation name, JsonObject object,
                                       LootItemCondition[] conditionsIn) {
            Item addition = ForgeRegistries.ITEMS.getValue(
                    new ResourceLocation(GsonHelper.getAsString(object, "addition")));
            return new AncientCityLootModifier(conditionsIn, addition);
        }

        @Override
        public JsonObject write(AncientCityLootModifier instance) {
            JsonObject json = makeConditions(instance.conditions);
            json.addProperty("addition", ForgeRegistries.ITEMS.getKey(instance.addition).toString());
            return json;
        }
    }
}

 

Link to comment
Share on other sites

31 minutes ago, diesieben07 said:

Deserialize it from JSON and then call its apply method.

Is there an example of how I would do that? I'm not sure how json serialization works, I've just been following this tutorial

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