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.flag; 020 021import com.google.common.base.Preconditions; 022import com.plotsquared.core.plot.flag.implementations.AnalysisFlag; 023import com.plotsquared.core.plot.flag.implementations.AnimalAttackFlag; 024import com.plotsquared.core.plot.flag.implementations.AnimalCapFlag; 025import com.plotsquared.core.plot.flag.implementations.AnimalInteractFlag; 026import com.plotsquared.core.plot.flag.implementations.BeaconEffectsFlag; 027import com.plotsquared.core.plot.flag.implementations.BlockBurnFlag; 028import com.plotsquared.core.plot.flag.implementations.BlockIgnitionFlag; 029import com.plotsquared.core.plot.flag.implementations.BlockedCmdsFlag; 030import com.plotsquared.core.plot.flag.implementations.BreakFlag; 031import com.plotsquared.core.plot.flag.implementations.ChatFlag; 032import com.plotsquared.core.plot.flag.implementations.ConcreteHardenFlag; 033import com.plotsquared.core.plot.flag.implementations.CopperOxideFlag; 034import com.plotsquared.core.plot.flag.implementations.CoralDryFlag; 035import com.plotsquared.core.plot.flag.implementations.CropGrowFlag; 036import com.plotsquared.core.plot.flag.implementations.DenyExitFlag; 037import com.plotsquared.core.plot.flag.implementations.DenyPortalTravelFlag; 038import com.plotsquared.core.plot.flag.implementations.DenyPortalsFlag; 039import com.plotsquared.core.plot.flag.implementations.DenyTeleportFlag; 040import com.plotsquared.core.plot.flag.implementations.DescriptionFlag; 041import com.plotsquared.core.plot.flag.implementations.DeviceInteractFlag; 042import com.plotsquared.core.plot.flag.implementations.DisablePhysicsFlag; 043import com.plotsquared.core.plot.flag.implementations.DoneFlag; 044import com.plotsquared.core.plot.flag.implementations.DropProtectionFlag; 045import com.plotsquared.core.plot.flag.implementations.EditSignFlag; 046import com.plotsquared.core.plot.flag.implementations.EntityCapFlag; 047import com.plotsquared.core.plot.flag.implementations.EntityChangeBlockFlag; 048import com.plotsquared.core.plot.flag.implementations.ExplosionFlag; 049import com.plotsquared.core.plot.flag.implementations.FarewellFlag; 050import com.plotsquared.core.plot.flag.implementations.FeedFlag; 051import com.plotsquared.core.plot.flag.implementations.FishingFlag; 052import com.plotsquared.core.plot.flag.implementations.FlyFlag; 053import com.plotsquared.core.plot.flag.implementations.ForcefieldFlag; 054import com.plotsquared.core.plot.flag.implementations.GamemodeFlag; 055import com.plotsquared.core.plot.flag.implementations.GrassGrowFlag; 056import com.plotsquared.core.plot.flag.implementations.GreetingFlag; 057import com.plotsquared.core.plot.flag.implementations.GuestGamemodeFlag; 058import com.plotsquared.core.plot.flag.implementations.HangingBreakFlag; 059import com.plotsquared.core.plot.flag.implementations.HangingPlaceFlag; 060import com.plotsquared.core.plot.flag.implementations.HealFlag; 061import com.plotsquared.core.plot.flag.implementations.HideInfoFlag; 062import com.plotsquared.core.plot.flag.implementations.HostileAttackFlag; 063import com.plotsquared.core.plot.flag.implementations.HostileCapFlag; 064import com.plotsquared.core.plot.flag.implementations.HostileInteractFlag; 065import com.plotsquared.core.plot.flag.implementations.IceFormFlag; 066import com.plotsquared.core.plot.flag.implementations.IceMeltFlag; 067import com.plotsquared.core.plot.flag.implementations.InstabreakFlag; 068import com.plotsquared.core.plot.flag.implementations.InvincibleFlag; 069import com.plotsquared.core.plot.flag.implementations.ItemDropFlag; 070import com.plotsquared.core.plot.flag.implementations.KeepFlag; 071import com.plotsquared.core.plot.flag.implementations.KeepInventoryFlag; 072import com.plotsquared.core.plot.flag.implementations.KelpGrowFlag; 073import com.plotsquared.core.plot.flag.implementations.LeafDecayFlag; 074import com.plotsquared.core.plot.flag.implementations.LecternReadBookFlag; 075import com.plotsquared.core.plot.flag.implementations.LiquidFlowFlag; 076import com.plotsquared.core.plot.flag.implementations.MiscBreakFlag; 077import com.plotsquared.core.plot.flag.implementations.MiscCapFlag; 078import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag; 079import com.plotsquared.core.plot.flag.implementations.MiscPlaceFlag; 080import com.plotsquared.core.plot.flag.implementations.MobBreakFlag; 081import com.plotsquared.core.plot.flag.implementations.MobCapFlag; 082import com.plotsquared.core.plot.flag.implementations.MobPlaceFlag; 083import com.plotsquared.core.plot.flag.implementations.MusicFlag; 084import com.plotsquared.core.plot.flag.implementations.MycelGrowFlag; 085import com.plotsquared.core.plot.flag.implementations.NoWorldeditFlag; 086import com.plotsquared.core.plot.flag.implementations.NotifyEnterFlag; 087import com.plotsquared.core.plot.flag.implementations.NotifyLeaveFlag; 088import com.plotsquared.core.plot.flag.implementations.PlaceFlag; 089import com.plotsquared.core.plot.flag.implementations.PlayerInteractFlag; 090import com.plotsquared.core.plot.flag.implementations.PlotTitleFlag; 091import com.plotsquared.core.plot.flag.implementations.PreventCreativeCopyFlag; 092import com.plotsquared.core.plot.flag.implementations.PriceFlag; 093import com.plotsquared.core.plot.flag.implementations.ProjectileChangeBlockFlag; 094import com.plotsquared.core.plot.flag.implementations.ProjectilesFlag; 095import com.plotsquared.core.plot.flag.implementations.PveFlag; 096import com.plotsquared.core.plot.flag.implementations.PvpFlag; 097import com.plotsquared.core.plot.flag.implementations.RedstoneFlag; 098import com.plotsquared.core.plot.flag.implementations.SculkSensorInteractFlag; 099import com.plotsquared.core.plot.flag.implementations.ServerPlotFlag; 100import com.plotsquared.core.plot.flag.implementations.SnowFormFlag; 101import com.plotsquared.core.plot.flag.implementations.SnowMeltFlag; 102import com.plotsquared.core.plot.flag.implementations.SoilDryFlag; 103import com.plotsquared.core.plot.flag.implementations.TamedAttackFlag; 104import com.plotsquared.core.plot.flag.implementations.TamedInteractFlag; 105import com.plotsquared.core.plot.flag.implementations.TimeFlag; 106import com.plotsquared.core.plot.flag.implementations.TitlesFlag; 107import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; 108import com.plotsquared.core.plot.flag.implementations.UseFlag; 109import com.plotsquared.core.plot.flag.implementations.VehicleBreakFlag; 110import com.plotsquared.core.plot.flag.implementations.VehicleCapFlag; 111import com.plotsquared.core.plot.flag.implementations.VehiclePlaceFlag; 112import com.plotsquared.core.plot.flag.implementations.VehicleUseFlag; 113import com.plotsquared.core.plot.flag.implementations.VillagerInteractFlag; 114import com.plotsquared.core.plot.flag.implementations.VineGrowFlag; 115import com.plotsquared.core.plot.flag.implementations.WeatherFlag; 116import org.checkerframework.checker.nullness.qual.NonNull; 117 118import java.util.Collections; 119import java.util.HashMap; 120import java.util.Locale; 121import java.util.Map; 122 123public final class GlobalFlagContainer extends FlagContainer { 124 125 private static GlobalFlagContainer instance; 126 private static Map<String, Class<?>> stringClassMap; 127 128 private GlobalFlagContainer() { 129 super(null, (flag, type) -> { 130 if (type == PlotFlagUpdateType.FLAG_ADDED) { 131 stringClassMap.put(flag.getName().toLowerCase(Locale.ENGLISH), flag.getClass()); 132 } 133 }); 134 stringClassMap = new HashMap<>(); 135 136 // Register all default flags here 137 // Block type list flags 138 this.addFlag(BreakFlag.BREAK_NONE); 139 this.addFlag(UseFlag.USE_NONE); 140 this.addFlag(PlaceFlag.PLACE_NONE); 141 142 // Boolean flags 143 this.addFlag(AnimalAttackFlag.ANIMAL_ATTACK_FALSE); 144 this.addFlag(AnimalInteractFlag.ANIMAL_INTERACT_FALSE); 145 this.addFlag(BlockBurnFlag.BLOCK_BURN_FALSE); 146 this.addFlag(BeaconEffectsFlag.BEACON_EFFECT_TRUE); 147 this.addFlag(BlockIgnitionFlag.BLOCK_IGNITION_TRUE); 148 this.addFlag(ChatFlag.CHAT_FLAG_TRUE); 149 this.addFlag(ConcreteHardenFlag.CONCRETE_HARDEN_TRUE); 150 this.addFlag(CopperOxideFlag.COPPER_OXIDE_FALSE); 151 this.addFlag(CoralDryFlag.CORAL_DRY_FALSE); 152 this.addFlag(CropGrowFlag.CROP_GROW_TRUE); 153 this.addFlag(DenyExitFlag.DENY_EXIT_FLAG_FALSE); 154 this.addFlag(DenyPortalsFlag.DENY_PORTALS_FALSE); 155 this.addFlag(DenyPortalTravelFlag.DENY_PORTAL_TRAVEL_FALSE); 156 this.addFlag(DeviceInteractFlag.DEVICE_INTERACT_FALSE); 157 this.addFlag(DisablePhysicsFlag.DISABLE_PHYSICS_FALSE); 158 this.addFlag(DropProtectionFlag.DROP_PROTECTION_FALSE); 159 this.addFlag(EditSignFlag.EDIT_SIGN_FALSE); 160 this.addFlag(EntityChangeBlockFlag.ENTITY_CHANGE_BLOCK_FALSE); 161 this.addFlag(ExplosionFlag.EXPLOSION_FALSE); 162 this.addFlag(FishingFlag.FISHING_FALSE); 163 this.addFlag(ForcefieldFlag.FORCEFIELD_FALSE); 164 this.addFlag(GrassGrowFlag.GRASS_GROW_TRUE); 165 this.addFlag(HangingBreakFlag.HANGING_BREAK_FALSE); 166 this.addFlag(HangingPlaceFlag.HANGING_PLACE_FALSE); 167 this.addFlag(HideInfoFlag.HIDE_INFO_FALSE); 168 this.addFlag(HostileAttackFlag.HOSTILE_ATTACK_FALSE); 169 this.addFlag(HostileInteractFlag.HOSTILE_INTERACT_FALSE); 170 this.addFlag(IceFormFlag.ICE_FORM_FALSE); 171 this.addFlag(IceMeltFlag.ICE_MELT_FALSE); 172 this.addFlag(InstabreakFlag.INSTABREAK_FALSE); 173 this.addFlag(InvincibleFlag.INVINCIBLE_FALSE); 174 this.addFlag(ItemDropFlag.ITEM_DROP_TRUE); 175 this.addFlag(KeepInventoryFlag.KEEP_INVENTORY_FALSE); 176 this.addFlag(KelpGrowFlag.KELP_GROW_TRUE); 177 this.addFlag(LeafDecayFlag.LEAF_DECAY_TRUE); 178 this.addFlag(LecternReadBookFlag.LECTERN_READ_BOOK_FALSE); 179 this.addFlag(MiscBreakFlag.MISC_BREAK_FALSE); 180 this.addFlag(MobBreakFlag.MOB_BREAK_FALSE); 181 this.addFlag(MobPlaceFlag.MOB_PLACE_FALSE); 182 this.addFlag(MiscInteractFlag.MISC_INTERACT_FALSE); 183 this.addFlag(SculkSensorInteractFlag.SCULK_SENSOR_INTERACT_FALSE); 184 this.addFlag(MiscPlaceFlag.MISC_PLACE_FALSE); 185 this.addFlag(MycelGrowFlag.MYCEL_GROW_TRUE); 186 this.addFlag(NotifyEnterFlag.NOTIFY_ENTER_FALSE); 187 this.addFlag(NotifyLeaveFlag.NOTIFY_LEAVE_FALSE); 188 this.addFlag(NoWorldeditFlag.NO_WORLDEDIT_FALSE); 189 this.addFlag(PlayerInteractFlag.PLAYER_INTERACT_FALSE); 190 this.addFlag(PreventCreativeCopyFlag.PREVENT_CREATIVE_COPY_FALSE); 191 this.addFlag(ProjectileChangeBlockFlag.PROJECTILE_CHANGE_BLOCK_FALSE); 192 this.addFlag(PveFlag.PVE_FALSE); 193 this.addFlag(PvpFlag.PVP_FALSE); 194 this.addFlag(RedstoneFlag.REDSTONE_TRUE); 195 this.addFlag(ServerPlotFlag.SERVER_PLOT_FALSE); 196 this.addFlag(SnowFormFlag.SNOW_FORM_FALSE); 197 this.addFlag(SnowMeltFlag.SNOW_MELT_TRUE); 198 this.addFlag(SoilDryFlag.SOIL_DRY_FALSE); 199 this.addFlag(TamedAttackFlag.TAMED_ATTACK_FALSE); 200 this.addFlag(TamedInteractFlag.TAMED_INTERACT_FALSE); 201 this.addFlag(UntrustedVisitFlag.UNTRUSTED_VISIT_FLAG_TRUE); 202 this.addFlag(VehicleBreakFlag.VEHICLE_BREAK_FALSE); 203 this.addFlag(VehiclePlaceFlag.VEHICLE_PLACE_FALSE); 204 this.addFlag(VehicleUseFlag.VEHICLE_USE_FALSE); 205 this.addFlag(VillagerInteractFlag.VILLAGER_INTERACT_FALSE); 206 this.addFlag(VineGrowFlag.VINE_GROW_TRUE); 207 this.addFlag(ProjectilesFlag.PROJECTILES_FALSE); 208 209 // Double flags 210 this.addFlag(PriceFlag.PRICE_NOT_BUYABLE); 211 212 // Enum Flags 213 this.addFlag(DenyTeleportFlag.DENY_TELEPORT_FLAG_NONE); 214 this.addFlag(FlyFlag.FLIGHT_FLAG_DEFAULT); 215 this.addFlag(LiquidFlowFlag.LIQUID_FLOW_DEFAULT); 216 this.addFlag(TitlesFlag.TITLES_NONE); 217 this.addFlag(WeatherFlag.PLOT_WEATHER_FLAG_OFF); 218 219 // Internal flags 220 this.addFlag(new AnalysisFlag(Collections.emptyList())); 221 this.addFlag(new DoneFlag("")); 222 223 // Integer flags 224 this.addFlag(AnimalCapFlag.ANIMAL_CAP_UNLIMITED); 225 this.addFlag(EntityCapFlag.ENTITY_CAP_UNLIMITED); 226 this.addFlag(HostileCapFlag.HOSTILE_CAP_UNLIMITED); 227 this.addFlag(MiscCapFlag.MISC_CAP_UNLIMITED); 228 this.addFlag(MobCapFlag.MOB_CAP_UNLIMITED); 229 this.addFlag(TimeFlag.TIME_DISABLED); 230 this.addFlag(VehicleCapFlag.VEHICLE_CAP_UNLIMITED); 231 232 // Misc 233 this.addFlag(BlockedCmdsFlag.BLOCKED_CMDS_FLAG_NONE); 234 this.addFlag(GamemodeFlag.GAMEMODE_FLAG_DEFAULT); 235 this.addFlag(GuestGamemodeFlag.GUEST_GAMEMODE_FLAG_DEFAULT); 236 this.addFlag(KeepFlag.KEEP_FLAG_FALSE); 237 this.addFlag(MusicFlag.MUSIC_FLAG_NONE); 238 239 // String flags 240 this.addFlag(DescriptionFlag.DESCRIPTION_FLAG_EMPTY); 241 this.addFlag(GreetingFlag.GREETING_FLAG_EMPTY); 242 this.addFlag(FarewellFlag.FAREWELL_FLAG_EMPTY); 243 this.addFlag(PlotTitleFlag.TITLE_FLAG_DEFAULT); 244 245 // Timed flags 246 this.addFlag(FeedFlag.FEED_NOTHING); 247 this.addFlag(HealFlag.HEAL_NOTHING); 248 } 249 250 public static void setup() { 251 Preconditions.checkState(instance == null, "Cannot setup the container twice"); 252 instance = new GlobalFlagContainer(); 253 } 254 255 public static GlobalFlagContainer getInstance() { 256 return GlobalFlagContainer.instance; 257 } 258 259 @Override 260 public PlotFlag<?, ?> getFlagErased(Class<?> flagClass) { 261 final PlotFlag<?, ?> flag = super.getFlagErased(flagClass); 262 if (flag != null) { 263 return flag; 264 } else { 265 throw new IllegalStateException(String.format("Unrecognized flag '%s'. All flag types" 266 + " must be present in the global flag container.", flagClass.getSimpleName())); 267 } 268 } 269 270 @NonNull 271 @Override 272 public <V, T extends PlotFlag<V, ?>> T getFlag(Class<? extends T> flagClass) { 273 final PlotFlag<?, ?> flag = super.getFlag(flagClass); 274 if (flag != null) { 275 return castUnsafe(flag); 276 } else { 277 throw new IllegalStateException(String.format("Unrecognized flag '%s'. All flag types" 278 + " must be present in the global flag container.", flagClass.getSimpleName())); 279 } 280 } 281 282 public Class<?> getFlagClassFromString(final String name) { 283 return stringClassMap.get(name.toLowerCase(Locale.ENGLISH)); 284 } 285 286 public PlotFlag<?, ?> getFlagFromString(final String name) { 287 final Class<?> flagClass = this.getFlagClassFromString(name); 288 if (flagClass == null) { 289 return null; 290 } 291 return getFlagErased(flagClass); 292 } 293 294}