Skip to content

Commit

Permalink
Wandering trades, Trades Config Rework (#184)
Browse files Browse the repository at this point in the history
* Start on WanderingTrades

Signed-off-by: Joseph T. McQuigg <[email protected]>

* Change Trades Config

Signed-off-by: Joseph T. McQuigg <[email protected]>

* Add Mushrooms, lily pads, and flowers to wandering traders

Signed-off-by: Joseph T. McQuigg <[email protected]>

* Update Config stuff

Signed-off-by: Joseph T. McQuigg <[email protected]>

* More Trade Config

Signed-off-by: Joseph T. McQuigg <[email protected]>

* Adjustments

Signed-off-by: Joseph T. McQuigg <[email protected]>

* ChangeLog

Signed-off-by: Joseph T. McQuigg <[email protected]>

* Fix some Issues with Null Issues

Signed-off-by: Joseph T. McQuigg <[email protected]>

---------

Signed-off-by: Joseph T. McQuigg <[email protected]>
  • Loading branch information
JT122406 authored Dec 26, 2024
1 parent 7f4dbb7 commit 0d79a54
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 50 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- Add Config to Enable/Disable Spawning of Oddion and Man O War
- Add Storage Block Tags (Allium and Rose Petal Blocks) (#c name space)
- Fix BWG trades on NeoForge
- Add BWG Items to Wandering Trader Trades
- New BWG Trades Config with Wandering Trader Options, and Option to disable Forager Trades

# 2.2.5
- Make Pale Pumpkin Set Compostable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class ConfigLoader {
*/
public static <T> T loadConfig(@NotNull Class<T> clazz, String name) {
try {
try {
Files.delete(PlatformHandler.PLATFORM_HANDLER.configPath().resolve(name + ".json5"));
} catch (Exception ignored) {}

Path configPath = PlatformHandler.PLATFORM_HANDLER.configPath().resolve(name + ".json");
T value = clazz.getConstructor().newInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public static <T> void createDefaultFile(Path path, Codec<T> codec, T config) {
throw new RuntimeException(e);
}
}

public record CommentValue<T>(String comment, T value) {
public static <T> CommentValue<T> of(String comment, T value) {
return new CommentValue<>(comment, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package net.potionstudios.biomeswevegone.config.configs;

import com.google.common.base.Suppliers;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import corgitaco.corgilib.serialization.codec.CommentedCodec;
import net.potionstudios.biomeswevegone.PlatformHandler;
import net.potionstudios.biomeswevegone.config.ConfigLoader;
import net.potionstudios.biomeswevegone.config.ConfigUtils;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.util.function.Supplier;
public class BWGTradesConfig {

public record BWGTradesConfig(boolean enableTrades, boolean enableVanillaTradeAdditions) {
public static final BWGTradesConfig INSTANCE = ConfigLoader.loadConfig(BWGTradesConfig.class, "trades");

private static final Path PATH = PlatformHandler.PLATFORM_HANDLER.configPath().resolve("trades.json5");
public BWGTrades trades = new BWGTrades();

@NotNull
public static Supplier<BWGTradesConfig> INSTANCE = Suppliers.memoize(BWGTradesConfig::getOrCreateConfigFromDisk);
public static class BWGTrades {
public ConfigUtils.CommentValue<Boolean> disableTrades = ConfigUtils.CommentValue.of("Disable All BWG Trades, If this is set to true none of the values below will matter", false);
}

private static final Codec<BWGTradesConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
CommentedCodec.of(Codec.BOOL, "enable_trades", "Whether to enable BWG Villager Trades").orElse(true).forGetter(config -> true),
CommentedCodec.of(Codec.BOOL, "enable_vanilla_trade_additions", "Whether to add BWG Items to Vanilla Villager Type Trades").orElse(true).forGetter(config -> true)
).apply(instance, BWGTradesConfig::new));
public BWGVillagerTradesConfig villagerTrades = new BWGVillagerTradesConfig();

private static BWGTradesConfig createDefault() {
return new BWGTradesConfig(true, true);
}
public static class BWGVillagerTradesConfig {
public ConfigUtils.CommentValue<Boolean> allowBWGForagerTrades = ConfigUtils.CommentValue.of("Allow BWG Forager Profession Trades", true);
public ConfigUtils.CommentValue<Boolean> enableBWGVanillaProfessionTradeAdditions = ConfigUtils.CommentValue.of("Allows BWG Items to be added to Vanilla Profession Trades", true);
}

private static BWGTradesConfig getOrCreateConfigFromDisk() {
return ConfigUtils.loadConfig(PATH, CODEC, createDefault());
}
public BWGWanderingTraderTradesConfig wanderingTraderTrades = new BWGWanderingTraderTradesConfig();

public static class BWGWanderingTraderTradesConfig {
public ConfigUtils.CommentValue<Boolean> enableBWGItemsTrades = ConfigUtils.CommentValue.of("Allows BWG Items to be added to Wandering Trader Offerings", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
import net.minecraft.world.item.trading.ItemCost;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.FlowerBlock;
import net.minecraft.world.level.block.SaplingBlock;
import net.potionstudios.biomeswevegone.config.configs.BWGTradesConfig;
import net.potionstudios.biomeswevegone.world.item.BWGItems;
import net.potionstudios.biomeswevegone.world.level.block.BWGBlocks;
import net.potionstudios.biomeswevegone.world.level.block.sand.BWGSandSet;
import net.potionstudios.biomeswevegone.world.level.block.wood.BWGWood;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -23,30 +28,31 @@ public class BWGVillagerTrades {
public static final Map<VillagerProfession, Int2ObjectMap<List<MerchantOffer>>> TRADES = new HashMap<>();

public static void makeTrades() {
TRADES.put(BWGVillagerProfessions.FORAGER.get(), toIntMap(ImmutableMap.of(
1, ImmutableList.of(
createEmeraldForItemsOffer(Items.RED_MUSHROOM, 10, 12, 2),
createEmeraldForItemsOffer(Items.BROWN_MUSHROOM, 10, 12, 2),
createEmeraldForItemsOffer(BWGBlocks.GREEN_MUSHROOM.get(), 10, 12, 2)
),
2, ImmutableList.of(
createEmeraldForItemsOffer(BWGBlocks.WOOD_BLEWIT.get(), 8, 12, 3),
createItemsForEmeraldsOffer(BWGItems.WHITE_PUFFBALL_CAP.get(), 4, 5, 4, 2, 0.05f)
),
3, ImmutableList.of(
createEmeraldForItemsOffer(BWGItems.WHITE_PUFFBALL_SPORES.get(), 4, 5, 4)
),
4, ImmutableList.of(
createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BRANCH.get(), 4, 9, 4, 3, 0.05f),
createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BLOSSOM.get(), 10, 1, 10, 3, 0.05f)
),
5, ImmutableList.of(
createItemsForEmeraldsOffer(BWGBlocks.SHELF_FUNGI.get(), 3, 9, 4, 4, 0.05f),
createEmeraldForItemsOffer(Items.SWEET_BERRIES, 16, 4, 2),
createEmeraldForItemsOffer(BWGItems.BLUEBERRIES.get(), 16, 4, 2)
)
)));
if (!BWGTradesConfig.INSTANCE.get().enableVanillaTradeAdditions()) return;
if (BWGTradesConfig.INSTANCE.villagerTrades.allowBWGForagerTrades.value())
TRADES.put(BWGVillagerProfessions.FORAGER.get(), toIntMap(ImmutableMap.of(
1, ImmutableList.of(
createEmeraldForItemsOffer(Items.RED_MUSHROOM, 10, 12, 2),
createEmeraldForItemsOffer(Items.BROWN_MUSHROOM, 10, 12, 2),
createEmeraldForItemsOffer(BWGBlocks.GREEN_MUSHROOM.get(), 10, 12, 2)
),
2, ImmutableList.of(
createEmeraldForItemsOffer(BWGBlocks.WOOD_BLEWIT.get(), 8, 12, 3),
createItemsForEmeraldsOffer(BWGItems.WHITE_PUFFBALL_CAP.get(), 4, 5, 4, 2, 0.05f)
),
3, ImmutableList.of(
createEmeraldForItemsOffer(BWGItems.WHITE_PUFFBALL_SPORES.get(), 4, 5, 4)
),
4, ImmutableList.of(
createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BRANCH.get(), 4, 9, 4, 3, 0.05f),
createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BLOSSOM.get(), 10, 1, 10, 3, 0.05f)
),
5, ImmutableList.of(
createItemsForEmeraldsOffer(BWGBlocks.SHELF_FUNGI.get(), 3, 9, 4, 4, 0.05f),
createEmeraldForItemsOffer(Items.SWEET_BERRIES, 16, 4, 2),
createEmeraldForItemsOffer(BWGItems.BLUEBERRIES.get(), 16, 4, 2)
)
)));
if (!BWGTradesConfig.INSTANCE.villagerTrades.enableBWGVanillaProfessionTradeAdditions.value()) return;
TRADES.put(VillagerProfession.BUTCHER, toIntMap(ImmutableMap.of(
2, ImmutableList.of(
createEmeraldForItemsOffer(BWGItems.BLUEBERRIES.get(), 10, 12, 2)
Expand Down Expand Up @@ -81,6 +87,24 @@ public static void makeTrades() {
)));
}

public static final Int2ObjectMap<List<MerchantOffer>> WANDERING_TRADER_TRADES = new Int2ObjectOpenHashMap<>();

public static void makeWanderingTrades() {
List<MerchantOffer> level1Items = new ArrayList<>();
BWGWood.WOOD.stream().filter(item -> item.get() instanceof SaplingBlock).forEach(item ->
level1Items.add(createItemsForEmeraldsOffer(item.get(), 5, 1, 8, 1, 0.05f)));
BWGSandSet.getSandSets().forEach(bwgSandSet -> level1Items.add(
createItemsForEmeraldsOffer(bwgSandSet.getSand(), 1, 8, 8, 1, 0.05f)));
BWGBlocks.BLOCKS.stream().filter(block -> block.get() instanceof FlowerBlock).forEach(block ->
level1Items.add(createItemsForEmeraldsOffer(block.get(), 1, 1, 13, 1, 0.05f)));
level1Items.add(createItemsForEmeraldsOffer(BWGItems.TINY_LILY_PADS.get(), 1, 2, 5, 1, 0.05f));
level1Items.add(createItemsForEmeraldsOffer(BWGItems.FLOWERING_TINY_LILY_PADS.get(), 1, 2, 5, 1, 0.05f));
level1Items.add(createItemsForEmeraldsOffer(BWGBlocks.WEEPING_MILKCAP.get(), 1, 1, 12, 1, 0.05f));
level1Items.add(createItemsForEmeraldsOffer(BWGBlocks.GREEN_MUSHROOM.get(), 1, 1, 12, 1, 0.05f));
level1Items.add(createItemsForEmeraldsOffer(BWGBlocks.WOOD_BLEWIT.get(), 1, 1, 12, 1, 0.05f));
WANDERING_TRADER_TRADES.put(1, level1Items);
}

private static MerchantOffer createEmeraldForItemsOffer(ItemLike item, int cost, int maxUses, int villagerXp) {
return new MerchantOffer(new ItemCost(item, cost), new ItemStack(Items.EMERALD), maxUses, villagerXp, 0.05F);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public static void init() {
ToolInteractions.registerTillables((block, pair) -> TillableBlockRegistry.register(block, pair.getFirst(), pair.getSecond()));
registerBiomeModifiers();
registerLootModifiers();
registerTrades();
if (!BWGTradesConfig.INSTANCE.trades.disableTrades.value()) {
registerTrades();
if (BWGTradesConfig.INSTANCE.wanderingTraderTrades.enableBWGItemsTrades.value())
registerWanderingTrades();
}
FabricBrewingRecipeRegistryBuilder.BUILD.register(builder -> BWGBrewingRecipes.buildBrewingRecipes(builder::addMix));
}

Expand Down Expand Up @@ -66,8 +70,8 @@ private static void registerLootModifiers() {
}

private static void registerTrades() {
if (!BWGTradesConfig.INSTANCE.get().enableTrades()) return;
BWGVillagerTrades.makeTrades();
if (BWGVillagerTrades.TRADES.isEmpty()) return;
BWGVillagerTrades.TRADES.forEach((villagerProfession, offersMap) ->
offersMap.forEach((level, offers) ->
TradeOfferHelper.registerVillagerOffers(villagerProfession, level, factory -> {
Expand All @@ -76,4 +80,14 @@ private static void registerTrades() {
)
);
}

private static void registerWanderingTrades() {
if (!BWGTradesConfig.INSTANCE.wanderingTraderTrades.enableBWGItemsTrades.value()) return;
BWGVillagerTrades.makeWanderingTrades();
BWGVillagerTrades.WANDERING_TRADER_TRADES.forEach((level, offers) ->
TradeOfferHelper.registerWanderingTraderOffers(level, factory -> {
for (MerchantOffer offer : offers) factory.add((trader, random) -> offer);
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private void onInitialize(final FMLCommonSetupEvent event) {
private void onPostInitialize(final FMLLoadCompleteEvent event) {
event.enqueueWork(BiomesWeveGone::postInit);
BWGVillagerTrades.makeTrades();
BWGVillagerTrades.makeWanderingTrades();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.event.village.VillagerTradesEvent;
import net.minecraftforge.event.village.WandererTradesEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.IEventBus;
import net.potionstudios.biomeswevegone.util.BoneMealHandler;
Expand Down Expand Up @@ -51,7 +52,11 @@ public static void init() {
public static void registerVanillaCompatEvents(final IEventBus bus) {
bus.addListener(VanillaCompatForge::registerTillables);
bus.addListener(VanillaCompatForge::registerFuels);
if (BWGTradesConfig.INSTANCE.get().enableTrades()) bus.addListener(VanillaCompatForge::onVillagerTrade);
if (!BWGTradesConfig.INSTANCE.trades.disableTrades.value()) {
bus.addListener(VanillaCompatForge::onVillagerTrade);
if (BWGTradesConfig.INSTANCE.wanderingTraderTrades.enableBWGItemsTrades.value())
bus.addListener(VanillaCompatForge::onWanderingTrade);
}
bus.addListener(VanillaCompatForge::registerBrewingRecipes);
bus.addListener(VanillaCompatForge::onBoneMealUse);
bus.addListener(VanillaCompatForge::onEnderManAnger);
Expand Down Expand Up @@ -97,6 +102,16 @@ private static void onVillagerTrade(final VillagerTradesEvent event) {
}
}

/**
* Register wandering trader trades.
* @see WandererTradesEvent
*/
private static void onWanderingTrade(final WandererTradesEvent event) {
BWGVillagerTrades.WANDERING_TRADER_TRADES.forEach((level, offers) -> {
for (MerchantOffer offer : offers) event.getGenericTrades().add((trader, random) -> offer);
});
}

/**
* Register brewing recipes.
* @see BrewingRecipeRegisterEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private void onInitialize(final FMLCommonSetupEvent event) {
private void onPostInitialize(final FMLLoadCompleteEvent event) {
event.enqueueWork(BiomesWeveGone::postInit);
BWGVillagerTrades.makeTrades();
BWGVillagerTrades.makeWanderingTrades();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.neoforged.neoforge.event.entity.player.BonemealEvent;
import net.neoforged.neoforge.event.level.BlockEvent;
import net.neoforged.neoforge.event.village.VillagerTradesEvent;
import net.neoforged.neoforge.event.village.WandererTradesEvent;
import net.potionstudios.biomeswevegone.util.BoneMealHandler;
import net.potionstudios.biomeswevegone.config.configs.BWGTradesConfig;
import net.potionstudios.biomeswevegone.world.entity.npc.BWGVillagerTrades;
Expand Down Expand Up @@ -48,7 +49,11 @@ public static void init() {

public static void registerVanillaCompatEvents(final IEventBus bus) {
bus.addListener(VanillaCompatNeoForge::registerTillables);
if (BWGTradesConfig.INSTANCE.get().enableTrades()) bus.addListener(VanillaCompatNeoForge::onVillagerTrade);
if (!BWGTradesConfig.INSTANCE.trades.disableTrades.value()) {
bus.addListener(VanillaCompatNeoForge::onVillagerTrade);
if (BWGTradesConfig.INSTANCE.wanderingTraderTrades.enableBWGItemsTrades.value())
bus.addListener(VanillaCompatNeoForge::onWanderingTrade);
}
bus.addListener(VanillaCompatNeoForge::onBoneMealUse);
bus.addListener(VanillaCompatNeoForge::registerBrewingRecipes);
bus.addListener(VanillaCompatNeoForge::onEnderManAnger);
Expand Down Expand Up @@ -85,6 +90,16 @@ private static void onVillagerTrade(final VillagerTradesEvent event) {
}
}

/**
* Register wandering trader trades.
* @see WandererTradesEvent
*/
private static void onWanderingTrade(final WandererTradesEvent event) {
BWGVillagerTrades.WANDERING_TRADER_TRADES.forEach((level, offers) -> {
for (MerchantOffer offer : offers) event.getGenericTrades().add((trader, random) -> offer);
});
}

/**
* Register brewing recipes.
* @see RegisterBrewingRecipesEvent
Expand Down

0 comments on commit 0d79a54

Please sign in to comment.