Skip to content

Commit

Permalink
Update API to 2.3, Add new Eco API to conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed Aug 11, 2024
1 parent de0e784 commit bda9f5e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Vault
version: 2.2.1
version: 2.3.0
description: A PR-friendly version of Vault; Where PRs are actually accepted.
authors: [cereal, Sleaker, mung3r, creatorfromhell]
website: https://tnemc.net
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<bukkitVersion>1.13.1-R0.1-SNAPSHOT</bukkitVersion>
<mainClass>${project.groupId}.${project.artifactId}</mainClass>
<api.version>2.2</api.version>
<api.version>2.3</api.version>
</properties>

<!-- Project information -->
Expand Down
92 changes: 78 additions & 14 deletions src/net/milkbowl/vault/Vault.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
Expand All @@ -45,7 +46,7 @@

public class Vault extends JavaPlugin {

private static final String VAULT_BUKKIT_URL = "https://dev.bukkit.org/projects/Vault";
private static final String VAULT_BUKKIT_URL = "https://www.spigotmc.org/resources/vaultunlocked.117277/";
private Logger log;
private String newVersionTitle = "";
private double newVersion = 0;
Expand Down Expand Up @@ -108,8 +109,9 @@ public boolean onCommand(CommandSender sender, Command command, String commandLa

private void convertCommand(CommandSender sender, String[] args) {
final Collection<RegisteredServiceProvider<Economy>> econs = this.getServer().getServicesManager().getRegistrations(Economy.class);
final Collection<RegisteredServiceProvider<net.milkbowl.vault2.economy.Economy>> econs2 = this.getServer().getServicesManager().getRegistrations(net.milkbowl.vault2.economy.Economy.class);

if (econs == null || econs.size() < 2) {
if (econs == null || econs.size() < 2 && econs2.isEmpty()) {

sender.sendMessage("You must have at least 2 economies loaded to convert.");
return;
Expand All @@ -118,8 +120,11 @@ private void convertCommand(CommandSender sender, String[] args) {
sender.sendMessage("You must specify only the economy to convert from and the economy to convert to. (names should not contain spaces)");
return;
}

Economy econ1 = null;
Economy econ2 = null;
net.milkbowl.vault2.economy.Economy econ1Unlocked = null;
net.milkbowl.vault2.economy.Economy econ2Unlocked = null;

final StringBuilder economies = new StringBuilder();
for (RegisteredServiceProvider<Economy> econ : econs) {
Expand All @@ -140,32 +145,91 @@ private void convertCommand(CommandSender sender, String[] args) {
economies.append(econName);
}

if (econ1 == null) {
for (RegisteredServiceProvider<net.milkbowl.vault2.economy.Economy> econ : econs2) {

final String econName = econ.getProvider().getName().replace(" ", "");
if (econName.equalsIgnoreCase(args[0])) {

econ1Unlocked = econ.getProvider();
} else if (econName.equalsIgnoreCase(args[1])) {

econ2Unlocked = econ.getProvider();
}

if (economies.length() > 0) {

economies.append(", ");
}
economies.append(econName);
}

if (econ1 == null && econ1Unlocked == null) {

sender.sendMessage("Could not find " + args[0] + " loaded on the server, check your spelling.");
sender.sendMessage("Valid economies are: " + economies);
return;
} else if (econ2 == null) {
} else if (econ2 == null && econ2Unlocked == null) {

sender.sendMessage("Could not find " + args[1] + " loaded on the server, check your spelling.");
sender.sendMessage("Valid economies are: " + economies);
return;
}

sender.sendMessage("This may take some time to convert, expect server lag.");
final boolean useUnlocked1 = (econ1Unlocked != null);
final boolean useUnlocked2 = (econ2Unlocked != null);
final String pluginID = "vault conversion";

for (OfflinePlayer op : Bukkit.getServer().getOfflinePlayers()) {
if (econ1.hasAccount(op)) {
if (econ2.hasAccount(op)) {
continue;

if(useUnlocked1) {
if(useUnlocked2) {
if(econ2Unlocked.hasAccount(op.getUniqueId())) {
continue;
}
econ2Unlocked.createAccount(op.getUniqueId(), op.getName());
final BigDecimal diff = econ1Unlocked.getBalance(pluginID, op.getUniqueId()).subtract(econ2Unlocked.getBalance(pluginID, op.getUniqueId()));
if(diff.compareTo(BigDecimal.ZERO) > 0) {
econ2Unlocked.deposit(pluginID, op.getUniqueId(), diff);
} else if (diff.compareTo(BigDecimal.ZERO) < 0) {
econ2Unlocked.withdraw(pluginID, op.getUniqueId(), diff.negate());
}
} else {
if(econ2.hasAccount(op)) {
continue;
}
econ2.createPlayerAccount(op);
final BigDecimal diff = econ1Unlocked.getBalance(pluginID, op.getUniqueId()).subtract(BigDecimal.valueOf(econ2.getBalance(op)));
if(diff.compareTo(BigDecimal.ZERO) > 0) {
econ2.depositPlayer(op, diff.doubleValue());
} else if (diff.compareTo(BigDecimal.ZERO) < 0) {
econ2.withdrawPlayer(op, diff.negate().doubleValue());
}
}
econ2.createPlayerAccount(op);
double diff = econ1.getBalance(op) - econ2.getBalance(op);
if (diff > 0) {
econ2.depositPlayer(op, diff);
} else if (diff < 0) {
econ2.withdrawPlayer(op, -diff);
} else {
if(useUnlocked2) {
if(econ2Unlocked.hasAccount(op.getUniqueId())) {
continue;
}
econ2Unlocked.createAccount(op.getUniqueId(), op.getName());
final BigDecimal diff = BigDecimal.valueOf(econ1.getBalance(op)).subtract(econ2Unlocked.getBalance(pluginID, op.getUniqueId()));
if(diff.compareTo(BigDecimal.ZERO) > 0) {
econ2Unlocked.deposit(pluginID, op.getUniqueId(), diff);
} else if (diff.compareTo(BigDecimal.ZERO) < 0) {
econ2Unlocked.withdraw(pluginID, op.getUniqueId(), diff.negate());
}
} else {
if(econ2.hasAccount(op)) {
continue;
}
econ2.createPlayerAccount(op);
final BigDecimal diff = BigDecimal.valueOf(econ1.getBalance(op)).subtract(BigDecimal.valueOf(econ2.getBalance(op)));
if(diff.compareTo(BigDecimal.ZERO) > 0) {
econ2.depositPlayer(op, diff.doubleValue());
} else if (diff.compareTo(BigDecimal.ZERO) < 0) {
econ2.withdrawPlayer(op, diff.negate().doubleValue());
}
}

}
}
sender.sendMessage("Conversion complete, please verify the data before using it.");
Expand Down

0 comments on commit bda9f5e

Please sign in to comment.