Merge branch 'master' into Samkist_master
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
package com.willfp.eco.core.price;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A group of {@link ConfiguredPrice}s in order to show them
|
||||
* to players in one go.
|
||||
*/
|
||||
public final class CombinedDisplayPrice {
|
||||
/**
|
||||
* Maps configured prices to multipliers.
|
||||
*/
|
||||
private final Map<ConfiguredPrice, Double> prices;
|
||||
|
||||
/**
|
||||
* The player to format for.
|
||||
*/
|
||||
private final Player player;
|
||||
|
||||
/**
|
||||
* Initialize a new combined price mapping formatters to multipliers.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param prices The prices.
|
||||
*/
|
||||
private CombinedDisplayPrice(@NotNull final Player player,
|
||||
@NotNull final Map<ConfiguredPrice, Double> prices) {
|
||||
this.player = player;
|
||||
this.prices = prices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the display strings.
|
||||
*
|
||||
* @return The display strings.
|
||||
*/
|
||||
@NotNull
|
||||
public String[] getDisplayStrings() {
|
||||
List<String> displayStrings = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<ConfiguredPrice, Double> entry : prices.entrySet()) {
|
||||
displayStrings.add(entry.getKey().getDisplay(player, entry.getValue()));
|
||||
}
|
||||
|
||||
return displayStrings.toArray(new String[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The builder.
|
||||
*/
|
||||
public static class Builder {
|
||||
/**
|
||||
* All multiplied prices.
|
||||
*/
|
||||
private final List<MultipliedPrice> prices = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* The player.
|
||||
*/
|
||||
private final Player player;
|
||||
|
||||
/**
|
||||
* Create a new builder.
|
||||
*
|
||||
* @param player The player.
|
||||
*/
|
||||
Builder(@NotNull final Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new price with a certain multiplier.
|
||||
*
|
||||
* @param price The price.
|
||||
* @param multiplier The multiplier.
|
||||
* @return The builder.
|
||||
*/
|
||||
@NotNull
|
||||
public Builder add(@NotNull final ConfiguredPrice price,
|
||||
final double multiplier) {
|
||||
prices.add(new MultipliedPrice(price, multiplier));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a new price.
|
||||
*
|
||||
* @param price The price.
|
||||
* @return The builder.
|
||||
*/
|
||||
@NotNull
|
||||
public Builder add(@NotNull final ConfiguredPrice price) {
|
||||
return this.add(price, 1D);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build into a {@link CombinedDisplayPrice}.
|
||||
*
|
||||
* @return The combined price.
|
||||
*/
|
||||
@NotNull
|
||||
public CombinedDisplayPrice build() {
|
||||
Map<ConfiguredPrice, Double> unitPrices = new HashMap<>();
|
||||
|
||||
// Take first configured price at each ID as the format for all prices with that ID.
|
||||
for (MultipliedPrice price : prices) {
|
||||
// Find the base price.
|
||||
ConfiguredPrice base = unitPrices.keySet()
|
||||
.stream()
|
||||
.filter(it -> it.getIdentifier().equals(price.price().getIdentifier()))
|
||||
.findFirst()
|
||||
.orElse(price.price());
|
||||
|
||||
// Find the multiplier for a value of 1, e.g. a price that's worth 20 will be 0.05.
|
||||
double unitMultiplier = 1 / base.getValue(player);
|
||||
|
||||
double currentMultiplier = unitPrices.getOrDefault(base, 0D);
|
||||
currentMultiplier += unitMultiplier * price.price().getValue(player, price.multiplier());
|
||||
unitPrices.put(base, currentMultiplier);
|
||||
}
|
||||
|
||||
return new CombinedDisplayPrice(player, unitPrices);
|
||||
}
|
||||
|
||||
/**
|
||||
* A price with a multiplier.
|
||||
*
|
||||
* @param price The price.
|
||||
* @param multiplier The multiplier.
|
||||
*/
|
||||
private record MultipliedPrice(
|
||||
@NotNull ConfiguredPrice price,
|
||||
double multiplier
|
||||
) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder for a player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @return The builder.
|
||||
*/
|
||||
@NotNull
|
||||
public static Builder builder(@NotNull final Player player) {
|
||||
return new Builder(player);
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,11 @@ public final class ConfiguredPrice implements Price {
|
||||
this.price.setMultiplier(player, multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return this.price.getIdentifier();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price that this delegates to.
|
||||
*
|
||||
|
||||
@@ -132,6 +132,21 @@ public interface Price {
|
||||
throw new NotImplementedException("Override setMultiplier(Player, double) in your Price implementation!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the identifier of this price (as type/instance checks break with delegation,
|
||||
* this is used for combining prices, etc.)
|
||||
* <p>
|
||||
* By default, this uses the class name, but it's good practice to override this.
|
||||
* <p>
|
||||
* It's also good practice to prefix your identifiers with some kind of namespace or
|
||||
* internal ID, in order to prevent conflicts.
|
||||
*
|
||||
* @return The identifier.
|
||||
*/
|
||||
default String getIdentifier() {
|
||||
return this.getClass().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* If the price is backed by a value, get it here.
|
||||
*
|
||||
|
||||
@@ -85,4 +85,9 @@ public final class PriceEconomy implements Price {
|
||||
final double multiplier) {
|
||||
this.multipliers.put(player.getUniqueId(), multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return "eco:economy";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,4 +49,9 @@ public final class PriceFree implements Price {
|
||||
final double multiplier) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return "eco:free";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.eco.core.price.impl;
|
||||
|
||||
import com.willfp.eco.core.drops.DropQueue;
|
||||
import com.willfp.eco.core.items.HashedItem;
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import com.willfp.eco.core.math.MathContext;
|
||||
import com.willfp.eco.core.price.Price;
|
||||
@@ -151,4 +152,9 @@ public final class PriceItem implements Price {
|
||||
final double multiplier) {
|
||||
this.multipliers.put(player.getUniqueId(), multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return "eco:item-" + HashedItem.of(this.item.getItem()).getHash();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,8 @@ public final class ProxyConstants {
|
||||
"v1_17_R1",
|
||||
"v1_18_R1",
|
||||
"v1_18_R2",
|
||||
"v1_19_R1"
|
||||
"v1_19_R1",
|
||||
"v1_19_R2"
|
||||
);
|
||||
|
||||
private ProxyConstants() {
|
||||
|
||||
Reference in New Issue
Block a user