Delay economy initialization to server load (#4216)

This commit is contained in:
Hannes Greule 2023-10-29 10:55:31 +01:00 committed by GitHub
parent 95c7f621fb
commit 1c3776b605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 13 deletions

View File

@ -23,13 +23,13 @@ import com.google.inject.Provides;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import com.google.inject.assistedinject.FactoryModuleBuilder; import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.plotsquared.bukkit.BukkitPlatform; import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.listener.ServerListener;
import com.plotsquared.bukkit.listener.SingleWorldListener; import com.plotsquared.bukkit.listener.SingleWorldListener;
import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.bukkit.player.BukkitPlayerManager;
import com.plotsquared.bukkit.queue.BukkitChunkCoordinator; import com.plotsquared.bukkit.queue.BukkitChunkCoordinator;
import com.plotsquared.bukkit.queue.BukkitQueueCoordinator; import com.plotsquared.bukkit.queue.BukkitQueueCoordinator;
import com.plotsquared.bukkit.schematic.BukkitSchematicHandler; import com.plotsquared.bukkit.schematic.BukkitSchematicHandler;
import com.plotsquared.bukkit.util.BukkitChunkManager; import com.plotsquared.bukkit.util.BukkitChunkManager;
import com.plotsquared.bukkit.util.BukkitEconHandler;
import com.plotsquared.bukkit.util.BukkitInventoryUtil; import com.plotsquared.bukkit.util.BukkitInventoryUtil;
import com.plotsquared.bukkit.util.BukkitRegionManager; import com.plotsquared.bukkit.util.BukkitRegionManager;
import com.plotsquared.bukkit.util.BukkitSetupUtils; import com.plotsquared.bukkit.util.BukkitSetupUtils;
@ -47,6 +47,9 @@ import com.plotsquared.core.inject.factory.ChunkCoordinatorBuilderFactory;
import com.plotsquared.core.inject.factory.ChunkCoordinatorFactory; import com.plotsquared.core.inject.factory.ChunkCoordinatorFactory;
import com.plotsquared.core.inject.factory.HybridPlotWorldFactory; import com.plotsquared.core.inject.factory.HybridPlotWorldFactory;
import com.plotsquared.core.inject.factory.ProgressSubscriberFactory; 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.DefaultPlotAreaManager;
import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotAreaManager; import com.plotsquared.core.plot.world.SinglePlotAreaManager;
@ -72,6 +75,8 @@ import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Objects;
public class BukkitModule extends AbstractModule { public class BukkitModule extends AbstractModule {
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + BukkitModule.class.getSimpleName()); private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + BukkitModule.class.getSimpleName());
@ -128,21 +133,64 @@ public class BukkitModule extends AbstractModule {
@Provides @Provides
@Singleton @Singleton
@NonNull EconHandler provideEconHandler() { @NonNull EconHandler provideEconHandler() {
if (!Settings.Enabled_Components.ECONOMY) { if (!Settings.Enabled_Components.ECONOMY || !Bukkit.getPluginManager().isPluginEnabled("Vault")) {
return EconHandler.nullEconHandler(); return EconHandler.nullEconHandler();
} }
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) { // Guice eagerly initializes singletons, so we need to bring the laziness ourselves
try { return new LazyEconHandler();
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) { private static final class LazyEconHandler extends EconHandler implements ServerListener.MutableEconHandler {
private volatile EconHandler implementation;
public void setImplementation(EconHandler econHandler) {
this.implementation = econHandler;
} }
@Override
public boolean init() {
return get().init();
} }
return EconHandler.nullEconHandler();
@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.");
}
} }
} }

View File

@ -21,9 +21,14 @@ package com.plotsquared.bukkit.listener;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.plotsquared.bukkit.BukkitPlatform; import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; 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.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.ConsolePlayer; 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.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -32,6 +37,8 @@ import org.checkerframework.checker.nullness.qual.NonNull;
public class ServerListener implements Listener { public class ServerListener implements Listener {
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + ServerListener.class.getSimpleName());
private final BukkitPlatform plugin; private final BukkitPlatform plugin;
@Inject @Inject
@ -45,6 +52,29 @@ public class ServerListener implements Listener {
new MVdWPlaceholders(this.plugin, this.plugin.placeholderRegistry()); new MVdWPlaceholders(this.plugin, this.plugin.placeholderRegistry());
ConsolePlayer.getConsole().sendMessage(TranslatableCaption.of("placeholder.hooked")); 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);
} }
} }