Added basic abstracted events.

This commit is contained in:
sk89q 2014-07-12 20:44:06 -07:00
parent ab46fd6244
commit 9a4bc70cdd
16 changed files with 908 additions and 121 deletions

View File

@ -19,10 +19,19 @@
package com.sk89q.worldguard.bukkit;
import static com.sk89q.worldguard.bukkit.BukkitUtil.*;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.internal.Events;
import com.sk89q.worldguard.internal.cause.Causes;
import com.sk89q.worldguard.internal.event.Action;
import com.sk89q.worldguard.internal.event.BlockInteractEvent;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
@ -54,17 +63,7 @@
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.blacklist.events.BlockBreakBlacklistEvent;
import com.sk89q.worldguard.blacklist.events.BlockPlaceBlacklistEvent;
import com.sk89q.worldguard.blacklist.events.DestroyWithBlacklistEvent;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
/**
* The listener for block events.
@ -116,17 +115,12 @@ protected WorldConfiguration getWorldConfig(Player player) {
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockDamage(BlockDamageEvent event) {
Player player = event.getPlayer();
Block blockDamaged = event.getBlock();
Block target = event.getBlock();
// Cake are damaged and not broken when they are eaten, so we must
// handle them a bit separately
if (blockDamaged.getTypeId() == BlockID.CAKE_BLOCK) {
if (!plugin.getGlobalRegionManager().canBuild(player, blockDamaged)) {
player.sendMessage(ChatColor.DARK_RED + "You're not invited to this tea party!");
event.setCancelled(true);
return;
}
if (target.getType() == Material.CAKE_BLOCK) {
Events.fireToCancel(event, new BlockInteractEvent(event, Causes.create(event.getPlayer()), Action.INTERACT, target));
}
}
@ -136,48 +130,18 @@ public void onBlockDamage(BlockDamageEvent event) {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
Block target = event.getBlock();
WorldConfiguration wcfg = getWorldConfig(player);
if (!wcfg.itemDurability) {
ItemStack held = player.getItemInHand();
if (held.getTypeId() > 0
&& !(ItemType.usesDamageValue(held.getTypeId())
|| BlockType.usesData(held.getTypeId()))) {
if (held.getType() != Material.AIR && !(ItemType.usesDamageValue(held.getTypeId())|| BlockType.usesData(held.getTypeId()))) {
held.setDurability((short) 0);
player.setItemInHand(held);
}
}
if (!plugin.getGlobalRegionManager().canBuild(player, event.getBlock())
|| !plugin.getGlobalRegionManager().canConstruct(player, event.getBlock())) {
player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
event.setCancelled(true);
return;
}
if (wcfg.getBlacklist() != null) {
if (!wcfg.getBlacklist().check(
new BlockBreakBlacklistEvent(plugin.wrapPlayer(player),
toVector(event.getBlock()),
event.getBlock().getTypeId()), false, false)) {
event.setCancelled(true);
return;
}
if (!wcfg.getBlacklist().check(
new DestroyWithBlacklistEvent(plugin.wrapPlayer(player),
toVector(event.getBlock()),
player.getItemInHand().getTypeId()), false, false)) {
event.setCancelled(true);
return;
}
}
if (wcfg.isChestProtected(event.getBlock(), player)) {
player.sendMessage(ChatColor.DARK_RED + "The chest is protected.");
event.setCancelled(true);
return;
}
Events.fireToCancel(event, new BlockInteractEvent(event, Causes.create(event.getPlayer()), Action.BREAK, target));
}
/*
@ -498,48 +462,22 @@ public void onBlockPhysics(BlockPhysicsEvent event) {
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
Block blockPlaced = event.getBlock();
Player player = event.getPlayer();
World world = blockPlaced.getWorld();
Block target = event.getBlock();
World world = target.getWorld();
ConfigurationManager cfg = plugin.getGlobalStateManager();
WorldConfiguration wcfg = cfg.get(world);
if (wcfg.useRegions) {
final Location location = blockPlaced.getLocation();
if (!plugin.getGlobalRegionManager().canBuild(player, location)
|| !plugin.getGlobalRegionManager().canConstruct(player, location)) {
player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
event.setCancelled(true);
return;
}
}
Events.fireToCancel(event, new BlockInteractEvent(event, Causes.create(event.getPlayer()), Action.PLACE, target));
if (wcfg.getBlacklist() != null) {
if (!wcfg.getBlacklist().check(
new BlockPlaceBlacklistEvent(plugin.wrapPlayer(player), toVector(blockPlaced),
blockPlaced.getTypeId()), false, false)) {
event.setCancelled(true);
return;
}
}
if (wcfg.signChestProtection && wcfg.getChestProtection().isChest(blockPlaced.getTypeId())) {
if (wcfg.isAdjacentChestProtected(event.getBlock(), player)) {
player.sendMessage(ChatColor.DARK_RED + "This spot is for a chest that you don't have permission for.");
event.setCancelled(true);
return;
}
}
if (wcfg.simulateSponge && blockPlaced.getTypeId() == 19) {
if (wcfg.redstoneSponges && blockPlaced.isBlockIndirectlyPowered()) {
if (wcfg.simulateSponge && target.getType() == Material.SPONGE) {
if (wcfg.redstoneSponges && target.isBlockIndirectlyPowered()) {
return;
}
int ox = blockPlaced.getX();
int oy = blockPlaced.getY();
int oz = blockPlaced.getZ();
int ox = target.getX();
int oy = target.getY();
int oz = target.getZ();
SpongeUtil.clearSpongeWater(plugin, world, ox, oy, oz);
}

View File

@ -19,8 +19,10 @@
package com.sk89q.worldguard.bukkit;
import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
import com.sk89q.worldedit.blocks.ItemID;
import com.sk89q.worldguard.blacklist.events.BlockBreakBlacklistEvent;
import com.sk89q.worldguard.blacklist.events.ItemUseBlacklistEvent;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.block.Block;
@ -42,10 +44,7 @@
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.projectiles.ProjectileSource;
import com.sk89q.worldedit.blocks.ItemID;
import com.sk89q.worldguard.blacklist.events.BlockBreakBlacklistEvent;
import com.sk89q.worldguard.blacklist.events.ItemUseBlacklistEvent;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
/**
* Listener for painting related events.

View File

@ -19,32 +19,6 @@
package com.sk89q.worldguard.bukkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.bukkit.util.CommandsManagerRegistration;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandPermissionsException;
@ -59,9 +33,37 @@
import com.sk89q.worldguard.bukkit.commands.GeneralCommands;
import com.sk89q.worldguard.bukkit.commands.ProtectionCommands;
import com.sk89q.worldguard.bukkit.commands.ToggleCommands;
import com.sk89q.worldguard.internal.listener.BlacklistListener;
import com.sk89q.worldguard.internal.listener.ChestProtectionListener;
import com.sk89q.worldguard.internal.listener.RegionProtectionListener;
import com.sk89q.worldguard.protection.GlobalRegionManager;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.util.FatalConfigurationLoadingException;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
/**
* The main class for WorldGuard as a Bukkit plugin.
@ -182,6 +184,12 @@ public void run() {
(new WorldGuardVehicleListener(this)).registerEvents();
(new WorldGuardServerListener(this)).registerEvents();
(new WorldGuardHangingListener(this)).registerEvents();
// Modules
(new BlacklistListener(this)).registerEvents();
(new ChestProtectionListener(this)).registerEvents();
(new RegionProtectionListener(this)).registerEvents();
configuration.updateCommandBookGodMode();
if (getServer().getPluginManager().isPluginEnabled("CommandBook")) {

View File

@ -0,0 +1,55 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal;
import org.bukkit.Bukkit;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
/**
* Utility methods to deal with events.
*/
public final class Events {
private Events() {
}
/**
* Fire the {@code eventToFire} if {@code original} has not been cancelled
* and cancel the original if the fired event is cancelled.
*
* @param original the original event to potentially cancel
* @param eventToFire the event to fire to consider cancelling the original event
* @param <T> an event that can be fired and is cancellable
* @return true if the event was fired and it caused the original event to be cancelled
*/
public static <T extends Event & Cancellable> boolean fireToCancel(Cancellable original, T eventToFire) {
if (!original.isCancelled()) {
Bukkit.getServer().getPluginManager().callEvent(eventToFire);
if (eventToFire.isCancelled()) {
original.setCancelled(true);
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,38 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.cause;
/**
* Represents a possible cause of an event.
*
* <p>Example causes include players, blocks, entities, and many more.</p>
*
* @param <T> the wrapped object type
*/
public interface Cause<T> {
/**
* Get the underlying object.
*
* @return the underlying object
*/
T get();
}

View File

@ -0,0 +1,71 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.cause;
import org.bukkit.entity.Player;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* Utility methods to handle {@code Cause}s.
*/
public final class Causes {
private Causes() {
}
/**
* Get the first player that is in the list of causes.
*
* @param causes a list of causes, where the originating causes are at the beginning
* @return the player or null
*/
@Nullable
public static Player getInvolvedPlayer(List<? extends Cause<?>> causes) {
for (Cause cause : causes) {
if (cause instanceof PlayerCause) {
return ((PlayerCause) cause).get();
}
}
return null;
}
/**
* Create a list of causes from a list of objects representing causes.
*
* @param cause an array of causes, where the originating causes are at the beginning
* @return a list of causes, where the originating causes are at the beginning
*/
public static List<? extends Cause<?>> create(Object ... cause) {
List<Cause<?>> causes = new ArrayList<Cause<?>>(cause.length);
for (Object o : cause) {
if (o instanceof Player) {
causes.add(new PlayerCause((Player) o));
} else {
causes.add(new UnknownCause(o));
}
}
return causes;
}
}

View File

@ -0,0 +1,48 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.cause;
import org.bukkit.entity.Player;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A cause that is the player.
*/
public class PlayerCause implements Cause<Player> {
private final Player player;
/**
* Create a new instance.
*
* @param player the player
*/
public PlayerCause(Player player) {
checkNotNull(player);
this.player = player;
}
@Override
public Player get() {
return player;
}
}

View File

@ -0,0 +1,46 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.cause;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A cause that is not known.
*/
public class UnknownCause implements Cause<Object> {
private final Object cause;
/**
* Create a new instance.
*
* @param cause the underlying object
*/
public UnknownCause(Object cause) {
checkNotNull(cause);
this.cause = cause;
}
@Override
public Object get() {
return cause;
}
}

View File

@ -0,0 +1,92 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.event;
import com.sk89q.worldguard.internal.cause.Cause;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import java.util.Collections;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
abstract class AbstractInteractEvent extends Event implements Cancellable {
private final Event originalEvent;
private final List<? extends Cause<?>> causes;
private final Action action;
private boolean cancelled;
/**
* Create a new instance
*
* @param originalEvent the original event
* @param causes a list of causes, where the originating causes are at the beginning
* @param action the action that is being taken
*/
protected AbstractInteractEvent(Event originalEvent, List<? extends Cause<?>> causes, Action action) {
checkNotNull(originalEvent);
checkNotNull(causes);
checkNotNull(action);
this.originalEvent = originalEvent;
this.causes = causes;
this.action = action;
}
/**
* Get the original event.
*
* @return the original event
*/
public Event getOriginalEvent() {
return originalEvent;
}
/**
* Return an unmodifiable list of causes, where the originating causes are
* at the beginning of the list.
*
* @return a list of causes
*/
public List<? extends Cause<?>> getCauses() {
return Collections.unmodifiableList(causes);
}
/**
* Get the action that is being taken.
*
* @return the action
*/
public Action getAction() {
return action;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -0,0 +1,31 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.event;
/**
* Represents a possible act upon an object.
*/
public enum Action {
PLACE,
BREAK,
INTERACT
}

View File

@ -0,0 +1,71 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.event;
import com.sk89q.worldguard.internal.cause.Cause;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Fired when a block is interacted with.
*/
public class BlockInteractEvent extends AbstractInteractEvent {
private static final HandlerList handlers = new HandlerList();
private final Block target;
/**
* Create a new instance.
*
* @param originalEvent the original event
* @param causes a list of causes, where the originating causes are at the beginning
* @param action the action that is being taken
* @param target the target block being affected
*/
public BlockInteractEvent(Event originalEvent, List<? extends Cause<?>> causes, Action action, Block target) {
super(originalEvent, causes, action);
checkNotNull(target);
this.target = target;
}
/**
* Get the target block being affected.
*
* @return a block
*/
public Block getTarget() {
return target;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,70 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.event;
import com.sk89q.worldguard.internal.cause.Cause;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Fired when an entity is interacted with.
*/
public class EntityInteractEvent extends AbstractInteractEvent {
private static final HandlerList handlers = new HandlerList();
private final Entity target;
/**
* Create a new instance.
*
* @param originalEvent the original event
* @param causes a list of causes, where the originating causes are at the beginning
* @param action the action that is being taken
* @param target the target entity being affected
*/
public EntityInteractEvent(Event originalEvent, List<? extends Cause<?>> causes, Action action, Entity target) {
super(originalEvent, causes, action);
checkNotNull(target);
this.target = target;
}
/**
* Get the target entity.
*
* @return the target entity
*/
public Entity getTarget() {
return target;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,83 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.listener;
import com.sk89q.worldguard.bukkit.WorldConfiguration;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Abstract listener to ease creation of listeners.
*/
class AbstractListener implements Listener {
private final WorldGuardPlugin plugin;
/**
* Construct the listener.
*
* @param plugin an instance of WorldGuardPlugin
*/
public AbstractListener(WorldGuardPlugin plugin) {
checkNotNull(plugin);
this.plugin = plugin;
}
/**
* Register events.
*/
public void registerEvents() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
/**
* Get the plugin.
*
* @return the plugin
*/
protected WorldGuardPlugin getPlugin() {
return plugin;
}
/**
* Get the world configuration given a world.
*
* @param world The world to get the configuration for.
* @return The configuration for {@code world}
*/
protected WorldConfiguration getWorldConfig(World world) {
return plugin.getGlobalStateManager().get(world);
}
/**
* Get the world configuration given a player.
*
* @param player The player to get the wold from
* @return The {@link WorldConfiguration} for the player's world
*/
protected WorldConfiguration getWorldConfig(Player player) {
return getWorldConfig(player.getWorld());
}
}

View File

@ -0,0 +1,89 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.listener;
import com.sk89q.worldguard.blacklist.events.BlockBreakBlacklistEvent;
import com.sk89q.worldguard.blacklist.events.BlockPlaceBlacklistEvent;
import com.sk89q.worldguard.blacklist.events.DestroyWithBlacklistEvent;
import com.sk89q.worldguard.bukkit.WorldConfiguration;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.internal.cause.Causes;
import com.sk89q.worldguard.internal.event.BlockInteractEvent;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
/**
* Handle events that need to be processed by the blacklist.
*/
public class BlacklistListener extends AbstractListener {
/**
* Construct the listener.
*
* @param plugin an instance of WorldGuardPlugin
*/
public BlacklistListener(WorldGuardPlugin plugin) {
super(plugin);
}
@EventHandler(ignoreCancelled = true)
public void handleBlockInteract(BlockInteractEvent event) {
Player player = Causes.getInvolvedPlayer(event.getCauses());
Block target = event.getTarget();
WorldConfiguration wcfg = getWorldConfig(player);
// Blacklist guard
if (wcfg.getBlacklist() == null) {
return;
}
if (player != null) {
switch (event.getAction()) {
case BREAK:
if (!wcfg.getBlacklist().check(
new BlockBreakBlacklistEvent(getPlugin().wrapPlayer(player), toVector(target), target.getTypeId()), false, false)) {
event.setCancelled(true);
return;
}
if (!wcfg.getBlacklist().check(
new DestroyWithBlacklistEvent(getPlugin().wrapPlayer(player), toVector(target), player.getItemInHand().getTypeId()), false, false)) {
event.setCancelled(true);
return;
}
break;
case PLACE:
if (!wcfg.getBlacklist().check(
new BlockPlaceBlacklistEvent(getPlugin().wrapPlayer(player), toVector(target), target.getTypeId()), false, false)) {
event.setCancelled(true);
return;
}
break;
}
}
}
}

View File

@ -0,0 +1,76 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.listener;
import com.sk89q.worldguard.bukkit.WorldConfiguration;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.internal.cause.Causes;
import com.sk89q.worldguard.internal.event.Action;
import com.sk89q.worldguard.internal.event.BlockInteractEvent;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
/**
* Handle events that need to be processed by the chest protection.
*/
public class ChestProtectionListener extends AbstractListener {
/**
* Construct the listener.
*
* @param plugin an instance of WorldGuardPlugin
*/
public ChestProtectionListener(WorldGuardPlugin plugin) {
super(plugin);
}
@EventHandler(ignoreCancelled = true)
public void handleBlockInteract(BlockInteractEvent event) {
Player player = Causes.getInvolvedPlayer(event.getCauses());
Block target = event.getTarget();
WorldConfiguration wcfg = getWorldConfig(player);
// Early guard
if (!wcfg.signChestProtection) {
return;
}
if (player != null) {
if (wcfg.isChestProtected(target, player)) {
player.sendMessage(ChatColor.DARK_RED + "This chest is protected.");
event.setCancelled(true);
return;
}
if (event.getAction() == Action.PLACE) {
if (wcfg.getChestProtection().isChest(target.getTypeId())) {
if (wcfg.isAdjacentChestProtected(target, player)) {
player.sendMessage(ChatColor.DARK_RED + "This spot is for a chest that you don't have permission for.");
event.setCancelled(true);
return;
}
}
}
}
}
}

View File

@ -0,0 +1,72 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) 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 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 Lesser 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.internal.listener;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.internal.cause.Causes;
import com.sk89q.worldguard.internal.event.Action;
import com.sk89q.worldguard.internal.event.BlockInteractEvent;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
/**
* Handle events that need to be processed by region protection.
*/
public class RegionProtectionListener extends AbstractListener {
/**
* Construct the listener.
*
* @param plugin an instance of WorldGuardPlugin
*/
public RegionProtectionListener(WorldGuardPlugin plugin) {
super(plugin);
}
private void tellErrorMessage(CommandSender sender, Object subject) {
sender.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
}
@EventHandler(ignoreCancelled = true)
public void handleBlockInteract(BlockInteractEvent event) {
Player player = Causes.getInvolvedPlayer(event.getCauses());
Block target = event.getTarget();
if (player != null) {
if (!getPlugin().getGlobalRegionManager().canBuild(player, target)) {
tellErrorMessage(player, target);
event.setCancelled(true);
return;
}
if (event.getAction() != Action.INTERACT) {
if (!getPlugin().getGlobalRegionManager().canConstruct(player, target)) {
tellErrorMessage(player, target);
event.setCancelled(true);
return;
}
}
}
}
}