Added math stuff to calculate price based on points (thanks everett)

This commit is contained in:
2021-12-17 20:44:38 +01:00
parent 8d143b931a
commit 523a7fe16b
7 changed files with 57 additions and 11 deletions
@@ -3,6 +3,7 @@ package com.alttd.GUI.windows;
import com.alttd.GUI.GUIMerchant;
import com.alttd.VillagerUI;
import com.alttd.config.Config;
import com.alttd.config.WorthConfig;
import com.alttd.events.SpawnShopEvent;
import com.alttd.objects.EconUser;
import com.alttd.objects.Price;
@@ -39,7 +40,10 @@ public class BuyGUI extends GUIMerchant {
private void buy(VillagerType villagerType, Player player, Material material, int amount, Price price) {
Economy econ = VillagerUI.getInstance().getEconomy();
double balance = econ.getBalance(player);
double cost = price.getPrice(amount);
int trans_pts = (int) (Math.floor(price.getPrice(amount)/ WorthConfig.POINT_MOD) * amount);
EconUser econUser = EconUser.users.get(player.getUniqueId());
int oldPoints = econUser.getPointsMap().get(villagerType.getName());
double cost = price.calculatePriceThing(oldPoints, trans_pts);
if (balance < cost) {
player.sendMessage(MiniMessage.get().parse(Config.NOT_ENOUGH_MONEY,
@@ -47,11 +51,9 @@ public class BuyGUI extends GUIMerchant {
Template.of("price", String.valueOf(price))));
return;
}
EconUser econUser = EconUser.users.get(player.getUniqueId());
int oldPoints = econUser.getPointsMap().get(villagerType.getName());
econ.withdrawPlayer(player, cost);
econUser.addPoints(villagerType.getName(), price.getPoints());
econUser.addPoints(villagerType.getName(), trans_pts);
player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM,
Template.of("amount", String.valueOf(amount)),
Template.of("item", material.toString()),
@@ -3,6 +3,7 @@ package com.alttd.GUI.windows;
import com.alttd.GUI.GUIMerchant;
import com.alttd.VillagerUI;
import com.alttd.config.Config;
import com.alttd.config.WorthConfig;
import com.alttd.events.SpawnShopEvent;
import com.alttd.objects.EconUser;
import com.alttd.objects.Price;
@@ -39,9 +40,10 @@ public class SellGUI extends GUIMerchant {
private void sell(VillagerType villagerType, Player player, Material material, int amount, Price price) {
Economy econ = VillagerUI.getInstance().getEconomy();
double cost = price.getPrice(amount);
EconUser econUser = EconUser.users.get(player.getUniqueId());
int oldPoints = econUser.getPointsMap().get(villagerType.getName());
int trans_pts = (int) (Math.floor(price.getPrice(amount)/ WorthConfig.POINT_MOD) * amount);
double cost = price.calculatePriceThing(oldPoints, trans_pts);
econ.depositPlayer(player, cost);
econUser.addPoints(villagerType.getName(), -price.getPoints());
@@ -1,12 +1,8 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.objects.LoadedVillagers;
import com.alttd.objects.VillagerType;
import com.alttd.util.Logger;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Set;
import java.util.UUID;
public class VillagerConfig extends AbstractConfig {
@@ -46,4 +46,9 @@ public class WorthConfig extends AbstractConfig {
prices.put(Material.getMaterial(key), new Price(Utilities.round(worth.getDouble(key), 2)));
}
}
public static int POINT_MOD = 4;
private static void loadOtherStuff() {
POINT_MOD = config.getInt("point-mod", POINT_MOD);
}
}
@@ -5,6 +5,7 @@ import com.alttd.VillagerUI;
import com.alttd.config.Config;
import com.alttd.objects.LoadedVillagers;
import com.alttd.objects.VillagerType;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@@ -23,7 +24,7 @@ public class VillagerInteract implements Listener {
return;
if (!event.getPlayer().hasPermission(loadedVillager.getPermission())) {
event.getPlayer().sendMessage(Config.NO_PERMISSION); //TODO more specific message?
event.getPlayer().sendMessage(MiniMessage.get().parse(Config.NO_PERMISSION)); //TODO more specific message?
return;
}
new BukkitRunnable() {
@@ -3,11 +3,17 @@ package com.alttd.objects;
import com.alttd.config.Config;
import com.alttd.util.Logger;
import com.alttd.util.Utilities;
import org.apache.commons.math3.analysis.function.StepFunction;
import org.apache.commons.math3.analysis.integration.TrapezoidIntegrator;
public final class Price {
private final double price;
private final int points;
double x_mult[] = {0, 500, 2000, 4000};
double y_mult[] = {1.0, 1.5, 2.5, 5.0};
StepFunction multiplierModel = new StepFunction(x_mult, y_mult);
public Price(double price) {
this.price = price;
for (int key : Config.pointsRangeMap.keySet()) {
@@ -28,6 +34,12 @@ public final class Price {
return (Utilities.round(price * multiplier, 2));
}
public double calculatePriceThing(int oldPoints, int transPts) {
// Compute numerical integration to determine price
TrapezoidIntegrator trapez = new TrapezoidIntegrator();
return (price * trapez.integrate(10, multiplierModel, oldPoints, oldPoints + transPts));
}
public int getPoints() {
return (points);
}