mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2024-11-23 19:16:40 +01:00
Simplfy Cause objects.
This commit is contained in:
parent
0fc3d21b90
commit
f92e55e061
@ -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();
|
||||
|
||||
|
132
src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java
Normal file
132
src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java
Normal file
@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.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.
|
||||
*
|
||||
* <p>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.</p>
|
||||
*/
|
||||
public class Cause {
|
||||
|
||||
private static final Cause UNKNOWN = new Cause(Collections.emptyList());
|
||||
|
||||
private final List<Object> causes;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param causes a list of causes
|
||||
*/
|
||||
private Cause(List<Object> 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<Object> 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<Object> causes = new ArrayList<Object>(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;
|
||||
}
|
||||
|
||||
}
|
@ -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<? extends Cause<?>> 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<? extends Cause<?>> 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<? extends Cause<?>> getCauses() {
|
||||
return Collections.unmodifiableList(causes);
|
||||
public Cause getCause() {
|
||||
return cause;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<? extends Cause<?>> 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<? extends Cause<?>> 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;
|
||||
|
@ -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<? extends Cause<?>> causes, Block block) {
|
||||
super(originalEvent, causes, block);
|
||||
public BreakBlockEvent(Event originalEvent, Cause cause, Block block) {
|
||||
super(originalEvent, cause, block);
|
||||
}
|
||||
|
||||
public BreakBlockEvent(Event originalEvent, List<? extends Cause<?>> 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
|
||||
|
@ -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<? extends Cause<?>> causes, Block block) {
|
||||
super(originalEvent, causes, block);
|
||||
public PlaceBlockEvent(Event originalEvent, Cause cause, Block block) {
|
||||
super(originalEvent, cause, block);
|
||||
}
|
||||
|
||||
public PlaceBlockEvent(Event originalEvent, List<? extends Cause<?>> 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
|
||||
|
@ -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<? extends Cause<?>> causes, Block block) {
|
||||
super(originalEvent, causes, block);
|
||||
public UseBlockEvent(Event originalEvent, Cause cause, Block block) {
|
||||
super(originalEvent, cause, block);
|
||||
}
|
||||
|
||||
public UseBlockEvent(Event originalEvent, List<? extends Cause<?>> 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
|
||||
|
@ -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<? extends Cause<?>> 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<? extends Cause<?>> 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;
|
||||
|
@ -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<? extends Cause<?>> causes, Entity target) {
|
||||
super(originalEvent, causes, checkNotNull(target));
|
||||
public DestroyEntityEvent(Event originalEvent, Cause cause, Entity target) {
|
||||
super(originalEvent, cause, checkNotNull(target));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<? extends Cause<?>> 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<? extends Cause<?>> 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;
|
||||
}
|
||||
|
@ -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<? extends Cause<?>> causes, Entity target) {
|
||||
super(originalEvent, causes, checkNotNull(target));
|
||||
public UseEntityEvent(Event originalEvent, Cause cause, Entity target) {
|
||||
super(originalEvent, cause, checkNotNull(target));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<? extends Cause<?>> 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;
|
||||
|
@ -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;
|
||||
|
@ -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")) {
|
||||
|
@ -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());
|
||||
|
@ -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<? extends Cause<?>> causes) {
|
||||
return Joiner.on("|").join(causes);
|
||||
}
|
||||
|
||||
|
||||
private static String toBlockString(Location location) {
|
||||
return location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ();
|
||||
}
|
||||
|
@ -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.<Cause<?>>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<? extends Cause<?>> 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<? extends Cause<?>> 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<? extends Cause<?>> 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<? extends Cause<?>> 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.<Cause<?>>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.<Cause<?>>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.<Cause<?>>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.<Cause<?>>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<? extends Cause<?>> 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<? extends Cause<?>> 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 <T> the event type
|
||||
*/
|
||||
private static <T extends Event & Cancellable> void handleBlockRightClick(T event, List<? extends Cause<?>> causes, @Nullable ItemStack item, Block clicked, BlockFace faceClicked, Block placed) {
|
||||
private static <T extends Event & Cancellable> 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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.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<Block> {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* 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.util.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();
|
||||
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
/*
|
||||
* 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.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<? extends Cause<?>> 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.
|
||||
*
|
||||
* <p>An indirect cause would be a dispenser, for example.</p>
|
||||
*
|
||||
* @param causes a list of cuases
|
||||
* @return true if the event involved a player
|
||||
*/
|
||||
public static boolean mayInvolvePlayer(List<? extends Cause<?>> 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<? extends Cause<?>> create(Object ... cause) {
|
||||
List<Cause<?>> causes = new ArrayList<Cause<?>>(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;
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
/*
|
||||
* 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.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<Entity> {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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.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<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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return player.getName();
|
||||
}
|
||||
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
/*
|
||||
* 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.util.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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "unknown(" + cause + ")";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user