Reworked points to be based on item price

This commit is contained in:
stjn 2021-11-10 16:58:47 +01:00
parent 7584df32f4
commit 99d8a5007e
5 changed files with 104 additions and 21 deletions

View File

@ -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.events.SpawnShopEvent;
import com.alttd.objects.EconUser;
import com.alttd.objects.Price;
import com.alttd.objects.VillagerType;
@ -10,12 +11,11 @@ import com.alttd.util.Utilities;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class BuyGUI extends GUIMerchant {
private static final MiniMessage miniMessage = MiniMessage.get();
@ -54,6 +54,10 @@ public class BuyGUI extends GUIMerchant {
Template.of("amount", String.valueOf(amount)),
Template.of("item", material.toString()),
Template.of("price", String.valueOf(price))));
Bukkit.getServer().getPluginManager()
.callEvent(new SpawnShopEvent(player, amount, cost, material,
0, 0, true));
}
private ItemStack getPriceItem(double price) {

View File

@ -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.events.SpawnShopEvent;
import com.alttd.objects.EconUser;
import com.alttd.objects.Price;
import com.alttd.objects.VillagerType;
@ -10,6 +11,7 @@ import com.alttd.util.Utilities;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -46,6 +48,10 @@ public class SellGUI extends GUIMerchant {
Template.of("amount", String.valueOf(amount)),
Template.of("item", material.toString()),
Template.of("price", String.valueOf(price))));
Bukkit.getServer().getPluginManager()
.callEvent(new SpawnShopEvent(player, amount, cost, material,
0, 0, false));
}
private ItemStack getPriceItem(double price) {

View File

@ -3,6 +3,12 @@ package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.objects.VillagerType;
import com.alttd.util.Logger;
import com.google.common.collect.Range;
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectSortedMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectSortedMaps;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
@ -10,6 +16,8 @@ import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class Config extends AbstractConfig {
@ -114,6 +122,65 @@ public final class Config extends AbstractConfig {
});
}
public static Int2ObjectAVLTreeMap<Range<Double>> pointsRangeMap = new Int2ObjectAVLTreeMap<>();
private static void loadPointRange() {
pointsRangeMap.clear();
Pattern pattern = Pattern.compile("[1-9][0-9]{0,2}(.[0-9]{1,2})?-[1-9][0-9]{0,2}(.[0-9]{1,2})?");
ConfigurationSection configurationSection = config.getConfigurationSection("points");
if (configurationSection == null) {
Logger.severe("""
No point entries in config (see example). Please add them and restart the plugin.
points:
\t1: 0.5-1.5
\t3: 1.5-2.25
\t5: 2.25-0 #2.25 and higher""");
VillagerUI.getInstance().getServer().getPluginManager().disablePlugin(VillagerUI.getInstance());
return;
}
Set<String> keys = configurationSection.getKeys(false);
for (String key : keys) {
int points = Integer.parseInt(key);
if (points == 0) {
Logger.warning("Invalid point entry % in config", key);
continue;
}
String range = configurationSection.getString(key);
if (range == null) {
Logger.warning("Invalid point value for % in config", key);
continue;
}
Matcher matcher = pattern.matcher(range);
if (!matcher.matches()) {
Logger.warning("Invalid point value % for % in config " +
"should be double - double (0-2.05)", range, key);
continue;
}
String[] split = range.split("-");
if (split.length != 2) {
Logger.severe("""
The logic for the regex failed when loading points.
key:%
value:%""", key, range);
continue;
}
double d1 = Double.parseDouble(split[0]);
double d2 = Double.parseDouble(split[1]);
Range<Double> doubleRange;
if (d2 == 0 && d1 > d2)
doubleRange = Range.greaterThan(d1);
else if (d2 > d1)
doubleRange = Range.closed(d1, d2);
else {
Logger.warning("Invalid range d1:% to d2:%, can't be the same, d1 can't be bigger " +
"than d2 unless d2 is 0 (infinite)", String.valueOf(d1), String.valueOf(d2));
continue;
}
pointsRangeMap.put(points, doubleRange);
}
}
private static HashSet<ItemStack> loadProducts(ConfigurationSection productsSection) {
HashSet<ItemStack> products = new HashSet<>();
if (productsSection == null)

View File

@ -1,17 +1,13 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.objects.Price;
import com.alttd.util.Logger;
import com.alttd.util.Utilities;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import java.io.File;
import java.util.HashMap;
import java.util.Set;
public class WorthConfig extends AbstractConfig {
@ -32,22 +28,16 @@ public class WorthConfig extends AbstractConfig {
}
public static Object2ObjectOpenHashMap<Material, Price> prices = new Object2ObjectOpenHashMap<>();
private static void loadWorth() {
private static void loadWorth() { //TODO test after removing points
prices.clear();
ConfigurationSection worth = config.getConfigurationSection("worth");
Set<String> points = worth.getKeys(false);
for (String point : points) {
ConfigurationSection pointSection = worth.getConfigurationSection(point);
Set<String> materials = worth.getConfigurationSection(point).getKeys(false);
for (String key : materials) {
Material material = Material.getMaterial(key);
if (material == null) {
Logger.warning("Invalid key in worth.yml: %.", key);
continue;
}
prices.put(Material.getMaterial(key), new Price(Utilities.round(pointSection.getDouble(key), 2), Integer.parseInt(point)));
Set<String> materials = worth.getKeys(false);
for (String key : materials) {
if (key == null) {
Logger.warning("Invalid key in worth.yml: %.", key);
continue;
}
prices.put(Material.getMaterial(key), new Price(Utilities.round(worth.getDouble(key), 2)));
}
}
}

View File

@ -1,10 +1,25 @@
package com.alttd.objects;
import com.alttd.config.Config;
import com.alttd.util.Utilities;
public record Price(double price, int points) {
public final class Price {
private final double price;
private final int points;
public Price(double price) {
this.price = price;
for (int key : Config.pointsRangeMap.keySet()) {
if (Config.pointsRangeMap.get(key).contains(price)) {
points = key;
return;
}
}
points = -1; //TODO check for if points is -1
}
public static Price addPrice(Price one, Price two) {
return (new Price(Utilities.round(one.price() + two.price(), 2), one.points() + two.points()));
return (new Price(Utilities.round(one.getPrice(1) + two.getPrice(1), 2)));
}
public double getPrice(int multiplier) {
@ -14,4 +29,5 @@ public record Price(double price, int points) {
public int getPoints() {
return (points);
}
}