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.core.generator;
020
021import com.plotsquared.core.configuration.ConfigurationNode;
022import com.plotsquared.core.configuration.ConfigurationSection;
023import com.plotsquared.core.configuration.ConfigurationUtil;
024import com.plotsquared.core.configuration.Settings;
025import com.plotsquared.core.configuration.caption.TranslatableCaption;
026import com.plotsquared.core.configuration.file.YamlConfiguration;
027import com.plotsquared.core.inject.annotations.WorldConfig;
028import com.plotsquared.core.plot.BlockBucket;
029import com.plotsquared.core.plot.PlotId;
030import com.plotsquared.core.queue.GlobalBlockQueue;
031import com.sk89q.worldedit.function.pattern.Pattern;
032import com.sk89q.worldedit.world.block.BlockTypes;
033import org.apache.logging.log4j.LogManager;
034import org.apache.logging.log4j.Logger;
035import org.checkerframework.checker.nullness.qual.NonNull;
036
037import javax.annotation.Nullable;
038
039@SuppressWarnings("WeakerAccess")
040public abstract class ClassicPlotWorld extends SquarePlotWorld {
041
042    private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + ClassicPlotWorld.class.getSimpleName());
043
044    public int ROAD_HEIGHT = 62;
045    public int PLOT_HEIGHT = 62;
046    public int WALL_HEIGHT = 62;
047    public BlockBucket MAIN_BLOCK = new BlockBucket(BlockTypes.STONE);
048    public BlockBucket TOP_BLOCK = new BlockBucket(BlockTypes.GRASS_BLOCK);
049    public BlockBucket WALL_BLOCK = new BlockBucket(BlockTypes.STONE_SLAB);
050    public BlockBucket CLAIMED_WALL_BLOCK = new BlockBucket(BlockTypes.SANDSTONE_SLAB);
051    public BlockBucket WALL_FILLING = new BlockBucket(BlockTypes.STONE);
052    public BlockBucket ROAD_BLOCK = new BlockBucket(BlockTypes.QUARTZ_BLOCK);
053    public boolean PLOT_BEDROCK = true;
054    public boolean PLACE_TOP_BLOCK = true;
055
056    public ClassicPlotWorld(
057            final @NonNull String worldName,
058            final @Nullable String id,
059            final @NonNull IndependentPlotGenerator generator,
060            final @Nullable PlotId min,
061            final @Nullable PlotId max,
062            @WorldConfig final @NonNull YamlConfiguration worldConfiguration,
063            final @NonNull GlobalBlockQueue blockQueue
064    ) {
065        super(worldName, id, generator, min, max, worldConfiguration, blockQueue);
066    }
067
068    private static BlockBucket createCheckedBlockBucket(String input, BlockBucket def) {
069        final BlockBucket bucket = new BlockBucket(input);
070        Pattern pattern = null;
071        try {
072            pattern = bucket.toPattern();
073        } catch (Exception ignore) {
074        }
075        if (pattern == null) {
076            LOGGER.error("Failed to parse pattern '{}', check your worlds.yml", input);
077            LOGGER.error("Falling back to {}", def);
078            return def;
079        }
080        return bucket;
081    }
082
083    /**
084     * CONFIG NODE | DEFAULT VALUE | DESCRIPTION | CONFIGURATION TYPE | REQUIRED FOR INITIAL SETUP.
085     *
086     * <p>Set the last boolean to false if you do not check a specific config node to be set while using the setup
087     * command - this may be useful if a config value can be changed at a later date, and has no impact on the actual
088     * world generation</p>
089     */
090    @NonNull
091    @Override
092    public ConfigurationNode[] getSettingNodes() {
093        return new ConfigurationNode[]{
094                new ConfigurationNode("plot.height", this.PLOT_HEIGHT, TranslatableCaption.of("setup.plot_height"),
095                        ConfigurationUtil.INTEGER
096                ),
097                new ConfigurationNode("plot.size", this.PLOT_WIDTH, TranslatableCaption.of("setup.plot_width"),
098                        ConfigurationUtil.INTEGER
099                ),
100                new ConfigurationNode("plot.filling", this.MAIN_BLOCK, TranslatableCaption.of("setup.plot_block"),
101                        ConfigurationUtil.BLOCK_BUCKET
102                ),
103                new ConfigurationNode("wall.place_top_block", this.PLACE_TOP_BLOCK,
104                        TranslatableCaption.of("setup.top_block_boolean"), ConfigurationUtil.BOOLEAN
105                ),
106                new ConfigurationNode("plot.floor", this.TOP_BLOCK, TranslatableCaption.of("setup.plot_block_floor"),
107                        ConfigurationUtil.BLOCK_BUCKET
108                ),
109                new ConfigurationNode("wall.block", this.WALL_BLOCK, TranslatableCaption.of("setup.top_wall_block"),
110                        ConfigurationUtil.BLOCK_BUCKET
111                ),
112                new ConfigurationNode("wall.block_claimed", this.CLAIMED_WALL_BLOCK,
113                        TranslatableCaption.of("setup.wall_block_claimed"), ConfigurationUtil.BLOCK_BUCKET
114                ),
115                new ConfigurationNode("road.width", this.ROAD_WIDTH, TranslatableCaption.of("setup.road_width"),
116                        ConfigurationUtil.INTEGER
117                ),
118                new ConfigurationNode("road.height", this.ROAD_HEIGHT, TranslatableCaption.of("setup.road_height"),
119                        ConfigurationUtil.INTEGER
120                ),
121                new ConfigurationNode("road.block", this.ROAD_BLOCK, TranslatableCaption.of("setup.road_block"),
122                        ConfigurationUtil.BLOCK_BUCKET
123                ),
124                new ConfigurationNode("wall.filling", this.WALL_FILLING, TranslatableCaption.of("setup.wall_filling_block"),
125                        ConfigurationUtil.BLOCK_BUCKET
126                ),
127                new ConfigurationNode("wall.height", this.WALL_HEIGHT, TranslatableCaption.of("setup.wall_height"),
128                        ConfigurationUtil.INTEGER
129                ),
130                new ConfigurationNode("plot.bedrock", this.PLOT_BEDROCK, TranslatableCaption.of("setup.bedrock_boolean"),
131                        ConfigurationUtil.BOOLEAN
132                )};
133    }
134
135    /**
136     * This method is called when a world loads. Make sure you set all your constants here. You are provided with the
137     * configuration section for that specific world.
138     */
139    @Override
140    public void loadConfiguration(ConfigurationSection config) {
141        super.loadConfiguration(config);
142        this.PLOT_BEDROCK = config.getBoolean("plot.bedrock");
143        this.PLOT_HEIGHT = Math.min(getMaxGenHeight(), config.getInt("plot.height"));
144        this.MAIN_BLOCK = createCheckedBlockBucket(config.getString("plot.filling"), MAIN_BLOCK);
145        this.TOP_BLOCK = createCheckedBlockBucket(config.getString("plot.floor"), TOP_BLOCK);
146        this.WALL_BLOCK = createCheckedBlockBucket(config.getString("wall.block"), WALL_BLOCK);
147        this.ROAD_HEIGHT = Math.min(getMaxGenHeight(), config.getInt("road.height"));
148        this.ROAD_BLOCK = createCheckedBlockBucket(config.getString("road.block"), ROAD_BLOCK);
149        this.WALL_FILLING = createCheckedBlockBucket(config.getString("wall.filling"), WALL_FILLING);
150        this.PLACE_TOP_BLOCK = config.getBoolean("wall.place_top_block");
151        this.WALL_HEIGHT = Math.min(getMaxGenHeight() - (PLACE_TOP_BLOCK ? 1 : 0), config.getInt("wall.height"));
152        this.CLAIMED_WALL_BLOCK = createCheckedBlockBucket(config.getString("wall.block_claimed"), CLAIMED_WALL_BLOCK);
153    }
154
155    int schematicStartHeight() {
156        int plotRoadMin = Math.min(PLOT_HEIGHT, ROAD_HEIGHT);
157        if (!Settings.Schematics.USE_WALL_IN_ROAD_SCHEM_HEIGHT) {
158            return plotRoadMin;
159        }
160        return Math.min(WALL_HEIGHT, plotRoadMin);
161    }
162
163}