Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.mkram17.bazaarutils.utils.bazaar.gui.BazaarSlots;
import com.github.mkram17.bazaarutils.utils.bazaar.market.order.MarketType;
import com.github.mkram17.bazaarutils.utils.bazaar.market.order.OrderType;
import com.github.mkram17.bazaarutils.utils.bazaar.market.price.MarketPrices;
import com.github.mkram17.bazaarutils.utils.minecraft.gui.ScreenManager;
import com.teamresourceful.resourcefulconfig.api.annotations.Comment;
import com.teamresourceful.resourcefulconfig.api.annotations.ConfigEntry;
Expand Down Expand Up @@ -92,7 +91,7 @@ public BuyOrderAmountHelper(boolean enabled, int slotNumber) {
}

@Override
protected int computeFixedValue(TransactionState state, MarketPrices price) {
protected int computeFixedValue(TransactionState state) {
return getFixedAmount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.mkram17.bazaarutils.utils.bazaar.gui.BazaarSlots;
import com.github.mkram17.bazaarutils.utils.bazaar.market.order.MarketType;
import com.github.mkram17.bazaarutils.utils.bazaar.market.order.OrderType;
import com.github.mkram17.bazaarutils.utils.bazaar.market.price.MarketPrices;
import com.github.mkram17.bazaarutils.utils.minecraft.gui.ScreenManager;
import com.teamresourceful.resourcefulconfig.api.annotations.Comment;
import com.teamresourceful.resourcefulconfig.api.annotations.ConfigEntry;
Expand Down Expand Up @@ -92,7 +91,7 @@ public InstantBuyAmountHelper(boolean enabled, int slotNumber) {
}

@Override
protected int computeFixedValue(TransactionState state, MarketPrices price) {
protected int computeFixedValue(TransactionState state) {
return getFixedAmount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.mkram17.bazaarutils.utils.bazaar.gui.BazaarSlots;
import com.github.mkram17.bazaarutils.utils.bazaar.market.order.MarketType;
import com.github.mkram17.bazaarutils.utils.bazaar.market.order.OrderType;
import com.github.mkram17.bazaarutils.utils.bazaar.market.price.MarketPrices;
import com.github.mkram17.bazaarutils.utils.minecraft.gui.ScreenManager;
import com.teamresourceful.resourcefulconfig.api.annotations.Comment;
import com.teamresourceful.resourcefulconfig.api.annotations.ConfigEntry;
Expand Down Expand Up @@ -92,7 +91,7 @@ public SellOfferAmountHelper(boolean enabled, int slotNumber) {
}

@Override
protected int computeFixedValue(TransactionState state, MarketPrices price) {
protected int computeFixedValue(TransactionState state) {
return getFixedAmount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,17 @@ protected ResolvedInput resolveInput(TransactionState state) {
return new ResolvedInput.Value(0);
}

MarketPrices marketPrices = new MarketPrices(state.productId);

int amount = switch (getAmountStrategy()) {
case MAX -> computeMaxValue(state, marketPrices);
case FIXED -> computeFixedValue(state, marketPrices);
case MAX -> computeMaxValue(state);
case FIXED -> computeFixedValue(state);
};

return new ResolvedInput.Value(amount);
}

protected abstract int computeFixedValue(TransactionState state, MarketPrices price);
protected abstract int computeFixedValue(TransactionState state);

protected int computeMaxValue(TransactionState state, MarketPrices prices) {
protected int computeMaxValue(TransactionState state) {
return switch (getMarketType()) {
case INSTANT -> switch (getOrderType()) {
case BUY -> Optional.of(state.containerScreen())
Expand All @@ -268,7 +266,7 @@ protected int computeMaxValue(TransactionState state, MarketPrices prices) {
};
case ORDER -> switch (getOrderType()) {
case BUY -> {
int amountCanAfford = (int) (state.purse() / prices.getPriceForPosition(PricingPosition.COMPETITIVE, getMarketType().withIntention(getOrderType())));
int amountCanAfford = (int) (state.purse() / MarketPrices.getPriceForPosition(state.productId(), PricingPosition.COMPETITIVE, getMarketType().withIntention(getOrderType())));

yield BazaarScreens.findBuyOrderAmountLimit(state.inputSign().itemStack())
.map(limit -> Math.min(amountCanAfford, limit))
Expand Down Expand Up @@ -357,9 +355,7 @@ protected ResolvedInput resolveInput(TransactionState state) {
return new ResolvedInput.Value(0);
}

MarketPrices marketPrices = new MarketPrices(state.productId);

return new ResolvedInput.Value(marketPrices.getPriceForPosition(getPricingPosition(), getOrderType()));
return new ResolvedInput.Value(MarketPrices.getPriceForPosition(state.productId(), getPricingPosition(), getOrderType()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,56 @@
package com.github.mkram17.bazaarutils.utils.bazaar.market.price;

import com.github.mkram17.bazaarutils.events.BazaarDataUpdateEvent;
import com.github.mkram17.bazaarutils.events.listener.BUListener;
import com.github.mkram17.bazaarutils.utils.Util;
import com.github.mkram17.bazaarutils.utils.bazaar.data.BazaarDataManager;
import com.github.mkram17.bazaarutils.utils.bazaar.market.order.OrderType;
import lombok.Getter;
import meteordevelopment.orbit.EventHandler;

public class MarketPrices extends BUListener {
import java.util.OptionalDouble;

public class MarketPrices {

private final PriceInfo buyPriceInfo = new PriceInfo(null, OrderType.BUY);
private final PriceInfo sellPriceInfo = new PriceInfo(null, OrderType.SELL);
@Getter
private final String productID;

public MarketPrices(String productID) {
this.productID = productID;
subscribe();
updateMarketPrices(productID);
}

@EventHandler
private void onDataUpdate(BazaarDataUpdateEvent event) {
updateMarketPrices();
}

/**
* Refreshes cached market price data for this product.
* No-op: prices are now read fresh from {@link BazaarDataManager} on every call to
* {@link #getPriceForPosition}. Kept for API compatibility.
*/
public void updateMarketPrices() {
updateMarketPrices(productID);
}

private void updateMarketPrices(String productId) {
var buyPriceOpt = BazaarDataManager.findItemPriceOptional(productId, OrderType.BUY);
var sellPriceOpt = BazaarDataManager.findItemPriceOptional(productId, OrderType.SELL);

buyPriceOpt.ifPresent(price -> buyPriceInfo.setPricePerItem(Util.truncateNum(price)));
sellPriceOpt.ifPresent(price -> sellPriceInfo.setPricePerItem(Util.truncateNum(price)));
public Double getPriceForPosition(PricingPosition pricingPosition, OrderType orderType) {
return getPriceForPosition(productID, pricingPosition, orderType);
}

public Double getPriceForPosition(PricingPosition pricingPosition, OrderType orderType) {
double marketSellPrice = sellPriceInfo.getPricePerItem();
double marketBuyPrice = buyPriceInfo.getPricePerItem();
/**
* Computes the adjusted price for a product at the given pricing position and order type
* without creating a {@link MarketPrices} instance. Use this instead of instantiating
* {@link MarketPrices} transiently to avoid listener leaks.
*
* @param productId the Hypixel product ID to look up
* @param pricingPosition the positioning strategy (competitive, matched, outbid)
* @param orderType the order side (BUY or SELL)
* @return the market price adjusted for the given pricing position, or {@code 0.0} if no data is available
*/
public static double getPriceForPosition(String productId, PricingPosition pricingPosition, OrderType orderType) {
OptionalDouble priceOpt = BazaarDataManager.findItemPriceOptional(productId, orderType);
double price = Util.truncateNum(priceOpt.orElse(0.0));

return switch (orderType) {
case SELL -> switch (pricingPosition) {
case COMPETITIVE -> marketSellPrice - 0.1;
case MATCHED -> marketSellPrice;
case OUTBID -> marketSellPrice + 0.1;
case COMPETITIVE -> price - 0.1;
case MATCHED -> price;
case OUTBID -> price + 0.1;
};
case BUY -> switch (pricingPosition) {
case COMPETITIVE -> marketBuyPrice + 0.1;
case MATCHED -> marketBuyPrice;
case OUTBID -> marketBuyPrice - 0.1;
case COMPETITIVE -> price + 0.1;
case MATCHED -> price;
case OUTBID -> price - 0.1;
};
};
}
Expand Down