Rewrote greeting/farewell and notify-enter/notify-leave flags.

This commit is contained in:
sk89q 2011-06-05 17:18:27 -07:00
parent d9136aa407
commit c5f3a27d6b
6 changed files with 132 additions and 21 deletions

View File

@ -19,7 +19,6 @@
package com.sk89q.worldguard.bukkit;
import org.bukkit.entity.Player;
import com.sk89q.worldguard.blacklist.Blacklist;
public class BukkitBlacklist extends Blacklist {
@ -32,11 +31,7 @@ public BukkitBlacklist(Boolean useAsWhitelist, WorldGuardPlugin plugin) {
@Override
public void broadcastNotification(String msg) {
for (Player player : plugin.getServer().getOnlinePlayers()) {
if (plugin.hasPermission(player, "worldguard.notify")) {
player.sendMessage(msg);
}
}
plugin.broadcastNotification(msg);
}
}

View File

@ -29,12 +29,12 @@
import static com.sk89q.worldguard.bukkit.BukkitUtil.*;
/**
* This processed periodical tasks for flags that require them, such as with
* the healing flag.
* This processes per-player state information and is also meant to be used
* as a scheduled task.
*
* @author sk89q
*/
public class FlagScheduler implements Runnable {
public class FlagStateManager implements Runnable {
public static final int RUN_DELAY = 20;
@ -46,7 +46,7 @@ public class FlagScheduler implements Runnable {
*
* @param plugin
*/
public FlagScheduler(WorldGuardPlugin plugin) {
public FlagStateManager(WorldGuardPlugin plugin) {
this.plugin = plugin;
states = new HashMap<String, PlayerFlagState>();
@ -128,11 +128,32 @@ public synchronized void forget(Player player) {
states.remove(player.getName());
}
/**
* Get a player's flag state.
*
* @param player
* @return
*/
public synchronized PlayerFlagState getState(Player player) {
PlayerFlagState state = states.get(player.getName());
if (state == null) {
state = new PlayerFlagState();
states.put(player.getName(), state);
}
return state;
}
/**
* Keeps state per player.
*/
private static class PlayerFlagState {
private long lastHeal;
public static class PlayerFlagState {
public long lastHeal;
public String lastGreeting;
public String lastFarewell;
public Boolean notifiedForEnter = false;
public Boolean notifiedForLeave = false;
}
}

View File

@ -36,6 +36,7 @@
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.blacklist.events.*;
import com.sk89q.worldguard.bukkit.FlagStateManager.PlayerFlagState;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.RegionGroupFlag.RegionGroup;
@ -75,6 +76,7 @@ public void registerEvents() {
pm.registerEvent(Event.Type.PLAYER_RESPAWN, this, Priority.High, plugin);
pm.registerEvent(Event.Type.PLAYER_ITEM_HELD, this, Priority.High, plugin);
pm.registerEvent(Event.Type.PLAYER_BED_ENTER, this, Priority.High, plugin);
pm.registerEvent(Event.Type.PLAYER_MOVE, this, Priority.High, plugin);
}
/**
@ -151,6 +153,77 @@ public void onPlayerInteract(PlayerInteractEvent event) {
handlePhysicalInteract(event);
}
}
/**
* Called when a player attempts to move.
*
* @param event
*/
@Override
public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
World world = player.getWorld();
GlobalStateManager cfg = plugin.getGlobalStateManager();
WorldStateManager wcfg = cfg.get(world);
if (wcfg.useRegions) {
// Did we move a block?
if (event.getFrom().getBlockX() != event.getTo().getBlockX()
|| event.getFrom().getBlockY() != event.getTo().getBlockY()
|| event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
PlayerFlagState state = plugin.getFlagStateManager().getState(player);
RegionManager mgr = plugin.getGlobalRegionManager().get(world);
Vector pt = toVector(event.getTo());
ApplicableRegionSet set = mgr.getApplicableRegions(pt);
String greeting = set.getFlag(DefaultFlag.GREET_MESSAGE);
String farewell = set.getFlag(DefaultFlag.FAREWELL_MESSAGE);
Boolean notifyEnter = set.getFlag(DefaultFlag.NOTIFY_ENTER);
Boolean notifyLeave = set.getFlag(DefaultFlag.NOTIFY_LEAVE);
if (greeting != null && (state.lastGreeting == null
|| !state.lastGreeting.equals(greeting))) {
player.sendMessage(ChatColor.AQUA + " ** " + greeting);
}
if (state.lastFarewell != null && (farewell == null
|| !state.lastFarewell.equals(farewell))) {
player.sendMessage(ChatColor.AQUA + " ** " + state.lastFarewell);
}
if (notifyEnter != null && notifyEnter && (state.notifiedForEnter == null
|| !state.notifiedForEnter)) {
StringBuilder regionList = new StringBuilder();
for (ProtectedRegion region : set) {
if (regionList.length() != 0) {
regionList.append(", ");
}
regionList.append(region.getId());
}
plugin.broadcastNotification(ChatColor.GRAY + "WG: "
+ ChatColor.LIGHT_PURPLE + player.getName()
+ ChatColor.GOLD + " entered NOTIFY region: "
+ ChatColor.WHITE
+ regionList);
}
if ((notifyLeave == null || !notifyLeave)
&& state.notifiedForLeave != null && state.notifiedForLeave) {
plugin.broadcastNotification(ChatColor.GRAY + "WG: "
+ ChatColor.LIGHT_PURPLE + player.getName()
+ ChatColor.GOLD + " left NOTIFY region");
}
state.lastGreeting = greeting;
state.lastFarewell = farewell;
state.notifiedForEnter = notifyEnter;
state.notifiedForLeave = notifyLeave;
}
}
}
/**
* Called when a player left clicks air.

View File

@ -83,7 +83,7 @@ public class WorldGuardPlugin extends JavaPlugin {
/**
* Used for scheduling flags.
*/
private FlagScheduler flagScheduler;
private FlagStateManager flagStateManager;
/**
* Construct objects. Actual loading occurs when the plugin is enabled, so
@ -139,11 +139,11 @@ public void onEnable() {
(new WorldGuardEntityListener(this)).registerEvents();
(new WorldGuardWeatherListener(this)).registerEvents();
flagScheduler = new FlagScheduler(this);
flagStateManager = new FlagStateManager(this);
if (configuration.useRegionsScheduler) {
getServer().getScheduler().scheduleAsyncRepeatingTask(this,
flagScheduler, FlagScheduler.RUN_DELAY, FlagScheduler.RUN_DELAY);
flagStateManager, FlagStateManager.RUN_DELAY, FlagStateManager.RUN_DELAY);
}
if (configuration.suppressTickSyncWarnings) {
@ -217,6 +217,15 @@ public GlobalRegionManager getGlobalRegionManager() {
public GlobalStateManager getGlobalConfiguration() {
return getGlobalStateManager();
}
/**
* Gets the flag state manager.
*
* @return
*/
public FlagStateManager getFlagStateManager() {
return flagStateManager;
}
/**
* Get the WorldGuardConfiguraton.
@ -680,13 +689,26 @@ public static void createDefaultConfiguration(File actual,
}
}
/**
* Notifies all with the notify permission.
*
* @param msg
*/
public void broadcastNotification(String msg) {
for (Player player : getServer().getOnlinePlayers()) {
if (hasPermission(player, "worldguard.notify")) {
player.sendMessage(msg);
}
}
}
/**
* Forgets a player.
*
* @param player
*/
public void forgetPlayer(Player player) {
flagScheduler.forget(player);
flagStateManager.forget(player);
}
/**

View File

@ -36,7 +36,7 @@
*
* @author sk89q
*/
public class ApplicableRegionSet {
public class ApplicableRegionSet implements Iterable<ProtectedRegion> {
private Collection<ProtectedRegion> applicable;
private ProtectedRegion globalRegion;

View File

@ -46,8 +46,8 @@ public final class DefaultFlag {
public static final StateFlag SNOW_FALL = new StateFlag("snow-fall", true);
public static final StringFlag GREET_MESSAGE = new StringFlag("greeting");
public static final StringFlag FAREWELL_MESSAGE = new StringFlag("farewell");
public static final BooleanFlag NOTIFY_GREET = new BooleanFlag("notify-greet");
public static final BooleanFlag NOTIFY_FAREWELL = new BooleanFlag("notify-farewell");
public static final BooleanFlag NOTIFY_ENTER = new BooleanFlag("notify-enter");
public static final BooleanFlag NOTIFY_LEAVE = new BooleanFlag("notify-leave");
public static final StringFlag DENY_SPAWN = new StringFlag("deny-spawn");
public static final IntegerFlag HEAL_DELAY = new IntegerFlag("heal-delay");
public static final IntegerFlag HEAL_AMOUNT = new IntegerFlag("heal-amount");
@ -61,8 +61,8 @@ public final class DefaultFlag {
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,
USE, PLACE_VEHICLE, GREET_MESSAGE, FAREWELL_MESSAGE, NOTIFY_ENTER,
NOTIFY_LEAVE, DENY_SPAWN, HEAL_DELAY, HEAL_AMOUNT, TELE_LOC,
TELE_PERM, SPAWN_LOC, SPAWN_PERM, BUYABLE, PRICE, SNOW_FALL,
GHAST_FIREBALL
};