From b7c35fccdcce635f831263f9ce9829f77d9dc79c Mon Sep 17 00:00:00 2001 From: apple502j <33279053+apple502j@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:37:27 +0900 Subject: [PATCH] Delayed pool modification --- .../builder/v1/trade/TradeOfferHelper.java | 6 ----- .../object/builder/TradeOfferInternals.java | 25 ++++++++++++------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/trade/TradeOfferHelper.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/trade/TradeOfferHelper.java index 35572fd9dd..d0da6a300b 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/trade/TradeOfferHelper.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/trade/TradeOfferHelper.java @@ -225,11 +225,5 @@ default WanderingTraderOffersBuilder addAll(Identifier id, TradeOffers.Factory.. default WanderingTraderOffersBuilder addOffersToPool(Identifier pool, Collection factories) { return addOffersToPool(pool, factories.toArray(TradeOffers.Factory[]::new)); } - - /** - * Returns all registered pool IDs, including vanilla ones. - * @return an unmodifiable set containing all registered pool IDs - */ - Set getPoolIds(); } } diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/TradeOfferInternals.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/TradeOfferInternals.java index a167b8be23..f735309271 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/TradeOfferInternals.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/TradeOfferInternals.java @@ -17,12 +17,10 @@ package net.fabricmc.fabric.impl.object.builder; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Set; import java.util.function.Consumer; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -94,12 +92,14 @@ public static void printRefreshOffersWarning() { } public static class WanderingTraderOffersBuilderImpl implements TradeOfferHelper.WanderingTraderOffersBuilder { - static final Object2IntMap ID_TO_INDEX = Util.make(new Object2IntOpenHashMap<>(), idToIndex -> { + private static final Object2IntMap ID_TO_INDEX = Util.make(new Object2IntOpenHashMap<>(), idToIndex -> { idToIndex.put(BUY_ITEMS_POOL, 0); idToIndex.put(SELL_SPECIAL_ITEMS_POOL, 1); idToIndex.put(SELL_COMMON_ITEMS_POOL, 2); }); + private static final Map DELAYED_MODIFICATIONS = new HashMap<>(); + /** * Make the trade list modifiable. */ @@ -115,17 +115,29 @@ public TradeOfferHelper.WanderingTraderOffersBuilder pool(Identifier id, int cou if (count <= 0) throw new IllegalArgumentException("count must be positive"); Objects.requireNonNull(id, "id cannot be null"); + + if (ID_TO_INDEX.containsKey(id)) throw new IllegalArgumentException("pool id %s is already registered".formatted(id)); + Pair pool = Pair.of(factories, count); initWanderingTraderTrades(); ID_TO_INDEX.put(id, TradeOffers.REBALANCED_WANDERING_TRADER_TRADES.size()); TradeOffers.REBALANCED_WANDERING_TRADER_TRADES.add(pool); + TradeOffers.Factory[] delayedModifications = DELAYED_MODIFICATIONS.remove(id); + + if (delayedModifications != null) addOffersToPool(id, delayedModifications); + return this; } @Override public TradeOfferHelper.WanderingTraderOffersBuilder addOffersToPool(Identifier pool, TradeOffers.Factory... factories) { if (!ID_TO_INDEX.containsKey(pool)) { - throw new IllegalArgumentException("pool %s is not registered".formatted(pool)); + DELAYED_MODIFICATIONS.compute(pool, (id, current) -> { + if (current == null) return factories; + + return ArrayUtils.addAll(current, factories); + }); + return this; } int poolIndex = ID_TO_INDEX.getInt(pool); @@ -135,10 +147,5 @@ public TradeOfferHelper.WanderingTraderOffersBuilder addOffersToPool(Identifier TradeOffers.REBALANCED_WANDERING_TRADER_TRADES.set(poolIndex, Pair.of(modified, poolPair.getRight())); return this; } - - @Override - public Set getPoolIds() { - return Collections.unmodifiableSet(ID_TO_INDEX.keySet()); - } } }