001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.bukkit.inject;
020
021import com.google.inject.AbstractModule;
022import com.google.inject.Provides;
023import com.google.inject.Singleton;
024import com.google.inject.assistedinject.FactoryModuleBuilder;
025import com.plotsquared.bukkit.BukkitPlatform;
026import com.plotsquared.bukkit.listener.SingleWorldListener;
027import com.plotsquared.bukkit.player.BukkitPlayerManager;
028import com.plotsquared.bukkit.queue.BukkitChunkCoordinator;
029import com.plotsquared.bukkit.queue.BukkitQueueCoordinator;
030import com.plotsquared.bukkit.schematic.BukkitSchematicHandler;
031import com.plotsquared.bukkit.util.BukkitChunkManager;
032import com.plotsquared.bukkit.util.BukkitEconHandler;
033import com.plotsquared.bukkit.util.BukkitInventoryUtil;
034import com.plotsquared.bukkit.util.BukkitRegionManager;
035import com.plotsquared.bukkit.util.BukkitSetupUtils;
036import com.plotsquared.bukkit.util.BukkitUtil;
037import com.plotsquared.bukkit.util.fawe.FaweRegionManager;
038import com.plotsquared.bukkit.util.fawe.FaweSchematicHandler;
039import com.plotsquared.core.PlotPlatform;
040import com.plotsquared.core.PlotSquared;
041import com.plotsquared.core.configuration.Settings;
042import com.plotsquared.core.generator.HybridGen;
043import com.plotsquared.core.generator.IndependentPlotGenerator;
044import com.plotsquared.core.inject.annotations.ConsoleActor;
045import com.plotsquared.core.inject.annotations.DefaultGenerator;
046import com.plotsquared.core.inject.factory.ChunkCoordinatorBuilderFactory;
047import com.plotsquared.core.inject.factory.ChunkCoordinatorFactory;
048import com.plotsquared.core.inject.factory.HybridPlotWorldFactory;
049import com.plotsquared.core.inject.factory.ProgressSubscriberFactory;
050import com.plotsquared.core.plot.world.DefaultPlotAreaManager;
051import com.plotsquared.core.plot.world.PlotAreaManager;
052import com.plotsquared.core.plot.world.SinglePlotAreaManager;
053import com.plotsquared.core.queue.ChunkCoordinator;
054import com.plotsquared.core.queue.GlobalBlockQueue;
055import com.plotsquared.core.queue.QueueProvider;
056import com.plotsquared.core.queue.subscriber.DefaultProgressSubscriber;
057import com.plotsquared.core.queue.subscriber.ProgressSubscriber;
058import com.plotsquared.core.util.ChunkManager;
059import com.plotsquared.core.util.EconHandler;
060import com.plotsquared.core.util.InventoryUtil;
061import com.plotsquared.core.util.PlayerManager;
062import com.plotsquared.core.util.RegionManager;
063import com.plotsquared.core.util.SchematicHandler;
064import com.plotsquared.core.util.SetupUtils;
065import com.plotsquared.core.util.WorldUtil;
066import com.sk89q.worldedit.bukkit.WorldEditPlugin;
067import com.sk89q.worldedit.extension.platform.Actor;
068import org.apache.logging.log4j.LogManager;
069import org.apache.logging.log4j.Logger;
070import org.bukkit.Bukkit;
071import org.bukkit.command.ConsoleCommandSender;
072import org.bukkit.plugin.java.JavaPlugin;
073import org.checkerframework.checker.nullness.qual.NonNull;
074
075public class BukkitModule extends AbstractModule {
076
077    private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + BukkitModule.class.getSimpleName());
078
079    private final BukkitPlatform bukkitPlatform;
080
081    public BukkitModule(final @NonNull BukkitPlatform bukkitPlatform) {
082        this.bukkitPlatform = bukkitPlatform;
083    }
084
085    @Override
086    protected void configure() {
087        bind(PlayerManager.class).to(BukkitPlayerManager.class);
088        bind(JavaPlugin.class).toInstance(bukkitPlatform);
089        bind(PlotPlatform.class).toInstance(bukkitPlatform);
090        bind(BukkitPlatform.class).toInstance(bukkitPlatform);
091        bind(IndependentPlotGenerator.class).annotatedWith(DefaultGenerator.class).to(HybridGen.class);
092        // Console actor
093        @NonNull ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
094        WorldEditPlugin wePlugin = ((WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"));
095        bind(Actor.class).annotatedWith(ConsoleActor.class).toInstance(wePlugin.wrapCommandSender(console));
096        bind(InventoryUtil.class).to(BukkitInventoryUtil.class);
097        bind(SetupUtils.class).to(BukkitSetupUtils.class);
098        bind(WorldUtil.class).to(BukkitUtil.class);
099        install(new FactoryModuleBuilder()
100                .implement(ProgressSubscriber.class, DefaultProgressSubscriber.class)
101                .build(ProgressSubscriberFactory.class));
102        bind(ChunkManager.class).to(BukkitChunkManager.class);
103        if (PlotSquared.platform().isFaweHooking()) {
104            bind(SchematicHandler.class).to(FaweSchematicHandler.class);
105            bind(RegionManager.class).to(FaweRegionManager.class);
106        } else {
107            bind(SchematicHandler.class).to(BukkitSchematicHandler.class);
108            bind(RegionManager.class).to(BukkitRegionManager.class);
109        }
110        bind(GlobalBlockQueue.class).toInstance(new GlobalBlockQueue(QueueProvider.of(BukkitQueueCoordinator.class)));
111        if (Settings.Enabled_Components.WORLDS) {
112            bind(PlotAreaManager.class).to(SinglePlotAreaManager.class);
113            try {
114                bind(SingleWorldListener.class).toInstance(new SingleWorldListener());
115            } catch (Exception e) {
116                e.printStackTrace();
117            }
118        } else {
119            bind(PlotAreaManager.class).to(DefaultPlotAreaManager.class);
120        }
121        install(new FactoryModuleBuilder().build(HybridPlotWorldFactory.class));
122        install(new FactoryModuleBuilder()
123                .implement(ChunkCoordinator.class, BukkitChunkCoordinator.class)
124                .build(ChunkCoordinatorFactory.class));
125        install(new FactoryModuleBuilder().build(ChunkCoordinatorBuilderFactory.class));
126    }
127
128    @Provides
129    @Singleton
130    @NonNull EconHandler provideEconHandler() {
131        if (!Settings.Enabled_Components.ECONOMY) {
132            return EconHandler.nullEconHandler();
133        }
134        if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
135            try {
136                BukkitEconHandler econHandler = new BukkitEconHandler();
137                if (!econHandler.init()) {
138                    LOGGER.warn("Economy is enabled but no plugin is providing an economy service. Falling back...");
139                    return EconHandler.nullEconHandler();
140                }
141                return econHandler;
142            } catch (final Exception ignored) {
143            }
144        }
145        return EconHandler.nullEconHandler();
146    }
147
148}