-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out the listener class from ItemsAdderHook.
The embedded class was hard to mock when running Level addon tests.
- Loading branch information
1 parent
1447290
commit be672f4
Showing
2 changed files
with
58 additions
and
51 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/world/bentobox/bentobox/hooks/BlockInteractListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package world.bentobox.bentobox.hooks; | ||
|
||
import org.bukkit.Location; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.entity.EntityExplodeEvent; | ||
|
||
import world.bentobox.bentobox.BentoBox; | ||
import world.bentobox.bentobox.api.flags.FlagListener; | ||
import world.bentobox.bentobox.api.user.User; | ||
|
||
/** | ||
* Listens for changes to the ItemsAdder flag | ||
*/ | ||
public class BlockInteractListener extends FlagListener { | ||
|
||
/** | ||
* Handles explosions of ItemAdder items | ||
* @param event explosion event | ||
*/ | ||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) | ||
public void onExplosion(EntityExplodeEvent event) { | ||
if (!EntityType.PLAYER.equals(event.getEntityType())) { | ||
// Ignore non-player explosions. | ||
return; | ||
} | ||
|
||
Player player = (Player) event.getEntity(); | ||
|
||
if (!player.hasPermission("XXXXXX")) { | ||
// Ignore players that does not have magic XXXXXX permission. | ||
return; | ||
} | ||
|
||
// Use BentoBox flag processing system to validate usage. | ||
// Technically not necessary as internally it should be cancelled by BentoBox. | ||
|
||
if (!this.checkIsland(event, player, event.getLocation(), ItemsAdderHook.ITEMS_ADDER_EXPLOSIONS)) { | ||
// Remove any blocks from the explosion list if required | ||
event.blockList().removeIf(block -> this.protect(player, block.getLocation())); | ||
event.setCancelled(this.protect(player, event.getLocation())); | ||
} | ||
} | ||
|
||
/** | ||
* This method returns if the protection in given location is enabled or not. | ||
* @param player Player who triggers explosion. | ||
* @param location Location where explosion happens. | ||
* @return {@code true} if location is protected, {@code false} otherwise. | ||
*/ | ||
private boolean protect(Player player, Location location) { | ||
return BentoBox.getInstance().getIslands().getProtectedIslandAt(location) | ||
.map(island -> !island.isAllowed(User.getInstance(player), ItemsAdderHook.ITEMS_ADDER_EXPLOSIONS)) | ||
.orElse(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters