Added handling for weather events. Depends on my craftbukkit pull request.

This commit is contained in:
Wizjany 2011-04-24 15:55:26 -04:00
parent 925fa2ee1b
commit 19d447d67d
4 changed files with 176 additions and 6 deletions

View File

@ -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

View File

@ -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(

View File

@ -0,0 +1,134 @@
// $Id$
/*
* WorldGuard
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}
}
}

View File

@ -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() {