From ff89bda57e4a2ddc28ce13863108b400c4181661 Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Sun, 7 Aug 2011 21:03:36 -0600 Subject: [PATCH] Add ability to disable weather per world. --- .../onarandombox/MultiverseCore/MVWorld.java | 25 ++++++++-- .../MultiverseCore/MultiverseCore.java | 8 ++++ .../MultiverseCore/commands/InfoCommand.java | 6 +-- .../commands/ModifyCommand.java | 2 +- .../listeners/MVWeatherListener.java | 46 +++++++++++++++++++ 5 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index 986345a7..7ce42529 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -96,6 +96,7 @@ public class MVWorld { private Permission exempt; private boolean canSave = false; // Prevents all the setters from constantly saving to the config when being called from the constructor. + private boolean allowWeather; public MVWorld(World world, Configuration config, MultiverseCore instance, Long seed, String generatorString) { this.config = config; @@ -126,6 +127,7 @@ public class MVWorld { this.setPvp(config.getBoolean("worlds." + this.name + ".pvp", true)); this.setScaling(config.getDouble("worlds." + this.name + ".scale", 1.0)); this.setRespawnToWorld(config.getString("worlds." + this.name + ".respawnworld", "")); + this.setEnableWeather(config.getBoolean("worlds." + this.name + ".allowweather", true)); this.setAnimals(config.getBoolean("worlds." + this.name + ".animals.spawn", true)); this.setMonsters(config.getBoolean("worlds." + this.name + ".monsters.spawn", true)); @@ -152,6 +154,17 @@ public class MVWorld { } } + private void setEnableWeather(boolean weather) { + this.allowWeather = weather; + // Disable any current weather + if (!weather) { + this.getCBWorld().setStorm(false); + this.getCBWorld().setThundering(false); + } + this.config.setProperty("worlds." + this.name + ".allowweather", weather); + saveConfig(); + } + private void addToUpperLists(Permission permission) { Permission all = this.plugin.getServer().getPluginManager().getPermission("multiverse.*"); Permission allWorlds = this.plugin.getServer().getPluginManager().getPermission("multiverse.access.*"); @@ -347,8 +360,10 @@ public class MVWorld { this.setMonsters(value); } else if (name.equalsIgnoreCase("memory") || name.equalsIgnoreCase("spawnmemory")) { this.setSpawnInMemory(value); + } else if (name.equalsIgnoreCase("weather") || name.equalsIgnoreCase("storm")) { + this.setEnableWeather(value); } else { - return false; + return false; } return true; } @@ -375,7 +390,7 @@ public class MVWorld { /** * This is the one people have access to. It'll handle the rest. - * + * * @param name * @param value * @return @@ -532,7 +547,7 @@ public class MVWorld { /** * Sets the chat color from a string. - * + * * @param aliasColor */ public void setAliasColor(String aliasColor) { @@ -622,4 +637,8 @@ public class MVWorld { this.config.save(); } } + + public boolean getWeatherEnabled() { + return this.allowWeather; + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index f30fadb4..67b16b7a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -23,6 +23,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.Event.Priority; +import org.bukkit.event.Listener; import org.bukkit.generator.ChunkGenerator; import org.bukkit.permissions.Permission; import org.bukkit.plugin.Plugin; @@ -62,6 +63,7 @@ import com.onarandombox.MultiverseCore.configuration.MVCoreConfigMigrator; import com.onarandombox.MultiverseCore.listeners.MVEntityListener; import com.onarandombox.MultiverseCore.listeners.MVPlayerListener; import com.onarandombox.MultiverseCore.listeners.MVPluginListener; +import com.onarandombox.MultiverseCore.listeners.MVWeatherListener; import com.onarandombox.utils.DebugLog; import com.onarandombox.utils.DestinationFactory; import com.onarandombox.utils.ExactDestination; @@ -98,6 +100,7 @@ public class MultiverseCore extends JavaPlugin implements LoggablePlugin { private MVEntityListener entityListener = new MVEntityListener(this); private MVPluginListener pluginListener = new MVPluginListener(this); + private MVWeatherListener weatherListener = new MVWeatherListener(this); public UpdateChecker updateCheck; @@ -112,6 +115,7 @@ public class MultiverseCore extends JavaPlugin implements LoggablePlugin { protected MVConfigMigrator migrator = new MVCoreConfigMigrator(this); protected int pluginCount; private DestinationFactory destFactory; + @Override public void onLoad() { @@ -193,6 +197,10 @@ public class MultiverseCore extends JavaPlugin implements LoggablePlugin { pm.registerEvent(Event.Type.PLUGIN_ENABLE, this.pluginListener, Priority.Monitor, this); pm.registerEvent(Event.Type.PLUGIN_DISABLE, this.pluginListener, Priority.Monitor, this); + + pm.registerEvent(Event.Type.WEATHER_CHANGE, this.weatherListener, Priority.Normal, this); + pm.registerEvent(Event.Type.THUNDER_CHANGE, this.weatherListener, Priority.Normal, this); + pm.registerEvent(Event.Type.LIGHTNING_STRIKE, this.weatherListener, Priority.Normal, this); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java index f024e821..0256bb46 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java @@ -101,12 +101,12 @@ public class InfoCommand extends MultiverseCommand { if (world.getRespawnToWorld() != null) { MVWorld respawn = this.plugin.getMVWorld(world.getRespawnToWorld()); - if(respawn != null) { - message.add(new FancyMessage("Players will respawn in: ", respawn.getColoredWorldString(), colors)); + if (respawn != null) { + message.add(new FancyMessage("Players will respawn in: ", respawn.getColoredWorldString(), colors)); } else { message.add(new FancyMessage("Players will respawn in: ", ChatColor.RED + "!!INVALID!!", colors)); } - + } worldInfo.add(message); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyCommand.java index 5bb6af6a..1ddf8a03 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyCommand.java @@ -21,7 +21,7 @@ enum Action { // Color == Aliascolor enum SetProperties { - alias, animals, monsters, pvp, scaling, aliascolor, color, respawn, currency, curr, price, scale, spawnmemory, memory + alias, animals, monsters, pvp, scaling, aliascolor, color, respawn, currency, curr, price, scale, spawnmemory, memory, weather, storm } public class ModifyCommand extends MultiverseCommand { diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java new file mode 100644 index 00000000..24c9b3bd --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java @@ -0,0 +1,46 @@ +package com.onarandombox.MultiverseCore.listeners; + +import org.bukkit.event.weather.LightningStrikeEvent; +import org.bukkit.event.weather.ThunderChangeEvent; +import org.bukkit.event.weather.WeatherChangeEvent; +import org.bukkit.event.weather.WeatherListener; + +import com.onarandombox.MultiverseCore.MVWorld; +import com.onarandombox.MultiverseCore.MultiverseCore; + +public class MVWeatherListener extends WeatherListener { + private MultiverseCore plugin; + + public MVWeatherListener(MultiverseCore plugin) { + this.plugin = plugin; + } + + @Override + public void onWeatherChange(WeatherChangeEvent event) { + + MVWorld world = this.plugin.getMVWorld(event.getWorld().getName()); + if (world != null) { + // If it's going to start raining and we have weather disabled + event.setCancelled((event.toWeatherState() && !world.getWeatherEnabled())); + } + } + + @Override + public void onThunderChange(ThunderChangeEvent event) { + + MVWorld world = this.plugin.getMVWorld(event.getWorld().getName()); + if (world != null) { + // If it's going to start raining and we have weather disabled + event.setCancelled((event.toThunderState() && !world.getWeatherEnabled())); + } + } + + @Override + public void onLightningStrike(LightningStrikeEvent event) { + MVWorld world = this.plugin.getMVWorld(event.getWorld().getName()); + if (world != null) { + // If it's going to start raining and we have weather disabled + event.setCancelled((!world.getWeatherEnabled())); + } + } +}