Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/com/yourorg/servershop/gui/SellMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,30 @@ public void refresh(Player p) {
}

private void sellAll(Player p) {
double total = 0.0; int stacks = 0;
double subtotal = 0.0; double taxTotal = 0.0; double total = 0.0; int stacks = 0; int qtyTotal = 0;
double taxRate = plugin.getConfig().getDouble("taxRate", 0.0);
for (int i = 0; i < p.getInventory().getSize(); i++) {
ItemStack s = p.getInventory().getItem(i);
if (s == null) continue; var m = s.getType();
var e = plugin.catalog().get(m).orElse(null); if (e == null || !e.canSell()) continue;
int qty = s.getAmount();
double unit = plugin.shop().priceSell(m);
double amount = unit * qty;
total += amount; stacks++;
double tax = amount * taxRate;
double payout = amount - tax;
subtotal += amount; taxTotal += tax; total += payout; stacks++; qtyTotal += qty;
p.getInventory().setItem(i, null);
plugin.logger().logAsync(new com.yourorg.servershop.logging.Transaction(java.time.Instant.now(), p.getName(), com.yourorg.servershop.logging.Transaction.Type.SELL, m, qty, amount));
plugin.logger().logAsync(new com.yourorg.servershop.logging.Transaction(java.time.Instant.now(), p.getName(), com.yourorg.servershop.logging.Transaction.Type.SELL, m, qty, payout));
// TODO: consider calling plugin.dynamic().adjustOnSell(m, qty) per TODO list
}
if (total > 0) plugin.economy().depositPlayer(p, total);
p.sendMessage(plugin.prefixed(plugin.getConfig().getString("messages.soldall").replace("%count%", String.valueOf(stacks)).replace("%total%", String.format("%.2f", total))));
if (qtyTotal > 0) {
double avgUnit = subtotal / qtyTotal;
String breakdown = org.bukkit.ChatColor.translateAlternateColorCodes('&',
"&6$"+String.format("%.2f", avgUnit)+" &7× &e"+qtyTotal+" &7→ &c$"+String.format("%.2f", taxTotal)+" &7→ &a$"+String.format("%.2f", total));
p.sendMessage(plugin.prefixed(breakdown));
}
refresh(p);
}

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/yourorg/servershop/shop/ShopService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public Optional<String> buy(Player p, Material mat, int qty) {
String cat = plugin.catalog().categoryOf(mat);
if (!plugin.categorySettings().isEnabled(cat)) return Optional.of("Category disabled: "+cat);
double unit = plugin.dynamic().buyPrice(mat, opt.get().buyPrice());
double total = unit * qty;
double subtotal = unit * qty;
double taxRate = plugin.getConfig().getDouble("taxRate", 0.0);
double tax = subtotal * taxRate;
double total = subtotal + tax;
var econ = plugin.economy();
if (econ.getBalance(p) + 1e-9 < total) {
double need = Math.max(0, total - econ.getBalance(p));
Expand All @@ -34,6 +37,9 @@ public Optional<String> buy(Player p, Material mat, int qty) {
plugin.dynamic().adjustOnBuy(mat, qty);
plugin.logger().logAsync(new Transaction(Instant.now(), p.getName(), Transaction.Type.BUY, mat, qty, total));
p.sendMessage(plugin.prefixed(msg("purchased").replace("%qty%", String.valueOf(qty)).replace("%material%", mat.name()).replace("%price%", fmt(total))));
String breakdown = org.bukkit.ChatColor.translateAlternateColorCodes('&',
"&6$"+fmt(unit)+" &7× &e"+qty+" &7→ &c$"+fmt(tax)+" &7→ &a$"+fmt(total));
p.sendMessage(plugin.prefixed(breakdown));
return Optional.empty();
}

Expand All @@ -45,11 +51,17 @@ public Optional<String> sell(Player p, Material mat, int qty) {
int removed = removeFromInventory(p, mat, qty);
if (removed <= 0) return Optional.of("You don't have that.");
double unit = plugin.dynamic().sellPrice(mat, opt.get().sellPrice());
double total = unit * removed;
double subtotal = unit * removed;
double taxRate = plugin.getConfig().getDouble("taxRate", 0.0);
double tax = subtotal * taxRate;
double total = subtotal - tax;
plugin.economy().depositPlayer(p, total);
plugin.dynamic().adjustOnSell(mat, removed);
plugin.logger().logAsync(new Transaction(Instant.now(), p.getName(), Transaction.Type.SELL, mat, removed, total));
p.sendMessage(plugin.prefixed(msg("sold").replace("%qty%", String.valueOf(removed)).replace("%material%", mat.name()).replace("%price%", fmt(total))));
String breakdown = org.bukkit.ChatColor.translateAlternateColorCodes('&',
"&6$"+fmt(unit)+" &7× &e"+removed+" &7→ &c$"+fmt(tax)+" &7→ &a$"+fmt(total));
p.sendMessage(plugin.prefixed(breakdown));
return Optional.empty();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
messages:
prefix: '&6[Shop] &7'
taxRate: 0.0
weekly:
count: 6
discount: 0.80
Expand Down
Loading