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.listener;
020
021import com.google.inject.Inject;
022import com.plotsquared.bukkit.generator.BukkitPlotGenerator;
023import com.plotsquared.core.PlotSquared;
024import com.plotsquared.core.generator.GeneratorWrapper;
025import com.plotsquared.core.plot.world.PlotAreaManager;
026import com.plotsquared.core.plot.world.SinglePlotAreaManager;
027import org.bukkit.World;
028import org.bukkit.event.EventHandler;
029import org.bukkit.event.EventPriority;
030import org.bukkit.event.Listener;
031import org.bukkit.event.world.WorldInitEvent;
032import org.bukkit.generator.ChunkGenerator;
033import org.checkerframework.checker.nullness.qual.NonNull;
034
035@SuppressWarnings("unused")
036public class WorldEvents implements Listener {
037
038    private final PlotAreaManager plotAreaManager;
039
040    @Inject
041    public WorldEvents(final @NonNull PlotAreaManager plotAreaManager) {
042        this.plotAreaManager = plotAreaManager;
043    }
044
045    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
046    public void onWorldInit(WorldInitEvent event) {
047        World world = event.getWorld();
048        String name = world.getName();
049        if (this.plotAreaManager instanceof final SinglePlotAreaManager single) {
050            if (single.isWorld(name)) {
051                world.setKeepSpawnInMemory(false);
052                return;
053            }
054        }
055        ChunkGenerator gen = world.getGenerator();
056        if (gen instanceof GeneratorWrapper) {
057            PlotSquared.get().loadWorld(name, (GeneratorWrapper<?>) gen);
058        } else {
059            PlotSquared.get().loadWorld(name, new BukkitPlotGenerator(name, gen, this.plotAreaManager));
060        }
061    }
062
063}