Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: taxes and fees #157

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use nix
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "de.eldoria"
version = "1.0.8"
version = "1.1.0"

subprojects {
apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Order(Plugin plugin, ACompanyData companyData, AOrderData orderData, Econ
builder.withDefaultCommand(list);
commands.add(new Abort(plugin, companyData, orderData, list));
commands.add(new Accept(plugin, companyData, orderData, configuration, messageBlocker));
commands.add(new Deliver(plugin, companyData, orderData, economy, info, messageBlocker));
commands.add(new Deliver(plugin, companyData, orderData, economy, info, messageBlocker, configuration));
commands.add(list);
commands.add(info);
commands.add(new Search(plugin, orderData, economy, messageBlocker));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package de.eldoria.companies.commands.company.order;

import de.eldoria.companies.configuration.Configuration;
import de.eldoria.companies.data.repository.ACompanyData;
import de.eldoria.companies.data.repository.AOrderData;
import de.eldoria.companies.data.wrapper.company.CompanyProfile;
Expand Down Expand Up @@ -35,8 +36,9 @@ public class Deliver extends AdvancedCommand implements IPlayerTabExecutor {
private final AOrderData orderData;
private final Info info;
private final MessageBlocker messageBlocker;
private final Configuration configuration;

public Deliver(Plugin plugin, ACompanyData companyData, AOrderData orderData, Economy economy, Info info, MessageBlocker messageBlocker) {
public Deliver(Plugin plugin, ACompanyData companyData, AOrderData orderData, Economy economy, Info info, MessageBlocker messageBlocker, Configuration configuration) {
super(plugin, CommandMeta.builder("deliver")
.addArgument("words.id", true)
.addArgument("words.material", true)
Expand All @@ -47,6 +49,7 @@ public Deliver(Plugin plugin, ACompanyData companyData, AOrderData orderData, Ec
this.companyData = companyData;
this.info = info;
this.messageBlocker = messageBlocker;
this.configuration = configuration;
}

@Override
Expand Down Expand Up @@ -148,14 +151,15 @@ private void orderDone(FullOrder order, CompanyProfile profile) {
.callEvent(new OrderDoneEvent(order, profile)));
CompletableBukkitFuture.runAsync(() -> {
for (var entry : payments.entrySet()) {
double taxed = configuration.companySettings().taxes().take(entry.getValue());
var player = plugin().getServer()
.getOfflinePlayer(entry.getKey());
var event = new OrderPaymentEvent(order, player, entry.getValue());
var event = new OrderPaymentEvent(order, player, taxed);
plugin().getServer()
.getPluginManager()
.callEvent(event);
if (event.isCancelled()) continue;
economy.depositPlayer(player, entry.getValue());
economy.depositPlayer(player, taxed);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private List<String> completeAmount(@NotNull Arguments args, OrderBuilder builde
if (args.sizeIs(3)) {
var material = EnumUtil.parse(args.asString(1), Material.class);
var max = configuration.orderSetting()
.maxItems() - builder.amount(material.orElse(null));
.maxItems() - builder.amount(material.orElse(null));
return Completion.completeInt(args.asString(2), 1, max);
}

Expand All @@ -189,7 +189,7 @@ private List<String> completeAdd(@NotNull Arguments args, OrderBuilder builder)
if (args.asString(2)
.isEmpty()) return Collections.singletonList(localizer().localize("words.amount"));
var max = configuration.orderSetting()
.maxItems() - builder.amount();
.maxItems() - builder.amount();
return Completion.completeInt(args.asString(2), 1, max);
}
var amount = Parser.parseInt(args.asString(2));
Expand Down Expand Up @@ -309,7 +309,7 @@ private void amount(Player player, Arguments args) throws CommandException {

var builder = getPlayerBuilder(player);
builder.changeContentAmount(material, Math.min(configuration.orderSetting()
.maxItems() - builder.amount(material), amount));
.maxItems() - builder.amount(material), amount));
}

private void done(Player player) throws CommandException {
Expand All @@ -322,6 +322,8 @@ private void done(Player player) throws CommandException {
.isEmpty(), "order.create.error.empty", TagResolver.empty());

var price = order.price();
var fee = configuration.orderSetting().fees().orderFee(price);
var fullPrice = price + fee;

orderData.retrievePlayerOrderCount(player)
.whenComplete(count -> {
Expand All @@ -331,10 +333,10 @@ private void done(Player player) throws CommandException {
return;
}
CompletableBukkitFuture.supplyAsync(() -> {
if (!economy.has(player, price)) {
if (!economy.has(player, fullPrice)) {
return false;
}
economy.withdrawPlayer(player, price);
economy.withdrawPlayer(player, fullPrice);
return true;
})
.whenComplete(result -> {
Expand All @@ -352,7 +354,7 @@ private void done(Player player) throws CommandException {
.isBlank() ? fallbackCurr : economy.currencyNamePlural();
messageSender().sendError(player, "error.insufficientCurrency",
Replacement.create("currency", curr),
Replacement.create("amount", economy.format(price)));
Replacement.create("amount", economy.format(fullPrice)));
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package de.eldoria.companies.configuration.elements;

import de.eldoria.companies.configuration.elements.companylevel.CompanyLevel;
import de.eldoria.companies.configuration.elements.companysettings.CompanyTaxes;
import de.eldoria.companies.data.wrapper.company.CompanyStats;

import java.util.ArrayList;
Expand All @@ -20,6 +21,7 @@ public class CompanySettings {
private double renamePrice = 10000.0;
private int expiredOrderPenalty = 3;
private int abortedOrderPenalty = 1;
private CompanyTaxes taxes = new CompanyTaxes();

public CompanySettings() {
updateLevel();
Expand All @@ -32,6 +34,10 @@ private void updateLevel() {
}
}

public CompanyTaxes taxes() {
return taxes;
}

public int deliveryHours() {
return deliveryHours;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/
package de.eldoria.companies.configuration.elements;

import de.eldoria.companies.configuration.elements.ordersettings.OrderFees;

@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal", "RedundantNoArgConstructor"})
public class OrderSettings {
private int maxItems = 64 * 8;
private int maxMaterials = 5;
private int maxUnclaimedHours = 24 * 7;
private OrderFees fees = new OrderFees();

public OrderSettings() {
}
Expand Down Expand Up @@ -37,4 +40,8 @@ public int maxUnclaimedHours() {
public void maxUnclaimedHours(int maxUnclaimedHours) {
this.maxUnclaimedHours = maxUnclaimedHours;
}

public OrderFees fees() {
return fees;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C EldoriaRPG Team and Contributor
*/
package de.eldoria.companies.configuration.elements.companysettings;

public class CompanyTaxes {
private double percent = 0.1;

/**
* Returns the amount after subtracting the taxes
*
* @param amount amount
* @return amount after taxes
*/
public double take(double amount) {
return (1 - percent) * amount;
}

public double percent() {
return percent;
}

public void percent(double percent) {
this.percent = percent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C EldoriaRPG Team and Contributor
*/
package de.eldoria.companies.configuration.elements.ordersettings;

public enum FeeMode {
/**
* No fees applied
*/
NONE,
/**
* The max value of percent or fixed
*/
MAX,
/**
* A percentage of the sum
*/
PERCENT,
/**
* A fixed amount
*/
FIXED;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C EldoriaRPG Team and Contributor
*/
package de.eldoria.companies.configuration.elements.ordersettings;

import de.eldoria.companies.orders.OrderBuilder;

@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal", "RedundantNoArgConstructor"})
public class OrderFees {
private FeeMode mode = FeeMode.NONE;
private double fixed = 200.0;
private double percent = .1;

public OrderFees() {
}

/**
* Get the fees for the items contained in the order builder
*
* @param builder builder of the order
* @return a double which represents the fee to publish this order.
*/
public double orderFee(OrderBuilder builder) {
double price = builder.price();
return switch (mode) {
case NONE -> .0;
case MAX -> Math.max(fixed, price * percent);
case PERCENT -> price * percent;
case FIXED -> fixed;
};
}
/**
* Get the fees for the items contained in the order builder
*
* @param price price of the order
* @return a double which represents the fee to publish this order.
*/
public double orderFee(double price) {
return switch (mode) {
case NONE -> .0;
case MAX -> Math.max(fixed, price * percent);
case PERCENT -> price * percent;
case FIXED -> fixed;
};
}



public FeeMode mode() {
return mode;
}

public double fixed() {
return fixed;
}

public double percent() {
return percent;
}

public void mode(FeeMode mode) {
this.mode = mode;
}

public void fixed(double fixed) {
this.fixed = fixed;
}

public void percent(double percent) {
this.percent = percent;
}
}
Loading