From 9fcf9447e43ac5ed70168464d26a17bb137de30e Mon Sep 17 00:00:00 2001 From: Ayutac Date: Sun, 23 Jan 2022 19:35:26 +0100 Subject: [PATCH] Added Rubber Sapling to wandering trader (#2772) --- .../reborncore/common/util/TradeUtils.java | 42 +++++++++++++++++++ .../java/techreborn/events/ModRegistry.java | 23 ++++++++++ 2 files changed, 65 insertions(+) create mode 100644 RebornCore/src/main/java/reborncore/common/util/TradeUtils.java diff --git a/RebornCore/src/main/java/reborncore/common/util/TradeUtils.java b/RebornCore/src/main/java/reborncore/common/util/TradeUtils.java new file mode 100644 index 000000000..9f6b40c2a --- /dev/null +++ b/RebornCore/src/main/java/reborncore/common/util/TradeUtils.java @@ -0,0 +1,42 @@ +package reborncore.common.util; + +import net.minecraft.entity.Entity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemConvertible; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.village.TradeOffer; +import net.minecraft.village.TradeOffers; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.Nullable; + +import java.util.Random; + +public class TradeUtils { + + private TradeUtils() {/* No instantiation.*/} + + public static TradeOffer create(ItemConvertible item, int price, int count, int maxUses, int experience) { + return new TradeOffer(new ItemStack(Items.EMERALD, price), new ItemStack(item, count), maxUses, experience, 0.05F); + } + + @Contract("null -> null; !null -> new") + public static TradeOffer copy(TradeOffer tradeOffer) { + if (tradeOffer == null) + return null; + return new TradeOffer(tradeOffer.toNbt()); + } + + public static TradeOffers.Factory asFactory(TradeOffer tradeOffer) { + return new TradeOffers.Factory() { + private final TradeOffer offer = copy(tradeOffer); + + @Nullable + @Override + public TradeOffer create(Entity entity, Random random) { + return copy(offer); + } + }; + } + +} diff --git a/src/main/java/techreborn/events/ModRegistry.java b/src/main/java/techreborn/events/ModRegistry.java index 0db05814c..647b06eb6 100644 --- a/src/main/java/techreborn/events/ModRegistry.java +++ b/src/main/java/techreborn/events/ModRegistry.java @@ -25,6 +25,7 @@ package techreborn.events; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; +import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper; import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage; import net.minecraft.block.*; import net.minecraft.entity.EquipmentSlot; @@ -32,8 +33,11 @@ import net.minecraft.item.Item; import net.minecraft.item.Item.Settings; import net.minecraft.item.Items; import net.minecraft.sound.BlockSoundGroup; +import net.minecraft.village.TradeOffer; +import net.minecraft.village.TradeOffers; import reborncore.RebornRegistry; import reborncore.common.powerSystem.RcEnergyTier; +import reborncore.common.util.TradeUtils; import team.reborn.energy.api.EnergyStorage; import techreborn.TechReborn; import techreborn.blockentity.cable.CableBlockEntity; @@ -56,6 +60,10 @@ import techreborn.items.tool.vanilla.*; import techreborn.utils.InitUtils; import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; /** * @author drcrazy @@ -69,6 +77,7 @@ public class ModRegistry { registerFluids(); registerSounds(); registerApis(); + registerWanderingTraderTrades(); } private static void registerBlocks() { @@ -247,4 +256,18 @@ public class ModRegistry { EnergyStorage.SIDED.registerForBlockEntity(CableBlockEntity::getSideEnergyStorage, TRBlockEntities.CABLE); ItemStorage.SIDED.registerForBlockEntity(StorageUnitBaseBlockEntity::getExposedStorage, TRBlockEntities.STORAGE_UNIT); } + + private static void registerWanderingTraderTrades() { + List extraCommonTrades = new LinkedList<>(); + List extraRareTrades = new LinkedList<>(); + // specify extra trades here + extraCommonTrades.add(TradeUtils.create(TRContent.RUBBER_SAPLING, 5, 1, 8, 1)); + // registration of the trades + TradeOfferHelper.registerWanderingTraderOffers(1, allTradesList -> allTradesList.addAll( + extraCommonTrades.stream().map(TradeUtils::asFactory).collect(Collectors.toList()) + )); + TradeOfferHelper.registerWanderingTraderOffers(2, allTradesList -> allTradesList.addAll( + extraRareTrades.stream().map(TradeUtils::asFactory).collect(Collectors.toList()) + )); + } }