From 19d447d67d0c4620368069a00c20812325916b2e Mon Sep 17 00:00:00 2001 From: Wizjany Date: Sun, 24 Apr 2011 15:55:26 -0400 Subject: [PATCH] Added handling for weather events. Depends on my craftbukkit pull request. --- .../bukkit/WorldGuardEntityListener.java | 33 +++++ .../worldguard/bukkit/WorldGuardPlugin.java | 1 + .../bukkit/WorldGuardWeatherListener.java | 134 ++++++++++++++++++ .../protection/flags/DefaultFlag.java | 14 +- 4 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 src/com/sk89q/worldguard/bukkit/WorldGuardWeatherListener.java diff --git a/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java b/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java index ac79a413..f1a4403e 100644 --- a/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java +++ b/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java @@ -65,6 +65,8 @@ public void registerEvents() { pm.registerEvent(Event.Type.ENTITY_EXPLODE, this, Priority.High, plugin); pm.registerEvent(Event.Type.CREATURE_SPAWN, this, Priority.High, plugin); pm.registerEvent(Event.Type.ENTITY_INTERACT, this, Priority.High, plugin); + pm.registerEvent(Event.Type.CREEPER_POWER, this, Priority.High, plugin); + pm.registerEvent(Event.Type.PIG_ZAP, this, Priority.High, plugin); } @Override @@ -423,6 +425,37 @@ public void onCreatureSpawn(CreatureSpawnEvent event) { } } + /** + * Weather related entity events. + */ + @Override + public void onPigZap(PigZapEvent event) { + if (event.isCancelled()) { + return; + } + + ConfigurationManager cfg = plugin.getGlobalConfiguration(); + WorldConfiguration wcfg = cfg.get(event.getEntity().getWorld()); + + if (wcfg.disablePigZap) { + event.setCancelled(true); + } + } + + @Override + public void onCreeperPower(CreeperPowerEvent event) { + if (event.isCancelled()) { + return; + } + + ConfigurationManager cfg = plugin.getGlobalConfiguration(); + WorldConfiguration wcfg = cfg.get(event.getEntity().getWorld()); + + if (wcfg.disableCreeperPower) { + event.setCancelled(true); + } + } + /** * Find a position for the player to stand that is not inside a block. * Blocks above the player will be iteratively tested until there is diff --git a/src/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java b/src/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java index 0da9e506..a5ff0380 100644 --- a/src/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java +++ b/src/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java @@ -132,6 +132,7 @@ public void onEnable() { (new WorldGuardPlayerListener(this)).registerEvents(); (new WorldGuardBlockListener(this)).registerEvents(); (new WorldGuardEntityListener(this)).registerEvents(); + (new WorldGuardWeatherListener(this)).registerEvents(); // 25 equals about 1s real time getServer().getScheduler().scheduleSyncRepeatingTask( diff --git a/src/com/sk89q/worldguard/bukkit/WorldGuardWeatherListener.java b/src/com/sk89q/worldguard/bukkit/WorldGuardWeatherListener.java new file mode 100644 index 00000000..d71a4645 --- /dev/null +++ b/src/com/sk89q/worldguard/bukkit/WorldGuardWeatherListener.java @@ -0,0 +1,134 @@ +// $Id$ +/* + * WorldGuard + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.sk89q.worldguard.bukkit; + +import org.bukkit.block.Block; +import org.bukkit.event.Event.Priority; +import org.bukkit.event.Event; +import org.bukkit.plugin.PluginManager; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.*; +import org.bukkit.event.weather.*; +import org.bukkit.event.entity.*; +import org.bukkit.event.entity.PigZapEvent; +import org.bukkit.event.entity.CreeperPowerEvent; + +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.blocks.BlockType; + +import static com.sk89q.worldguard.bukkit.BukkitUtil.*; + +import com.sk89q.worldguard.protection.flags.DefaultFlag; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.ApplicableRegionSet; + +public class WorldGuardWeatherListener extends WeatherListener { + + /** + * Plugin. + */ + private WorldGuardPlugin plugin; + + /** + * Construct the object; + * + * @param plugin + */ + public WorldGuardWeatherListener(WorldGuardPlugin plugin) { + this.plugin = plugin; + } + + public void registerEvents() { + PluginManager pm = plugin.getServer().getPluginManager(); + + pm.registerEvent(Event.Type.LIGHTNING_STRIKE, this, Priority.High, plugin); + pm.registerEvent(Event.Type.THUNDER_CHANGE, this, Priority.High, plugin); + pm.registerEvent(Event.Type.WEATHER_CHANGE, this, Priority.High, plugin); + } + + @Override + public void onWeatherChange(WeatherChangeEvent event) { + if (event.isCancelled()) { + return; + } + ConfigurationManager cfg = plugin.getGlobalConfiguration(); + WorldConfiguration wcfg = cfg.get(event.getWorld()); + + if (event.toWeatherState()) { + if (wcfg.disableWeather) { + event.setCancelled(true); + } + } else { + if (!wcfg.disableWeather && wcfg.alwaysRaining) { + event.setCancelled(true); + } + } + } + + @Override + public void onThunderChange(ThunderChangeEvent event) { + if (event.isCancelled()) { + return; + } + + ConfigurationManager cfg = plugin.getGlobalConfiguration(); + WorldConfiguration wcfg = cfg.get(event.getWorld()); + + if (event.toThunderState()) { + if (wcfg.disableThunder) { + event.setCancelled(true); + } + } else { + if (!wcfg.disableWeather && wcfg.alwaysThundering) { + event.setCancelled(true); + } + } + } + + @Override + public void onLightningStrike(LightningStrikeEvent event) { + if (event.isCancelled()) { + return; + } + + ConfigurationManager cfg = plugin.getGlobalConfiguration(); + WorldConfiguration wcfg = cfg.get(event.getWorld()); + + if (wcfg.disallowedLightningBlocks.size() > 0) { + int targetId = event.getLightning().getLocation().getBlock().getTypeId(); + if (wcfg.disallowedLightningBlocks.contains(targetId)) { + event.setCancelled(true); + } + } + + Location loc = event.getLightning().getLocation(); + if (wcfg.useRegions) { + Vector pt = toVector(loc); + RegionManager mgr = plugin.getGlobalRegionManager().get(loc.getWorld()); + ApplicableRegionSet set = mgr.getApplicableRegions(pt); + + if (!set.allows(DefaultFlag.LIGHTNING)) { + event.setCancelled(true); + } + } + } + +} diff --git a/src/com/sk89q/worldguard/protection/flags/DefaultFlag.java b/src/com/sk89q/worldguard/protection/flags/DefaultFlag.java index 49833080..673cbb67 100644 --- a/src/com/sk89q/worldguard/protection/flags/DefaultFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/DefaultFlag.java @@ -29,13 +29,14 @@ public final class DefaultFlag { public static final StateFlag BUILD = new StateFlag("build", 'b', true); public static final StateFlag PVP = new StateFlag("pvp", 'p', true); public static final StateFlag MOB_DAMAGE = new StateFlag("mob-damage", 'm', true); - public static final StateFlag MOB_SPAWNING = new StateFlag("mob-spawning", true); + public static final StateFlag MOB_SPAWNING = new StateFlag("mob-spawning", 'M', true); public static final StateFlag CREEPER_EXPLOSION = new StateFlag("creeper-explosion", 'c', true); public static final StateFlag SLEEP = new StateFlag("sleep", true); public static final StateFlag TNT = new StateFlag("tnt", 't', true); public static final StateFlag LIGHTER = new StateFlag("lighter", 'l', true); public static final StateFlag FIRE_SPREAD = new StateFlag("fire-spread", 'f', true); public static final StateFlag LAVA_FIRE = new StateFlag("lava-fire", 'F', true); + public static final StateFlag LIGHTNING = new StateFlag("lightning", true); public static final StateFlag CHEST_ACCESS = new StateFlag("chest-access", 'C', false); public static final StateFlag WATER_FLOW = new StateFlag("water-flow", true); public static final StateFlag LAVA_FLOW = new StateFlag("lava-flow", true); @@ -56,11 +57,12 @@ public final class DefaultFlag { public static final DoubleFlag PRICE = new DoubleFlag("price"); public static final Flag[] flagsList = new Flag[] { - PASSTHROUGH, BUILD, PVP, MOB_DAMAGE, MOB_SPAWNING, CREEPER_EXPLOSION, SLEEP, - TNT, LIGHTER, FIRE_SPREAD, LAVA_FIRE, CHEST_ACCESS, WATER_FLOW, LAVA_FLOW, - USE, PLACE_VEHICLE, GREET_MESSAGE, FAREWELL_MESSAGE, NOTIFY_GREET, - NOTIFY_FAREWELL, DENY_SPAWN, HEAL_DELAY, HEAL_AMOUNT, TELE_LOC, - TELE_PERM, SPAWN_LOC, SPAWN_PERM, BUYABLE, PRICE + PASSTHROUGH, BUILD, PVP, MOB_DAMAGE, MOB_SPAWNING, CREEPER_EXPLOSION, + TNT, LIGHTER, FIRE_SPREAD, LAVA_FIRE, LIGHTNING, CHEST_ACCESS, + WATER_FLOW, LAVA_FLOW, USE, PLACE_VEHICLE, GREET_MESSAGE, + FAREWELL_MESSAGE, NOTIFY_GREET, NOTIFY_FAREWELL, DENY_SPAWN, + HEAL_DELAY, HEAL_AMOUNT, TELE_LOC,TELE_PERM, SPAWN_LOC, SPAWN_PERM, + BUYABLE, PRICE, SLEEP }; private DefaultFlag() {