Skip to content

Commit

Permalink
Delayed pool modification
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Sep 16, 2023
1 parent 56d6ddf commit b7c35fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,5 @@ default WanderingTraderOffersBuilder addAll(Identifier id, TradeOffers.Factory..
default WanderingTraderOffersBuilder addOffersToPool(Identifier pool, Collection<TradeOffers.Factory> 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<Identifier> getPoolIds();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,12 +92,14 @@ public static void printRefreshOffersWarning() {
}

public static class WanderingTraderOffersBuilderImpl implements TradeOfferHelper.WanderingTraderOffersBuilder {
static final Object2IntMap<Identifier> ID_TO_INDEX = Util.make(new Object2IntOpenHashMap<>(), idToIndex -> {
private static final Object2IntMap<Identifier> 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<Identifier, TradeOffers.Factory[]> DELAYED_MODIFICATIONS = new HashMap<>();

/**
* Make the trade list modifiable.
*/
Expand All @@ -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<TradeOffers.Factory[], Integer> 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);
Expand All @@ -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<Identifier> getPoolIds() {
return Collections.unmodifiableSet(ID_TO_INDEX.keySet());
}
}
}

0 comments on commit b7c35fc

Please sign in to comment.