2021-08-24 21:15:43 +02:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
2021-08-24 21:14:18 +02:00
|
|
|
|
2021-08-24 22:33:11 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.UTags;
|
2021-11-05 14:18:32 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
|
2023-02-27 16:21:07 +01:00
|
|
|
import com.minelittlepony.unicopia.entity.effect.UPotions;
|
2023-02-28 17:44:14 +01:00
|
|
|
import com.minelittlepony.unicopia.item.EnchantableItem;
|
2021-08-24 21:14:18 +02:00
|
|
|
import com.minelittlepony.unicopia.item.UItems;
|
2022-12-19 00:12:49 +01:00
|
|
|
import com.minelittlepony.unicopia.util.RegistryUtils;
|
2022-03-26 20:34:15 +01:00
|
|
|
|
2021-08-24 21:14:18 +02:00
|
|
|
import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper;
|
2021-08-24 22:33:11 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.item.Item;
|
2021-08-24 21:14:18 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.Items;
|
2023-02-27 16:21:07 +01:00
|
|
|
import net.minecraft.potion.PotionUtil;
|
2022-12-18 22:07:24 +01:00
|
|
|
import net.minecraft.registry.tag.ItemTags;
|
|
|
|
import net.minecraft.registry.tag.TagKey;
|
2021-08-24 22:33:11 +02:00
|
|
|
import net.minecraft.util.Util;
|
2022-06-25 00:19:55 +02:00
|
|
|
import net.minecraft.util.math.random.Random;
|
2021-08-24 21:14:18 +02:00
|
|
|
import net.minecraft.village.TradeOffer;
|
2021-08-24 22:33:11 +02:00
|
|
|
import net.minecraft.village.TradeOffers;
|
2021-08-24 21:14:18 +02:00
|
|
|
import net.minecraft.village.VillagerProfession;
|
2023-02-27 16:21:07 +01:00
|
|
|
import net.minecraft.village.TradeOffers.Factory;
|
2021-08-24 21:14:18 +02:00
|
|
|
|
|
|
|
public interface UTradeOffers {
|
|
|
|
static void bootstrap() {
|
|
|
|
TradeOfferHelper.registerVillagerOffers(VillagerProfession.MASON, 1, factories -> {
|
2021-08-24 22:33:11 +02:00
|
|
|
factories.add(buyForEmeralds(UItems.GEMSTONE, 1, 1, 30, 2, 0.05F));
|
2021-08-24 21:14:18 +02:00
|
|
|
});
|
|
|
|
TradeOfferHelper.registerVillagerOffers(VillagerProfession.LIBRARIAN, 1, factories -> {
|
2022-09-26 13:43:35 +02:00
|
|
|
factories.add(buyForEmeralds(UItems.GEMSTONE, 2, 1, 2, 1, 0.05F));
|
|
|
|
factories.add(buyForEmeralds(UItems.PEGASUS_FEATHER, 7, 5, 50, 3, 0.15F));
|
|
|
|
factories.add(buyForEmeralds(UItems.GRYPHON_FEATHER, 4, 3, 50, 2, 0.12F));
|
2021-08-24 21:14:18 +02:00
|
|
|
});
|
|
|
|
TradeOfferHelper.registerVillagerOffers(VillagerProfession.CARTOGRAPHER, 1, factories -> {
|
2021-08-24 22:33:11 +02:00
|
|
|
factories.add(buyForEmeralds(UItems.GEMSTONE, 3, 1, 20, 1, 0.05F));
|
2021-08-24 21:14:18 +02:00
|
|
|
});
|
2022-09-26 13:43:35 +02:00
|
|
|
TradeOfferHelper.registerVillagerOffers(VillagerProfession.FARMER, 2, factories -> {
|
|
|
|
factories.add(buy(Items.EMERALD, 4, UTags.APPLE_SEEDS, 2, 20, 1, 0.05F));
|
2023-05-21 18:37:38 +02:00
|
|
|
factories.add(buy(Items.EMERALD, 8, UItems.MANGO, 1, 15, 1, 0.025F));
|
2022-09-26 13:43:35 +02:00
|
|
|
});
|
2021-08-24 21:44:52 +02:00
|
|
|
|
|
|
|
TradeOfferHelper.registerWanderingTraderOffers(1, factories -> {
|
2021-08-24 22:33:11 +02:00
|
|
|
factories.add(buyTiered(UItems.GEMSTONE, 30, UItems.GOLDEN_FEATHER, 1, UItems.GOLDEN_WING, 1, 30, 2, 0.05F));
|
2023-02-28 17:44:14 +01:00
|
|
|
factories.add((e, rng) -> new TradeOffer(new ItemStack(UItems.GEMSTONE, 3), EnchantableItem.enchant(UItems.GEMSTONE.getDefaultStack(), SpellType.REGISTRY.getRandom(rng).get().value()), 20, 1, 0.05F));
|
2021-08-24 22:33:11 +02:00
|
|
|
factories.add(buy(UItems.GEMSTONE, 20, UItems.HAY_FRIES, 5, 50, 3, 0.06F));
|
|
|
|
factories.add(buy(Items.WHEAT, 17, UItems.HAY_BURGER, 1, 10, 6, 0.08F));
|
|
|
|
factories.add(buy(ItemTags.SMALL_FLOWERS, 2, UItems.DAFFODIL_DAISY_SANDWICH, 1, 10, 6, 0.08F));
|
|
|
|
factories.add(buy(UItems.ZAP_APPLE, 45, UItems.ZAP_APPLE_JAM_JAR, 5, 50, 3, 0.07F));
|
|
|
|
factories.add(buy(UItems.CIDER, 1, UItems.FRIENDSHIP_BRACELET, 1, 6, 1, 0.05F));
|
2021-08-24 22:33:41 +02:00
|
|
|
factories.add(buy(UItems.GEMSTONE, 5, UTags.FRESH_APPLES, 2, 12, 3, 0.05F));
|
2023-05-21 18:37:38 +02:00
|
|
|
factories.add(buy(Items.EMERALD, 4, UItems.MANGO, 1, 35, 1, 0.025F));
|
2021-08-24 22:33:11 +02:00
|
|
|
factories.add(new JarredItemTradeOfferFactory());
|
2021-08-24 21:44:52 +02:00
|
|
|
});
|
2021-08-24 22:33:11 +02:00
|
|
|
TradeOfferHelper.registerWanderingTraderOffers(2, factories -> {
|
|
|
|
factories.add(buy(UItems.COOKED_ZAP_APPLE, 45, UItems.ZAP_APPLE_JAM_JAR, 15, 50, 7, 0.17F));
|
|
|
|
factories.add(buy(UItems.CRYSTAL_HEART, 1, UItems.MUSIC_DISC_CRUSADE, 1, 10, 6, 0.08F));
|
|
|
|
factories.add(buy(UItems.PEGASUS_AMULET, 1, UItems.ALICORN_AMULET, 1, 2, 6, 0.05F));
|
2022-09-26 13:43:35 +02:00
|
|
|
factories.add(buyForEmeralds(UItems.FRIENDSHIP_BRACELET, 2, 1, 10, 7, 0.17F));
|
2023-02-27 16:21:07 +01:00
|
|
|
factories.add(new SellPotionHoldingItemFactory(
|
|
|
|
new Item[] { Items.ARROW, Items.GLASS_BOTTLE, Items.GLASS_BOTTLE },
|
|
|
|
new Item[] { Items.TIPPED_ARROW, Items.POTION, Items.SPLASH_POTION },
|
|
|
|
5, 5, 2, 12, 30));
|
2022-09-26 13:43:35 +02:00
|
|
|
});
|
2021-08-24 22:33:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static TradeOffers.Factory buyForEmeralds(Item item, int count, int returnCount, int maxUses, int experience, float priceChange) {
|
|
|
|
return buy(item, count, Items.EMERALD, returnCount, maxUses, experience, priceChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static TradeOffers.Factory buy(Item item, int count, Item returnItem, int returnCount, int maxUses, int experience, float priceChange) {
|
|
|
|
return (e, rng) -> new TradeOffer(new ItemStack(item, count), new ItemStack(returnItem, returnCount), maxUses, experience, priceChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static TradeOffers.Factory buyTiered(Item item, int count, Item intermediate, int intermediatCount, Item returnItem, int returnCount, int maxUses, int experience, float priceChange) {
|
|
|
|
return (e, rng) -> new TradeOffer(new ItemStack(item, count), new ItemStack(intermediate, intermediatCount), new ItemStack(returnItem, returnCount), maxUses, experience, priceChange);
|
|
|
|
}
|
|
|
|
|
2022-03-26 20:34:15 +01:00
|
|
|
private static TradeOffers.Factory buy(TagKey<Item> item, int count, Item returnItem, int returnCount, int maxUses, int experience, float priceChange) {
|
|
|
|
return (e, rng) -> new TradeOffer(new ItemStack(random(e, item, rng), count), new ItemStack(returnItem, returnCount), maxUses, experience, priceChange);
|
2021-08-24 22:33:11 +02:00
|
|
|
}
|
|
|
|
|
2022-03-26 20:34:15 +01:00
|
|
|
private static TradeOffers.Factory buy(Item item, int count, TagKey<Item> returnItem, int returnCount, int maxUses, int experience, float priceChange) {
|
|
|
|
return (e, rng) -> new TradeOffer(new ItemStack(item, count), new ItemStack(random(e, returnItem, rng), returnCount), maxUses, experience, priceChange);
|
2021-08-24 22:33:11 +02:00
|
|
|
}
|
|
|
|
|
2022-03-26 20:34:15 +01:00
|
|
|
private static Item random(Entity e, TagKey<Item> item, Random rng) {
|
2023-06-03 13:40:54 +02:00
|
|
|
return RegistryUtils.entriesForTag(e.getWorld(), item).getRandom(rng).get().value();
|
2021-08-24 22:33:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static class JarredItemTradeOfferFactory implements TradeOffers.Factory {
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public TradeOffer create(Entity entity, Random rng) {
|
|
|
|
TradeOffers.Factory factory = Util.getRandom(TradeOffers.WANDERING_TRADER_TRADES.get(1), rng);
|
|
|
|
|
|
|
|
if (factory == this) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
TradeOffer offer = factory.create(entity, rng);
|
|
|
|
|
|
|
|
return new TradeOffer(offer.getOriginalFirstBuyItem(), offer.getSecondBuyItem(), UItems.FILLED_JAR.withContents(offer.getSellItem()), offer.getUses(), offer.getMaxUses(), offer.getMerchantExperience(), offer.getPriceMultiplier(), offer.getDemandBonus());
|
|
|
|
}
|
2021-08-24 21:14:18 +02:00
|
|
|
}
|
2023-02-27 16:21:07 +01:00
|
|
|
|
|
|
|
record SellPotionHoldingItemFactory (Item[] secondBuy, Item[] tippedArrow, int secondCount, int sellCount, int price, int maxUses, int experience) implements Factory {
|
|
|
|
@Override
|
|
|
|
public TradeOffer create(Entity entity, Random random) {
|
|
|
|
int index = random.nextInt(tippedArrow.length);
|
|
|
|
return new TradeOffer(
|
|
|
|
new ItemStack(Items.EMERALD, price),
|
|
|
|
new ItemStack(secondBuy[index], secondCount),
|
|
|
|
PotionUtil.setPotion(new ItemStack(tippedArrow[index], sellCount), UPotions.REGISTRY.get(random.nextInt(UPotions.REGISTRY.size()))),
|
|
|
|
maxUses, experience, 0.05f
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 21:14:18 +02:00
|
|
|
}
|