Added support for the region query cache on Bukkit's side and added it to BukkitContext.

This commit is contained in:
sk89q 2012-11-10 10:48:25 -08:00
parent f4e52f5cd7
commit 579143a5dd
14 changed files with 155 additions and 75 deletions

View File

@ -26,13 +26,16 @@
import com.sk89q.rulelists.Context; import com.sk89q.rulelists.Context;
/** /**
* An implementation of a {@link Context} for Bukkit. * An implementation of a {@link Context} for Bukkit. This object is thread-safe, but
* the underlying objects returned by this class may not be.
* *
* @see Context * @see Context
*/ */
public class BukkitContext extends Context { public class BukkitContext extends Context {
private final RegionQueryCache regionCache;
private final Event event; private final Event event;
private RegionQuery regionQuery;
private String message; private String message;
private Entity sourceEntity; private Entity sourceEntity;
private Entity targetEntity; private Entity targetEntity;
@ -44,9 +47,11 @@ public class BukkitContext extends Context {
/** /**
* Construct a context linked to the given event. * Construct a context linked to the given event.
* *
* @param plugin the WorldGuard plugin
* @param event the event * @param event the event
*/ */
public BukkitContext(Event event) { BukkitContext(WorldGuardPlugin plugin, Event event) {
this.regionCache = plugin.getRegionCache();
this.event = event; this.event = event;
} }
@ -197,4 +202,16 @@ public Event getEvent() {
return event; return event;
} }
/**
* Get the region query object.
*
* @return region query object
*/
public synchronized RegionQuery getRegionQuery() {
if (regionQuery == null) {
regionQuery = regionCache.against(event);
}
return regionQuery;
}
} }

View File

@ -22,28 +22,28 @@
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.GlobalRegionManager; import com.sk89q.worldguard.protection.GlobalRegionManager;
import com.sk89q.worldguard.protection.RegionQueryCache; import com.sk89q.worldguard.protection.RegionQueryCacheEntry;
/** /**
* An implementation of {@link RegionQueryCache} for Bukkit. This implementation is * A class that will cache all region queries. Instances of this class are created
* thread-safe. * via an instance of {@link RegionQueryCache} and represent a point in time.
*/ */
public class BukkitRegionQueryCache extends RegionQueryCache { public final class RegionQuery {
private final WorldGuardPlugin plugin; private final WorldGuardPlugin plugin;
private final RegionQueryCacheEntry entry;
/** /**
* Construct the cache with the given plugin instance. * Construct the instance with the underlying cache entry.
* *
* @param plugin plugin instance * @param entry
*/ */
public BukkitRegionQueryCache(WorldGuardPlugin plugin) { RegionQuery(WorldGuardPlugin plugin, RegionQueryCacheEntry entry) {
this.plugin = plugin; this.plugin = plugin;
this.entry = entry;
} }
/** /**
@ -51,11 +51,10 @@ public BukkitRegionQueryCache(WorldGuardPlugin plugin) {
* temporarily cached for the given event, or null if region protection has * temporarily cached for the given event, or null if region protection has
* been disabled for the the world of the given location. * been disabled for the the world of the given location.
* *
* @param event the event to cache against
* @param location the location to check * @param location the location to check
* @return the set, or null if region protection is disabled for the given location * @return the set, or null if region protection is disabled for the given location
*/ */
public ApplicableRegionSet lookup(Event event, Location location) { public ApplicableRegionSet lookup(Location location) {
World world = location.getWorld(); World world = location.getWorld();
Vector vector = BukkitUtil.toVector(location); Vector vector = BukkitUtil.toVector(location);
@ -67,7 +66,7 @@ public ApplicableRegionSet lookup(Event event, Location location) {
return null; return null;
} }
return get(event).lookup(regionManager.get(world), vector); return entry.lookup(regionManager.get(world), vector);
} }
/** /**
@ -75,12 +74,11 @@ public ApplicableRegionSet lookup(Event event, Location location) {
* temporarily cached for the given event, or null if region protection has * temporarily cached for the given event, or null if region protection has
* been disabled for the the world of the given block. * been disabled for the the world of the given block.
* *
* @param event the event to cache against
* @param block the block to check * @param block the block to check
* @return the set, or null if region protection is disabled for the given block * @return the set, or null if region protection is disabled for the given block
*/ */
public ApplicableRegionSet lookup(Event event, Block block) { public ApplicableRegionSet lookup(Block block) {
return lookup(event, block.getLocation()); return lookup(block.getLocation());
} }
/** /**
@ -88,12 +86,11 @@ public ApplicableRegionSet lookup(Event event, Block block) {
* temporarily cached for the given event, or null if region protection has * temporarily cached for the given event, or null if region protection has
* been disabled for the the world of the given entity. * been disabled for the the world of the given entity.
* *
* @param event the event to cache against
* @param entity the entity to check * @param entity the entity to check
* @return the set, or null if region protection is disabled for the given entity * @return the set, or null if region protection is disabled for the given entity
*/ */
public ApplicableRegionSet lookup(Event event, Entity entity) { public ApplicableRegionSet lookup(Entity entity) {
return lookup(event, entity.getLocation()); return lookup(entity.getLocation());
} }
} }

View File

@ -0,0 +1,53 @@
// $Id$
/*
* This file is a part of WorldGuard.
* Copyright (c) sk89q <http://www.sk89q.com>
* Copyright (c) the WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* (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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.bukkit;
import org.bukkit.event.Event;
import com.sk89q.worldguard.protection.AbstractRegionQueryCache;
/**
* An implementation of {@link AbstractRegionQueryCache} for Bukkit. This implementation
* is thread-safe.
*/
public final class RegionQueryCache extends AbstractRegionQueryCache {
private final WorldGuardPlugin plugin;
/**
* Construct the cache with the given plugin instance.
*
* @param plugin plugin instance
*/
public RegionQueryCache(WorldGuardPlugin plugin) {
this.plugin = plugin;
}
/**
* Get a {@link RegionQuery} object that can cache region queries. The object
* returned by this method does not need to be cached. It is merely a proxy class.
*
* @param event the event to cache against
* @return an object to perform lookups against
*/
public RegionQuery against(Event event) {
return new RegionQuery(plugin, get(event));
}
}

View File

@ -83,7 +83,7 @@ public void onBlockDamage(BlockDamageEvent event) {
if (blockDamaged.getTypeId() == BlockID.CAKE_BLOCK) { if (blockDamaged.getTypeId() == BlockID.CAKE_BLOCK) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(player); context.setSourceEntity(player);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
@ -103,7 +103,7 @@ public void onBlockBreak(BlockBreakEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_BREAK); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_BREAK);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(player); context.setSourceEntity(player);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
@ -143,7 +143,7 @@ public void onBlockFromTo(BlockFromToEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_SPREAD); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_SPREAD);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceBlock(event.getBlock().getState()); context.setSourceBlock(event.getBlock().getState());
context.setTargetBlock(event.getToBlock().getState()); context.setTargetBlock(event.getToBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
@ -177,7 +177,7 @@ public void onBlockIgnite(BlockIgniteEvent event) {
case FLINT_AND_STEEL: case FLINT_AND_STEEL:
// Consider flint and steel as an item use // Consider flint and steel as an item use
rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE); rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE);
context = new BukkitContext(event); context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
context.setItem(event.getPlayer().getItemInHand()); // Should be flint and steel context.setItem(event.getPlayer().getItemInHand()); // Should be flint and steel
@ -196,7 +196,7 @@ public void onBlockIgnite(BlockIgniteEvent event) {
case SPREAD: case SPREAD:
// Consider everything else as a block spread // Consider everything else as a block spread
rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_SPREAD); rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_SPREAD);
context = new BukkitContext(event); context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
// Make a virtual source state // Make a virtual source state
@ -218,7 +218,7 @@ public void onBlockIgnite(BlockIgniteEvent event) {
case LIGHTNING: case LIGHTNING:
// Consider everything else as a block spread // Consider everything else as a block spread
rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_PLACE); rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_PLACE);
context = new BukkitContext(event); context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
// Make a virtual new state // Make a virtual new state
@ -250,7 +250,7 @@ public void onBlockBurn(BlockBurnEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_BREAK); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_BREAK);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
BlockState virtualFireState = event.getBlock().getState(); BlockState virtualFireState = event.getBlock().getState();
virtualFireState.setType(Material.FIRE); virtualFireState.setType(Material.FIRE);
context.setSourceBlock(virtualFireState); context.setSourceBlock(virtualFireState);
@ -270,7 +270,7 @@ public void onBlockPhysics(BlockPhysicsEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_PHYSICS); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_PHYSICS);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -306,7 +306,7 @@ public void onBlockPlace(BlockPlaceEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_PLACE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_PLACE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
context.setPlacedBlock(event.getBlockReplacedState()); context.setPlacedBlock(event.getBlockReplacedState());
if (rules.process(context)) { if (rules.process(context)) {
@ -379,7 +379,7 @@ public void onSignChange(SignChangeEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -394,7 +394,7 @@ public void onLeavesDecay(LeavesDecayEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_FADE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_FADE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -409,7 +409,7 @@ public void onBlockForm(BlockFormEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_FORM); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_FORM);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
context.setPlacedBlock(event.getNewState()); context.setPlacedBlock(event.getNewState());
if (rules.process(context)) { if (rules.process(context)) {
@ -425,7 +425,7 @@ public void onBlockSpread(BlockSpreadEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_SPREAD); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_SPREAD);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceBlock(event.getSource().getState()); context.setSourceBlock(event.getSource().getState());
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
context.setPlacedBlock(event.getNewState()); context.setPlacedBlock(event.getNewState());
@ -442,7 +442,7 @@ public void onBlockFade(BlockFadeEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_FADE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_FADE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
context.setPlacedBlock(event.getNewState()); context.setPlacedBlock(event.getNewState());
if (rules.process(context)) { if (rules.process(context)) {
@ -458,7 +458,7 @@ public void onBlockDispense(BlockDispenseEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_DROP); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_DROP);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceBlock(event.getBlock().getState()); context.setSourceBlock(event.getBlock().getState());
context.setItem(event.getItem()); context.setItem(event.getItem());
if (rules.process(context)) { if (rules.process(context)) {

View File

@ -79,7 +79,7 @@ public void onEntityInteract(EntityInteractEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getEntity()); context.setSourceEntity(event.getEntity());
context.setTargetBlock(event.getBlock().getState()); context.setTargetBlock(event.getBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
@ -97,7 +97,7 @@ public void onEntityDeath(EntityDeathEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DEATH); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DEATH);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
// Set a message if it's a death from a player // Set a message if it's a death from a player
@ -122,7 +122,7 @@ public void onEntityDamage(EntityDamageEvent event) {
if (event instanceof EntityDamageByEntityEvent) { if (event instanceof EntityDamageByEntityEvent) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(((EntityDamageByEntityEvent) event).getDamager()); context.setSourceEntity(((EntityDamageByEntityEvent) event).getDamager());
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {
@ -134,7 +134,7 @@ public void onEntityDamage(EntityDamageEvent event) {
} else if (event instanceof EntityDamageByBlockEvent) { } else if (event instanceof EntityDamageByBlockEvent) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
Block damager = ((EntityDamageByBlockEvent) event).getDamager(); Block damager = ((EntityDamageByBlockEvent) event).getDamager();
if (damager != null) { // Should NOT be null! if (damager != null) { // Should NOT be null!
context.setSourceBlock(damager.getState()); context.setSourceBlock(damager.getState());
@ -149,7 +149,7 @@ public void onEntityDamage(EntityDamageEvent event) {
} else { } else {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -167,7 +167,7 @@ public void onEntityCombust(EntityCombustEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_IGNITE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_IGNITE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(entity); context.setTargetEntity(entity);
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -184,7 +184,7 @@ public void onEntityExplode(EntityExplodeEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_EXPLODE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_EXPLODE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -199,7 +199,7 @@ public void onEntityExplode(EntityExplodeEvent event) {
Block block = iter.next(); Block block = iter.next();
rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_BREAK); rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_BREAK);
context = new BukkitContext(event); context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getEntity()); context.setSourceEntity(event.getEntity());
context.setTargetBlock(block.getState()); context.setTargetBlock(block.getState());
rules.process(context); rules.process(context);
@ -216,7 +216,7 @@ public void onExplosionPrime(ExplosionPrimeEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_EXPLODE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_EXPLODE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -231,7 +231,7 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_SPAWN); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_SPAWN);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -246,7 +246,7 @@ public void onPigZap(PigZapEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_STRIKE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_STRIKE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -261,7 +261,7 @@ public void onCreeperPower(CreeperPowerEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_STRIKE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_STRIKE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
@ -279,7 +279,7 @@ public void onEntityChangeBlock(EntityChangeBlockEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(event.getTo() == Material.AIR ? RuleSet rules = wcfg.getRuleList().get(event.getTo() == Material.AIR ?
KnownAttachment.BLOCK_BREAK : KnownAttachment.BLOCK_PLACE); KnownAttachment.BLOCK_BREAK : KnownAttachment.BLOCK_PLACE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(entity); context.setSourceEntity(entity);
BlockState newState = event.getBlock().getState(); // This event is lame BlockState newState = event.getBlock().getState(); // This event is lame
newState.setType(event.getTo()); // Need to construct our own BlockState newState.setType(event.getTo()); // Need to construct our own BlockState

View File

@ -64,7 +64,7 @@ public void onHangingingBreak(HangingBreakEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
if (event instanceof HangingBreakByEntityEvent) { if (event instanceof HangingBreakByEntityEvent) {
context.setSourceEntity(((HangingBreakByEntityEvent) event).getRemover()); context.setSourceEntity(((HangingBreakByEntityEvent) event).getRemover());
} }
@ -85,7 +85,7 @@ public void onHangingPlace(HangingPlaceEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_SPAWN); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_SPAWN);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setTargetEntity(event.getEntity()); context.setTargetEntity(event.getEntity());
if (rules.process(context)) { if (rules.process(context)) {

View File

@ -66,7 +66,7 @@ public void onPaintingBreak(PaintingBreakEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
if (event instanceof PaintingBreakByEntityEvent) { if (event instanceof PaintingBreakByEntityEvent) {
context.setSourceEntity(((PaintingBreakByEntityEvent) event).getRemover()); context.setSourceEntity(((PaintingBreakByEntityEvent) event).getRemover());
} }
@ -87,7 +87,7 @@ public void onPaintingPlace(PaintingPlaceEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_SPAWN); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_SPAWN);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setTargetEntity(event.getPainting()); context.setTargetEntity(event.getPainting());
if (rules.process(context)) { if (rules.process(context)) {

View File

@ -97,7 +97,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.PLAYER_JOIN); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.PLAYER_JOIN);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(player); context.setTargetEntity(player);
rules.process(context); rules.process(context);
} }
@ -109,7 +109,7 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.CHAT); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.CHAT);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setMessage(event.getMessage()); context.setMessage(event.getMessage());
@ -160,7 +160,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.PLAYER_QUIT); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.PLAYER_QUIT);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(player); context.setSourceEntity(player);
rules.process(context); rules.process(context);
@ -186,7 +186,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
if (block != null) { if (block != null) {
rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT); rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT);
context = new BukkitContext(event); context = new BukkitContext(plugin, event);
context.setSourceEntity(player); context.setSourceEntity(player);
context.setTargetBlock(event.getClickedBlock().getState()); context.setTargetBlock(event.getClickedBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
@ -198,7 +198,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
ItemStack heldItem = event.getPlayer().getItemInHand(); ItemStack heldItem = event.getPlayer().getItemInHand();
if (heldItem != null) { if (heldItem != null) {
rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE); rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE);
context = new BukkitContext(event); context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setItem(heldItem); context.setItem(heldItem);
if (rules.process(context)) { if (rules.process(context)) {
@ -215,7 +215,7 @@ public void onPlayerDropItem(PlayerDropItemEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_DROP); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_DROP);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(player); context.setSourceEntity(player);
context.setItem(event.getItemDrop().getItemStack()); context.setItem(event.getItemDrop().getItemStack());
if (rules.process(context)) { if (rules.process(context)) {
@ -231,7 +231,7 @@ public void onPlayerPickupItem(PlayerPickupItemEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_PICKUP); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_PICKUP);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setItem(event.getItem().getItemStack()); context.setItem(event.getItem().getItemStack());
if (rules.process(context)) { if (rules.process(context)) {
@ -251,7 +251,7 @@ public void onPlayerDeath(PlayerDeathEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DEATH); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DEATH);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetEntity(player); context.setTargetEntity(player);
rules.process(context); rules.process(context);
} }
@ -266,7 +266,7 @@ public void onPlayerBucketFill(PlayerBucketFillEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setItem(event.getItemStack()); context.setItem(event.getItemStack());
if (rules.process(context)) { if (rules.process(context)) {
@ -285,7 +285,7 @@ public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
context.setItem(event.getItemStack()); context.setItem(event.getItemStack());
if (rules.process(context)) { if (rules.process(context)) {
@ -305,7 +305,7 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.PLAYER_RESPAWN); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.PLAYER_RESPAWN);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(event.getPlayer()); context.setSourceEntity(event.getPlayer());
rules.process(context); rules.process(context);
} }
@ -319,7 +319,7 @@ public void onPlayerBedEnter(PlayerBedEnterEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(player); context.setSourceEntity(player);
context.setTargetBlock(event.getBed().getState()); context.setTargetBlock(event.getBed().getState());
if (rules.process(context)) { if (rules.process(context)) {

View File

@ -109,6 +109,7 @@ public class WorldGuardPlugin extends JavaPlugin {
private final ConfigurationManager configuration; private final ConfigurationManager configuration;
private FlagStateManager flagStateManager; private FlagStateManager flagStateManager;
private RuleListsManager ruleListsManager; private RuleListsManager ruleListsManager;
private RegionQueryCache regionCache;
private final LagStopMode lagStopper; private final LagStopMode lagStopper;
private WorldGuardWorldListener worldListener; private WorldGuardWorldListener worldListener;
@ -141,6 +142,9 @@ public void onEnable() {
ruleListsManager = new RuleListsManager(); ruleListsManager = new RuleListsManager();
registerRuleList(); registerRuleList();
// Setup the cache
regionCache = new RegionQueryCache(this);
// Set the proper command injector // Set the proper command injector
commands.setInjector(new SimpleInjector(this)); commands.setInjector(new SimpleInjector(this));
@ -300,6 +304,15 @@ public RuleListsManager getRulesListManager() {
return ruleListsManager; return ruleListsManager;
} }
/**
* Get the region query cache.
*
* @return region query cache
*/
public RegionQueryCache getRegionCache() {
return regionCache;
}
/** /**
* Get the built-in rules. * Get the built-in rules.
* *

View File

@ -46,7 +46,7 @@ public void onVehicleDestroy(VehicleDestroyEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.ENTITY_DAMAGE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setSourceEntity(destroyer); context.setSourceEntity(destroyer);
context.setTargetEntity(vehicle); context.setTargetEntity(vehicle);
if (rules.process(context)) { if (rules.process(context)) {

View File

@ -59,7 +59,7 @@ public void onWeatherChange(WeatherChangeEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WEATHER_TRANSITION); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WEATHER_TRANSITION);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -73,7 +73,7 @@ public void onThunderChange(ThunderChangeEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WEATHER_TRANSITION); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WEATHER_TRANSITION);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -89,7 +89,7 @@ public void onLightningStrike(LightningStrikeEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WEATHER_PHENOMENON); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WEATHER_PHENOMENON);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
context.setTargetBlock(loc.getBlock().getState()); context.setTargetBlock(loc.getBlock().getState());
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);

View File

@ -46,7 +46,7 @@ public void onWorldLoad(WorldLoadEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WORLD_LOAD); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WORLD_LOAD);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
rules.process(context); rules.process(context);
} }
@ -57,7 +57,7 @@ public void onWorldLoad(WorldUnloadEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WORLD_UNLOAD); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WORLD_UNLOAD);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
if (rules.process(context)) { if (rules.process(context)) {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -73,7 +73,7 @@ public void onWorldSave(WorldSaveEvent event) {
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WORLD_SAVE); RuleSet rules = wcfg.getRuleList().get(KnownAttachment.WORLD_SAVE);
BukkitContext context = new BukkitContext(event); BukkitContext context = new BukkitContext(plugin, event);
rules.process(context); rules.process(context);
} }
} }

View File

@ -35,12 +35,12 @@
* This class is abstract because implementing class should provide methods to check * This class is abstract because implementing class should provide methods to check
* against a standard key object. This cache itself is thread-safe. * against a standard key object. This cache itself is thread-safe.
*/ */
public abstract class RegionQueryCache { public abstract class AbstractRegionQueryCache {
private final WeakHashMap<Object, RegionQueryCacheEntry> cache = private final WeakHashMap<Object, RegionQueryCacheEntry> cache =
new WeakHashMap<Object, RegionQueryCacheEntry>(); new WeakHashMap<Object, RegionQueryCacheEntry>();
public RegionQueryCache() { public AbstractRegionQueryCache() {
} }
/** /**

View File

@ -30,9 +30,9 @@
* An object to cache a list of {@link ApplicableRegionSet}s. This class is * An object to cache a list of {@link ApplicableRegionSet}s. This class is
* thread-safe. * thread-safe.
* *
* @see RegionQueryCache * @see AbstractRegionQueryCache
*/ */
public class RegionQueryCacheEntry { public final class RegionQueryCacheEntry {
private final Map<BlockVector, ApplicableRegionSet> cachedPoints = private final Map<BlockVector, ApplicableRegionSet> cachedPoints =
new HashMap<BlockVector, ApplicableRegionSet>(); new HashMap<BlockVector, ApplicableRegionSet>();