Check for entities and projectiles removing items from frames too.

This commit is contained in:
wizjany 2013-12-09 17:46:33 -05:00
parent bbd1a0ef10
commit 2d90fddbee

View File

@ -236,18 +236,12 @@ private void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
}
}
}
}
if (defender instanceof ItemFrame && wcfg.useRegions) {
// bukkit throws this event when a player attempts to remove an item from a frame
World world = player.getWorld();
RegionManager mgr = plugin.getGlobalRegionManager().get(world);
if (!plugin.getGlobalRegionManager().hasBypass(player, world)
&& !mgr.getApplicableRegions(defender.getLocation())
.canBuild(plugin.wrapPlayer(player))) {
player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
event.setCancelled(true);
return;
}
if (defender instanceof ItemFrame) {
if (checkItemFrameProtection(attacker, (ItemFrame) defender)) {
event.setCancelled(true);
return;
}
}
@ -451,6 +445,11 @@ private void onEntityDamageByProjectile(EntityDamageByEntityEvent event) {
}
}
}
} else if (defender instanceof ItemFrame) {
if (checkItemFrameProtection(attacker, (ItemFrame) defender)) {
event.setCancelled(true);
return;
}
}
}
@ -1003,4 +1002,41 @@ public void tryCancelPVPEvent(final Player attackingPlayer, final Player defendi
event.setCancelled(true);
}
}
/**
* Checks regions and config settings to protect items from being knocked
* out of item frames.
* @param attacker attacking entity
* @param defender item frame being damaged
* @return true if the event should be cancelled
*/
private boolean checkItemFrameProtection(Entity attacker, ItemFrame defender) {
World world = attacker.getWorld();
ConfigurationManager cfg = plugin.getGlobalStateManager();
WorldConfiguration wcfg = cfg.get(world);
if (wcfg.useRegions) {
// bukkit throws this event when a player attempts to remove an item from a frame
RegionManager mgr = plugin.getGlobalRegionManager().get(world);
if (attacker instanceof Player) {
Player player = (Player) attacker;
LocalPlayer localPlayer = plugin.wrapPlayer(player);
if (!plugin.getGlobalRegionManager().hasBypass(player, world)
&& !mgr.getApplicableRegions(defender.getLocation())
.canBuild(localPlayer)) {
player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
return true;
}
} else {
if (!plugin.getGlobalRegionManager().allows(
DefaultFlag.ENTITY_ITEM_FRAME_DESTROY, defender.getLocation())) {
return true;
}
}
}
if (wcfg.blockEntityItemFrameDestroy && !(attacker instanceof Player)) {
return true;
}
return false;
}
}