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.plot.world; 020 021import com.plotsquared.core.location.Location; 022import com.plotsquared.core.plot.PlotArea; 023import com.plotsquared.core.plot.PlotAreaType; 024import com.plotsquared.core.util.StringMan; 025import com.sk89q.worldedit.regions.CuboidRegion; 026import org.checkerframework.checker.nullness.qual.NonNull; 027import org.checkerframework.checker.nullness.qual.Nullable; 028 029import java.util.Collections; 030import java.util.HashSet; 031import java.util.Set; 032import java.util.function.Consumer; 033 034public interface PlotAreaManager { 035 036 /** 037 * Get the plot area for a particular location. This 038 * method assumes that the caller already knows that 039 * the location belongs to a plot area, in which 040 * case it will return the appropriate plot area. 041 * <p> 042 * If the location does not belong to a plot area, 043 * it may still return an area. 044 * 045 * @param location The location 046 * @return An applicable area, or null 047 */ 048 @Nullable PlotArea getApplicablePlotArea(@Nullable Location location); 049 050 /** 051 * Get the plot area, if there is any, for the given 052 * location. This may return null, if given location 053 * does not belong to a plot area. 054 * 055 * @param location The location 056 * @return The area if found, else {@code null} 057 */ 058 @Nullable PlotArea getPlotArea(@NonNull Location location); 059 060 /** 061 * Get the plot area in a world with an (optional ID). 062 * If the world has more than one plot area, and ID must be 063 * supplied. If the world only has one plot area, the ID will 064 * be ignored 065 * 066 * @param world World name 067 * @param id Area ID 068 * @return Plot area matching the criteria 069 */ 070 @Nullable PlotArea getPlotArea(@NonNull String world, @Nullable String id); 071 072 /** 073 * Get all plot areas in a world, with an optional region constraint 074 * 075 * @param world World name 076 * @param region Optional region 077 * @return All plots in the region 078 */ 079 @NonNull PlotArea[] getPlotAreas(@NonNull String world, @Nullable CuboidRegion region); 080 081 /** 082 * Get all plot areas recognized by PlotSquared 083 * 084 * @return All plot areas 085 */ 086 @NonNull PlotArea[] getAllPlotAreas(); 087 088 /** 089 * Get all worlds recognized by PlotSquared 090 * 091 * @return All world names 092 */ 093 @NonNull String[] getAllWorlds(); 094 095 /** 096 * Add a plot area 097 * 098 * @param area Area 099 */ 100 void addPlotArea(@NonNull PlotArea area); 101 102 /** 103 * Remove a plot area 104 * 105 * @param area Area 106 */ 107 void removePlotArea(@NonNull PlotArea area); 108 109 /** 110 * Add a world 111 * 112 * @param worldName Name of the world to add 113 * @return {@code true} if successful, {@code false} if world already existed 114 * @since 7.0.0 115 */ 116 boolean addWorld(@NonNull String worldName); 117 118 /** 119 * Remove a world 120 * 121 * @param worldName Name of the world to remove 122 */ 123 void removeWorld(@NonNull String worldName); 124 125 /** 126 * Method that delegates to {@link #getPlotAreas(String, CuboidRegion)} but returns an 127 * immutable set, instead of an array 128 * 129 * @param world World name 130 * @param region Optional region 131 * @return All areas in the world (and region) 132 */ 133 default @NonNull Set<@NonNull PlotArea> getPlotAreasSet( 134 final @NonNull String world, 135 final @Nullable CuboidRegion region 136 ) { 137 final PlotArea[] areas = this.getPlotAreas(world, region); 138 final Set<PlotArea> set = new HashSet<>(); 139 Collections.addAll(set, areas); 140 return Collections.unmodifiableSet(set); 141 } 142 143 /** 144 * Method identical to {@link #getPlotAreasSet(String, CuboidRegion)} but that 145 * does not take in a region, and returns a modifiable set 146 * 147 * @param world World name 148 * @return Modifiable set containing all plot areas in the specified world 149 */ 150 default @NonNull Set<@NonNull PlotArea> getPlotAreasSet(final @NonNull String world) { 151 final Set<PlotArea> set = new HashSet<>(); 152 Collections.addAll(set, this.getPlotAreas(world, null)); 153 return set; 154 } 155 156 /** 157 * Get a plot area from a search string in the format "world;id" or "world,id" 158 * where the ID portion is optional 159 * 160 * @param search Search string 161 * @return An area that matches the search string, or {@code null} 162 */ 163 default @Nullable PlotArea getPlotAreaByString(final @NonNull String search) { 164 String[] split = search.split("[;,]"); 165 PlotArea[] areas = this.getPlotAreas(split[0], null); 166 if (areas == null) { 167 for (PlotArea area : this.getAllPlotAreas()) { 168 if (area.getWorldName().equalsIgnoreCase(split[0])) { 169 if (area.getId() == null || split.length == 2 && area.getId() 170 .equalsIgnoreCase(split[1])) { 171 return area; 172 } 173 } 174 } 175 return null; 176 } 177 if (areas.length == 1) { 178 return areas[0]; 179 } else if (split.length == 1) { 180 return null; 181 } else { 182 for (PlotArea area : areas) { 183 if (StringMan.isEqual(split[1], area.getId())) { 184 return area; 185 } 186 } 187 return null; 188 } 189 } 190 191 /** 192 * Check if a plot world. 193 * 194 * <p> 195 * Use {@link #getPlotAreaByString(String)} to get the PlotArea object 196 * </p> 197 * 198 * @param world the world 199 * @return if a plot world is registered 200 */ 201 default boolean hasPlotArea(final @NonNull String world) { 202 return this.getPlotAreas(world, null).length != 0; 203 } 204 205 /** 206 * Check if a given world is an augmented plot world 207 * 208 * @param world World name 209 * @return {@code true} if the world is augmented plot world, {@code false} if not 210 */ 211 default boolean isAugmented(final @NonNull String world) { 212 final PlotArea[] areas = this.getPlotAreas(world, null); 213 return areas != null && (areas.length > 1 || areas[0].getType() != PlotAreaType.NORMAL); 214 } 215 216 /** 217 * Perform an action on each recognized plot area 218 * 219 * @param action Action to perform 220 */ 221 default void forEachPlotArea(final @NonNull Consumer<? super PlotArea> action) { 222 for (final PlotArea area : this.getAllPlotAreas()) { 223 action.accept(area); 224 } 225 } 226 227}