Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.briarcraft.sponge.zone;
- import java.util.Optional;
- import org.apache.commons.lang3.tuple.Pair;
- import org.slf4j.Logger;
- import org.spongepowered.api.block.BlockSnapshot;
- import org.spongepowered.api.entity.Entity;
- import org.spongepowered.api.entity.living.Villager;
- import org.spongepowered.api.entity.living.animal.Animal;
- import org.spongepowered.api.entity.living.golem.Golem;
- import org.spongepowered.api.entity.living.monster.Enderman;
- import org.spongepowered.api.entity.living.monster.Monster;
- import org.spongepowered.api.entity.living.player.Player;
- import org.spongepowered.api.event.Listener;
- import org.spongepowered.api.event.Order;
- import org.spongepowered.api.event.block.ChangeBlockEvent;
- import org.spongepowered.api.event.block.InteractBlockEvent;
- import org.spongepowered.api.event.entity.DisplaceEntityEvent;
- import org.spongepowered.api.event.entity.InteractEntityEvent;
- import org.spongepowered.api.event.entity.SpawnEntityEvent;
- import org.spongepowered.api.world.Location;
- import org.spongepowered.api.world.World;
- import net.briarcraft.sponge.service.ZoneService;
- import net.briarcraft.sponge.util.EntityUtil;
- public class ZoneEventListener {
- private final Logger log;
- private final ZoneService zoneService;
- public ZoneEventListener(final Logger log, final ZoneService zoneService) {
- this.log = log;
- this.zoneService = zoneService;
- }
- @Listener(order = Order.FIRST)
- public void onPlayer(final ChangeBlockEvent.Break event) {
- final Optional<Player> player = event.getCause().first(Player.class);
- if (player.isPresent()) {
- zoneService.verifyTransactions(event, Zone.PERM_PLAYER_BLOCK_MODIFY, player.get());
- }
- }
- @Listener(order = Order.FIRST)
- public void onPlayer(final ChangeBlockEvent.Place event) {
- final Optional<Player> player = event.getCause().first(Player.class);
- if (player.isPresent()) {
- zoneService.verifyTransactions(event, Zone.PERM_PLAYER_BLOCK_MODIFY, player.get());
- }
- }
- @Listener(order = Order.FIRST)
- public void onPlayer(final InteractBlockEvent event) {
- final Optional<Player> player = event.getCause().first(Player.class);
- if (player.isPresent()) {
- final BlockSnapshot snapshot = event.getTargetBlock();
- final Optional<Location<World>> location = snapshot.getLocation();
- if (location.isPresent() && !zoneService.checkPlayerAndZonePermission(player.get(),
- Zone.PERM_PLAYER_BLOCK_INTERACT, location.get())) {
- event.setCancelled(true);
- }
- }
- }
- @Listener(order = Order.FIRST)
- public void onPlayer(final InteractEntityEvent.Primary event) {
- final Optional<Player> player = event.getCause().first(Player.class);
- if (player.isPresent()) {
- final Entity entity = event.getTargetEntity();
- final Location<World> location = entity.getLocation();
- if (EntityUtil.isPlayer(entity)
- && !zoneService.checkPlayerAndZonePermission(player.get(),
- Zone.PERM_PLAYER_PLAYER_DAMAGE, location)) {
- event.setCancelled(true);
- } else {
- if (EntityUtil.isMonster(entity)
- && !zoneService.checkPlayerAndZonePermission(player.get(),
- Zone.PERM_PLAYER_MONSTER_DAMAGE, location)) {
- event.setCancelled(true);
- } else if (EntityUtil.isPassive(entity)
- && !zoneService.checkPlayerAndZonePermission(player.get(),
- Zone.PERM_PLAYER_PASSIVE_DAMAGE, location)) {
- event.setCancelled(true);
- }
- }
- }
- }
- @Listener(order = Order.FIRST)
- public void onPlayer(final InteractEntityEvent.Secondary event) {
- final Optional<Player> player = event.getCause().first(Player.class);
- if (player.isPresent()) {
- final Entity entity = event.getTargetEntity();
- final Location<World> location = entity.getLocation();
- if (!EntityUtil.isPlayer(entity)) {
- if (EntityUtil.isPassive(entity)
- && !zoneService.checkPlayerAndZonePermission(player.get(),
- Zone.PERM_PLAYER_PASSIVE_INTERACT, location)) {
- event.setCancelled(true);
- }
- }
- }
- }
- // TODO need to test
- @Listener(order = Order.FIRST)
- public void onPlayer(final ChangeBlockEvent.Fluid event) {
- final Optional<Player> player = event.getCause().first(Player.class);
- if (player.isPresent()) {
- zoneService.verifyTransactions(event, Zone.PERM_ENVIRONMENT_BLOCK_MODIFY, player.get());
- }
- }
- // TODO is this what allows spawn eggs?
- @Listener(order = Order.FIRST)
- public void onPlayer(final SpawnEntityEvent event) {
- final Optional<Player> player = event.getCause().first(Player.class);
- if (player.isPresent()) {
- final Location<World> location = player.get().getLocation();
- if (!zoneService.checkPlayerAndZonePermission(player.get(),
- Zone.PERM_ENVIRONMENT_PLAYER_ENTER, location)) {
- // Redirect to the world spawn location
- player.get().setLocation(location.getExtent().getSpawnLocation());
- }
- }
- }
- @Listener(order = Order.FIRST)
- public void onPlayer(final DisplaceEntityEvent event) {
- final Entity entity = event.getTargetEntity();
- if (EntityUtil.isPlayer(entity)) {
- final Player player = (Player) entity;
- final Location<World> fromLocation = event.getFromTransform().getLocation();
- final Location<World> toLocation = event.getToTransform().getLocation();
- final Optional<Pair<Optional<Zone>, Optional<Zone>>> transition = zoneService
- .getZoneTransition(fromLocation, toLocation);
- if (transition.isPresent()) {
- // Check exit
- final Optional<Zone> fromZone = transition.get().getLeft();
- if (fromZone.isPresent() && !zoneService.checkPlayerAndZonePermission(player,
- Zone.PERM_ENVIRONMENT_PLAYER_EXIT, fromZone.get())) {
- event.setCancelled(true);
- }
- // Check entry
- final Optional<Zone> toZone = transition.get().getRight();
- if (toZone.isPresent() && !zoneService.checkPlayerAndZonePermission(player,
- Zone.PERM_ENVIRONMENT_PLAYER_ENTER, toZone.get())) {
- event.setCancelled(true);
- }
- }
- }
- }
- @Listener(order = Order.FIRST)
- public void onMonster(final ChangeBlockEvent event) {
- final Optional<Enderman> enderman = event.getCause().first(Enderman.class);
- if (enderman.isPresent()) {
- zoneService.verifyTransactions(event, Zone.PERM_MONSTER_BLOCK_MODIFY);
- }
- }
- // TODO this doesn't work
- @Listener(order = Order.FIRST)
- public void onMonster(final InteractEntityEvent event) {
- final Optional<Monster> monster = event.getCause().first(Monster.class);
- if (monster.isPresent()) {
- final Entity entity = event.getTargetEntity();
- final Location<World> location = entity.getLocation();
- if (EntityUtil.isPlayer(entity)) {
- if (!zoneService.checkZonePermission(Zone.PERM_MONSTER_PLAYER_DAMAGE, location)) {
- event.setCancelled(true);
- }
- } else if (EntityUtil.isPassive(entity)) {
- if (!zoneService.checkZonePermission(Zone.PERM_MONSTER_PASSIVE_DAMAGE, location)) {
- event.setCancelled(true);
- }
- }
- }
- }
- // TODO doesn't prevent spawn eggs
- // TODO spawn in over time
- @Listener(order = Order.FIRST)
- public void onMonster(final SpawnEntityEvent event) {
- final Optional<Monster> monster = event.getCause().first(Monster.class);
- if (monster.isPresent()) {
- final Location<World> location = monster.get().getLocation();
- if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_MONSTER_ENTER, location)) {
- event.setCancelled(true);
- }
- }
- }
- // TODO this doesn't work
- @Listener(order = Order.FIRST)
- public void onMonster(final DisplaceEntityEvent event) {
- final Entity entity = event.getTargetEntity();
- if (EntityUtil.isMonster(entity)) {
- final Location<World> fromLocation = event.getFromTransform().getLocation();
- final Location<World> toLocation = event.getToTransform().getLocation();
- final Optional<Pair<Optional<Zone>, Optional<Zone>>> transition = zoneService
- .getZoneTransition(fromLocation, toLocation);
- if (transition.isPresent()) {
- // Check exit
- final Optional<Zone> fromZone = transition.get().getLeft();
- if (fromZone.isPresent() && !zoneService
- .checkZonePermission(Zone.PERM_ENVIRONMENT_MONSTER_EXIT, fromZone.get())) {
- event.setCancelled(true);
- }
- // Check entry
- final Optional<Zone> toZone = transition.get().getRight();
- if (toZone.isPresent() && !zoneService
- .checkZonePermission(Zone.PERM_ENVIRONMENT_MONSTER_ENTER, toZone.get())) {
- event.setCancelled(true);
- }
- }
- }
- }
- // TODO doesn't prevent spawn eggs
- // TODO spawn in over time
- @Listener(order = Order.FIRST)
- public void onAnimal(final SpawnEntityEvent event) {
- final Optional<Animal> animal = event.getCause().first(Animal.class);
- if (animal.isPresent()) {
- final Location<World> location = animal.get().getLocation();
- if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, location)) {
- event.setCancelled(true);
- }
- }
- }
- // TODO doesn't prevent spawn eggs
- // TODO spawn in over time
- @Listener(order = Order.FIRST)
- public void onGolem(final SpawnEntityEvent event) {
- final Optional<Golem> golem = event.getCause().first(Golem.class);
- if (golem.isPresent()) {
- final Location<World> location = golem.get().getLocation();
- if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, location)) {
- event.setCancelled(true);
- }
- }
- }
- // TODO doesn't prevent spawn eggs
- // TODO spawn in over time
- @Listener(order = Order.FIRST)
- public void onVillager(final SpawnEntityEvent event) {
- final Optional<Villager> villager = event.getCause().first(Villager.class);
- if (villager.isPresent()) {
- final Location<World> location = villager.get().getLocation();
- if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, location)) {
- event.setCancelled(true);
- }
- }
- }
- // TODO need to test
- @Listener(order = Order.FIRST)
- public void onPassive(final DisplaceEntityEvent event) {
- final Entity entity = event.getTargetEntity();
- if (EntityUtil.isPassive(entity)) {
- final Location<World> fromLocation = event.getFromTransform().getLocation();
- final Location<World> toLocation = event.getToTransform().getLocation();
- final Optional<Pair<Optional<Zone>, Optional<Zone>>> transition = zoneService
- .getZoneTransition(fromLocation, toLocation);
- if (transition.isPresent()) {
- // Check exit
- final Optional<Zone> fromZone = transition.get().getLeft();
- if (fromZone.isPresent() && !zoneService
- .checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_EXIT, fromZone.get())) {
- event.setCancelled(true);
- }
- // Check entry
- final Optional<Zone> toZone = transition.get().getRight();
- if (toZone.isPresent() && !zoneService
- .checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, toZone.get())) {
- event.setCancelled(true);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement