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.util;
020
021import com.plotsquared.bukkit.generator.BukkitAugmentedGenerator;
022import com.plotsquared.core.PlotSquared;
023import com.plotsquared.core.generator.GeneratorWrapper;
024import com.plotsquared.core.util.SetupUtils;
025import org.bukkit.World;
026import org.bukkit.generator.ChunkGenerator;
027
028import java.lang.reflect.Field;
029import java.util.ArrayList;
030
031public class SetGenCB {
032
033    public static void setGenerator(World world) throws Exception {
034        PlotSquared.platform().setupUtils().updateGenerators(false);
035        PlotSquared.get().removePlotAreas(world.getName());
036        ChunkGenerator gen = world.getGenerator();
037        if (gen == null) {
038            return;
039        }
040        String name = gen.getClass().getCanonicalName();
041        boolean set = false;
042        for (GeneratorWrapper<?> wrapper : SetupUtils.generators.values()) {
043            ChunkGenerator newGen = (ChunkGenerator) wrapper.getPlatformGenerator();
044            if (newGen == null) {
045                newGen = (ChunkGenerator) wrapper;
046            }
047            if (newGen.getClass().getCanonicalName().equals(name)) {
048                // set generator
049                Field generator = world.getClass().getDeclaredField("generator");
050                Field populators = world.getClass().getDeclaredField("populators");
051                generator.setAccessible(true);
052                populators.setAccessible(true);
053                // Set populators (just in case)
054                populators.set(world, new ArrayList<>());
055                // Set generator
056                generator.set(world, newGen);
057                populators.set(world, newGen.getDefaultPopulators(world));
058                // end
059                set = true;
060                break;
061            }
062        }
063        if (!set) {
064            world.getPopulators()
065                    .removeIf(blockPopulator -> blockPopulator instanceof BukkitAugmentedGenerator);
066        }
067        PlotSquared.get()
068                .loadWorld(world.getName(), PlotSquared.platform().getGenerator(world.getName(), null));
069    }
070
071}