Made declaring the player mandatory when calling FlagListener#checkIsland(...) (#543)

* WIP - See FlagListener.java for changes

Makes declaring the player mandatory for checkIsland method so it cannot
be forgotten and will be fresh every check.

See BlockInteractionListener for how it will look.

* Update src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java

Co-Authored-By: tastybento <tastybento@users.noreply.github.com>

* Update src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java

Co-Authored-By: tastybento <tastybento@users.noreply.github.com>

* Adjusted all the flags to new API
This commit is contained in:
tastybento 2019-02-12 22:55:36 -08:00 committed by Florian CUNY
parent 3ca0c440e4
commit f89419f3d8
23 changed files with 133 additions and 198 deletions

View File

@ -1,6 +1,5 @@
package world.bentobox.bentobox.api.flags; package world.bentobox.bentobox.api.flags;
import java.lang.reflect.Method;
import java.util.Optional; import java.util.Optional;
import org.bukkit.Location; import org.bukkit.Location;
@ -69,37 +68,6 @@ public abstract class FlagListener implements Listener {
this.plugin = plugin; this.plugin = plugin;
} }
/**
* Sets the player associated with this event.
* If the user is a fake player, they are not counted.
* @param e - event
* @return true if found, otherwise false
*/
private boolean createEventUser(@NonNull Event e) {
try {
// Use reflection to get the getPlayer method if it exists
Method getPlayer = e.getClass().getMethod("getPlayer");
if (getPlayer != null) {
setUser(User.getInstance((Player)getPlayer.invoke(e)));
return true;
}
} catch (Exception e1) { // Do nothing
}
return false;
}
/**
* Explicitly set the user for the next {@link #checkIsland(Event, Location, Flag)} or {@link #checkIsland(Event, Location, Flag, boolean)}
* @param user - the User
*/
@NonNull
public FlagListener setUser(@NonNull User user) {
if (!plugin.getSettings().getFakePlayers().contains(user.getName())) {
this.user = user;
}
return this;
}
/* /*
* The following methods cover the cancellable events and enable a simple noGo(e) to be used to cancel and send the error message * The following methods cover the cancellable events and enable a simple noGo(e) to be used to cancel and send the error message
*/ */
@ -132,25 +100,29 @@ public abstract class FlagListener implements Listener {
} }
/** /**
* Check if flag is allowed at location * Check if flag is allowed at location. Uses player object because Bukkit events provide player.
* @param e - event * @param e - event
* @param player - player affected by this flag, or null if none
* @param loc - location * @param loc - location
* @param flag - flag {@link world.bentobox.bentobox.lists.Flags} * @param flag - flag {@link world.bentobox.bentobox.lists.Flags}
* @return true if allowed, false if not * @return true if allowed, false if not
*/ */
public boolean checkIsland(@NonNull Event e, @NonNull Location loc, @NonNull Flag flag) { public boolean checkIsland(@NonNull Event e, @Nullable Player player, @NonNull Location loc, @NonNull Flag flag) {
return checkIsland(e, loc, flag, false); return checkIsland(e, player, loc, flag, false);
} }
/** /**
* Check if flag is allowed at location * Check if flag is allowed at location
* @param e - event * @param e - event
* @param player - player affected by this flag, or null if none
* @param loc - location * @param loc - location
* @param flag - flag {@link world.bentobox.bentobox.lists.Flags} * @param flag - flag {@link world.bentobox.bentobox.lists.Flags}
* @param silent - if true, no attempt is made to tell the user * @param silent - if true, no attempt is made to tell the user
* @return true if the check is okay, false if it was disallowed * @return true if the check is okay, false if it was disallowed
*/ */
public boolean checkIsland(@NonNull Event e, @NonNull Location loc, @NonNull Flag flag, boolean silent) { public boolean checkIsland(@NonNull Event e, @Nullable Player player, @NonNull Location loc, @NonNull Flag flag, boolean silent) {
// Set user
user = User.getInstance(player);
// If this is not an Island World or a standard Nether or End, skip // If this is not an Island World or a standard Nether or End, skip
if (!plugin.getIWM().inWorld(loc)) { if (!plugin.getIWM().inWorld(loc)) {
report(user, e, loc, flag, Why.UNPROTECTED_WORLD); report(user, e, loc, flag, Why.UNPROTECTED_WORLD);
@ -171,15 +143,6 @@ public abstract class FlagListener implements Listener {
} }
// Protection flag // Protection flag
// If the user is not set already, try to get it from the event
// Set the user associated with this event
// The user is not set, and the event does not hold a getPlayer, so return false
// TODO: is this the correct handling here?
if (user == null && !createEventUser(e)) {
plugin.logError("Check island had no associated user! " + e.getEventName());
report(user, e, loc, flag, Why.ERROR_NO_ASSOCIATED_USER);
return false;
}
// Ops or "bypass everywhere" moderators can do anything // Ops or "bypass everywhere" moderators can do anything
if (user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + ".mod.bypass." + flag.getID() + ".everywhere")) { if (user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + ".mod.bypass." + flag.getID() + ".everywhere")) {
@ -188,7 +151,6 @@ public abstract class FlagListener implements Listener {
} else { } else {
report(user, e, loc, flag, Why.BYPASS_EVERYWHERE); report(user, e, loc, flag, Why.BYPASS_EVERYWHERE);
} }
user = null;
return true; return true;
} }
@ -196,13 +158,11 @@ public abstract class FlagListener implements Listener {
if (flag.getType().equals(Flag.Type.WORLD_SETTING)) { if (flag.getType().equals(Flag.Type.WORLD_SETTING)) {
if (flag.isSetForWorld(loc.getWorld())) { if (flag.isSetForWorld(loc.getWorld())) {
report(user, e, loc, flag, Why.ALLOWED_IN_WORLD); report(user, e, loc, flag, Why.ALLOWED_IN_WORLD);
user = null;
return true; return true;
} }
report(user, e, loc, flag, Why.NOT_ALLOWED_IN_WORLD); report(user, e, loc, flag, Why.NOT_ALLOWED_IN_WORLD);
noGo(e, flag, silent); noGo(e, flag, silent);
// Clear the user for the next time // Clear the user for the next time
user = null;
return false; return false;
} }
@ -213,28 +173,23 @@ public abstract class FlagListener implements Listener {
// If it is not allowed on the island, "bypass island" moderators can do anything // If it is not allowed on the island, "bypass island" moderators can do anything
if (island.get().isAllowed(user, flag)) { if (island.get().isAllowed(user, flag)) {
report(user, e, loc, flag, Why.RANK_ALLOWED); report(user, e, loc, flag, Why.RANK_ALLOWED);
user = null;
return true; return true;
} else if (user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + ".mod.bypass." + flag.getID() + ".island")) { } else if (user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + ".mod.bypass." + flag.getID() + ".island")) {
report(user, e, loc, flag, Why.BYPASS_ISLAND); report(user, e, loc, flag, Why.BYPASS_ISLAND);
user = null;
return true; return true;
} }
report(user, e, loc, flag, Why.NOT_ALLOWED_ON_ISLAND); report(user, e, loc, flag, Why.NOT_ALLOWED_ON_ISLAND);
noGo(e, flag, silent); noGo(e, flag, silent);
// Clear the user for the next time // Clear the user for the next time
user = null;
return false; return false;
} }
// The player is in the world, but not on an island, so general world settings apply // The player is in the world, but not on an island, so general world settings apply
if (flag.isSetForWorld(loc.getWorld())) { if (flag.isSetForWorld(loc.getWorld())) {
report(user, e, loc, flag, Why.ALLOWED_IN_WORLD); report(user, e, loc, flag, Why.ALLOWED_IN_WORLD);
user = null;
return true; return true;
} else { } else {
report(user, e, loc, flag, Why.NOT_ALLOWED_IN_WORLD); report(user, e, loc, flag, Why.NOT_ALLOWED_IN_WORLD);
noGo(e, flag, silent); noGo(e, flag, silent);
user = null;
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@ package world.bentobox.bentobox.listeners.flags.protection;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -10,10 +11,10 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
* Handle interaction with blocks
* @author tastybento * @author tastybento
*/ */
public class BlockInteractionListener extends FlagListener { public class BlockInteractionListener extends FlagListener {
@ -27,7 +28,7 @@ public class BlockInteractionListener extends FlagListener {
// For some items, we need to do a specific check for RIGHT_CLICK_BLOCK // For some items, we need to do a specific check for RIGHT_CLICK_BLOCK
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)
&& e.getClickedBlock().getType().equals(Material.ITEM_FRAME)) { && e.getClickedBlock().getType().equals(Material.ITEM_FRAME)) {
checkIsland(e, e.getClickedBlock().getLocation(), Flags.ITEM_FRAME); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.ITEM_FRAME);
return; return;
} }
@ -35,23 +36,21 @@ public class BlockInteractionListener extends FlagListener {
if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
return; return;
} }
// Set user
this.setUser(User.getInstance(e.getPlayer()));
// Check clicked block // Check clicked block
checkClickedBlock(e, e.getClickedBlock().getLocation(), e.getClickedBlock().getType()); checkClickedBlock(e, e.getPlayer(), e.getClickedBlock().getLocation(), e.getClickedBlock().getType());
// Now check for in-hand items // Now check for in-hand items
if (e.getItem() != null) { if (e.getItem() != null) {
if (e.getItem().getType().name().contains("BOAT")) { if (e.getItem().getType().name().contains("BOAT")) {
checkIsland(e, e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS);
return; return;
} }
switch (e.getItem().getType()) { switch (e.getItem().getType()) {
case ENDER_PEARL: case ENDER_PEARL:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.ENDER_PEARL); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.ENDER_PEARL);
break; break;
case BONE_MEAL: case BONE_MEAL:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS);
break; break;
case BAT_SPAWN_EGG: case BAT_SPAWN_EGG:
case BLAZE_SPAWN_EGG: case BLAZE_SPAWN_EGG:
@ -104,7 +103,7 @@ public class BlockInteractionListener extends FlagListener {
case ZOMBIE_PIGMAN_SPAWN_EGG: case ZOMBIE_PIGMAN_SPAWN_EGG:
case ZOMBIE_SPAWN_EGG: case ZOMBIE_SPAWN_EGG:
case ZOMBIE_VILLAGER_SPAWN_EGG: case ZOMBIE_VILLAGER_SPAWN_EGG:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.SPAWN_EGGS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.SPAWN_EGGS);
break; break;
default: default:
break; break;
@ -116,21 +115,22 @@ public class BlockInteractionListener extends FlagListener {
/** /**
* Check if an action can occur on a clicked block * Check if an action can occur on a clicked block
* @param e - event called * @param e - event called
* @param player - player
* @param loc - location of clicked block * @param loc - location of clicked block
* @param type - material type of clicked block * @param type - material type of clicked block
*/ */
private void checkClickedBlock(Event e, Location loc, Material type) { private void checkClickedBlock(Event e, Player player, Location loc, Material type) {
// Handle pots // Handle pots
if (type.name().startsWith("POTTED")) { if (type.name().startsWith("POTTED")) {
checkIsland(e, loc, Flags.CONTAINER); checkIsland(e, player, loc, Flags.CONTAINER);
return; return;
} }
switch (type) { switch (type) {
case ANVIL: case ANVIL:
checkIsland(e, loc, Flags.ANVIL); checkIsland(e, player, loc, Flags.ANVIL);
break; break;
case BEACON: case BEACON:
checkIsland(e, loc, Flags.BEACON); checkIsland(e, player, loc, Flags.BEACON);
break; break;
case BLACK_BED: case BLACK_BED:
case BLUE_BED: case BLUE_BED:
@ -148,11 +148,11 @@ public class BlockInteractionListener extends FlagListener {
case RED_BED: case RED_BED:
case WHITE_BED: case WHITE_BED:
case YELLOW_BED: case YELLOW_BED:
checkIsland(e, loc, Flags.BED); checkIsland(e, player, loc, Flags.BED);
break; break;
case BREWING_STAND: case BREWING_STAND:
case CAULDRON: case CAULDRON:
checkIsland(e, loc, Flags.BREWING); checkIsland(e, player, loc, Flags.BREWING);
break; break;
case CHEST: case CHEST:
case CHEST_MINECART: case CHEST_MINECART:
@ -175,17 +175,17 @@ public class BlockInteractionListener extends FlagListener {
case YELLOW_SHULKER_BOX: case YELLOW_SHULKER_BOX:
case SHULKER_BOX: case SHULKER_BOX:
case FLOWER_POT: case FLOWER_POT:
checkIsland(e, loc, Flags.CONTAINER); checkIsland(e, player, loc, Flags.CONTAINER);
break; break;
case DISPENSER: case DISPENSER:
checkIsland(e, loc, Flags.DISPENSER); checkIsland(e, player, loc, Flags.DISPENSER);
break; break;
case DROPPER: case DROPPER:
checkIsland(e, loc, Flags.DROPPER); checkIsland(e, player, loc, Flags.DROPPER);
break; break;
case HOPPER: case HOPPER:
case HOPPER_MINECART: case HOPPER_MINECART:
checkIsland(e, loc, Flags.HOPPER); checkIsland(e, player, loc, Flags.HOPPER);
break; break;
case ACACIA_DOOR: case ACACIA_DOOR:
case BIRCH_DOOR: case BIRCH_DOOR:
@ -194,7 +194,7 @@ public class BlockInteractionListener extends FlagListener {
case JUNGLE_DOOR: case JUNGLE_DOOR:
case SPRUCE_DOOR: case SPRUCE_DOOR:
case OAK_DOOR: case OAK_DOOR:
checkIsland(e, loc, Flags.DOOR); checkIsland(e, player, loc, Flags.DOOR);
break; break;
case ACACIA_TRAPDOOR: case ACACIA_TRAPDOOR:
case BIRCH_TRAPDOOR: case BIRCH_TRAPDOOR:
@ -203,7 +203,7 @@ public class BlockInteractionListener extends FlagListener {
case JUNGLE_TRAPDOOR: case JUNGLE_TRAPDOOR:
case SPRUCE_TRAPDOOR: case SPRUCE_TRAPDOOR:
case IRON_TRAPDOOR: case IRON_TRAPDOOR:
checkIsland(e, loc, Flags.TRAPDOOR); checkIsland(e, player, loc, Flags.TRAPDOOR);
break; break;
case ACACIA_FENCE_GATE: case ACACIA_FENCE_GATE:
case BIRCH_FENCE_GATE: case BIRCH_FENCE_GATE:
@ -211,25 +211,25 @@ public class BlockInteractionListener extends FlagListener {
case OAK_FENCE_GATE: case OAK_FENCE_GATE:
case JUNGLE_FENCE_GATE: case JUNGLE_FENCE_GATE:
case SPRUCE_FENCE_GATE: case SPRUCE_FENCE_GATE:
checkIsland(e, loc, Flags.GATE); checkIsland(e, player, loc, Flags.GATE);
break; break;
case FURNACE: case FURNACE:
checkIsland(e, loc, Flags.FURNACE); checkIsland(e, player, loc, Flags.FURNACE);
break; break;
case ENCHANTING_TABLE: case ENCHANTING_TABLE:
checkIsland(e, loc, Flags.ENCHANTING); checkIsland(e, player, loc, Flags.ENCHANTING);
break; break;
case ENDER_CHEST: case ENDER_CHEST:
checkIsland(e, loc, Flags.ENDER_CHEST); checkIsland(e, player, loc, Flags.ENDER_CHEST);
break; break;
case JUKEBOX: case JUKEBOX:
checkIsland(e, loc, Flags.JUKEBOX); checkIsland(e, player, loc, Flags.JUKEBOX);
break; break;
case NOTE_BLOCK: case NOTE_BLOCK:
checkIsland(e, loc, Flags.NOTE_BLOCK); checkIsland(e, player, loc, Flags.NOTE_BLOCK);
break; break;
case CRAFTING_TABLE: case CRAFTING_TABLE:
checkIsland(e, loc, Flags.CRAFTING); checkIsland(e, player, loc, Flags.CRAFTING);
break; break;
case STONE_BUTTON: case STONE_BUTTON:
case ACACIA_BUTTON: case ACACIA_BUTTON:
@ -238,24 +238,24 @@ public class BlockInteractionListener extends FlagListener {
case JUNGLE_BUTTON: case JUNGLE_BUTTON:
case OAK_BUTTON: case OAK_BUTTON:
case SPRUCE_BUTTON: case SPRUCE_BUTTON:
checkIsland(e, loc, Flags.BUTTON); checkIsland(e, player, loc, Flags.BUTTON);
break; break;
case LEVER: case LEVER:
checkIsland(e, loc, Flags.LEVER); checkIsland(e, player, loc, Flags.LEVER);
break; break;
case REPEATER: case REPEATER:
case COMPARATOR: case COMPARATOR:
case DAYLIGHT_DETECTOR: case DAYLIGHT_DETECTOR:
checkIsland(e, loc, Flags.REDSTONE); checkIsland(e, player, loc, Flags.REDSTONE);
break; break;
case DRAGON_EGG: case DRAGON_EGG:
checkIsland(e, loc, Flags.BREAK_BLOCKS); checkIsland(e, player, loc, Flags.BREAK_BLOCKS);
break; break;
case END_PORTAL_FRAME: case END_PORTAL_FRAME:
checkIsland(e, loc, Flags.PLACE_BLOCKS); checkIsland(e, player, loc, Flags.PLACE_BLOCKS);
break; break;
case ITEM_FRAME: case ITEM_FRAME:
checkIsland(e, loc, Flags.ITEM_FRAME); checkIsland(e, player, loc, Flags.ITEM_FRAME);
break; break;
default: default:
break; break;
@ -273,7 +273,6 @@ public class BlockInteractionListener extends FlagListener {
*/ */
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onBlockBreak(final BlockBreakEvent e) { public void onBlockBreak(final BlockBreakEvent e) {
setUser(User.getInstance(e.getPlayer())); checkClickedBlock(e, e.getPlayer(), e.getBlock().getLocation(), e.getBlock().getType());
checkClickedBlock(e, e.getBlock().getLocation(), e.getBlock().getType());
} }
} }

View File

@ -17,7 +17,6 @@ import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.util.BlockIterator; import org.bukkit.util.BlockIterator;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
public class BreakBlocksListener extends FlagListener { public class BreakBlocksListener extends FlagListener {
@ -29,7 +28,7 @@ public class BreakBlocksListener extends FlagListener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onBlockBreak(final BlockBreakEvent e) { public void onBlockBreak(final BlockBreakEvent e) {
setUser(User.getInstance(e.getPlayer())).checkIsland(e, e.getBlock().getLocation(), Flags.BREAK_BLOCKS); checkIsland(e, e.getPlayer(), e.getBlock().getLocation(), Flags.BREAK_BLOCKS);
} }
/** /**
@ -40,7 +39,7 @@ public class BreakBlocksListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onBreakHanging(final HangingBreakByEntityEvent e) { public void onBreakHanging(final HangingBreakByEntityEvent e) {
if (e.getRemover() instanceof Player) { if (e.getRemover() instanceof Player) {
setUser(User.getInstance(e.getRemover())).checkIsland(e, e.getEntity().getLocation(), Flags.BREAK_BLOCKS); checkIsland(e, (Player)e.getRemover(), e.getEntity().getLocation(), Flags.BREAK_BLOCKS);
} }
} }
@ -62,7 +61,7 @@ public class BreakBlocksListener extends FlagListener {
while (iterator.hasNext()) { while (iterator.hasNext()) {
Block lastBlock = iterator.next(); Block lastBlock = iterator.next();
if (lastBlock.getType().toString().endsWith("_SKULL") || (lastBlock.getType().toString().endsWith("_HEAD") && !lastBlock.getType().equals(Material.PISTON_HEAD))) { if (lastBlock.getType().toString().endsWith("_SKULL") || (lastBlock.getType().toString().endsWith("_HEAD") && !lastBlock.getType().equals(Material.PISTON_HEAD))) {
checkIsland(e, lastBlock.getLocation(), Flags.BREAK_BLOCKS); checkIsland(e, e.getPlayer(), lastBlock.getLocation(), Flags.BREAK_BLOCKS);
return; return;
} }
} }
@ -74,7 +73,7 @@ public class BreakBlocksListener extends FlagListener {
case CAKE: case CAKE:
case DRAGON_EGG: case DRAGON_EGG:
case SPAWNER: case SPAWNER:
setUser(User.getInstance(e.getPlayer())).checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS);
break; break;
default: default:
break; break;
@ -88,8 +87,7 @@ public class BreakBlocksListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onVehicleDamageEvent(VehicleDamageEvent e) { public void onVehicleDamageEvent(VehicleDamageEvent e) {
if (getIWM().inWorld(e.getVehicle().getLocation()) && e.getAttacker() instanceof Player) { if (getIWM().inWorld(e.getVehicle().getLocation()) && e.getAttacker() instanceof Player) {
setUser(User.getInstance((Player) e.getAttacker())); checkIsland(e, (Player)e.getAttacker(), e.getVehicle().getLocation(), Flags.BREAK_BLOCKS);
checkIsland(e, e.getVehicle().getLocation(), Flags.BREAK_BLOCKS);
} }
} }
@ -106,11 +104,11 @@ public class BreakBlocksListener extends FlagListener {
// Get the attacker // Get the attacker
if (e.getDamager() instanceof Player) { if (e.getDamager() instanceof Player) {
setUser(User.getInstance(e.getDamager())).checkIsland(e, e.getEntity().getLocation(), Flags.BREAK_BLOCKS); checkIsland(e, (Player)e.getDamager(), e.getEntity().getLocation(), Flags.BREAK_BLOCKS);
} else if (e.getDamager() instanceof Projectile) { } else if (e.getDamager() instanceof Projectile) {
// Find out who fired the arrow // Find out who fired the arrow
Projectile p = (Projectile) e.getDamager(); Projectile p = (Projectile) e.getDamager();
if (p.getShooter() instanceof Player && !setUser(User.getInstance((Player)p.getShooter())).checkIsland(e, e.getEntity().getLocation(), Flags.BREAK_BLOCKS)) { if (p.getShooter() instanceof Player && !checkIsland(e, (Player)p.getShooter(), e.getEntity().getLocation(), Flags.BREAK_BLOCKS)) {
e.getEntity().setFireTicks(0); e.getEntity().setFireTicks(0);
e.getDamager().remove(); e.getDamager().remove();
} }

View File

@ -40,7 +40,7 @@ public class BreedingListener extends FlagListener {
if (e.getHand().equals(EquipmentSlot.OFF_HAND)) { if (e.getHand().equals(EquipmentSlot.OFF_HAND)) {
inHand = e.getPlayer().getInventory().getItemInOffHand(); inHand = e.getPlayer().getInventory().getItemInOffHand();
} }
if (inHand != null && BREEDING_ITEMS.contains(inHand.getType()) && !checkIsland(e, e.getRightClicked().getLocation(), Flags.BREEDING)) { if (inHand != null && BREEDING_ITEMS.contains(inHand.getType()) && !checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.BREEDING)) {
((Animals)e.getRightClicked()).setBreed(false); ((Animals)e.getRightClicked()).setBreed(false);
} }
} }

View File

@ -29,7 +29,7 @@ public class BucketListener extends FlagListener {
if (e.getBlockClicked() != null) { if (e.getBlockClicked() != null) {
// This is where the water or lava actually will be dumped // This is where the water or lava actually will be dumped
Block dumpBlock = e.getBlockClicked().getRelative(e.getBlockFace()); Block dumpBlock = e.getBlockClicked().getRelative(e.getBlockFace());
checkIsland(e, dumpBlock.getLocation(), Flags.BUCKET); checkIsland(e, e.getPlayer(), dumpBlock.getLocation(), Flags.BUCKET);
} }
} }
@ -40,23 +40,23 @@ public class BucketListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onBucketFill(final PlayerBucketFillEvent e) { public void onBucketFill(final PlayerBucketFillEvent e) {
// Check filling of various liquids // Check filling of various liquids
if (e.getItemStack().getType().equals(Material.LAVA_BUCKET) && (!checkIsland(e, e.getBlockClicked().getLocation(), Flags.COLLECT_LAVA))) { if (e.getItemStack().getType().equals(Material.LAVA_BUCKET) && (!checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.COLLECT_LAVA))) {
return; return;
} }
if (e.getItemStack().getType().equals(Material.WATER_BUCKET) && (!checkIsland(e, e.getBlockClicked().getLocation(), Flags.COLLECT_WATER))) { if (e.getItemStack().getType().equals(Material.WATER_BUCKET) && (!checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.COLLECT_WATER))) {
return; return;
} }
if (e.getItemStack().getType().equals(Material.MILK_BUCKET) && (!checkIsland(e, e.getBlockClicked().getLocation(), Flags.MILKING))) { if (e.getItemStack().getType().equals(Material.MILK_BUCKET) && (!checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.MILKING))) {
return; return;
} }
// Check general bucket use // Check general bucket use
checkIsland(e, e.getBlockClicked().getLocation(), Flags.BUCKET); checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.BUCKET);
} }
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onTropicalFishScooping(final PlayerInteractEntityEvent e) { public void onTropicalFishScooping(final PlayerInteractEntityEvent e) {
if (e.getRightClicked() instanceof TropicalFish && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.WATER_BUCKET)) { if (e.getRightClicked() instanceof TropicalFish && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.WATER_BUCKET)) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.FISH_SCOOPING); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.FISH_SCOOPING);
} }
} }
} }

View File

@ -20,7 +20,7 @@ public class EggListener extends FlagListener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEggThrow(PlayerEggThrowEvent e) { public void onEggThrow(PlayerEggThrowEvent e) {
if (!checkIsland(e, e.getEgg().getLocation(), Flags.EGGS)) { if (!checkIsland(e, e.getPlayer(), e.getEgg().getLocation(), Flags.EGGS)) {
e.setHatching(false); e.setHatching(false);
} }
} }

View File

@ -26,7 +26,7 @@ public class EntityInteractListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onPlayerInteractAtEntity(final PlayerInteractAtEntityEvent e) { public void onPlayerInteractAtEntity(final PlayerInteractAtEntityEvent e) {
if (e.getRightClicked() instanceof ArmorStand) { if (e.getRightClicked() instanceof ArmorStand) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.ARMOR_STAND); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.ARMOR_STAND);
} }
} }
@ -35,24 +35,24 @@ public class EntityInteractListener extends FlagListener {
if (e.getRightClicked() instanceof Vehicle) { if (e.getRightClicked() instanceof Vehicle) {
// Animal riding // Animal riding
if (e.getRightClicked() instanceof Animals) { if (e.getRightClicked() instanceof Animals) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.RIDING); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.RIDING);
} }
// Minecart riding // Minecart riding
else if (e.getRightClicked() instanceof Minecart) { else if (e.getRightClicked() instanceof Minecart) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.MINECART); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.MINECART);
} }
// Boat riding // Boat riding
else if (e.getRightClicked() instanceof Boat) { else if (e.getRightClicked() instanceof Boat) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.BOAT); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.BOAT);
} }
} }
// Villager trading // Villager trading
else if (e.getRightClicked().getType().equals(EntityType.VILLAGER)) { else if (e.getRightClicked().getType().equals(EntityType.VILLAGER)) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.TRADING); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.TRADING);
} }
// Name tags // Name tags
else if (e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.NAME_TAG)) { else if (e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.NAME_TAG)) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.NAME_TAG); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.NAME_TAG);
} }
} }
} }

View File

@ -5,8 +5,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent; import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -20,9 +20,8 @@ public class ExperiencePickupListener extends FlagListener {
public void onExperienceOrbTargetPlayer(EntityTargetLivingEntityEvent e) { public void onExperienceOrbTargetPlayer(EntityTargetLivingEntityEvent e) {
// Make sure the target is a Player and the entity is an experience orb // Make sure the target is a Player and the entity is an experience orb
if (e.getTarget() instanceof Player && e.getEntity() instanceof ExperienceOrb) { if (e.getTarget() instanceof Player && e.getEntity() instanceof ExperienceOrb) {
setUser(User.getInstance((Player) e.getTarget())); if (!checkIsland(e, (Player) e.getTarget(), e.getEntity().getLocation(), Flags.EXPERIENCE_PICKUP)) {
if (!checkIsland(e, e.getEntity().getLocation(), Flags.EXPERIENCE_PICKUP)) { // Canceling the event won't be enough, we need to explicitly set the target to null
// Cancelling the event won't be enough, we need to explicitly set the target to null
e.setTarget(null); e.setTarget(null);
} }
} }

View File

@ -77,7 +77,7 @@ public class FireListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent e) { public void onPlayerInteract(PlayerInteractEvent e) {
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getMaterial() != null && e.getMaterial().equals(Material.FLINT_AND_STEEL)) { if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getMaterial() != null && e.getMaterial().equals(Material.FLINT_AND_STEEL)) {
checkIsland(e, e.getClickedBlock().getLocation(), Flags.FIRE); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.FIRE);
} }
// Look along player's sight line to see if any blocks are fire. Players can hit fire out quite a long way away. // Look along player's sight line to see if any blocks are fire. Players can hit fire out quite a long way away.
try { try {
@ -85,7 +85,7 @@ public class FireListener extends FlagListener {
while (iter.hasNext()) { while (iter.hasNext()) {
Block lastBlock = iter.next(); Block lastBlock = iter.next();
if (lastBlock.getType().equals(Material.FIRE)) { if (lastBlock.getType().equals(Material.FIRE)) {
checkIsland(e, lastBlock.getLocation(), Flags.FIRE_EXTINGUISH); checkIsland(e, e.getPlayer(), lastBlock.getLocation(), Flags.FIRE_EXTINGUISH);
} }
} }
} catch (Exception ex) { } catch (Exception ex) {

View File

@ -1,7 +1,6 @@
package world.bentobox.bentobox.listeners.flags.protection; package world.bentobox.bentobox.listeners.flags.protection;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Animals; import org.bukkit.entity.Animals;
@ -32,7 +31,6 @@ import org.bukkit.potion.PotionEffect;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -42,7 +40,7 @@ import world.bentobox.bentobox.lists.Flags;
*/ */
public class HurtingListener extends FlagListener { public class HurtingListener extends FlagListener {
private HashMap<Integer, UUID> thrownPotions = new HashMap<>(); private HashMap<Integer, Player> thrownPotions = new HashMap<>();
/** /**
* Handles mob and monster protection * Handles mob and monster protection
@ -72,11 +70,11 @@ public class HurtingListener extends FlagListener {
private void respond(EntityDamageByEntityEvent e, Entity damager, Flag flag) { private void respond(EntityDamageByEntityEvent e, Entity damager, Flag flag) {
// Get the attacker // Get the attacker
if (damager instanceof Player) { if (damager instanceof Player) {
setUser(User.getInstance(damager)).checkIsland(e, damager.getLocation(), flag); checkIsland(e, (Player)damager, damager.getLocation(), flag);
} else if (damager instanceof Projectile) { } else if (damager instanceof Projectile) {
// Find out who fired the projectile // Find out who fired the projectile
Projectile p = (Projectile) damager; Projectile p = (Projectile) damager;
if (p.getShooter() instanceof Player && !setUser(User.getInstance((Player)p.getShooter())).checkIsland(e, damager.getLocation(), flag)) { if (p.getShooter() instanceof Player && !checkIsland(e, (Player)p.getShooter(), damager.getLocation(), flag)) {
e.getEntity().setFireTicks(0); e.getEntity().setFireTicks(0);
damager.remove(); damager.remove();
} }
@ -93,14 +91,14 @@ public class HurtingListener extends FlagListener {
return; return;
} }
if (((e.getCaught() instanceof Animals || e.getCaught() instanceof IronGolem || e.getCaught() instanceof Snowman) && checkIsland(e, e.getCaught().getLocation(), Flags.HURT_ANIMALS)) if (((e.getCaught() instanceof Animals || e.getCaught() instanceof IronGolem || e.getCaught() instanceof Snowman) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_ANIMALS))
|| ((e.getCaught() instanceof Monster || e.getCaught() instanceof Squid || e.getCaught() instanceof Slime) && checkIsland(e, e.getCaught().getLocation(), Flags.HURT_MONSTERS)) || ((e.getCaught() instanceof Monster || e.getCaught() instanceof Squid || e.getCaught() instanceof Slime) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_MONSTERS))
|| (e.getCaught() instanceof Villager && checkIsland(e, e.getCaught().getLocation(), Flags.HURT_VILLAGERS))) { || (e.getCaught() instanceof Villager && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_VILLAGERS))) {
e.getHook().remove(); e.getHook().remove();
} }
// Handle Armor stands that can be pulled using a rod // Handle Armor stands that can be pulled using a rod
if (e.getCaught() instanceof ArmorStand && checkIsland(e, e.getCaught().getLocation(), Flags.ARMOR_STAND)) { if (e.getCaught() instanceof ArmorStand && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.ARMOR_STAND)) {
e.getHook().remove(); e.getHook().remove();
} }
} }
@ -114,7 +112,7 @@ public class HurtingListener extends FlagListener {
if (e.getRightClicked() instanceof Parrot if (e.getRightClicked() instanceof Parrot
&& (e.getHand().equals(EquipmentSlot.HAND) && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.COOKIE)) && (e.getHand().equals(EquipmentSlot.HAND) && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.COOKIE))
|| (e.getHand().equals(EquipmentSlot.OFF_HAND) && e.getPlayer().getInventory().getItemInOffHand().getType().equals(Material.COOKIE))) { || (e.getHand().equals(EquipmentSlot.OFF_HAND) && e.getPlayer().getInventory().getItemInOffHand().getType().equals(Material.COOKIE))) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.HURT_ANIMALS); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.HURT_ANIMALS);
} }
} }
@ -128,7 +126,6 @@ public class HurtingListener extends FlagListener {
Projectile projectile = e.getEntity(); Projectile projectile = e.getEntity();
if (projectile.getShooter() instanceof Player) { if (projectile.getShooter() instanceof Player) {
Player attacker = (Player)projectile.getShooter(); Player attacker = (Player)projectile.getShooter();
setUser(User.getInstance(attacker));
// Run through all the affected entities // Run through all the affected entities
for (LivingEntity entity: e.getAffectedEntities()) { for (LivingEntity entity: e.getAffectedEntities()) {
// Self damage // Self damage
@ -137,7 +134,7 @@ public class HurtingListener extends FlagListener {
} }
// Monsters being hurt // Monsters being hurt
if ((entity instanceof Monster || entity instanceof Slime || entity instanceof Squid) if ((entity instanceof Monster || entity instanceof Slime || entity instanceof Squid)
&& !setUser(User.getInstance(attacker)).checkIsland(e, entity.getLocation(), Flags.HURT_MONSTERS)) { && !checkIsland(e, attacker, entity.getLocation(), Flags.HURT_MONSTERS)) {
for (PotionEffect effect : e.getPotion().getEffects()) { for (PotionEffect effect : e.getPotion().getEffects()) {
entity.removePotionEffect(effect.getType()); entity.removePotionEffect(effect.getType());
} }
@ -145,14 +142,14 @@ public class HurtingListener extends FlagListener {
// Mobs being hurt // Mobs being hurt
if ((entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman) if ((entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman)
&& !checkIsland(e, entity.getLocation(), Flags.HURT_ANIMALS)) { && !checkIsland(e, attacker, entity.getLocation(), Flags.HURT_ANIMALS)) {
for (PotionEffect effect : e.getPotion().getEffects()) { for (PotionEffect effect : e.getPotion().getEffects()) {
entity.removePotionEffect(effect.getType()); entity.removePotionEffect(effect.getType());
} }
} }
// Villagers being hurt // Villagers being hurt
if (entity instanceof Villager && !checkIsland(e, entity.getLocation(), Flags.HURT_VILLAGERS)) { if (entity instanceof Villager && !checkIsland(e, attacker, entity.getLocation(), Flags.HURT_VILLAGERS)) {
for (PotionEffect effect : e.getPotion().getEffects()) { for (PotionEffect effect : e.getPotion().getEffects()) {
entity.removePotionEffect(effect.getType()); entity.removePotionEffect(effect.getType());
} }
@ -170,9 +167,8 @@ public class HurtingListener extends FlagListener {
// Try to get the shooter // Try to get the shooter
Projectile projectile = e.getEntity(); Projectile projectile = e.getEntity();
if (projectile.getShooter() instanceof Player) { if (projectile.getShooter() instanceof Player) {
UUID uuid = ((Player)projectile.getShooter()).getUniqueId();
// Store it and remove it when the effect is gone // Store it and remove it when the effect is gone
thrownPotions.put(e.getAreaEffectCloud().getEntityId(), uuid); thrownPotions.put(e.getAreaEffectCloud().getEntityId(), (Player)projectile.getShooter());
getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> thrownPotions.remove(e.getAreaEffectCloud().getEntityId()), e.getAreaEffectCloud().getDuration()); getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> thrownPotions.remove(e.getAreaEffectCloud().getEntityId()), e.getAreaEffectCloud().getDuration());
} }
} }
@ -184,23 +180,23 @@ public class HurtingListener extends FlagListener {
} }
if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && thrownPotions.containsKey(e.getDamager().getEntityId())) { if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && thrownPotions.containsKey(e.getDamager().getEntityId())) {
UUID attacker = thrownPotions.get(e.getDamager().getEntityId()); Player attacker = thrownPotions.get(e.getDamager().getEntityId());
// Self damage // Self damage
if (attacker.equals(e.getEntity().getUniqueId())) { if (attacker == null || attacker.equals(e.getEntity())) {
return; return;
} }
Entity entity = e.getEntity(); Entity entity = e.getEntity();
// Monsters being hurt // Monsters being hurt
if (entity instanceof Monster || entity instanceof Slime || entity instanceof Squid) { if (entity instanceof Monster || entity instanceof Slime || entity instanceof Squid) {
checkIsland(e, entity.getLocation(), Flags.HURT_MONSTERS); checkIsland(e, attacker, entity.getLocation(), Flags.HURT_MONSTERS);
} }
// Mobs being hurt // Mobs being hurt
if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman) { if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman) {
checkIsland(e, entity.getLocation(), Flags.HURT_ANIMALS); checkIsland(e, attacker, entity.getLocation(), Flags.HURT_ANIMALS);
} }
// Villagers being hurt // Villagers being hurt
if (entity instanceof Villager) { if (entity instanceof Villager) {
checkIsland(e, entity.getLocation(), Flags.HURT_VILLAGERS); checkIsland(e, attacker, entity.getLocation(), Flags.HURT_VILLAGERS);
} }
} }
} }

View File

@ -9,13 +9,13 @@ import org.bukkit.block.Furnace;
import org.bukkit.block.Hopper; import org.bukkit.block.Hopper;
import org.bukkit.block.ShulkerBox; import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Animals; import org.bukkit.entity.Animals;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -31,34 +31,34 @@ public class InventoryListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onInventoryClick(InventoryClickEvent e) { public void onInventoryClick(InventoryClickEvent e) {
InventoryHolder inventoryHolder = e.getInventory().getHolder(); InventoryHolder inventoryHolder = e.getInventory().getHolder();
if (inventoryHolder == null) { if (inventoryHolder == null || !(e.getWhoClicked() instanceof Player)) {
return; return;
} }
setUser(User.getInstance(e.getWhoClicked())); Player player = (Player)e.getWhoClicked();
if (inventoryHolder instanceof Animals) { if (inventoryHolder instanceof Animals) {
checkIsland(e, e.getInventory().getLocation(), Flags.MOUNT_INVENTORY); checkIsland(e, player, e.getInventory().getLocation(), Flags.MOUNT_INVENTORY);
} }
else if (inventoryHolder instanceof Chest else if (inventoryHolder instanceof Chest
|| inventoryHolder instanceof ShulkerBox) { || inventoryHolder instanceof ShulkerBox) {
checkIsland(e, e.getInventory().getLocation(), Flags.CONTAINER); checkIsland(e, player, e.getInventory().getLocation(), Flags.CONTAINER);
} }
else if (inventoryHolder instanceof Dispenser) { else if (inventoryHolder instanceof Dispenser) {
checkIsland(e, e.getInventory().getLocation(), Flags.DISPENSER); checkIsland(e, player, e.getInventory().getLocation(), Flags.DISPENSER);
} }
else if (inventoryHolder instanceof Dropper) { else if (inventoryHolder instanceof Dropper) {
checkIsland(e, e.getInventory().getLocation(), Flags.DROPPER); checkIsland(e, player, e.getInventory().getLocation(), Flags.DROPPER);
} }
else if (inventoryHolder instanceof Hopper) { else if (inventoryHolder instanceof Hopper) {
checkIsland(e, e.getInventory().getLocation(), Flags.HOPPER); checkIsland(e, player, e.getInventory().getLocation(), Flags.HOPPER);
} }
else if (inventoryHolder instanceof Furnace) { else if (inventoryHolder instanceof Furnace) {
checkIsland(e, e.getInventory().getLocation(), Flags.FURNACE); checkIsland(e, player, e.getInventory().getLocation(), Flags.FURNACE);
} }
else if (inventoryHolder instanceof BrewingStand) { else if (inventoryHolder instanceof BrewingStand) {
checkIsland(e, e.getInventory().getLocation(), Flags.BREWING); checkIsland(e, player, e.getInventory().getLocation(), Flags.BREWING);
} }
else if (inventoryHolder instanceof Beacon) { else if (inventoryHolder instanceof Beacon) {
checkIsland(e, e.getInventory().getLocation(), Flags.BEACON); checkIsland(e, player, e.getInventory().getLocation(), Flags.BEACON);
} }
} }
} }

View File

@ -7,7 +7,6 @@ import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -20,7 +19,7 @@ public class ItemDropPickUpListener extends FlagListener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onDrop(PlayerDropItemEvent e) { public void onDrop(PlayerDropItemEvent e) {
checkIsland(e, e.getItemDrop().getLocation(), Flags.ITEM_DROP); checkIsland(e, e.getPlayer(), e.getItemDrop().getLocation(), Flags.ITEM_DROP);
} }
/* /*
@ -29,7 +28,7 @@ public class ItemDropPickUpListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPickup(EntityPickupItemEvent e) { public void onPickup(EntityPickupItemEvent e) {
if (e.getEntity() instanceof Player) { if (e.getEntity() instanceof Player) {
setUser(User.getInstance(e.getEntity())).checkIsland(e, e.getItem().getLocation(), Flags.ITEM_PICKUP); checkIsland(e, (Player)e.getEntity(), e.getItem().getLocation(), Flags.ITEM_PICKUP);
} }
} }
} }

View File

@ -23,7 +23,7 @@ public class LeashListener extends FlagListener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onLeash(PlayerLeashEntityEvent e) { public void onLeash(PlayerLeashEntityEvent e) {
checkIsland(e, e.getEntity().getLocation(), Flags.LEASH); checkIsland(e, e.getPlayer(), e.getEntity().getLocation(), Flags.LEASH);
} }
/** /**
@ -33,7 +33,7 @@ public class LeashListener extends FlagListener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onUnleash(PlayerUnleashEntityEvent e) { public void onUnleash(PlayerUnleashEntityEvent e) {
checkIsland(e, e.getEntity().getLocation(), Flags.LEASH); checkIsland(e, e.getPlayer(), e.getEntity().getLocation(), Flags.LEASH);
} }
/** /**
@ -43,7 +43,7 @@ public class LeashListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerLeashHitch(final HangingPlaceEvent e) { public void onPlayerLeashHitch(final HangingPlaceEvent e) {
if (e.getEntity() != null && e.getEntity().getType().equals(EntityType.LEASH_HITCH)) { if (e.getEntity() != null && e.getEntity().getType().equals(EntityType.LEASH_HITCH)) {
checkIsland(e, e.getEntity().getLocation(), Flags.LEASH); checkIsland(e, e.getPlayer(), e.getEntity().getLocation(), Flags.LEASH);
} }
} }

View File

@ -9,7 +9,6 @@ import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -31,7 +30,7 @@ public class PhysicalInteractionListener extends FlagListener {
switch (e.getClickedBlock().getType()) { switch (e.getClickedBlock().getType()) {
case FARMLAND: case FARMLAND:
// Crop trample // Crop trample
checkIsland(e, e.getPlayer().getLocation(), Flags.CROP_TRAMPLE); checkIsland(e, e.getPlayer(), e.getPlayer().getLocation(), Flags.CROP_TRAMPLE);
break; break;
case ACACIA_PRESSURE_PLATE: case ACACIA_PRESSURE_PLATE:
@ -44,11 +43,11 @@ public class PhysicalInteractionListener extends FlagListener {
case SPRUCE_PRESSURE_PLATE: case SPRUCE_PRESSURE_PLATE:
case STONE_PRESSURE_PLATE: case STONE_PRESSURE_PLATE:
// Pressure plates // Pressure plates
checkIsland(e, e.getPlayer().getLocation(), Flags.PRESSURE_PLATE); checkIsland(e, e.getPlayer(), e.getPlayer().getLocation(), Flags.PRESSURE_PLATE);
break; break;
case TURTLE_EGG: case TURTLE_EGG:
checkIsland(e, e.getPlayer().getLocation(), Flags.TURTLE_EGGS); checkIsland(e, e.getPlayer(), e.getPlayer().getLocation(), Flags.TURTLE_EGGS);
break; break;
default: default:
@ -68,8 +67,6 @@ public class PhysicalInteractionListener extends FlagListener {
} }
Projectile p = (Projectile)e.getEntity(); Projectile p = (Projectile)e.getEntity();
if (p.getShooter() instanceof Player && e.getBlock() != null) { if (p.getShooter() instanceof Player && e.getBlock() != null) {
// Set the user to the shooter
setUser(User.getInstance((Player)p.getShooter()));
switch(e.getBlock().getType()) { switch(e.getBlock().getType()) {
case ACACIA_BUTTON: case ACACIA_BUTTON:
@ -79,7 +76,7 @@ public class PhysicalInteractionListener extends FlagListener {
case SPRUCE_BUTTON: case SPRUCE_BUTTON:
case STONE_BUTTON: case STONE_BUTTON:
case DARK_OAK_BUTTON: case DARK_OAK_BUTTON:
checkIsland(e, e.getBlock().getLocation(), Flags.BUTTON); checkIsland(e, (Player)p.getShooter(), e.getBlock().getLocation(), Flags.BUTTON);
break; break;
case ACACIA_PRESSURE_PLATE: case ACACIA_PRESSURE_PLATE:
case BIRCH_PRESSURE_PLATE: case BIRCH_PRESSURE_PLATE:
@ -91,7 +88,7 @@ public class PhysicalInteractionListener extends FlagListener {
case SPRUCE_PRESSURE_PLATE: case SPRUCE_PRESSURE_PLATE:
case STONE_PRESSURE_PLATE: case STONE_PRESSURE_PLATE:
// Pressure plates // Pressure plates
checkIsland(e, e.getBlock().getLocation(), Flags.PRESSURE_PLATE); checkIsland(e, (Player)p.getShooter(), e.getBlock().getLocation(), Flags.PRESSURE_PLATE);
break; break;
default: default:
break; break;

View File

@ -12,7 +12,6 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -30,7 +29,7 @@ public class PlaceBlocksListener extends FlagListener {
if (e.getBlock().getType().equals(Material.FIRE)) { if (e.getBlock().getType().equals(Material.FIRE)) {
return; return;
} }
checkIsland(e, e.getBlock().getLocation(), Flags.PLACE_BLOCKS); checkIsland(e, e.getPlayer(), e.getBlock().getLocation(), Flags.PLACE_BLOCKS);
} }
/** /**
@ -40,7 +39,7 @@ public class PlaceBlocksListener extends FlagListener {
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerHitItemFrame(PlayerInteractEntityEvent e) { public void onPlayerHitItemFrame(PlayerInteractEntityEvent e) {
if (e.getRightClicked().getType().equals(EntityType.ITEM_FRAME)) { if (e.getRightClicked().getType().equals(EntityType.ITEM_FRAME)) {
checkIsland(e, e.getRightClicked().getLocation(), Flags.PLACE_BLOCKS); checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.PLACE_BLOCKS);
} }
} }
@ -57,7 +56,7 @@ public class PlaceBlocksListener extends FlagListener {
switch (e.getClickedBlock().getType()) { switch (e.getClickedBlock().getType()) {
case FIREWORK_ROCKET: case FIREWORK_ROCKET:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS);
return; return;
case RAIL: case RAIL:
case POWERED_RAIL: case POWERED_RAIL:
@ -65,7 +64,7 @@ public class PlaceBlocksListener extends FlagListener {
case ACTIVATOR_RAIL: case ACTIVATOR_RAIL:
if (e.getMaterial() != null && (e.getMaterial() == Material.MINECART || e.getMaterial() == Material.CHEST_MINECART || e.getMaterial() == Material.HOPPER_MINECART if (e.getMaterial() != null && (e.getMaterial() == Material.MINECART || e.getMaterial() == Material.CHEST_MINECART || e.getMaterial() == Material.HOPPER_MINECART
|| e.getMaterial() == Material.TNT_MINECART || e.getMaterial() == Material.FURNACE_MINECART)) { || e.getMaterial() == Material.TNT_MINECART || e.getMaterial() == Material.FURNACE_MINECART)) {
checkIsland(e, e.getClickedBlock().getLocation(), Flags.MINECART); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.MINECART);
} }
return; return;
default: default:
@ -76,10 +75,10 @@ public class PlaceBlocksListener extends FlagListener {
|| e.getMaterial().equals(Material.END_CRYSTAL) || e.getMaterial().equals(Material.END_CRYSTAL)
//|| Tag.DOORS.isTagged(e.getMaterial()) //|| Tag.DOORS.isTagged(e.getMaterial())
|| e.getMaterial().equals(Material.CHEST) || e.getMaterial().equals(Material.TRAPPED_CHEST)) { || e.getMaterial().equals(Material.CHEST) || e.getMaterial().equals(Material.TRAPPED_CHEST)) {
checkIsland(e, e.getPlayer().getLocation(), Flags.PLACE_BLOCKS); checkIsland(e, e.getPlayer(), e.getPlayer().getLocation(), Flags.PLACE_BLOCKS);
} }
else if (e.getMaterial().name().contains("BOAT")) { else if (e.getMaterial().name().contains("BOAT")) {
checkIsland(e, e.getPlayer().getLocation(), Flags.BOAT); checkIsland(e, e.getPlayer(), e.getPlayer().getLocation(), Flags.BOAT);
} }
} }
} }
@ -92,8 +91,7 @@ public class PlaceBlocksListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onBlockForm(EntityBlockFormEvent e) { public void onBlockForm(EntityBlockFormEvent e) {
if (e.getNewState().getType().equals(Material.FROSTED_ICE) && e.getEntity() instanceof Player) { if (e.getNewState().getType().equals(Material.FROSTED_ICE) && e.getEntity() instanceof Player) {
setUser(User.getInstance((Player)e.getEntity())); checkIsland(e, (Player)e.getEntity(), e.getBlock().getLocation(), Flags.FROST_WALKER);
checkIsland(e, e.getBlock().getLocation(), Flags.FROST_WALKER);
} }
} }

View File

@ -17,9 +17,9 @@ public class PortalListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerPortal(PlayerPortalEvent e) { public void onPlayerPortal(PlayerPortalEvent e) {
if (e.getCause().equals(TeleportCause.NETHER_PORTAL)) { if (e.getCause().equals(TeleportCause.NETHER_PORTAL)) {
checkIsland(e, e.getFrom(), Flags.NETHER_PORTAL); checkIsland(e, e.getPlayer(), e.getFrom(), Flags.NETHER_PORTAL);
} else if (e.getCause().equals(TeleportCause.END_PORTAL)) { } else if (e.getCause().equals(TeleportCause.END_PORTAL)) {
checkIsland(e, e.getFrom(), Flags.END_PORTAL); checkIsland(e, e.getPlayer(), e.getFrom(), Flags.END_PORTAL);
} }
} }
} }

View File

@ -17,7 +17,7 @@ public class ShearingListener extends FlagListener {
// Protect sheep // Protect sheep
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onShear(final PlayerShearEntityEvent e) { public void onShear(final PlayerShearEntityEvent e) {
checkIsland(e, e.getEntity().getLocation(), Flags.SHEARING); checkIsland(e, e.getPlayer(), e.getEntity().getLocation(), Flags.SHEARING);
} }
} }

View File

@ -12,7 +12,6 @@ import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -37,9 +36,7 @@ public class TNTListener extends FlagListener {
Projectile projectile = (Projectile) e.getEntity(); Projectile projectile = (Projectile) e.getEntity();
// Find out who fired it // Find out who fired it
if (projectile.getShooter() instanceof Player && projectile.getFireTicks() > 0) { if (projectile.getShooter() instanceof Player && projectile.getFireTicks() > 0) {
Player shooter = (Player)projectile.getShooter(); if (!checkIsland(e, (Player)projectile.getShooter(), e.getBlock().getLocation(), Flags.BREAK_BLOCKS)) {
setUser(User.getInstance(shooter));
if (!setUser(User.getInstance(shooter)).checkIsland(e, e.getBlock().getLocation(), Flags.BREAK_BLOCKS)) {
// Remove the arrow // Remove the arrow
projectile.remove(); projectile.remove();
e.setCancelled(true); e.setCancelled(true);
@ -59,7 +56,7 @@ public class TNTListener extends FlagListener {
&& e.getClickedBlock().getType().equals(Material.TNT) && e.getClickedBlock().getType().equals(Material.TNT)
&& e.getMaterial() != null && e.getMaterial() != null
&& e.getMaterial().equals(Material.FLINT_AND_STEEL)) { && e.getMaterial().equals(Material.FLINT_AND_STEEL)) {
checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS);
} }
} }

View File

@ -24,9 +24,9 @@ public class TeleportationListener extends FlagListener {
public void onPlayerTeleport(final PlayerTeleportEvent e) { public void onPlayerTeleport(final PlayerTeleportEvent e) {
if (e.getCause() != null) { if (e.getCause() != null) {
if (e.getCause().equals(TeleportCause.ENDER_PEARL)) { if (e.getCause().equals(TeleportCause.ENDER_PEARL)) {
checkIsland(e, e.getTo(), Flags.ENDER_PEARL); checkIsland(e, e.getPlayer(), e.getTo(), Flags.ENDER_PEARL);
} else if (e.getCause().equals(TeleportCause.CHORUS_FRUIT)) { } else if (e.getCause().equals(TeleportCause.CHORUS_FRUIT)) {
checkIsland(e, e.getTo(), Flags.CHORUS_FRUIT); checkIsland(e, e.getPlayer(), e.getTo(), Flags.CHORUS_FRUIT);
} }
} }
} }

View File

@ -8,7 +8,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.entity.ProjectileLaunchEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
@ -26,8 +25,7 @@ public class ThrowingListener extends FlagListener {
public void onPlayerThrowPotion(ProjectileLaunchEvent e) { public void onPlayerThrowPotion(ProjectileLaunchEvent e) {
if (e.getEntity().getShooter() instanceof Player if (e.getEntity().getShooter() instanceof Player
&& (e.getEntity() instanceof ThrownPotion || e.getEntity() instanceof ThrownExpBottle)) { && (e.getEntity() instanceof ThrownPotion || e.getEntity() instanceof ThrownExpBottle)) {
setUser(User.getInstance((Player)e.getEntity().getShooter())); checkIsland(e, (Player)e.getEntity().getShooter(), e.getEntity().getLocation(), Flags.POTION_THROWING);
checkIsland(e, e.getEntity().getLocation(), Flags.POTION_THROWING);
} }
} }
} }

View File

@ -72,7 +72,7 @@ public class PVPListener extends FlagListener {
// Get the attacker // Get the attacker
if (damager instanceof Player) { if (damager instanceof Player) {
User user = User.getInstance(damager); User user = User.getInstance(damager);
if (!setUser(user).checkIsland((Event)e, damager.getLocation(), flag)) { if (!checkIsland((Event)e, (Player)damager, damager.getLocation(), flag)) {
user.notify(Flags.PVP_OVERWORLD.getHintReference()); user.notify(Flags.PVP_OVERWORLD.getHintReference());
e.setCancelled(true); e.setCancelled(true);
} }
@ -86,7 +86,7 @@ public class PVPListener extends FlagListener {
return; return;
} }
User user = User.getInstance((Player)p.getShooter()); User user = User.getInstance((Player)p.getShooter());
if (!setUser(user).checkIsland((Event)e, damager.getLocation(), flag)) { if (!checkIsland((Event)e, (Player)entity, damager.getLocation(), flag)) {
damager.setFireTicks(0); damager.setFireTicks(0);
hurtEntity.setFireTicks(0); hurtEntity.setFireTicks(0);
damager.remove(); damager.remove();
@ -108,7 +108,7 @@ public class PVPListener extends FlagListener {
if (protectedVisitor((Player)e.getCaught())) { if (protectedVisitor((Player)e.getCaught())) {
User.getInstance(e.getPlayer()).notify(Flags.INVINCIBLE_VISITORS.getHintReference()); User.getInstance(e.getPlayer()).notify(Flags.INVINCIBLE_VISITORS.getHintReference());
e.setCancelled(true); e.setCancelled(true);
} else if (!checkIsland(e, e.getCaught().getLocation(), getFlag(e.getCaught().getWorld()))) { } else if (!checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), getFlag(e.getCaught().getWorld()))) {
e.getHook().remove(); e.getHook().remove();
User.getInstance(e.getPlayer()).notify(Flags.PVP_OVERWORLD.getHintReference()); User.getInstance(e.getPlayer()).notify(Flags.PVP_OVERWORLD.getHintReference());
e.setCancelled(true); e.setCancelled(true);
@ -149,7 +149,7 @@ public class PVPListener extends FlagListener {
return true; return true;
} }
// Check if PVP is allowed or not // Check if PVP is allowed or not
if (!checkIsland(e, le.getLocation(), flag)) { if (!checkIsland(e, user.getPlayer(), le.getLocation(), flag)) {
user.notify(Flags.PVP_OVERWORLD.getHintReference()); user.notify(Flags.PVP_OVERWORLD.getHintReference());
return true; return true;
} }

View File

@ -469,15 +469,15 @@ public class TestBentoBox {
// checking events - vistor // checking events - vistor
Event e3 = new BlockBreakEvent(block, visitorToIsland); Event e3 = new BlockBreakEvent(block, visitorToIsland);
Assert.assertFalse(fl.checkIsland(e3, location, Flags.BREAK_BLOCKS, true)); Assert.assertFalse(fl.checkIsland(e3, visitorToIsland, location, Flags.BREAK_BLOCKS, true));
// checking events - owner // checking events - owner
Event e = new BlockBreakEvent(block, ownerOfIsland); Event e = new BlockBreakEvent(block, ownerOfIsland);
Assert.assertTrue(fl.checkIsland(e, location, Flags.BREAK_BLOCKS, true)); Assert.assertTrue(fl.checkIsland(e, ownerOfIsland, location, Flags.BREAK_BLOCKS, true));
// checking events - member // checking events - member
Event e2 = new BlockBreakEvent(block, player); Event e2 = new BlockBreakEvent(block, player);
Assert.assertTrue(fl.checkIsland(e2, location, Flags.BREAK_BLOCKS, true)); Assert.assertTrue(fl.checkIsland(e2, player, location, Flags.BREAK_BLOCKS, true));
} }

View File

@ -196,7 +196,6 @@ public class EnderChestListenerTest {
// Enderchest use is okay // Enderchest use is okay
Flags.ENDER_CHEST.setSetting(world, true); Flags.ENDER_CHEST.setSetting(world, true);
BlockInteractionListener bil = new BlockInteractionListener(); BlockInteractionListener bil = new BlockInteractionListener();
bil.setUser(User.getInstance(player));
bil.onPlayerInteract(e); bil.onPlayerInteract(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
Mockito.verify(notifier, Mockito.never()).notify(Mockito.anyObject(), Mockito.anyString()); Mockito.verify(notifier, Mockito.never()).notify(Mockito.anyObject(), Mockito.anyString());