Skip to content

Commit

Permalink
Migrate to new Vault provider and economy integration (#3975)
Browse files Browse the repository at this point in the history
This commit:
- Implements a new provider for VaultAPI's `Economy`
  - The legacy provider built into Vault uses player names, and has not changed since Vault was invented in 1864.
  - This properly supports UUIDs and works more predictably with EssentialsX.
- Replaces the Register method economy abstraction layer abstraction layer with a new `EconomyLayer` economy abstraction layer abstraction layer.
  - This opens the pathway for future economy abstraction layers to be supported.
  - This change also removes dubiously-licensed code from the project.

For users encountering userdata issues on this build, see this FAQ entry:
#3956 (comment)

Fixes #4110.
Closes #3344.
Closes #2401.
  • Loading branch information
JRoy committed May 10, 2021
1 parent 8b23c2c commit 071f995
Show file tree
Hide file tree
Showing 15 changed files with 666 additions and 838 deletions.
13 changes: 10 additions & 3 deletions Essentials/src/main/java/com/earth2me/essentials/Essentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import com.earth2me.essentials.commands.NoChargeException;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import com.earth2me.essentials.commands.QuietAbortException;
import com.earth2me.essentials.economy.EconomyLayers;
import com.earth2me.essentials.economy.vault.VaultEconomyProvider;
import com.earth2me.essentials.items.AbstractItemDb;
import com.earth2me.essentials.items.CustomItemResolver;
import com.earth2me.essentials.items.FlatItemDb;
import com.earth2me.essentials.items.LegacyItemDb;
import com.earth2me.essentials.metrics.MetricsWrapper;
import com.earth2me.essentials.perm.PermissionsDefaults;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import com.earth2me.essentials.signs.SignBlockListener;
import com.earth2me.essentials.signs.SignEntityListener;
import com.earth2me.essentials.signs.SignPlayerListener;
Expand Down Expand Up @@ -98,6 +99,7 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.bukkit.scheduler.BukkitScheduler;
Expand Down Expand Up @@ -158,8 +160,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private transient Map<String, IEssentialsCommand> commandMap = new HashMap<>();

static {
// TODO: improve legacy code
Methods.init();
EconomyLayers.init();
}

public Essentials() {
Expand Down Expand Up @@ -204,6 +205,12 @@ public void setupForTesting(final Server server) throws IOException, InvalidDesc
kits = new Kits(this);
}

@Override
public void onLoad() {
// Vault registers their Essentials provider at low priority, so we have to use normal priority here
getServer().getServicesManager().register(net.milkbowl.vault.economy.Economy.class, new VaultEconomyProvider(this), this, ServicePriority.Normal);
}

@Override
public void onEnable() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.earth2me.essentials;

import com.earth2me.essentials.register.payment.Methods;
import com.earth2me.essentials.economy.EconomyLayer;
import com.earth2me.essentials.economy.EconomyLayers;
import net.ess3.api.IEssentials;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -12,9 +13,19 @@

public class EssentialsPluginListener implements Listener, IConf {
private final transient IEssentials ess;
private boolean serverLoaded = false;

public EssentialsPluginListener(final IEssentials ess) {
this.ess = ess;

// Run on first server tick
ess.scheduleSyncDelayedTask(() -> {
if (EconomyLayers.getSelectedLayer() == null || serverLoaded) {
return;
}
serverLoaded = true;
EconomyLayers.onServerLoad();
});
}

@EventHandler(priority = EventPriority.MONITOR)
Expand All @@ -25,8 +36,9 @@ public void onPluginEnable(final PluginEnableEvent event) {
ess.getPermissionsHandler().setUseSuperperms(ess.getSettings().useBukkitPermissions());
ess.getPermissionsHandler().checkPermissions();
ess.getAlternativeCommandsHandler().addPlugin(event.getPlugin());
if (!Methods.hasMethod() && Methods.setMethod(ess.getServer().getPluginManager())) {
ess.getLogger().log(Level.INFO, "Payment method found (" + Methods.getMethod().getLongName() + " version: " + Methods.getMethod().getVersion() + ")");
final EconomyLayer layer = EconomyLayers.onPluginEnable(event.getPlugin());
if (layer != null) {
ess.getLogger().log(Level.INFO, "Essentials found a compatible payment resolution method: " + layer.getName() + " (v" + layer.getPluginVersion() + ")!");
}
}

Expand All @@ -37,10 +49,13 @@ public void onPluginDisable(final PluginDisableEvent event) {
}
ess.getPermissionsHandler().checkPermissions();
ess.getAlternativeCommandsHandler().removePlugin(event.getPlugin());
// Check to see if the plugin thats being disabled is the one we are using
if (Methods.hasMethod() && Methods.checkDisabled(event.getPlugin())) {
Methods.reset();
ess.getLogger().log(Level.INFO, "Payment method was disabled. No longer accepting payments.");
if (EconomyLayers.onPluginDisable(event.getPlugin(), serverLoaded)) {
final EconomyLayer layer = EconomyLayers.getSelectedLayer();
if (layer != null) {
ess.getLogger().log(Level.INFO, "Essentials found a new compatible payment resolution method: " + layer.getName() + " (v" + layer.getPluginVersion() + ")!");
} else {
ess.getLogger().log(Level.INFO, "Active payment resolution method has been disabled! Falling back to Essentials' default payment resolution system!");
}
}
}

Expand Down
50 changes: 17 additions & 33 deletions Essentials/src/main/java/com/earth2me/essentials/User.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.earth2me.essentials;

import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.economy.EconomyLayer;
import com.earth2me.essentials.economy.EconomyLayers;
import com.earth2me.essentials.messaging.IMessageRecipient;
import com.earth2me.essentials.messaging.SimpleMessageRecipient;
import com.earth2me.essentials.register.payment.Method;
import com.earth2me.essentials.register.payment.Methods;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.FormatUtil;
Expand Down Expand Up @@ -485,16 +485,9 @@ private BigDecimal _getMoney() {
}
return BigDecimal.ZERO;
}
if (Methods.hasMethod()) {
try {
final Method method = Methods.getMethod();
if (!method.hasAccount(this.getName())) {
throw new Exception();
}
final Method.MethodAccount account = Methods.getMethod().getAccount(this.getName());
return BigDecimal.valueOf(account.balance());
} catch (final Exception ignored) {
}
final EconomyLayer layer = EconomyLayers.getSelectedLayer();
if (layer != null && (layer.hasAccount(getBase()) || layer.createPlayerAccount(getBase()))) {
return layer.getBalance(getBase());
}
return super.getMoney();
}
Expand All @@ -512,31 +505,22 @@ public void setMoney(final BigDecimal value, final UserBalanceUpdateEvent.Cause
ess.getServer().getPluginManager().callEvent(updateEvent);
final BigDecimal newBalance = updateEvent.getNewBalance();

if (Methods.hasMethod()) {
try {
final Method method = Methods.getMethod();
if (!method.hasAccount(this.getName())) {
throw new Exception();
}
final Method.MethodAccount account = Methods.getMethod().getAccount(this.getName());
account.set(newBalance.doubleValue());
} catch (final Exception ignored) {
}
final EconomyLayer layer = EconomyLayers.getSelectedLayer();
if (layer != null && (layer.hasAccount(getBase()) || layer.createPlayerAccount(getBase()))) {
layer.set(getBase(), newBalance);
}
super.setMoney(newBalance, true);
Trade.log("Update", "Set", "API", getName(), new Trade(newBalance, ess), null, null, null, newBalance, ess);
}

public void updateMoneyCache(final BigDecimal value) {
if (ess.getSettings().isEcoDisabled()) {
if (ess.getSettings().isEcoDisabled() || !EconomyLayers.isLayerSelected() || super.getMoney().equals(value)) {
return;
}
if (Methods.hasMethod() && !super.getMoney().equals(value)) {
try {
super.setMoney(value, false);
} catch (final MaxMoneyException ex) {
// We don't want to throw any errors here, just updating a cache
}
try {
super.setMoney(value, false);
} catch (final MaxMoneyException ex) {
// We don't want to throw any errors here, just updating a cache
}
}

Expand Down Expand Up @@ -629,7 +613,7 @@ public boolean checkJailTimeout(final long currentTime) {
}
}

if (getJailTimeout() < currentTime && isJailed() ) {
if (getJailTimeout() < currentTime && isJailed()) {
final JailStatusChangeEvent event = new JailStatusChangeEvent(this, null, false);
ess.getServer().getPluginManager().callEvent(event);

Expand Down Expand Up @@ -724,9 +708,9 @@ public void checkActivity() {

final long autoafkkick = ess.getSettings().getAutoAfkKick();
if (autoafkkick > 0
&& lastActivity > 0 && (lastActivity + (autoafkkick * 1000)) < System.currentTimeMillis()
&& !isAuthorized("essentials.kick.exempt")
&& !isAuthorized("essentials.afk.kickexempt")) {
&& lastActivity > 0 && (lastActivity + (autoafkkick * 1000)) < System.currentTimeMillis()
&& !isAuthorized("essentials.kick.exempt")
&& !isAuthorized("essentials.afk.kickexempt")) {
final String kickReason = tl("autoAfkKickReason", autoafkkick / 60.0);
lastActivity = 0;
this.getBase().kickPlayer(kickReason);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void trackUUID(final UUID uuid, final String name, final boolean replace)
if (!names.containsKey(keyName)) {
names.put(keyName, uuid);
uuidMap.writeUUIDMap();
} else if (!names.get(keyName).equals(uuid)) {
} else if (!isUUIDMatch(uuid, keyName)) {
if (replace) {
ess.getLogger().info("Found new UUID for " + name + ". Replacing " + names.get(keyName).toString() + " with " + uuid.toString());
names.put(keyName, uuid);
Expand All @@ -143,6 +143,10 @@ public void trackUUID(final UUID uuid, final String name, final boolean replace)
}
}

public boolean isUUIDMatch(final UUID uuid, final String name) {
return names.containsKey(name) && names.get(name).equals(uuid);
}

@Override
public User load(final String stringUUID) throws Exception {
final UUID uuid = UUID.fromString(stringUUID);
Expand Down
Loading

0 comments on commit 071f995

Please sign in to comment.