From f92e55e0616d6be3b3836d625e6406b53d424bbd Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 12 Aug 2014 19:48:20 -0700 Subject: [PATCH] Simplfy Cause objects. --- .../worldguard/bukkit/WorldGuardPlugin.java | 2 + .../sk89q/worldguard/bukkit/cause/Cause.java | 132 ++++++++++++++++++ .../bukkit/event/AbstractInteractEvent.java | 24 ++-- .../event/block/AbstractBlockEvent.java | 11 +- .../bukkit/event/block/BreakBlockEvent.java | 12 +- .../bukkit/event/block/PlaceBlockEvent.java | 12 +- .../bukkit/event/block/UseBlockEvent.java | 12 +- .../event/entity/AbstractEntityEvent.java | 25 +--- .../event/entity/DestroyEntityEvent.java | 14 +- .../bukkit/event/entity/SpawnEntityEvent.java | 12 +- .../bukkit/event/entity/UseEntityEvent.java | 14 +- .../bukkit/event/inventory/UseItemEvent.java | 16 +-- .../bukkit/listener/BlacklistListener.java | 15 +- .../listener/BlockedPotionsListener.java | 5 +- .../listener/ChestProtectionListener.java | 7 +- .../bukkit/listener/DebuggingListener.java | 23 ++- .../listener/EventAbstractionListener.java | 77 +++++----- .../listener/RegionProtectionListener.java | 19 ++- .../worldguard/util/cause/BlockCause.java | 53 ------- .../sk89q/worldguard/util/cause/Cause.java | 38 ----- .../sk89q/worldguard/util/cause/Causes.java | 104 -------------- .../worldguard/util/cause/EntityCause.java | 52 ------- .../worldguard/util/cause/PlayerCause.java | 53 ------- .../worldguard/util/cause/UnknownCause.java | 50 ------- 24 files changed, 249 insertions(+), 533 deletions(-) create mode 100644 src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java delete mode 100644 src/main/java/com/sk89q/worldguard/util/cause/BlockCause.java delete mode 100644 src/main/java/com/sk89q/worldguard/util/cause/Cause.java delete mode 100644 src/main/java/com/sk89q/worldguard/util/cause/Causes.java delete mode 100644 src/main/java/com/sk89q/worldguard/util/cause/EntityCause.java delete mode 100644 src/main/java/com/sk89q/worldguard/util/cause/PlayerCause.java delete mode 100644 src/main/java/com/sk89q/worldguard/util/cause/UnknownCause.java diff --git a/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java b/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java index 75e8a2b6..99cf1b65 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java @@ -36,6 +36,7 @@ import com.sk89q.worldguard.bukkit.listener.BlacklistListener; import com.sk89q.worldguard.bukkit.listener.BlockedPotionsListener; import com.sk89q.worldguard.bukkit.listener.ChestProtectionListener; +import com.sk89q.worldguard.bukkit.listener.DebuggingListener; import com.sk89q.worldguard.bukkit.listener.EventAbstractionListener; import com.sk89q.worldguard.bukkit.listener.RegionProtectionListener; import com.sk89q.worldguard.bukkit.listener.WorldGuardBlockListener; @@ -202,6 +203,7 @@ public void run() { (new RegionProtectionListener(this)).registerEvents(); (new BlockedPotionsListener(this)).registerEvents(); (new EventAbstractionListener(this)).registerEvents(); + (new DebuggingListener(this, getLogger())).registerEvents(); configuration.updateCommandBookGodMode(); diff --git a/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java b/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java new file mode 100644 index 00000000..c9adfe32 --- /dev/null +++ b/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java @@ -0,0 +1,132 @@ +/* + * WorldGuard, a suite of tools for Minecraft + * Copyright (C) sk89q + * 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 . + */ + +package com.sk89q.worldguard.bukkit.cause; + +import com.google.common.base.Joiner; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * An instance of this object describes the actors that played a role in + * causing an event, with the ability to describe a situation where one actor + * controls several other actors to create the event. + * + *

For example, if a player fires an arrow that hits an item frame, the player + * is the initiator, while the arrow is merely controlled by the player to + * hit the item frame.

+ */ +public class Cause { + + private static final Cause UNKNOWN = new Cause(Collections.emptyList()); + + private final List causes; + + /** + * Create a new instance. + * + * @param causes a list of causes + */ + private Cause(List causes) { + checkNotNull(causes); + this.causes = causes; + } + + /** + * Return whether a cause is known. + * + * @return true if known + */ + public boolean isKnown() { + return !causes.isEmpty(); + } + + @Nullable + public Player getPlayerRootCause() { + for (Object object : causes) { + if (object instanceof Player) { + return (Player) object; + } + } + + return null; + } + + @Override + public String toString() { + return Joiner.on(" | ").join(causes); + } + + /** + * Expand an cause object. + * + * @param list the list to add elements to + * @param element an array of objects + */ + private static void expand(List list, @Nullable Object ... element) { + if (element != null) { + for (Object o : element) { + if (o == null) { + continue; + } + + if (o instanceof Projectile) { + expand(list, ((Projectile) o).getShooter()); + } else { + list.add(o); + } + } + } + } + + /** + * Create a new instance with the given objects as the cause, + * where the first-most object is the initial initiator and those + * following it are controlled by the previous entry. + * + * @param cause an array of causing objects + * @return a cause + */ + public static Cause create(@Nullable Object ... cause) { + if (cause != null) { + List causes = new ArrayList(cause.length); + expand(causes, cause); + return new Cause(causes); + } else { + return UNKNOWN; + } + } + + /** + * Create a new instance that indicates that the cause is not known. + * + * @return a cause + */ + public static Cause unknown() { + return UNKNOWN; + } + +} diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/AbstractInteractEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/AbstractInteractEvent.java index a2a8d22e..0c4aa410 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/AbstractInteractEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/AbstractInteractEvent.java @@ -19,32 +19,29 @@ package com.sk89q.worldguard.bukkit.event; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.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; public abstract class AbstractInteractEvent extends Event implements Cancellable { private final Event originalEvent; - private final List> causes; + private final Cause cause; 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 cause the cause */ - protected AbstractInteractEvent(Event originalEvent, List> causes) { + protected AbstractInteractEvent(Event originalEvent, Cause cause) { checkNotNull(originalEvent); - checkNotNull(causes); + checkNotNull(cause); this.originalEvent = originalEvent; - this.causes = causes; + this.cause = cause; } /** @@ -57,13 +54,12 @@ public Event getOriginalEvent() { } /** - * Return an unmodifiable list of causes, where the originating causes are - * at the beginning of the list. + * Return the cause. * - * @return a list of causes + * @return the cause */ - public List> getCauses() { - return Collections.unmodifiableList(causes); + public Cause getCause() { + return cause; } @Override diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/block/AbstractBlockEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/block/AbstractBlockEvent.java index a9f423ef..1d01971d 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/block/AbstractBlockEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/block/AbstractBlockEvent.java @@ -19,7 +19,7 @@ package com.sk89q.worldguard.bukkit.event.block; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import com.sk89q.worldguard.bukkit.event.AbstractInteractEvent; import org.bukkit.Location; import org.bukkit.Material; @@ -27,7 +27,6 @@ import org.bukkit.event.Event; import javax.annotation.Nullable; -import java.util.List; import static com.google.common.base.Preconditions.checkNotNull; @@ -38,16 +37,16 @@ abstract class AbstractBlockEvent extends AbstractInteractEvent { private final Block block; private final Material effectiveMaterial; - protected AbstractBlockEvent(Event originalEvent, List> causes, Block block) { - super(originalEvent, causes); + protected AbstractBlockEvent(Event originalEvent, Cause cause, Block block) { + super(originalEvent, cause); checkNotNull(block); this.target = block.getLocation(); this.block = block; this.effectiveMaterial = block.getType(); } - protected AbstractBlockEvent(Event originalEvent, List> causes, Location target, Material effectiveMaterial) { - super(originalEvent, causes); + protected AbstractBlockEvent(Event originalEvent, Cause cause, Location target, Material effectiveMaterial) { + super(originalEvent, cause); this.target = target; this.block = null; this.effectiveMaterial = effectiveMaterial; diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/block/BreakBlockEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/block/BreakBlockEvent.java index fb929316..e3647c62 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/block/BreakBlockEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/block/BreakBlockEvent.java @@ -19,25 +19,23 @@ package com.sk89q.worldguard.bukkit.event.block; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import java.util.List; - public class BreakBlockEvent extends AbstractBlockEvent { private static final HandlerList handlers = new HandlerList(); - public BreakBlockEvent(Event originalEvent, List> causes, Block block) { - super(originalEvent, causes, block); + public BreakBlockEvent(Event originalEvent, Cause cause, Block block) { + super(originalEvent, cause, block); } - public BreakBlockEvent(Event originalEvent, List> causes, Location target, Material effectiveMaterial) { - super(originalEvent, causes, target, effectiveMaterial); + public BreakBlockEvent(Event originalEvent, Cause cause, Location target, Material effectiveMaterial) { + super(originalEvent, cause, target, effectiveMaterial); } @Override diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/block/PlaceBlockEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/block/PlaceBlockEvent.java index 68d0d7c9..7a02a876 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/block/PlaceBlockEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/block/PlaceBlockEvent.java @@ -19,25 +19,23 @@ package com.sk89q.worldguard.bukkit.event.block; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import java.util.List; - public class PlaceBlockEvent extends AbstractBlockEvent { private static final HandlerList handlers = new HandlerList(); - public PlaceBlockEvent(Event originalEvent, List> causes, Block block) { - super(originalEvent, causes, block); + public PlaceBlockEvent(Event originalEvent, Cause cause, Block block) { + super(originalEvent, cause, block); } - public PlaceBlockEvent(Event originalEvent, List> causes, Location target, Material effectiveMaterial) { - super(originalEvent, causes, target, effectiveMaterial); + public PlaceBlockEvent(Event originalEvent, Cause cause, Location target, Material effectiveMaterial) { + super(originalEvent, cause, target, effectiveMaterial); } @Override diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/block/UseBlockEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/block/UseBlockEvent.java index 21ba32de..bd22dbf0 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/block/UseBlockEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/block/UseBlockEvent.java @@ -19,15 +19,13 @@ package com.sk89q.worldguard.bukkit.event.block; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import java.util.List; - /** * Fired when a block is interacted with. */ @@ -35,12 +33,12 @@ public class UseBlockEvent extends AbstractBlockEvent { private static final HandlerList handlers = new HandlerList(); - public UseBlockEvent(Event originalEvent, List> causes, Block block) { - super(originalEvent, causes, block); + public UseBlockEvent(Event originalEvent, Cause cause, Block block) { + super(originalEvent, cause, block); } - public UseBlockEvent(Event originalEvent, List> causes, Location target, Material effectiveMaterial) { - super(originalEvent, causes, target, effectiveMaterial); + public UseBlockEvent(Event originalEvent, Cause cause, Location target, Material effectiveMaterial) { + super(originalEvent, cause, target, effectiveMaterial); } @Override diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/AbstractEntityEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/AbstractEntityEvent.java index bb723f4b..093325e8 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/AbstractEntityEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/AbstractEntityEvent.java @@ -19,14 +19,13 @@ package com.sk89q.worldguard.bukkit.event.entity; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import com.sk89q.worldguard.bukkit.event.AbstractInteractEvent; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.event.Event; import javax.annotation.Nullable; -import java.util.List; import static com.google.common.base.Preconditions.checkNotNull; @@ -36,29 +35,15 @@ abstract class AbstractEntityEvent extends AbstractInteractEvent { @Nullable private final Entity entity; - /** - * Create a new instance - * - * @param originalEvent the original event - * @param causes a list of causes, where the originating causes are at the beginning - * @param entity the target - */ - protected AbstractEntityEvent(Event originalEvent, List> causes, Entity entity) { - super(originalEvent, causes); + protected AbstractEntityEvent(Event originalEvent, Cause cause, Entity entity) { + super(originalEvent, cause); checkNotNull(entity); this.target = entity.getLocation(); this.entity = entity; } - /** - * Create a new instance - * - * @param originalEvent the original event - * @param causes a list of causes, where the originating causes are at the beginning - * @param target the target - */ - protected AbstractEntityEvent(Event originalEvent, List> causes, Location target) { - super(originalEvent, causes); + protected AbstractEntityEvent(Event originalEvent, Cause cause, Location target) { + super(originalEvent, cause); checkNotNull(target); this.target = target; this.entity = null; diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/DestroyEntityEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/DestroyEntityEvent.java index 3b74ee09..db53d8eb 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/DestroyEntityEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/DestroyEntityEvent.java @@ -19,13 +19,12 @@ package com.sk89q.worldguard.bukkit.event.entity; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import org.bukkit.entity.Entity; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import javax.annotation.Nonnull; -import java.util.List; import static com.google.common.base.Preconditions.checkNotNull; @@ -33,15 +32,8 @@ public class DestroyEntityEvent extends AbstractEntityEvent { private static final HandlerList handlers = new HandlerList(); - /** - * Create a new instance. - * - * @param originalEvent the original event - * @param causes a list of causes, where the originating causes are at the beginning - * @param target the target entity being affected - */ - public DestroyEntityEvent(Event originalEvent, List> causes, Entity target) { - super(originalEvent, causes, checkNotNull(target)); + public DestroyEntityEvent(Event originalEvent, Cause cause, Entity target) { + super(originalEvent, cause, checkNotNull(target)); } @Override diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/SpawnEntityEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/SpawnEntityEvent.java index bfd41a9f..05fee2f7 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/SpawnEntityEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/SpawnEntityEvent.java @@ -19,15 +19,13 @@ package com.sk89q.worldguard.bukkit.event.entity; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import java.util.List; - import static com.google.common.base.Preconditions.checkNotNull; public class SpawnEntityEvent extends AbstractEntityEvent { @@ -35,13 +33,13 @@ public class SpawnEntityEvent extends AbstractEntityEvent { private static final HandlerList handlers = new HandlerList(); private final EntityType effectiveType; - public SpawnEntityEvent(Event originalEvent, List> causes, Entity target) { - super(originalEvent, causes, checkNotNull(target)); + public SpawnEntityEvent(Event originalEvent, Cause cause, Entity target) { + super(originalEvent, cause, checkNotNull(target)); this.effectiveType = target.getType(); } - public SpawnEntityEvent(Event originalEvent, List> causes, Location location, EntityType type) { - super(originalEvent, causes, location); + public SpawnEntityEvent(Event originalEvent, Cause cause, Location location, EntityType type) { + super(originalEvent, cause, location); checkNotNull(type); this.effectiveType = type; } diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/UseEntityEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/UseEntityEvent.java index 08933202..66b34282 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/entity/UseEntityEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/entity/UseEntityEvent.java @@ -19,13 +19,12 @@ package com.sk89q.worldguard.bukkit.event.entity; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import org.bukkit.entity.Entity; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import javax.annotation.Nonnull; -import java.util.List; import static com.google.common.base.Preconditions.checkNotNull; @@ -36,15 +35,8 @@ public class UseEntityEvent extends AbstractEntityEvent { private static final HandlerList handlers = new HandlerList(); - /** - * Create a new instance. - * - * @param originalEvent the original event - * @param causes a list of causes, where the originating causes are at the beginning - * @param target the target entity being affected - */ - public UseEntityEvent(Event originalEvent, List> causes, Entity target) { - super(originalEvent, causes, checkNotNull(target)); + public UseEntityEvent(Event originalEvent, Cause cause, Entity target) { + super(originalEvent, cause, checkNotNull(target)); } @Override diff --git a/src/main/java/com/sk89q/worldguard/bukkit/event/inventory/UseItemEvent.java b/src/main/java/com/sk89q/worldguard/bukkit/event/inventory/UseItemEvent.java index 2d35d804..acd0185e 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/event/inventory/UseItemEvent.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/event/inventory/UseItemEvent.java @@ -19,15 +19,13 @@ package com.sk89q.worldguard.bukkit.event.inventory; -import com.sk89q.worldguard.util.cause.Cause; +import com.sk89q.worldguard.bukkit.cause.Cause; import com.sk89q.worldguard.bukkit.event.AbstractInteractEvent; import org.bukkit.World; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import org.bukkit.inventory.ItemStack; -import java.util.List; - import static com.google.common.base.Preconditions.checkNotNull; /** @@ -39,16 +37,8 @@ public class UseItemEvent extends AbstractInteractEvent { private final World world; private final ItemStack itemStack; - /** - * Create a new instance. - * - * @param originalEvent the original event - * @param causes a list of causes, where the originating causes are at the beginning - * @param world the world - * @param itemStack the item - */ - public UseItemEvent(Event originalEvent, List> causes, World world, ItemStack itemStack) { - super(originalEvent, causes); + public UseItemEvent(Event originalEvent, Cause cause, World world, ItemStack itemStack) { + super(originalEvent, cause); checkNotNull(world); checkNotNull(itemStack); this.world = world; diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/BlacklistListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/BlacklistListener.java index 7399d3ea..549981fe 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/BlacklistListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/BlacklistListener.java @@ -31,14 +31,13 @@ import com.sk89q.worldguard.bukkit.ConfigurationManager; import com.sk89q.worldguard.bukkit.WorldConfiguration; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.bukkit.util.Materials; -import com.sk89q.worldguard.util.cause.Causes; import com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent; import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent; import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent; import com.sk89q.worldguard.bukkit.event.entity.DestroyEntityEvent; import com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent; import com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent; +import com.sk89q.worldguard.bukkit.util.Materials; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Entity; @@ -70,7 +69,7 @@ public BlacklistListener(WorldGuardPlugin plugin) { @EventHandler(ignoreCancelled = true) public void onBreakBlock(BreakBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); if (player == null) { return; @@ -96,7 +95,7 @@ public void onBreakBlock(BreakBlockEvent event) { @EventHandler(ignoreCancelled = true) public void onPlaceBlock(PlaceBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); if (player == null) { return; @@ -119,7 +118,7 @@ localPlayer, toVector(event.getTarget()), createTarget(target, event.getEffectiv @EventHandler(ignoreCancelled = true) public void onUseBlock(UseBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); if (player == null) { return; @@ -142,7 +141,7 @@ localPlayer, toVector(event.getTarget()), createTarget(target, event.getEffectiv @EventHandler(ignoreCancelled = true) public void onSpawnEntity(SpawnEntityEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); if (player == null) { return; @@ -166,7 +165,7 @@ public void onSpawnEntity(SpawnEntityEvent event) { @EventHandler(ignoreCancelled = true) public void onDestroyEntity(DestroyEntityEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); if (player == null) { return; @@ -192,7 +191,7 @@ public void onDestroyEntity(DestroyEntityEvent event) { @EventHandler(ignoreCancelled = true) public void onUseItem(UseItemEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); if (player == null) { return; diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/BlockedPotionsListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/BlockedPotionsListener.java index a02a979c..96c32236 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/BlockedPotionsListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/BlockedPotionsListener.java @@ -23,7 +23,6 @@ import com.sk89q.worldguard.bukkit.ConfigurationManager; import com.sk89q.worldguard.bukkit.WorldConfiguration; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.util.cause.Causes; import com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -51,7 +50,7 @@ public BlockedPotionsListener(WorldGuardPlugin plugin) { @EventHandler public void onItemInteract(UseItemEvent event) { // We only care about player caused events - if (!Causes.mayInvolvePlayer(event.getCauses())) { + if (event.getCause().getPlayerRootCause() == null) { return; } @@ -84,7 +83,7 @@ public void onItemInteract(UseItemEvent event) { } if (blockedEffect != null) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); if (player != null) { if (getPlugin().hasPermission(player, "worldguard.override.potions")) { diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/ChestProtectionListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/ChestProtectionListener.java index fc91edbb..9f8536c0 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/ChestProtectionListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/ChestProtectionListener.java @@ -22,7 +22,6 @@ import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldguard.bukkit.WorldConfiguration; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.util.cause.Causes; import com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent; import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent; import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent; @@ -48,7 +47,7 @@ public ChestProtectionListener(WorldGuardPlugin plugin) { @EventHandler(ignoreCancelled = true) public void onPlaceBlock(PlaceBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); if (player != null) { @@ -68,7 +67,7 @@ public void onPlaceBlock(PlaceBlockEvent event) { @EventHandler(ignoreCancelled = true) public void onBreakBlock(BreakBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); WorldConfiguration wcfg = getWorldConfig(target.getWorld()); @@ -93,7 +92,7 @@ public void onBreakBlock(BreakBlockEvent event) { @EventHandler(ignoreCancelled = true) public void onUseBlock(UseBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); WorldConfiguration wcfg = getWorldConfig(target.getWorld()); diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/DebuggingListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/DebuggingListener.java index 83b1ecaa..4943be13 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/DebuggingListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/DebuggingListener.java @@ -19,7 +19,6 @@ package com.sk89q.worldguard.bukkit.listener; -import com.google.common.base.Joiner; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent; import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent; @@ -28,11 +27,9 @@ import com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent; import com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent; import com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent; -import com.sk89q.worldguard.util.cause.Cause; import org.bukkit.Location; import org.bukkit.event.EventHandler; -import java.util.List; import java.util.logging.Logger; import static com.google.common.base.Preconditions.checkNotNull; @@ -62,7 +59,7 @@ public void onPlaceBlock(PlaceBlockEvent event) { builder.append(" "); builder.append("@").append(toBlockString(event.getTarget())); builder.append(" "); - builder.append("[").append(toCause(event.getCauses())).append("]"); + builder.append("[").append(event.getCause()).append("]"); builder.append(" "); builder.append(":").append(event.getOriginalEvent().getEventName()); logger.info(builder.toString()); @@ -75,7 +72,7 @@ public void onBreakBlock(BreakBlockEvent event) { builder.append(" "); builder.append("").append(event.getEffectiveMaterial()); builder.append(" "); - builder.append("[").append(toCause(event.getCauses())).append("]"); + builder.append("[").append(event.getCause()).append("]"); builder.append(" "); builder.append("@").append(toBlockString(event.getTarget())); builder.append(" "); @@ -90,7 +87,7 @@ public void onUseBlock(UseBlockEvent event) { builder.append(" "); builder.append("").append(event.getEffectiveMaterial()); builder.append(" "); - builder.append("[").append(toCause(event.getCauses())).append("]"); + builder.append("[").append(event.getCause()).append("]"); builder.append(" "); builder.append("@").append(toBlockString(event.getTarget())); builder.append(" "); @@ -105,7 +102,7 @@ public void onSpawnEntity(SpawnEntityEvent event) { builder.append(" "); builder.append("").append(event.getEffectiveType()); builder.append(" "); - builder.append("[").append(toCause(event.getCauses())).append("]"); + builder.append("[").append(event.getCause()).append("]"); builder.append(" "); builder.append("@").append(toBlockString(event.getTarget())); builder.append(" "); @@ -120,7 +117,7 @@ public void onDestroyEntity(DestroyEntityEvent event) { builder.append(" "); builder.append("").append(event.getEntity().getType()); builder.append(" "); - builder.append("[").append(toCause(event.getCauses())).append("]"); + builder.append("[").append(event.getCause()).append("]"); builder.append(" "); builder.append("@").append(toBlockString(event.getTarget())); builder.append(" "); @@ -135,7 +132,7 @@ public void onUseEntity(UseEntityEvent event) { builder.append(" "); builder.append("").append(event.getEntity().getType()); builder.append(" "); - builder.append("[").append(toCause(event.getCauses())).append("]"); + builder.append("[").append(event.getCause()).append("]"); builder.append(" "); builder.append("@").append(toBlockString(event.getTarget())); builder.append(" "); @@ -150,18 +147,14 @@ public void onUseItem(UseItemEvent event) { builder.append(" "); builder.append("").append(event.getItemStack().getType()); builder.append(" "); - builder.append("[").append(toCause(event.getCauses())).append("]"); + builder.append("[").append(event.getCause()).append("]"); builder.append(" "); builder.append("@").append(event.getWorld().getName()); builder.append(" "); builder.append(":").append(event.getOriginalEvent().getEventName()); logger.info(builder.toString()); } - - private static String toCause(List> causes) { - return Joiner.on("|").join(causes); - } - + private static String toBlockString(Location location) { return location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ(); } diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java index d6d0c757..c4e83da4 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java @@ -20,6 +20,7 @@ package com.sk89q.worldguard.bukkit.listener; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.bukkit.cause.Cause; import com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent; import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent; import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent; @@ -31,8 +32,6 @@ import com.sk89q.worldguard.bukkit.util.Events; import com.sk89q.worldguard.bukkit.util.Materials; import com.sk89q.worldguard.bukkit.util.WGMetadata; -import com.sk89q.worldguard.util.cause.Cause; -import com.sk89q.worldguard.util.cause.Causes; import org.bukkit.DyeColor; import org.bukkit.Material; import org.bukkit.World; @@ -90,12 +89,10 @@ import org.bukkit.material.MaterialData; import javax.annotation.Nullable; -import java.util.Collections; -import java.util.List; +import static com.sk89q.worldguard.bukkit.cause.Cause.create; import static com.sk89q.worldguard.bukkit.util.Materials.isBlockModifiedOnClick; import static com.sk89q.worldguard.bukkit.util.Materials.isItemAppliedToBlock; -import static com.sk89q.worldguard.util.cause.Causes.create; public class EventAbstractionListener implements Listener { @@ -139,7 +136,7 @@ public void onBlockPlace(BlockPlaceEvent event) { @EventHandler public void onBlockBurn(BlockBurnEvent event) { - Events.fireToCancel(event, new UseBlockEvent(event, Collections.>emptyList(), event.getBlock())); + Events.fireToCancel(event, new UseBlockEvent(event, Cause.unknown(), event.getBlock())); } // TODO: Handle EntityCreatePortalEvent? @@ -166,17 +163,17 @@ public void onEntityChangeBlock(EntityChangeBlockEvent event) { Events.fireToCancel(event, new BreakBlockEvent(event, create(entity), event.getBlock())); } } else { - List> causes; + Cause cause; // Return the source for falling blocks if (entity instanceof FallingBlock) { Block source = WGMetadata.getIfPresent(entity, FALLING_SOURCE_KEY, Block.class); - causes = create(source, entity); + cause = create(source, entity); } else { - causes = create(entity); + cause = create(entity); } - Events.fireToCancel(event, new PlaceBlockEvent(event, causes, event.getBlock().getLocation(), to)); + Events.fireToCancel(event, new PlaceBlockEvent(event, cause, event.getBlock().getLocation(), to)); } } } @@ -205,11 +202,11 @@ public void onPlayerInteract(PlayerInteractEvent event) { @Nullable ItemStack item = player.getItemInHand(); Block clicked = event.getClickedBlock(); Block placed; - List> causes = create(player); + Cause cause = create(player); switch (event.getAction()) { case PHYSICAL: - if (Events.fireAndTestCancel(new UseBlockEvent(event, causes, clicked))) { + if (Events.fireAndTestCancel(new UseBlockEvent(event, cause, clicked))) { event.setUseInteractedBlock(Result.DENY); event.setCancelled(true); } @@ -228,7 +225,7 @@ public void onPlayerInteract(PlayerInteractEvent event) { if (!player.isSneaking() || event.getAction() == Action.LEFT_CLICK_BLOCK) { // Only fire events for blocks that are modified when right clicked if (isBlockModifiedOnClick(clicked.getType()) || (item != null && isItemAppliedToBlock(item.getType(), clicked.getType()))) { - if (Events.fireAndTestCancel(new UseBlockEvent(event, causes, clicked))) { + if (Events.fireAndTestCancel(new UseBlockEvent(event, cause, clicked))) { event.setUseInteractedBlock(Result.DENY); } @@ -260,7 +257,7 @@ public void onPlayerInteract(PlayerInteractEvent event) { case LEFT_CLICK_AIR: case RIGHT_CLICK_AIR: - if (item != null && !item.getType().isBlock() && Events.fireAndTestCancel(new UseItemEvent(event, causes, player.getWorld(), item))) { + if (item != null && !item.getType().isBlock() && Events.fireAndTestCancel(new UseItemEvent(event, cause, player.getWorld(), item))) { event.setUseItemInHand(Result.DENY); } @@ -276,27 +273,27 @@ public void onEntityInteract(org.bukkit.event.entity.EntityInteractEvent event) @EventHandler public void onBlockIgnite(BlockIgniteEvent event) { Block block = event.getBlock(); - List> causes; + Cause cause; // Find the cause if (event.getPlayer() != null) { - causes = create(event.getPlayer()); + cause = create(event.getPlayer()); } else if (event.getIgnitingEntity() != null) { - causes = create(event.getIgnitingEntity()); + cause = create(event.getIgnitingEntity()); } else if (event.getIgnitingBlock() != null) { - causes = create(event.getIgnitingBlock()); + cause = create(event.getIgnitingBlock()); } else { - causes = Collections.emptyList(); + cause = Cause.unknown(); } if (block.getType() != Material.AIR) { - Events.fireToCancel(event, new BreakBlockEvent(event, causes, event.getBlock())); + Events.fireToCancel(event, new BreakBlockEvent(event, cause, event.getBlock())); } // This is also handled in the PlayerInteractEvent listener if (event.getCause() == IgniteCause.FLINT_AND_STEEL || event.getCause() == IgniteCause.FIREBALL) { // TODO: Test location of block - Events.fireToCancel(event, new PlaceBlockEvent(event, causes, event.getBlock().getLocation(), Material.FIRE)); + Events.fireToCancel(event, new PlaceBlockEvent(event, cause, event.getBlock().getLocation(), Material.FIRE)); } } @@ -354,13 +351,13 @@ public void onBlockFromTo(BlockFromToEvent event) { return; } - List> causes = create(from); + Cause cause = create(from); if (from.getType() != Material.AIR) { - Events.fireToCancel(event, new BreakBlockEvent(event, causes, to)); + Events.fireToCancel(event, new BreakBlockEvent(event, cause, to)); } - Events.fireToCancel(event, new PlaceBlockEvent(event, causes, to.getLocation(), from.getType())); + Events.fireToCancel(event, new PlaceBlockEvent(event, cause, to.getLocation(), from.getType())); } } @@ -370,7 +367,7 @@ public void onBlockFromTo(BlockFromToEvent event) { @EventHandler public void onCreatureSpawn(CreatureSpawnEvent event) { - Events.fireToCancel(event, new SpawnEntityEvent(event, Collections.>emptyList(), event.getEntity())); + Events.fireToCancel(event, new SpawnEntityEvent(event, Cause.unknown(), event.getEntity())); } @EventHandler @@ -383,7 +380,7 @@ public void onHangingBreak(HangingBreakEvent event) { if (event instanceof HangingBreakByEntityEvent) { Events.fireToCancel(event, new DestroyEntityEvent(event, create(((HangingBreakByEntityEvent) event).getRemover()), event.getEntity())); } else { - Events.fireToCancel(event, new DestroyEntityEvent(event, Collections.>emptyList(), event.getEntity())); + Events.fireToCancel(event, new DestroyEntityEvent(event, Cause.unknown(), event.getEntity())); } } @@ -431,7 +428,7 @@ public void onEntityDamage(EntityDamageEvent event) { } } else { - Events.fireToCancel(event, new UseEntityEvent(event, Collections.>emptyList(), event.getEntity())); + Events.fireToCancel(event, new UseEntityEvent(event, Cause.unknown(), event.getEntity())); } } @@ -444,7 +441,7 @@ public void onEntityCombust(EntityCombustEvent event) { Events.fireToCancel(event, new UseEntityEvent(event, create(((EntityCombustByEntityEvent) event).getCombuster()), event.getEntity())); } else { - Events.fireToCancel(event, new UseEntityEvent(event, Collections.>emptyList(), event.getEntity())); + Events.fireToCancel(event, new UseEntityEvent(event, Cause.unknown(), event.getEntity())); } } @@ -497,17 +494,17 @@ public void onPotionSplash(PotionSplashEvent event) { Entity entity = event.getEntity(); ThrownPotion potion = event.getPotion(); World world = entity.getWorld(); - List> causes = Causes.create(potion.getShooter()); + Cause cause = create(potion.getShooter()); // Fire item interaction event - Events.fireToCancel(event, new UseItemEvent(event, causes, world, potion.getItem())); + Events.fireToCancel(event, new UseItemEvent(event, cause, world, potion.getItem())); // Fire entity interaction event if (!event.isCancelled()) { int blocked = 0; for (LivingEntity affected : event.getAffectedEntities()) { - if (Events.fireAndTestCancel(new UseEntityEvent(event, causes, affected))) { + if (Events.fireAndTestCancel(new UseEntityEvent(event, cause, affected))) { event.setIntensity(affected, 0); blocked++; } @@ -521,19 +518,19 @@ public void onPotionSplash(PotionSplashEvent event) { @EventHandler public void onBlockDispense(BlockDispenseEvent event) { - List> causes = create(event.getBlock()); + Cause cause = create(event.getBlock()); Block dispenserBlock = event.getBlock(); ItemStack item = event.getItem(); MaterialData materialData = dispenserBlock.getState().getData(); - Events.fireToCancel(event, new UseItemEvent(event, causes, dispenserBlock.getWorld(), item)); + Events.fireToCancel(event, new UseItemEvent(event, cause, dispenserBlock.getWorld(), item)); // Simulate right click event as players have it if (materialData instanceof Dispenser) { Dispenser dispenser = (Dispenser) materialData; Block placed = dispenserBlock.getRelative(dispenser.getFacing()); Block clicked = placed.getRelative(dispenser.getFacing()); - handleBlockRightClick(event, causes, item, clicked, dispenser.getFacing().getOppositeFace(), placed); + handleBlockRightClick(event, cause, item, clicked, dispenser.getFacing().getOppositeFace(), placed); } } @@ -541,38 +538,38 @@ public void onBlockDispense(BlockDispenseEvent event) { * Handle the right click of a block while an item is held. * * @param event the original event - * @param causes the list of causes + * @param cause the list of cause * @param item the item * @param clicked the clicked block * @param faceClicked the face of the clicked block * @param placed the placed block * @param the event type */ - private static void handleBlockRightClick(T event, List> causes, @Nullable ItemStack item, Block clicked, BlockFace faceClicked, Block placed) { + private static void handleBlockRightClick(T event, Cause cause, @Nullable ItemStack item, Block clicked, BlockFace faceClicked, Block placed) { if (item != null && item.getType() == Material.TNT) { // Workaround for a bug that allowed TNT to trigger instantly if placed // next to redstone, without plugins getting the clicked place event // (not sure if this actually still happens) - Events.fireToCancel(event, new UseBlockEvent(event, causes, clicked.getLocation(), Material.TNT)); + Events.fireToCancel(event, new UseBlockEvent(event, cause, clicked.getLocation(), Material.TNT)); } // Handle created Minecarts if (item != null && Materials.isMinecart(item.getType())) { // TODO: Give a more specific Minecart type - Events.fireToCancel(event, new SpawnEntityEvent(event, causes, placed.getLocation().add(0.5, 0, 0.5), EntityType.MINECART)); + Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), EntityType.MINECART)); } // Handle cocoa beans if (item != null && item.getType() == Material.INK_SACK && Materials.isDyeColor(item.getData(), DyeColor.BROWN)) { // CraftBukkit doesn't or didn't throw a clicked place for this if (!(faceClicked == BlockFace.DOWN || faceClicked == BlockFace.UP)) { - Events.fireToCancel(event, new PlaceBlockEvent(event, causes, placed.getLocation(), Material.COCOA)); + Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), Material.COCOA)); } } // Workaround for http://leaky.bukkit.org/issues/1034 if (item != null && item.getType() == Material.TNT) { - Events.fireToCancel(event, new PlaceBlockEvent(event, causes, placed.getLocation(), Material.TNT)); + Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), Material.TNT)); } } diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java index 4c9c1a4c..ee6bc082 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java @@ -20,16 +20,15 @@ package com.sk89q.worldguard.bukkit.listener; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.bukkit.util.Entities; -import com.sk89q.worldguard.bukkit.util.Materials; -import com.sk89q.worldguard.bukkit.util.RegionQuery; -import com.sk89q.worldguard.util.cause.Causes; import com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent; import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent; import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent; import com.sk89q.worldguard.bukkit.event.entity.DestroyEntityEvent; import com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent; import com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent; +import com.sk89q.worldguard.bukkit.util.Entities; +import com.sk89q.worldguard.bukkit.util.Materials; +import com.sk89q.worldguard.bukkit.util.RegionQuery; import com.sk89q.worldguard.protection.flags.DefaultFlag; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -59,7 +58,7 @@ private void tellErrorMessage(CommandSender sender, Object subject) { @EventHandler(ignoreCancelled = true) public void onPlaceBlock(PlaceBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); Material type = event.getEffectiveMaterial(); @@ -84,7 +83,7 @@ public void onPlaceBlock(PlaceBlockEvent event) { @EventHandler(ignoreCancelled = true) public void onBreakBlock(BreakBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); if (player != null) { @@ -100,7 +99,7 @@ public void onBreakBlock(BreakBlockEvent event) { @EventHandler(ignoreCancelled = true) public void onUseBlock(UseBlockEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); Material type = event.getEffectiveMaterial(); @@ -140,7 +139,7 @@ public void onUseBlock(UseBlockEvent event) { @EventHandler(ignoreCancelled = true) public void onSpawnEntity(SpawnEntityEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); EntityType type = event.getEffectiveType(); @@ -163,7 +162,7 @@ public void onSpawnEntity(SpawnEntityEvent event) { @EventHandler(ignoreCancelled = true) public void onDestroyEntity(DestroyEntityEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); EntityType type = event.getEntity().getType(); @@ -186,7 +185,7 @@ public void onDestroyEntity(DestroyEntityEvent event) { @EventHandler(ignoreCancelled = true) public void onUseEntity(UseEntityEvent event) { - Player player = Causes.getInvolvedPlayer(event.getCauses()); + Player player = event.getCause().getPlayerRootCause(); Location target = event.getTarget(); if (player != null) { diff --git a/src/main/java/com/sk89q/worldguard/util/cause/BlockCause.java b/src/main/java/com/sk89q/worldguard/util/cause/BlockCause.java deleted file mode 100644 index e1c7998d..00000000 --- a/src/main/java/com/sk89q/worldguard/util/cause/BlockCause.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * WorldGuard, a suite of tools for Minecraft - * Copyright (C) sk89q - * 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 . - */ - -package com.sk89q.worldguard.util.cause; - -import org.bukkit.block.Block; - -/** - * A cause that is a block. - */ -import static com.google.common.base.Preconditions.checkNotNull; - -public class BlockCause implements Cause { - - private final Block block; - - /** - * Create a new instance. - * - * @param block the block - */ - public BlockCause(Block block) { - checkNotNull(block); - this.block = block; - } - - @Override - public Block get() { - return block; - } - - @Override - public String toString() { - return block.toString(); - } - -} diff --git a/src/main/java/com/sk89q/worldguard/util/cause/Cause.java b/src/main/java/com/sk89q/worldguard/util/cause/Cause.java deleted file mode 100644 index 65a0a4b4..00000000 --- a/src/main/java/com/sk89q/worldguard/util/cause/Cause.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * WorldGuard, a suite of tools for Minecraft - * Copyright (C) sk89q - * 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 . - */ - -package com.sk89q.worldguard.util.cause; - -/** - * Represents a possible cause of an event. - * - *

Example causes include players, blocks, entities, and many more.

- * - * @param the wrapped object type - */ -public interface Cause { - - /** - * Get the underlying object. - * - * @return the underlying object - */ - T get(); - -} diff --git a/src/main/java/com/sk89q/worldguard/util/cause/Causes.java b/src/main/java/com/sk89q/worldguard/util/cause/Causes.java deleted file mode 100644 index 4f171369..00000000 --- a/src/main/java/com/sk89q/worldguard/util/cause/Causes.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * WorldGuard, a suite of tools for Minecraft - * Copyright (C) sk89q - * 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 . - */ - -package com.sk89q.worldguard.util.cause; - -import org.bukkit.block.Block; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; - -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> causes) { - for (Cause cause : causes) { - if (cause instanceof PlayerCause) { - return ((PlayerCause) cause).get(); - } - } - - return null; - } - - /** - * Test whether the list of causes may indicate that a player was part of - * the cause, directly or indirectly. - * - *

An indirect cause would be a dispenser, for example.

- * - * @param causes a list of cuases - * @return true if the event involved a player - */ - public static boolean mayInvolvePlayer(List> causes) { - for (Cause cause : causes) { - if (cause instanceof PlayerCause || cause instanceof BlockCause) { - return true; // This needs to be made smarter later - } - } - - return false; - } - - /** - * 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> create(Object ... cause) { - List> causes = new ArrayList>(cause.length); - for (Object o : cause) { - if (o == null) { - continue; - } - - if (o instanceof Player) { - causes.add(new PlayerCause((Player) o)); - } else if (o instanceof Block) { - causes.add(new BlockCause((Block) o)); - } else if (o instanceof Projectile) { - causes.addAll(create(((Projectile) o).getShooter())); - causes.add(new EntityCause((Entity) o)); - } else if (o instanceof Entity) { - causes.add(new EntityCause((Entity) o)); - } else { - causes.add(new UnknownCause(o)); - } - } - return causes; - } - -} diff --git a/src/main/java/com/sk89q/worldguard/util/cause/EntityCause.java b/src/main/java/com/sk89q/worldguard/util/cause/EntityCause.java deleted file mode 100644 index 09e962dc..00000000 --- a/src/main/java/com/sk89q/worldguard/util/cause/EntityCause.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * WorldGuard, a suite of tools for Minecraft - * Copyright (C) sk89q - * 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 . - */ - -package com.sk89q.worldguard.util.cause; - -import org.bukkit.entity.Entity; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * A cause that is an entity. - */ -public class EntityCause implements Cause { - - private final Entity entity; - - /** - * Create a new instance. - * - * @param entity the entity - */ - public EntityCause(Entity entity) { - checkNotNull(entity); - this.entity = entity; - } - - @Override - public Entity get() { - return entity; - } - - @Override - public String toString() { - return entity.toString(); - } -} diff --git a/src/main/java/com/sk89q/worldguard/util/cause/PlayerCause.java b/src/main/java/com/sk89q/worldguard/util/cause/PlayerCause.java deleted file mode 100644 index 03a473b3..00000000 --- a/src/main/java/com/sk89q/worldguard/util/cause/PlayerCause.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * WorldGuard, a suite of tools for Minecraft - * Copyright (C) sk89q - * 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 . - */ - -package com.sk89q.worldguard.util.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 { - - 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; - } - - @Override - public String toString() { - return player.getName(); - } - -} diff --git a/src/main/java/com/sk89q/worldguard/util/cause/UnknownCause.java b/src/main/java/com/sk89q/worldguard/util/cause/UnknownCause.java deleted file mode 100644 index 40cfcfc6..00000000 --- a/src/main/java/com/sk89q/worldguard/util/cause/UnknownCause.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * WorldGuard, a suite of tools for Minecraft - * Copyright (C) sk89q - * 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 . - */ - -package com.sk89q.worldguard.util.cause; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * A cause that is not known. - */ -public class UnknownCause implements Cause { - - 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; - } - - @Override - public String toString() { - return "unknown(" + cause + ")"; - } -}