2019-01-19 16:52:04 +01:00
|
|
|
package world.bentobox.greenhouses.listeners;
|
|
|
|
|
2019-01-22 00:44:01 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2021-02-18 01:49:42 +01:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.block.Block;
|
2019-01-19 16:52:04 +01:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.block.BlockFromToEvent;
|
2021-02-18 01:49:42 +01:00
|
|
|
import org.bukkit.event.block.BlockPistonExtendEvent;
|
2021-02-19 02:09:42 +01:00
|
|
|
import org.bukkit.event.block.BlockPistonRetractEvent;
|
2022-02-27 20:33:21 +01:00
|
|
|
import org.bukkit.event.entity.CreatureSpawnEvent;
|
2019-01-19 16:52:04 +01:00
|
|
|
|
|
|
|
import world.bentobox.greenhouses.Greenhouses;
|
2019-01-22 00:44:01 +01:00
|
|
|
import world.bentobox.greenhouses.data.Greenhouse;
|
2019-01-19 16:52:04 +01:00
|
|
|
|
|
|
|
public class GreenhouseGuard implements Listener {
|
2019-01-26 20:10:30 +01:00
|
|
|
private final Greenhouses addon;
|
2019-01-19 16:52:04 +01:00
|
|
|
|
2019-01-26 20:10:30 +01:00
|
|
|
public GreenhouseGuard(final Greenhouses addon) {
|
|
|
|
this.addon = addon;
|
2019-01-19 16:52:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop lava flow or water into or out of a greenhouse
|
2021-02-18 01:49:42 +01:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
2019-01-19 16:52:04 +01:00
|
|
|
public void onFlow(final BlockFromToEvent e) {
|
2019-01-22 00:44:01 +01:00
|
|
|
// Flow may be allowed anyway
|
2019-01-26 20:10:30 +01:00
|
|
|
if (addon.getSettings().isAllowFlowIn() && addon.getSettings().isAllowFlowOut()) {
|
2019-01-22 00:44:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-01-26 20:10:30 +01:00
|
|
|
if (!addon.getActiveWorlds().contains(e.getBlock().getWorld())) {
|
2019-01-22 00:44:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Get To and From
|
2019-01-26 20:10:30 +01:00
|
|
|
Optional<Greenhouse> to = addon.getManager().getMap().getGreenhouse(e.getToBlock().getLocation());
|
|
|
|
Optional<Greenhouse> from = addon.getManager().getMap().getGreenhouse(e.getBlock().getLocation());
|
2019-01-22 00:44:01 +01:00
|
|
|
// Scenarios
|
|
|
|
// 1. inside district or outside - always ok
|
|
|
|
// 2. inside to outside - allowFlowOut determines
|
|
|
|
// 3. outside to inside - allowFlowIn determines
|
2021-08-01 08:10:07 +02:00
|
|
|
if (to.isEmpty() && from.isEmpty()) {
|
2019-01-22 00:44:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (to.isPresent() && from.isPresent() && to.equals(from)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// to is a greenhouse
|
2019-01-26 20:10:30 +01:00
|
|
|
if (to.isPresent() && addon.getSettings().isAllowFlowIn()) {
|
2019-01-22 00:44:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// from is a greenhouse
|
2019-01-26 20:10:30 +01:00
|
|
|
if (from.isPresent() && addon.getSettings().isAllowFlowOut()) {
|
2019-01-22 00:44:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Otherwise cancel - the flow is not allowed
|
|
|
|
e.setCancelled(true);
|
2019-01-19 16:52:04 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 01:49:42 +01:00
|
|
|
/**
|
|
|
|
* Prevents pistons from pushing greenhouse wall or roof blocks
|
|
|
|
* @param e - event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
|
|
|
public void onPistonPush(BlockPistonExtendEvent e) {
|
|
|
|
e.setCancelled(e.getBlocks().stream()
|
|
|
|
.map(Block::getLocation)
|
2021-08-01 08:10:07 +02:00
|
|
|
.anyMatch(this::inGreenhouse));
|
2021-02-18 01:49:42 +01:00
|
|
|
}
|
2019-01-19 16:52:04 +01:00
|
|
|
|
2021-02-19 02:09:42 +01:00
|
|
|
/**
|
|
|
|
* Prevents sticky pistons from pulling greenhouse wall or roof blocks
|
|
|
|
* @param e - event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
|
|
|
public void onPistonPull(BlockPistonRetractEvent e) {
|
|
|
|
e.setCancelled(e.getBlocks().stream()
|
|
|
|
.map(Block::getLocation)
|
2021-08-01 08:10:07 +02:00
|
|
|
.anyMatch(this::inGreenhouse));
|
2021-02-19 02:09:42 +01:00
|
|
|
}
|
|
|
|
|
2022-02-27 20:33:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Guard Greenhouse from natural entity spawning.
|
|
|
|
*
|
|
|
|
* @param spawnEvent the spawn event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
|
|
|
public void onCreatureSpawn(CreatureSpawnEvent spawnEvent)
|
|
|
|
{
|
|
|
|
if (CreatureSpawnEvent.SpawnReason.NATURAL == spawnEvent.getSpawnReason())
|
|
|
|
{
|
|
|
|
// Natural spawn events should be cancelled. Greenhouse spawns its own mobs.
|
|
|
|
spawnEvent.setCancelled(this.inGreenhouse(spawnEvent.getLocation()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-18 01:49:42 +01:00
|
|
|
private boolean inGreenhouse(Location l) {
|
|
|
|
return addon.getManager().getMap().getGreenhouse(l).map(g -> g.isRoofOrWallBlock(l)).orElse(false);
|
|
|
|
}
|
2019-01-19 16:52:04 +01:00
|
|
|
}
|
|
|
|
|