PageRenderTime 53ms CodeModel.GetById 37ms app.highlight 12ms RepoModel.GetById 0ms app.codeStats 0ms

/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/ConfigurationManager.java

https://gitlab.com/igserfurtmcschulserver/CustomWorldGuard
Java | 296 lines | 177 code | 36 blank | 83 comment | 18 complexity | 4c061162c569c664886027abb6ba3ee1 MD5 | raw file
  1/*
  2 * WorldGuard, a suite of tools for Minecraft
  3 * Copyright (C) sk89q <http://www.sk89q.com>
  4 * Copyright (C) WorldGuard team and contributors
  5 *
  6 * This program is free software: you can redistribute it and/or modify it
  7 * under the terms of the GNU Lesser General Public License as published by the
  8 * Free Software Foundation, either version 3 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 14 * for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public License
 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20package com.sk89q.worldguard.bukkit;
 21
 22import com.google.common.collect.ImmutableMap;
 23import com.sk89q.util.yaml.YAMLFormat;
 24import com.sk89q.util.yaml.YAMLProcessor;
 25import com.sk89q.worldguard.protection.managers.storage.DriverType;
 26import com.sk89q.worldguard.protection.managers.storage.RegionDriver;
 27import com.sk89q.worldguard.protection.managers.storage.file.DirectoryYamlDriver;
 28import com.sk89q.worldguard.protection.managers.storage.sql.SQLDriver;
 29import com.sk89q.worldguard.session.handler.WaterBreathing;
 30import com.sk89q.worldguard.util.report.Unreported;
 31import com.sk89q.worldguard.util.sql.DataSourceConfig;
 32import org.bukkit.World;
 33import org.bukkit.entity.Player;
 34
 35import java.io.File;
 36import java.io.IOException;
 37import java.util.HashMap;
 38import java.util.Map;
 39import java.util.concurrent.ConcurrentHashMap;
 40import java.util.concurrent.ConcurrentMap;
 41import java.util.logging.Logger;
 42
 43/**
 44 * Represents the global configuration and also delegates configuration
 45 * for individual worlds.
 46 *
 47 * @author sk89q
 48 * @author Michael
 49 */
 50public class ConfigurationManager {
 51
 52    private static final Logger log = Logger.getLogger(ConfigurationManager.class.getCanonicalName());
 53
 54    private static final String CONFIG_HEADER = "#\r\n" +
 55            "# WorldGuard's main configuration file\r\n" +
 56            "#\r\n" +
 57            "# This is the global configuration file. Anything placed into here will\r\n" +
 58            "# be applied to all worlds. However, each world has its own configuration\r\n" +
 59            "# file to allow you to replace most settings in here for that world only.\r\n" +
 60            "#\r\n" +
 61            "# About editing this file:\r\n" +
 62            "# - DO NOT USE TABS. You MUST use spaces or Bukkit will complain. If\r\n" +
 63            "#   you use an editor like Notepad++ (recommended for Windows users), you\r\n" +
 64            "#   must configure it to \"replace tabs with spaces.\" In Notepad++, this can\r\n" +
 65            "#   be changed in Settings > Preferences > Language Menu.\r\n" +
 66            "# - Don't get rid of the indents. They are indented so some entries are\r\n" +
 67            "#   in categories (like \"enforce-single-session\" is in the \"protection\"\r\n" +
 68            "#   category.\r\n" +
 69            "# - If you want to check the format of this file before putting it\r\n" +
 70            "#   into WorldGuard, paste it into http://yaml-online-parser.appspot.com/\r\n" +
 71            "#   and see if it gives \"ERROR:\".\r\n" +
 72            "# - Lines starting with # are comments and so they are ignored.\r\n" +
 73            "#\r\n";
 74
 75    @Unreported private WorldGuardPlugin plugin;
 76    @Unreported private ConcurrentMap<String, WorldConfiguration> worlds;
 77    @Unreported private YAMLProcessor config;
 78
 79    private boolean hasCommandBookGodMode = false;
 80
 81    public boolean useRegionsCreatureSpawnEvent;
 82    public boolean activityHaltToggle = false;
 83    public boolean useGodPermission;
 84    public boolean useGodGroup;
 85    public boolean useAmphibiousGroup;
 86    public boolean usePlayerMove;
 87    public boolean usePlayerTeleports;
 88    public boolean deopOnJoin;
 89    public boolean blockInGameOp;
 90    public boolean migrateRegionsToUuid;
 91    public boolean keepUnresolvedNames;
 92
 93    @Unreported public Map<String, String> hostKeys = new HashMap<String, String>();
 94
 95    /**
 96     * Region Storage Configuration method, and config values
 97     */
 98    @Unreported public RegionDriver selectedRegionStoreDriver;
 99    @Unreported public Map<DriverType, RegionDriver> regionStoreDriverMap;
100
101    /**
102     * Construct the object.
103     *
104     * @param plugin The plugin instance
105     */
106    public ConfigurationManager(WorldGuardPlugin plugin) {
107        this.plugin = plugin;
108        this.worlds = new ConcurrentHashMap<String, WorldConfiguration>();
109    }
110
111    /**
112     * Get the folder for storing data files and configuration.
113     *
114     * @return the data folder
115     */
116    public File getDataFolder() {
117        return plugin.getDataFolder();
118    }
119
120    /**
121     * Get the folder for storing data files and configuration for each
122     * world.
123     *
124     * @return the data folder
125     */
126    public File getWorldsDataFolder() {
127        return new File(getDataFolder(), "worlds");
128    }
129
130    /**
131     * Load the configuration.
132     */
133    @SuppressWarnings("unchecked")
134    public void load() {
135        // Create the default configuration file
136        plugin.createDefaultConfiguration(
137                new File(plugin.getDataFolder(), "config.yml"), "config.yml");
138
139        config = new YAMLProcessor(new File(plugin.getDataFolder(), "config.yml"), true, YAMLFormat.EXTENDED);
140        try {
141            config.load();
142        } catch (IOException e) {
143            log.severe("Error reading configuration for global config: ");
144            e.printStackTrace();
145        }
146
147        config.removeProperty("suppress-tick-sync-warnings");
148        migrateRegionsToUuid = config.getBoolean("regions.uuid-migration.perform-on-next-start", true);
149        keepUnresolvedNames = config.getBoolean("regions.uuid-migration.keep-names-that-lack-uuids", true);
150        useRegionsCreatureSpawnEvent = config.getBoolean("regions.use-creature-spawn-event", true);
151        useGodPermission = config.getBoolean("auto-invincible", config.getBoolean("auto-invincible-permission", false));
152        useGodGroup = config.getBoolean("auto-invincible-group", false);
153        useAmphibiousGroup = config.getBoolean("auto-no-drowning-group", false);
154        config.removeProperty("auto-invincible-permission");
155        usePlayerMove = config.getBoolean("use-player-move-event", true);
156        usePlayerTeleports = config.getBoolean("use-player-teleports", true);
157
158        deopOnJoin = config.getBoolean("security.deop-everyone-on-join", false);
159        blockInGameOp = config.getBoolean("security.block-in-game-op-command", false);
160
161        hostKeys = new HashMap<String, String>();
162        Object hostKeysRaw = config.getProperty("host-keys");
163        if (hostKeysRaw == null || !(hostKeysRaw instanceof Map)) {
164            config.setProperty("host-keys", new HashMap<String, String>());
165        } else {
166            for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) hostKeysRaw).entrySet()) {
167                String key = String.valueOf(entry.getKey());
168                String value = String.valueOf(entry.getValue());
169                hostKeys.put(key.toLowerCase(), value);
170            }
171        }
172
173        // ====================================================================
174        // Region store drivers
175        // ====================================================================
176
177        boolean useSqlDatabase = config.getBoolean("regions.sql.use", false);
178        String sqlDsn = config.getString("regions.sql.dsn", "jdbc:mysql://localhost/worldguard");
179        String sqlUsername = config.getString("regions.sql.username", "worldguard");
180        String sqlPassword = config.getString("regions.sql.password", "worldguard");
181        String sqlTablePrefix = config.getString("regions.sql.table-prefix", "");
182
183        DataSourceConfig dataSourceConfig = new DataSourceConfig(sqlDsn, sqlUsername, sqlPassword, sqlTablePrefix);
184        SQLDriver sqlDriver = new SQLDriver(dataSourceConfig);
185        DirectoryYamlDriver yamlDriver = new DirectoryYamlDriver(getWorldsDataFolder(), "regions.yml");
186
187        this.regionStoreDriverMap = ImmutableMap.<DriverType, RegionDriver>builder()
188                .put(DriverType.MYSQL, sqlDriver)
189                .put(DriverType.YAML, yamlDriver)
190                .build();
191        this.selectedRegionStoreDriver = useSqlDatabase ? sqlDriver : yamlDriver;
192
193        // Load configurations for each world
194        for (World world : plugin.getServer().getWorlds()) {
195            get(world);
196        }
197
198        config.setHeader(CONFIG_HEADER);
199    }
200
201    /**
202     * Unload the configuration.
203     */
204    public void unload() {
205        worlds.clear();
206    }
207
208    public void disableUuidMigration() {
209        config.setProperty("regions.uuid-migration.perform-on-next-start", false);
210        if (!config.save()) {
211            log.severe("Error saving configuration!");
212        }
213    }
214
215    /**
216     * Get the configuration for a world.
217     *
218     * @param world The world to get the configuration for
219     * @return {@code world}'s configuration
220     */
221    public WorldConfiguration get(World world) {
222        String worldName = world.getName();
223        WorldConfiguration config = worlds.get(worldName);
224        WorldConfiguration newConfig = null;
225
226        while (config == null) {
227            if (newConfig == null) {
228                newConfig = new WorldConfiguration(plugin, worldName, this.config);
229            }
230            worlds.putIfAbsent(world.getName(), newConfig);
231            config = worlds.get(world.getName());
232        }
233
234        return config;
235    }
236
237    /**
238     * Check to see if god mode is enabled for a player.
239     *
240     * @param player The player to check
241     * @return Whether the player has godmode through WorldGuard or CommandBook
242     */
243    public boolean hasGodMode(Player player) {
244        return plugin.getSessionManager().get(player).isInvincible(player);
245    }
246
247    /**
248     * Enable amphibious mode for a player.
249     *
250     * @param player The player to enable amphibious mode for
251     */
252    public void enableAmphibiousMode(Player player) {
253        WaterBreathing handler = plugin.getSessionManager().get(player).getHandler(WaterBreathing.class);
254        if (handler != null) {
255            handler.setWaterBreathing(true);
256        }
257    }
258
259    /**
260     * Disable amphibious mode  for a player.
261     *
262     * @param player The player to disable amphibious mode for
263     */
264    public void disableAmphibiousMode(Player player) {
265        WaterBreathing handler = plugin.getSessionManager().get(player).getHandler(WaterBreathing.class);
266        if (handler != null) {
267            handler.setWaterBreathing(false);
268        }
269    }
270
271    /**
272     * Check to see if amphibious mode is enabled for a player.
273     *
274     * @param player The player to check
275     * @return Whether {@code player} has amphibious mode
276     */
277    public boolean hasAmphibiousMode(Player player) {
278        WaterBreathing handler = plugin.getSessionManager().get(player).getHandler(WaterBreathing.class);
279        return handler != null && handler.hasWaterBreathing();
280    }
281
282    public void updateCommandBookGodMode() {
283        try {
284            if (plugin.getServer().getPluginManager().isPluginEnabled("CommandBook")) {
285                Class.forName("com.sk89q.commandbook.GodComponent");
286                hasCommandBookGodMode = true;
287                return;
288            }
289        } catch (ClassNotFoundException ignore) {}
290        hasCommandBookGodMode = false;
291    }
292
293    public boolean hasCommandBookGodMode() {
294        return hasCommandBookGodMode;
295    }
296}