-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from m7rlin/dev/marcin
(chore) release 1.3.0
- Loading branch information
Showing
11 changed files
with
248 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
plugin/src/main/java/pl/mgtm/magicznakraina/api/economy/DecimalFormats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pl.mgtm.magicznakraina.api.economy; | ||
|
||
import java.text.DecimalFormat; | ||
|
||
public enum DecimalFormats { | ||
FORMAT_1("#,##0.00"), | ||
FORMAT_2("#,##0.000"), | ||
FORMAT_3("#,##0.0000"), | ||
FORMAT_4("#,###0.00"), | ||
FORMAT_5("0.00"), | ||
FORMAT_6("0.000"), | ||
FORMAT_7("#,###,###.00"), | ||
FORMAT_8("###0.00"), | ||
FORMAT_9("0.00%"), | ||
FORMAT_10("0.##E0"), | ||
EUROPEAN("#,##0.00"); | ||
|
||
private final String pattern; | ||
|
||
DecimalFormats(String pattern) { | ||
this.pattern = pattern; | ||
} | ||
|
||
public DecimalFormat getDecimalFormat() { | ||
return new DecimalFormat(pattern); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
plugin/src/main/java/pl/mgtm/magicznakraina/api/economy/EconomyAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package pl.mgtm.magicznakraina.api.economy; | ||
|
||
import org.bukkit.OfflinePlayer; | ||
|
||
import java.text.DecimalFormat; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class EconomyAPI { | ||
|
||
private Map<String, Double> balances; | ||
private double tax; | ||
private TaxType taxType; | ||
private String currencySymbol; | ||
private DecimalFormat decimalFormat; | ||
|
||
public EconomyAPI(double tax, TaxType taxType, String currencySymbol, DecimalFormats decimalFormat) { | ||
this.balances = new HashMap<>(); | ||
this.tax = tax; | ||
this.taxType = taxType; | ||
this.currencySymbol = currencySymbol; | ||
this.decimalFormat = decimalFormat.getDecimalFormat(); | ||
} | ||
|
||
public double getBalance(OfflinePlayer player) { | ||
String playerName = player.getUniqueId().toString(); | ||
if (balances.containsKey(playerName)) { | ||
return balances.get(playerName); | ||
} | ||
return 0.0; // Default balance if player does not exist | ||
} | ||
|
||
public void deposit(OfflinePlayer player, double amount) { | ||
double balance = getBalance(player); | ||
balance += amount; | ||
balances.put(player.getName(), balance); | ||
} | ||
|
||
public void withdraw(OfflinePlayer player, double amount) { | ||
double balance = getBalance(player); | ||
balance -= amount; | ||
if (balance < 0) { | ||
balance = 0; | ||
} | ||
balances.put(player.getName(), balance); | ||
} | ||
|
||
public boolean has(OfflinePlayer player, double amount) { | ||
return getBalance(player) >= amount; | ||
} | ||
|
||
public boolean has(OfflinePlayer player, String worldName, double amount) { | ||
// Implementation-specific logic for checking balance in a specific world | ||
// If the economy plugin does not support per-world balances, the global balance will be returned | ||
return has(player, amount); | ||
} | ||
|
||
public void sendMoney(OfflinePlayer sender, OfflinePlayer recipient, double amount) { | ||
double senderBalance = getBalance(sender); | ||
double recipientBalance = getBalance(recipient); | ||
|
||
double senderTax = calculateTax(amount); | ||
double taxedAmount = amount - senderTax; | ||
|
||
if (senderBalance >= amount) { | ||
withdraw(sender, amount); | ||
deposit(recipient, taxedAmount); | ||
} | ||
} | ||
|
||
private double calculateTax(double amount) { | ||
if (taxType == TaxType.FIXED_AMOUNT) { | ||
return tax; | ||
} else if (taxType == TaxType.PERCENTAGE) { | ||
return amount * (tax / 100); | ||
} else if (taxType == TaxType.COMBINED) { | ||
return tax + (amount * (tax / 100)); | ||
} | ||
return 0.0; | ||
} | ||
|
||
public String formatBalance(double amount) { | ||
return currencySymbol + decimalFormat.format(amount); | ||
} | ||
|
||
public void createAccount(OfflinePlayer player, double initialBalance) { | ||
String playerName = player.getUniqueId().toString(); | ||
if (!balances.containsKey(playerName)) { | ||
balances.put(playerName, initialBalance); | ||
} | ||
} | ||
|
||
public void createAccount(String playerName, double initialBalance) { | ||
if (!balances.containsKey(playerName)) { | ||
balances.put(playerName, initialBalance); | ||
} | ||
} | ||
|
||
public void removeAccount(OfflinePlayer player) { | ||
String playerName = player.getUniqueId().toString(); | ||
if (balances.containsKey(playerName)) { | ||
balances.remove(playerName); | ||
} | ||
} | ||
|
||
public void removeAccount(String playerName) { | ||
if (balances.containsKey(playerName)) { | ||
balances.remove(playerName); | ||
} | ||
} | ||
|
||
public boolean hasAccount(OfflinePlayer player) { | ||
String playerName = player.getUniqueId().toString(); | ||
return balances.containsKey(playerName); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
plugin/src/main/java/pl/mgtm/magicznakraina/api/economy/TaxType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package pl.mgtm.magicznakraina.api.economy; | ||
|
||
public enum TaxType { | ||
FIXED_AMOUNT, | ||
PERCENTAGE, | ||
COMBINED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
plugin/src/main/java/pl/mgtm/magicznakraina/modules/economy/EconomyModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pl.mgtm.magicznakraina.modules.economy; | ||
|
||
import pl.mgtm.magicznakraina.MagicznaKraina; | ||
import pl.mgtm.magicznakraina.module.ModuleInfo; | ||
import pl.mgtm.magicznakraina.module.PluginModule; | ||
import pl.mgtm.magicznakraina.modules.economy.commands.MoneyCommand; | ||
|
||
@ModuleInfo(name = "economy") | ||
public class EconomyModule extends PluginModule { | ||
|
||
private final MagicznaKraina pl = MagicznaKraina.getInstance(); | ||
|
||
|
||
public EconomyModule() { | ||
super(); | ||
|
||
super.registerCommand(new MoneyCommand()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
plugin/src/main/java/pl/mgtm/magicznakraina/modules/economy/commands/MoneyCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pl.mgtm.magicznakraina.modules.economy.commands; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.entity.Player; | ||
import pl.mgtm.magicznakraina.MagicznaKraina; | ||
import pl.mgtm.magicznakraina.command.CommandInfo; | ||
import pl.mgtm.magicznakraina.command.PluginCommand; | ||
|
||
@CommandInfo(name = "money", permission = "", requiresPlayer = true) | ||
public class MoneyCommand extends PluginCommand { | ||
private MagicznaKraina pl = MagicznaKraina.getInstance(); | ||
|
||
@Override | ||
public void execute(Player player, String[] args) { | ||
super.execute(player, args); | ||
|
||
player.sendMessage(ChatColor.GRAY + "Twoje saldo: " + ChatColor.GREEN + pl.getEco().formatBalance(pl.getEco().getBalance(player))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,5 @@ commands: | |
- v | ||
resetworlds: | ||
description: Resetuje nether i end. | ||
money: | ||
description: Pokazuje ile masz pieniędzy. |