2019-02-03 10:22:44 +01:00
|
|
|
package world.bentobox.bentobox.listeners.flags.protection;
|
2018-02-02 05:30:57 +01:00
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
import org.bukkit.Location;
|
2018-02-02 05:30:57 +01:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
2018-02-20 23:06:09 +01:00
|
|
|
import org.bukkit.event.Cancellable;
|
2018-02-02 05:30:57 +01:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.block.Action;
|
|
|
|
import org.bukkit.event.block.BlockBurnEvent;
|
|
|
|
import org.bukkit.event.block.BlockIgniteEvent;
|
|
|
|
import org.bukkit.event.block.BlockSpreadEvent;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
2020-06-03 03:28:56 +02:00
|
|
|
import org.bukkit.event.weather.LightningStrikeEvent;
|
2018-02-02 05:30:57 +01:00
|
|
|
import org.bukkit.util.BlockIterator;
|
2019-07-05 01:54:05 +02:00
|
|
|
|
2018-07-31 18:03:32 +02:00
|
|
|
import world.bentobox.bentobox.api.flags.Flag;
|
2018-11-10 20:45:18 +01:00
|
|
|
import world.bentobox.bentobox.api.flags.FlagListener;
|
2018-07-31 18:03:32 +02:00
|
|
|
import world.bentobox.bentobox.lists.Flags;
|
2018-02-02 05:30:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles fire
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2018-11-04 16:40:20 +01:00
|
|
|
public class FireListener extends FlagListener {
|
2018-02-02 05:30:57 +01:00
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
/**
|
|
|
|
* Checks if fire is allowed. If not, cancels the action
|
|
|
|
* @param e - cancellable event
|
|
|
|
* @param l - location
|
|
|
|
* @param flag - flag to check
|
|
|
|
* @return - true if cancelled, false if not
|
|
|
|
*/
|
2018-02-25 16:01:30 +01:00
|
|
|
public boolean checkFire(Cancellable e, Location l, Flag flag) {
|
2018-02-20 23:06:09 +01:00
|
|
|
// Check world
|
2018-06-20 07:30:23 +02:00
|
|
|
if (!getIWM().inWorld(l)) {
|
2018-02-20 23:06:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check if the island exists and if fire is allowed
|
2021-01-09 05:05:23 +01:00
|
|
|
boolean cancel = getIslands().getIslandAt(l).map(i -> !i.isAllowed(flag)).orElseGet(() -> !flag.isSetForWorld(l.getWorld()));
|
2018-02-20 23:06:09 +01:00
|
|
|
e.setCancelled(cancel);
|
|
|
|
return cancel;
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:30:57 +01:00
|
|
|
/**
|
|
|
|
* Prevents fire spread
|
2018-02-18 02:01:25 +01:00
|
|
|
* @param e - event
|
2018-02-02 05:30:57 +01:00
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2022-01-22 21:16:27 +01:00
|
|
|
public void onBlockBurn(BlockBurnEvent e) {
|
|
|
|
checkFire(e, e.getBlock().getLocation(), Flags.FIRE_BURNING);
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent fire spread
|
2018-02-18 02:01:25 +01:00
|
|
|
* @param e - event
|
2018-02-02 05:30:57 +01:00
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2022-01-22 21:16:27 +01:00
|
|
|
public void onBlockSpread(BlockSpreadEvent e) {
|
|
|
|
if (e.getSource().getType().equals(Material.FIRE)) {
|
|
|
|
checkFire(e, e.getBlock().getLocation(), Flags.FIRE_SPREAD);
|
|
|
|
}
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Igniting fires
|
2018-02-18 02:01:25 +01:00
|
|
|
* @param e - event
|
2018-02-02 05:30:57 +01:00
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2022-01-22 21:16:27 +01:00
|
|
|
public void onBlockIgnite(BlockIgniteEvent e) {
|
2018-02-02 05:30:57 +01:00
|
|
|
// Check if this is a portal lighting - that is allowed any time
|
2022-01-22 21:16:27 +01:00
|
|
|
if (!e.getBlock().getType().equals(Material.OBSIDIAN)) {
|
|
|
|
checkFire(e, e.getBlock().getLocation(), Flags.FIRE_IGNITE);
|
|
|
|
}
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-18 02:01:25 +01:00
|
|
|
* Flint and Steel and Extinguishing fire
|
|
|
|
* @param e - event
|
2018-02-02 05:30:57 +01:00
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
|
|
|
public void onPlayerInteract(PlayerInteractEvent e) {
|
2019-03-24 15:47:25 +01:00
|
|
|
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)
|
2019-02-19 10:56:11 +01:00
|
|
|
&& (e.getMaterial() == Material.FLINT_AND_STEEL || e.getMaterial() == Material.FIRE_CHARGE)) {
|
|
|
|
checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.FLINT_AND_STEEL);
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|
|
|
|
// Look along player's sight line to see if any blocks are fire. Players can hit fire out quite a long way away.
|
|
|
|
try {
|
|
|
|
BlockIterator iter = new BlockIterator(e.getPlayer(), 10);
|
|
|
|
while (iter.hasNext()) {
|
2018-02-20 23:06:09 +01:00
|
|
|
Block lastBlock = iter.next();
|
2018-02-02 05:30:57 +01:00
|
|
|
if (lastBlock.getType().equals(Material.FIRE)) {
|
2019-02-13 07:55:36 +01:00
|
|
|
checkIsland(e, e.getPlayer(), lastBlock.getLocation(), Flags.FIRE_EXTINGUISH);
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
// To catch at block iterator exceptions that can happen in the void or at the very top of blocks
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 03:28:56 +02:00
|
|
|
|
2022-01-22 21:16:27 +01:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
2020-06-03 03:28:56 +02:00
|
|
|
public void onLightningStrikeEvent(LightningStrikeEvent e) {
|
|
|
|
checkFire(e, e.getLightning().getLocation(), Flags.FIRE_IGNITE);
|
|
|
|
}
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|