Skip to content

Commit

Permalink
Delay economy initialization to server load (#4216)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Oct 29, 2023
1 parent 95c7f62 commit 1c3776b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import com.google.inject.Singleton;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.listener.ServerListener;
import com.plotsquared.bukkit.listener.SingleWorldListener;
import com.plotsquared.bukkit.player.BukkitPlayerManager;
import com.plotsquared.bukkit.queue.BukkitChunkCoordinator;
import com.plotsquared.bukkit.queue.BukkitQueueCoordinator;
import com.plotsquared.bukkit.schematic.BukkitSchematicHandler;
import com.plotsquared.bukkit.util.BukkitChunkManager;
import com.plotsquared.bukkit.util.BukkitEconHandler;
import com.plotsquared.bukkit.util.BukkitInventoryUtil;
import com.plotsquared.bukkit.util.BukkitRegionManager;
import com.plotsquared.bukkit.util.BukkitSetupUtils;
Expand All @@ -47,6 +47,9 @@
import com.plotsquared.core.inject.factory.ChunkCoordinatorFactory;
import com.plotsquared.core.inject.factory.HybridPlotWorldFactory;
import com.plotsquared.core.inject.factory.ProgressSubscriberFactory;
import com.plotsquared.core.player.OfflinePlotPlayer;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.world.DefaultPlotAreaManager;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotAreaManager;
Expand All @@ -72,6 +75,8 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Objects;

public class BukkitModule extends AbstractModule {

private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + BukkitModule.class.getSimpleName());
Expand Down Expand Up @@ -128,21 +133,64 @@ protected void configure() {
@Provides
@Singleton
@NonNull EconHandler provideEconHandler() {
if (!Settings.Enabled_Components.ECONOMY) {
if (!Settings.Enabled_Components.ECONOMY || !Bukkit.getPluginManager().isPluginEnabled("Vault")) {
return EconHandler.nullEconHandler();
}
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
try {
BukkitEconHandler econHandler = new BukkitEconHandler();
if (!econHandler.init()) {
LOGGER.warn("Economy is enabled but no plugin is providing an economy service. Falling back...");
return EconHandler.nullEconHandler();
}
return econHandler;
} catch (final Exception ignored) {
}
// Guice eagerly initializes singletons, so we need to bring the laziness ourselves
return new LazyEconHandler();
}

private static final class LazyEconHandler extends EconHandler implements ServerListener.MutableEconHandler {
private volatile EconHandler implementation;

public void setImplementation(EconHandler econHandler) {
this.implementation = econHandler;
}
return EconHandler.nullEconHandler();

@Override
public boolean init() {
return get().init();
}

@Override
public double getBalance(final PlotPlayer<?> player) {
return get().getBalance(player);
}

@Override
public void withdrawMoney(final PlotPlayer<?> player, final double amount) {
get().withdrawMoney(player, amount);
}

@Override
public void depositMoney(final PlotPlayer<?> player, final double amount) {
get().depositMoney(player, amount);
}

@Override
public void depositMoney(final OfflinePlotPlayer player, final double amount) {
get().depositMoney(player, amount);
}

@Override
public boolean isEnabled(final PlotArea plotArea) {
return get().isEnabled(plotArea);
}

@Override
public @NonNull String format(final double balance) {
return get().format(balance);
}

@Override
public boolean isSupported() {
return get().isSupported();
}

private EconHandler get() {
return Objects.requireNonNull(this.implementation, "EconHandler not ready yet.");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
import com.google.inject.Inject;
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.placeholder.MVdWPlaceholders;
import com.plotsquared.bukkit.util.BukkitEconHandler;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.util.EconHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -32,6 +37,8 @@

public class ServerListener implements Listener {

private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + ServerListener.class.getSimpleName());

private final BukkitPlatform plugin;

@Inject
Expand All @@ -45,6 +52,29 @@ public void onServerLoad(ServerLoadEvent event) {
new MVdWPlaceholders(this.plugin, this.plugin.placeholderRegistry());
ConsolePlayer.getConsole().sendMessage(TranslatableCaption.of("placeholder.hooked"));
}
if (Settings.Enabled_Components.ECONOMY && Bukkit.getPluginManager().isPluginEnabled("Vault")) {
EconHandler econHandler = new BukkitEconHandler();
try {
if (!econHandler.init()) {
LOGGER.warn("Economy is enabled but no plugin is providing an economy service. Falling back...");
econHandler = EconHandler.nullEconHandler();
}
} catch (final Exception ignored) {
econHandler = EconHandler.nullEconHandler();
}
if (PlotSquared.platform().econHandler() instanceof MutableEconHandler meh) {
meh.setImplementation(econHandler);
}
}
}

/**
* Internal use only. Required to implement lazy econ loading using Guice.
*
* @since TODO
*/
public interface MutableEconHandler {
void setImplementation(EconHandler econHandler);
}

}

0 comments on commit 1c3776b

Please sign in to comment.