Greenhouses/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseGuard.java
BONNe a96d4d4c67
Release 1.7.0 (#91)
* Version 1.6.1

* Version 1.6.1

* Added Blue Orchids spawning in the Swamp biome

* Version 1.7.0

1.18.1 update

* German translation (#86)

* Translate de.yml via GitLocalize

* Translate de.yml via GitLocalize

Co-authored-by: mt-gitlocalize <mt@gitlocalize.com>
Co-authored-by: xXjojojXx <sven.f92@gmx.de>

* Translate hu.yml via GitLocalize (#87)

Co-authored-by: driverdakid <tamascsiszar99@icloud.com>

* Translate ja.yml via GitLocalize (#88)

Co-authored-by: tastybento <tastybento@wasteofplastic.com>

* Translate zh-CN.yml via GitLocalize (#89)

Co-authored-by: tastybento <tastybento@wasteofplastic.com>

* Code refactoring around BoundingBox

* Fix test.

* Remove saving of greenhouses on exit.

https://github.com/BentoBoxWorld/Greenhouses/issues/85

* Add Pladdon Class (#90)

* Add Pladdon Class

Add Greenhouses Pladdon class.

* Add Spigot Annotations API

* Fix test. Avoid ambiguity.

* Add natural spawn protection listener.

Fixes #84

* Update gitignore for Eclipse

* Remove some code smells.

Co-authored-by: tastybento <tastybento@wasteofplastic.com>
Co-authored-by: Florian CUNY <poslovitch@bentobox.world>
Co-authored-by: gitlocalize-app[bot] <55277160+gitlocalize-app[bot]@users.noreply.github.com>
Co-authored-by: mt-gitlocalize <mt@gitlocalize.com>
Co-authored-by: xXjojojXx <sven.f92@gmx.de>
Co-authored-by: driverdakid <tamascsiszar99@icloud.com>
2022-05-16 14:49:06 +03:00

104 lines
3.5 KiB
Java

package world.bentobox.greenhouses.listeners;
import java.util.Optional;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.data.Greenhouse;
public class GreenhouseGuard implements Listener {
private final Greenhouses addon;
public GreenhouseGuard(final Greenhouses addon) {
this.addon = addon;
}
// Stop lava flow or water into or out of a greenhouse
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onFlow(final BlockFromToEvent e) {
// Flow may be allowed anyway
if (addon.getSettings().isAllowFlowIn() && addon.getSettings().isAllowFlowOut()) {
return;
}
if (!addon.getActiveWorlds().contains(e.getBlock().getWorld())) {
return;
}
// Get To and From
Optional<Greenhouse> to = addon.getManager().getMap().getGreenhouse(e.getToBlock().getLocation());
Optional<Greenhouse> from = addon.getManager().getMap().getGreenhouse(e.getBlock().getLocation());
// Scenarios
// 1. inside district or outside - always ok
// 2. inside to outside - allowFlowOut determines
// 3. outside to inside - allowFlowIn determines
if (to.isEmpty() && from.isEmpty()) {
return;
}
if (to.isPresent() && from.isPresent() && to.equals(from)) {
return;
}
// to is a greenhouse
if (to.isPresent() && addon.getSettings().isAllowFlowIn()) {
return;
}
// from is a greenhouse
if (from.isPresent() && addon.getSettings().isAllowFlowOut()) {
return;
}
// Otherwise cancel - the flow is not allowed
e.setCancelled(true);
}
/**
* 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)
.anyMatch(this::inGreenhouse));
}
/**
* 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)
.anyMatch(this::inGreenhouse));
}
/**
* 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()));
}
}
private boolean inGreenhouse(Location l) {
return addon.getManager().getMap().getGreenhouse(l).map(g -> g.isRoofOrWallBlock(l)).orElse(false);
}
}