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.setup;
020
021
022import com.plotsquared.core.PlotSquared;
023import com.plotsquared.core.plot.PlotArea;
024import com.plotsquared.core.plot.PlotAreaTerrainType;
025import com.plotsquared.core.plot.PlotAreaType;
026import com.plotsquared.core.plot.PlotId;
027import com.plotsquared.core.util.SetupUtils;
028import org.jetbrains.annotations.Contract;
029import org.jetbrains.annotations.NotNull;
030import org.jetbrains.annotations.Nullable;
031
032import java.util.Objects;
033
034public class PlotAreaBuilder {
035
036    private String generatorName;
037    private String plotManager;
038    @Nullable
039    private PlotAreaType plotAreaType;
040    private PlotAreaTerrainType terrainType;
041    private String worldName;
042    private String areaName;
043    private PlotId minimumId;
044    private PlotId maximumId;
045    private SettingsNodesWrapper settingsNodesWrapper;
046    private SetupUtils setupManager;
047
048    private PlotAreaBuilder() {
049    }
050
051    public static PlotAreaBuilder newBuilder() {
052        return new PlotAreaBuilder();
053    }
054
055    public static PlotAreaBuilder ofPlotArea(PlotArea area) {
056        return new PlotAreaBuilder()
057                .worldName(area.getWorldName())
058                .areaName(area.getId())
059                .plotAreaType(area.getType())
060                .terrainType(area.getTerrain())
061                .generatorName(area.getGenerator().getName())
062                .plotManager(PlotSquared.platform().pluginName())
063                .minimumId(area.getMin())
064                .maximumId(area.getMax())
065                .settingsNodesWrapper(new SettingsNodesWrapper(area.getSettingNodes(), null));
066    }
067
068    public PlotAreaBuilder minimumId(PlotId minimumId) {
069        if (this.maximumId != null
070                && (minimumId.getX() > this.maximumId.getX() || minimumId.getY() > this.maximumId.getY())) {
071            throw new IllegalStateException("minId >= maxId");
072        }
073        this.minimumId = minimumId;
074        return this;
075    }
076
077    public PlotAreaBuilder maximumId(PlotId maximumId) {
078        if (this.minimumId != null
079                && (maximumId.getX() < this.minimumId.getX() || maximumId.getY() < this.minimumId.getY())) {
080            throw new IllegalStateException("maxId <= minId");
081        }
082        this.maximumId = maximumId;
083        return this;
084    }
085
086    public String generatorName() {
087        return this.generatorName;
088    }
089
090    public String plotManager() {
091        return this.plotManager;
092    }
093
094    @NotNull
095    @Contract(" -> !null")
096    public PlotAreaType plotAreaType() {
097        return Objects.requireNonNullElse(this.plotAreaType, PlotAreaType.NORMAL);
098    }
099
100    public PlotAreaTerrainType terrainType() {
101        return this.terrainType;
102    }
103
104    public String worldName() {
105        return this.worldName;
106    }
107
108    public String areaName() {
109        return this.areaName;
110    }
111
112    public PlotId minimumId() {
113        return this.minimumId;
114    }
115
116    public PlotId maximumId() {
117        return this.maximumId;
118    }
119
120    public SettingsNodesWrapper settingsNodesWrapper() {
121        return this.settingsNodesWrapper;
122    }
123
124    public SetupUtils setupManager() {
125        return this.setupManager;
126    }
127
128    public PlotAreaBuilder generatorName(String generatorName) {
129        this.generatorName = generatorName;
130        return this;
131    }
132
133    public PlotAreaBuilder plotManager(String plotManager) {
134        this.plotManager = plotManager;
135        return this;
136    }
137
138    public PlotAreaBuilder plotAreaType(@NotNull PlotAreaType plotAreaType) {
139        Objects.requireNonNull(plotAreaType, "PlotAreaType must not be null");
140        this.plotAreaType = plotAreaType;
141        return this;
142    }
143
144    public PlotAreaBuilder terrainType(PlotAreaTerrainType terrainType) {
145        this.terrainType = terrainType;
146        return this;
147    }
148
149    public PlotAreaBuilder worldName(String worldName) {
150        this.worldName = worldName;
151        return this;
152    }
153
154    public PlotAreaBuilder areaName(String areaName) {
155        this.areaName = areaName;
156        return this;
157    }
158
159    public PlotAreaBuilder settingsNodesWrapper(SettingsNodesWrapper settingsNodesWrapper) {
160        this.settingsNodesWrapper = settingsNodesWrapper;
161        return this;
162    }
163
164    public PlotAreaBuilder setupManager(SetupUtils setupManager) {
165        this.setupManager = setupManager;
166        return this;
167    }
168
169}