Fix the market

This commit is contained in:
tyuXX 2026-07-11 17:22:48 +03:00
parent 6a655eb289
commit b9e029cbe3
12 changed files with 52 additions and 42 deletions

Binary file not shown.

Binary file not shown.

View file

@ -18,7 +18,7 @@ public class Sethome {
PlayerMetadata meta = PlayerMetadata.getPlayerMetadata(player); PlayerMetadata meta = PlayerMetadata.getPlayerMetadata(player);
PlayerStats playerStats = PlayerStats.getPlayerStats(player); PlayerStats playerStats = PlayerStats.getPlayerStats(player);
boolean hasMultiHome = player.hasPermission("grimmsserver.multiHome"); boolean hasMultiHome = player.hasPermission("grimmsserver.multiHome");
if (!hasMultiHome && (long) meta.homes.size() >= Math.round(Math.cbrt((double) playerStats.getStat("level", Integer.class))) && !meta.homes.containsKey(homeName)) { if (!hasMultiHome && (long) meta.homes.size() >= Math.round(Math.cbrt((double) playerStats.getStat("level", Integer.class))) + playerStats.getStat("prestige", Integer.class) && !meta.homes.containsKey(homeName)) {
player.sendMessage("You do not have permission to set multiple homes and your level isn't enough."); player.sendMessage("You do not have permission to set multiple homes and your level isn't enough.");
return true; return true;
} else { } else {

View file

@ -36,13 +36,14 @@ public class Market {
} else { } else {
PlayerStats playerStats = PlayerStats.getPlayerStats(player); PlayerStats playerStats = PlayerStats.getPlayerStats(player);
this.items.putIfAbsent(itemStack.getType().getKey().getKey(), 0L); this.items.putIfAbsent(itemStack.getType().getKey().getKey(), 0L);
long initialAmount = this.items.get(itemStack.getType().getKey().getKey());
double sold = 0.0F; double sold = 0.0F;
for (int i = 0; i < itemStack.getAmount(); ++i) { for (int i = 0; i < itemStack.getAmount(); ++i) {
sold += this.getISPrice(itemStack.getType(), itemStack.getEnchantments()); sold += this.getISPriceWithAmount(itemStack.getType(), itemStack.getEnchantments(), initialAmount + i);
} }
this.items.put(itemStack.getType().getKey().getKey(), this.items.get(itemStack.getType().getKey().getKey()) + (long) itemStack.getAmount()); this.items.put(itemStack.getType().getKey().getKey(), initialAmount + (long) itemStack.getAmount());
playerStats.setStat("money", playerStats.getStat("money", Double.class) + sold); playerStats.setStat("money", playerStats.getStat("money", Double.class) + sold);
player.getInventory().removeItem(itemStack); player.getInventory().removeItem(itemStack);
return sold; return sold;
@ -68,14 +69,15 @@ public class Market {
} else { } else {
PlayerStats playerStats = PlayerStats.getPlayerStats(player); PlayerStats playerStats = PlayerStats.getPlayerStats(player);
this.items.putIfAbsent(item.getKey().getKey(), 0L); this.items.putIfAbsent(item.getKey().getKey(), 0L);
long initialAmount = this.items.get(item.getKey().getKey());
Double sold = (double) 0.0F; Double sold = (double) 0.0F;
int amount = player.getInventory().all(item).values().stream().mapToInt(ItemStack::getAmount).sum(); int amount = player.getInventory().all(item).values().stream().mapToInt(ItemStack::getAmount).sum();
for (int i = 0; i < amount; ++i) { for (int i = 0; i < amount; ++i) {
sold = sold + this.getPrice(item); sold = sold + this.getPriceWithAmount(item, initialAmount + i);
} }
this.items.put(item.getKey().getKey(), this.items.get(item.getKey().getKey()) + (long) amount); this.items.put(item.getKey().getKey(), initialAmount + (long) amount);
playerStats.setStat("money", playerStats.getStat("money", Double.class) + sold); playerStats.setStat("money", playerStats.getStat("money", Double.class) + sold);
player.getInventory().remove(item); player.getInventory().remove(item);
return Data.of(sold, amount); return Data.of(sold, amount);
@ -88,26 +90,28 @@ public class Market {
} else if ((long) amount > this.items.get(item.getKey().getKey())) { } else if ((long) amount > this.items.get(item.getKey().getKey())) {
return 0.0F; return 0.0F;
} else { } else {
double boughtP = 0.0F; long initialAmount = this.items.get(item.getKey().getKey());
int bought = 0;
// Calculate total buy cost (prices at current market state, then decreasing amounts)
double totalBuyCost = 0.0;
for (int i = 0; i < amount; ++i) { for (int i = 0; i < amount; ++i) {
totalBuyCost += this.getPriceWithAmount(item, initialAmount - i);
}
// Add a 50% tax to ensure buy cost is always significantly higher than any potential sell revenue
double tax = totalBuyCost * 0.5;
double totalCostWithTax = totalBuyCost + tax;
Double money = PlayerStats.getPlayerStats(player).getStat("money", Double.class); Double money = PlayerStats.getPlayerStats(player).getStat("money", Double.class);
if (!(money >= this.getPrice(item))) { if (!(money >= totalCostWithTax)) {
break; return 0.0F;
} }
PlayerStats.getPlayerStats(player).setStat("money", money - this.getPrice(item)); PlayerStats.getPlayerStats(player).setStat("money", money - totalCostWithTax);
boughtP += this.getPrice(item); this.items.put(item.getKey().getKey(), initialAmount - amount);
this.items.put(item.getKey().getKey(), this.items.get(item.getKey().getKey()) - 1L); player.getInventory().addItem(new ItemStack(item, amount));
++bought;
}
if (bought > 0) { return totalCostWithTax;
player.getInventory().addItem(new ItemStack(item, bought));
}
return boughtP;
} }
} }
@ -135,7 +139,11 @@ public class Market {
if (this.items.get(item.getKey().getKey()) == null) { if (this.items.get(item.getKey().getKey()) == null) {
return 0.0F; return 0.0F;
} else { } else {
long amount = this.items.get(item.getKey().getKey()); return this.getPriceWithAmount(item, this.items.get(item.getKey().getKey()));
}
}
public double getPriceWithAmount(Material item, long amount) {
double minPrice = ActiveConfig.getConfigValue(ConfigKey.MARKET_MIN_PRICE, Double.class); double minPrice = ActiveConfig.getConfigValue(ConfigKey.MARKET_MIN_PRICE, Double.class);
double rt; double rt;
if (MarketBaseValues.marketBaseValues.containsKey(item)) { if (MarketBaseValues.marketBaseValues.containsKey(item)) {
@ -146,7 +154,6 @@ public class Market {
return rt; return rt;
} }
}
public double getISPrice(Material item, Map<Enchantment, Integer> enchantments) { public double getISPrice(Material item, Map<Enchantment, Integer> enchantments) {
double rt = this.getPrice(item); double rt = this.getPrice(item);
@ -160,4 +167,17 @@ public class Market {
return rt; return rt;
} }
public double getISPriceWithAmount(Material item, Map<Enchantment, Integer> enchantments, long amount) {
double rt = this.getPriceWithAmount(item, amount);
for (Enchantment e : enchantments.keySet()) {
Double enchantValue = EnchantBaseValues.enchantBaseValues.get(e);
if (enchantValue != null) {
rt += enchantValue * Math.sqrt(enchantments.get(e));
}
}
return rt;
}
} }

View file

@ -26,16 +26,6 @@ public class InventoryClickEvent {
} }
return; return;
} }
// Handle SettingGUI
if (event.getView().getTitle().equals(SettingGUI.GUI_TITLE)) {
event.setCancelled(true);
SettingGUI settingGUI = SettingGUI.getGUI(player);
if (settingGUI != null) {
settingGUI.handleClick(event.getSlot());
}
return;
}
} }
} }
} }