Fix the market
This commit is contained in:
parent
6a655eb289
commit
b9e029cbe3
12 changed files with 52 additions and 42 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
Double money = PlayerStats.getPlayerStats(player).getStat("money", Double.class);
|
totalBuyCost += this.getPriceWithAmount(item, initialAmount - i);
|
||||||
if (!(money >= this.getPrice(item))) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
PlayerStats.getPlayerStats(player).setStat("money", money - this.getPrice(item));
|
|
||||||
boughtP += this.getPrice(item);
|
|
||||||
this.items.put(item.getKey().getKey(), this.items.get(item.getKey().getKey()) - 1L);
|
|
||||||
++bought;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bought > 0) {
|
// Add a 50% tax to ensure buy cost is always significantly higher than any potential sell revenue
|
||||||
player.getInventory().addItem(new ItemStack(item, bought));
|
double tax = totalBuyCost * 0.5;
|
||||||
|
double totalCostWithTax = totalBuyCost + tax;
|
||||||
|
|
||||||
|
Double money = PlayerStats.getPlayerStats(player).getStat("money", Double.class);
|
||||||
|
if (!(money >= totalCostWithTax)) {
|
||||||
|
return 0.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
return boughtP;
|
PlayerStats.getPlayerStats(player).setStat("money", money - totalCostWithTax);
|
||||||
|
this.items.put(item.getKey().getKey(), initialAmount - amount);
|
||||||
|
player.getInventory().addItem(new ItemStack(item, amount));
|
||||||
|
|
||||||
|
return totalCostWithTax;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,19 +139,22 @@ 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()));
|
||||||
double minPrice = ActiveConfig.getConfigValue(ConfigKey.MARKET_MIN_PRICE, Double.class);
|
|
||||||
double rt;
|
|
||||||
if (MarketBaseValues.marketBaseValues.containsKey(item)) {
|
|
||||||
rt = Math.max(Math.max(minPrice, Math.floor(MarketBaseValues.marketBaseValues.get(item) / (double) 100.0F)), MarketBaseValues.marketBaseValues.get(item) + this.NegMarketSaturation - Math.sqrt((double) amount));
|
|
||||||
} else {
|
|
||||||
rt = Math.max(minPrice, this.NegMarketSaturation / (double) 2.0F - Math.sqrt((double) amount));
|
|
||||||
}
|
|
||||||
|
|
||||||
return rt;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getPriceWithAmount(Material item, long amount) {
|
||||||
|
double minPrice = ActiveConfig.getConfigValue(ConfigKey.MARKET_MIN_PRICE, Double.class);
|
||||||
|
double rt;
|
||||||
|
if (MarketBaseValues.marketBaseValues.containsKey(item)) {
|
||||||
|
rt = Math.max(Math.max(minPrice, Math.floor(MarketBaseValues.marketBaseValues.get(item) / (double) 100.0F)), MarketBaseValues.marketBaseValues.get(item) + this.NegMarketSaturation - Math.sqrt((double) amount));
|
||||||
|
} else {
|
||||||
|
rt = Math.max(minPrice, this.NegMarketSaturation / (double) 2.0F - Math.sqrt((double) amount));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue