Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.60 KB | None | 0 0
  1. package net.briarcraft.sponge.zone;
  2.  
  3. import java.util.Optional;
  4.  
  5. import org.apache.commons.lang3.tuple.Pair;
  6. import org.slf4j.Logger;
  7. import org.spongepowered.api.block.BlockSnapshot;
  8. import org.spongepowered.api.entity.Entity;
  9. import org.spongepowered.api.entity.living.Villager;
  10. import org.spongepowered.api.entity.living.animal.Animal;
  11. import org.spongepowered.api.entity.living.golem.Golem;
  12. import org.spongepowered.api.entity.living.monster.Enderman;
  13. import org.spongepowered.api.entity.living.monster.Monster;
  14. import org.spongepowered.api.entity.living.player.Player;
  15. import org.spongepowered.api.event.Listener;
  16. import org.spongepowered.api.event.Order;
  17. import org.spongepowered.api.event.block.ChangeBlockEvent;
  18. import org.spongepowered.api.event.block.InteractBlockEvent;
  19. import org.spongepowered.api.event.entity.DisplaceEntityEvent;
  20. import org.spongepowered.api.event.entity.InteractEntityEvent;
  21. import org.spongepowered.api.event.entity.SpawnEntityEvent;
  22. import org.spongepowered.api.world.Location;
  23. import org.spongepowered.api.world.World;
  24.  
  25. import net.briarcraft.sponge.service.ZoneService;
  26. import net.briarcraft.sponge.util.EntityUtil;
  27.  
  28. public class ZoneEventListener {
  29.     private final Logger log;
  30.     private final ZoneService zoneService;
  31.  
  32.     public ZoneEventListener(final Logger log, final ZoneService zoneService) {
  33.         this.log = log;
  34.         this.zoneService = zoneService;
  35.     }
  36.  
  37.     @Listener(order = Order.FIRST)
  38.     public void onPlayer(final ChangeBlockEvent.Break event) {
  39.         final Optional<Player> player = event.getCause().first(Player.class);
  40.         if (player.isPresent()) {
  41.             zoneService.verifyTransactions(event, Zone.PERM_PLAYER_BLOCK_MODIFY, player.get());
  42.         }
  43.     }
  44.  
  45.     @Listener(order = Order.FIRST)
  46.     public void onPlayer(final ChangeBlockEvent.Place event) {
  47.         final Optional<Player> player = event.getCause().first(Player.class);
  48.         if (player.isPresent()) {
  49.             zoneService.verifyTransactions(event, Zone.PERM_PLAYER_BLOCK_MODIFY, player.get());
  50.         }
  51.     }
  52.  
  53.     @Listener(order = Order.FIRST)
  54.     public void onPlayer(final InteractBlockEvent event) {
  55.         final Optional<Player> player = event.getCause().first(Player.class);
  56.         if (player.isPresent()) {
  57.             final BlockSnapshot snapshot = event.getTargetBlock();
  58.             final Optional<Location<World>> location = snapshot.getLocation();
  59.             if (location.isPresent() && !zoneService.checkPlayerAndZonePermission(player.get(),
  60.                     Zone.PERM_PLAYER_BLOCK_INTERACT, location.get())) {
  61.                 event.setCancelled(true);
  62.             }
  63.         }
  64.     }
  65.  
  66.     @Listener(order = Order.FIRST)
  67.     public void onPlayer(final InteractEntityEvent.Primary event) {
  68.         final Optional<Player> player = event.getCause().first(Player.class);
  69.         if (player.isPresent()) {
  70.             final Entity entity = event.getTargetEntity();
  71.             final Location<World> location = entity.getLocation();
  72.             if (EntityUtil.isPlayer(entity)
  73.                     && !zoneService.checkPlayerAndZonePermission(player.get(),
  74.                             Zone.PERM_PLAYER_PLAYER_DAMAGE, location)) {
  75.                 event.setCancelled(true);
  76.             } else {
  77.                 if (EntityUtil.isMonster(entity)
  78.                         && !zoneService.checkPlayerAndZonePermission(player.get(),
  79.                                 Zone.PERM_PLAYER_MONSTER_DAMAGE, location)) {
  80.                     event.setCancelled(true);
  81.                 } else if (EntityUtil.isPassive(entity)
  82.                         && !zoneService.checkPlayerAndZonePermission(player.get(),
  83.                                 Zone.PERM_PLAYER_PASSIVE_DAMAGE, location)) {
  84.                     event.setCancelled(true);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     @Listener(order = Order.FIRST)
  91.     public void onPlayer(final InteractEntityEvent.Secondary event) {
  92.         final Optional<Player> player = event.getCause().first(Player.class);
  93.         if (player.isPresent()) {
  94.             final Entity entity = event.getTargetEntity();
  95.             final Location<World> location = entity.getLocation();
  96.             if (!EntityUtil.isPlayer(entity)) {
  97.                 if (EntityUtil.isPassive(entity)
  98.                         && !zoneService.checkPlayerAndZonePermission(player.get(),
  99.                                 Zone.PERM_PLAYER_PASSIVE_INTERACT, location)) {
  100.                     event.setCancelled(true);
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     // TODO need to test
  107.     @Listener(order = Order.FIRST)
  108.     public void onPlayer(final ChangeBlockEvent.Fluid event) {
  109.         final Optional<Player> player = event.getCause().first(Player.class);
  110.         if (player.isPresent()) {
  111.             zoneService.verifyTransactions(event, Zone.PERM_ENVIRONMENT_BLOCK_MODIFY, player.get());
  112.         }
  113.     }
  114.  
  115.     // TODO is this what allows spawn eggs?
  116.     @Listener(order = Order.FIRST)
  117.     public void onPlayer(final SpawnEntityEvent event) {
  118.         final Optional<Player> player = event.getCause().first(Player.class);
  119.         if (player.isPresent()) {
  120.             final Location<World> location = player.get().getLocation();
  121.             if (!zoneService.checkPlayerAndZonePermission(player.get(),
  122.                     Zone.PERM_ENVIRONMENT_PLAYER_ENTER, location)) {
  123.                 // Redirect to the world spawn location
  124.                 player.get().setLocation(location.getExtent().getSpawnLocation());
  125.             }
  126.         }
  127.     }
  128.  
  129.     @Listener(order = Order.FIRST)
  130.     public void onPlayer(final DisplaceEntityEvent event) {
  131.         final Entity entity = event.getTargetEntity();
  132.         if (EntityUtil.isPlayer(entity)) {
  133.             final Player player = (Player) entity;
  134.             final Location<World> fromLocation = event.getFromTransform().getLocation();
  135.             final Location<World> toLocation = event.getToTransform().getLocation();
  136.             final Optional<Pair<Optional<Zone>, Optional<Zone>>> transition = zoneService
  137.                     .getZoneTransition(fromLocation, toLocation);
  138.             if (transition.isPresent()) {
  139.                 // Check exit
  140.                 final Optional<Zone> fromZone = transition.get().getLeft();
  141.                 if (fromZone.isPresent() && !zoneService.checkPlayerAndZonePermission(player,
  142.                         Zone.PERM_ENVIRONMENT_PLAYER_EXIT, fromZone.get())) {
  143.                     event.setCancelled(true);
  144.                 }
  145.  
  146.                 // Check entry
  147.                 final Optional<Zone> toZone = transition.get().getRight();
  148.                 if (toZone.isPresent() && !zoneService.checkPlayerAndZonePermission(player,
  149.                         Zone.PERM_ENVIRONMENT_PLAYER_ENTER, toZone.get())) {
  150.                     event.setCancelled(true);
  151.                 }
  152.             }
  153.         }
  154.     }
  155.  
  156.     @Listener(order = Order.FIRST)
  157.     public void onMonster(final ChangeBlockEvent event) {
  158.         final Optional<Enderman> enderman = event.getCause().first(Enderman.class);
  159.         if (enderman.isPresent()) {
  160.             zoneService.verifyTransactions(event, Zone.PERM_MONSTER_BLOCK_MODIFY);
  161.         }
  162.     }
  163.  
  164.     // TODO this doesn't work
  165.     @Listener(order = Order.FIRST)
  166.     public void onMonster(final InteractEntityEvent event) {
  167.         final Optional<Monster> monster = event.getCause().first(Monster.class);
  168.         if (monster.isPresent()) {
  169.             final Entity entity = event.getTargetEntity();
  170.             final Location<World> location = entity.getLocation();
  171.             if (EntityUtil.isPlayer(entity)) {
  172.                 if (!zoneService.checkZonePermission(Zone.PERM_MONSTER_PLAYER_DAMAGE, location)) {
  173.                     event.setCancelled(true);
  174.                 }
  175.             } else if (EntityUtil.isPassive(entity)) {
  176.                 if (!zoneService.checkZonePermission(Zone.PERM_MONSTER_PASSIVE_DAMAGE, location)) {
  177.                     event.setCancelled(true);
  178.                 }
  179.             }
  180.         }
  181.     }
  182.  
  183.     // TODO doesn't prevent spawn eggs
  184.     // TODO spawn in over time
  185.     @Listener(order = Order.FIRST)
  186.     public void onMonster(final SpawnEntityEvent event) {
  187.         final Optional<Monster> monster = event.getCause().first(Monster.class);
  188.         if (monster.isPresent()) {
  189.             final Location<World> location = monster.get().getLocation();
  190.             if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_MONSTER_ENTER, location)) {
  191.                 event.setCancelled(true);
  192.             }
  193.         }
  194.     }
  195.  
  196.     // TODO this doesn't work
  197.     @Listener(order = Order.FIRST)
  198.     public void onMonster(final DisplaceEntityEvent event) {
  199.         final Entity entity = event.getTargetEntity();
  200.         if (EntityUtil.isMonster(entity)) {
  201.             final Location<World> fromLocation = event.getFromTransform().getLocation();
  202.             final Location<World> toLocation = event.getToTransform().getLocation();
  203.             final Optional<Pair<Optional<Zone>, Optional<Zone>>> transition = zoneService
  204.                     .getZoneTransition(fromLocation, toLocation);
  205.             if (transition.isPresent()) {
  206.                 // Check exit
  207.                 final Optional<Zone> fromZone = transition.get().getLeft();
  208.                 if (fromZone.isPresent() && !zoneService
  209.                         .checkZonePermission(Zone.PERM_ENVIRONMENT_MONSTER_EXIT, fromZone.get())) {
  210.                     event.setCancelled(true);
  211.                 }
  212.  
  213.                 // Check entry
  214.                 final Optional<Zone> toZone = transition.get().getRight();
  215.                 if (toZone.isPresent() && !zoneService
  216.                         .checkZonePermission(Zone.PERM_ENVIRONMENT_MONSTER_ENTER, toZone.get())) {
  217.                     event.setCancelled(true);
  218.                 }
  219.             }
  220.         }
  221.     }
  222.  
  223.     // TODO doesn't prevent spawn eggs
  224.     // TODO spawn in over time
  225.     @Listener(order = Order.FIRST)
  226.     public void onAnimal(final SpawnEntityEvent event) {
  227.         final Optional<Animal> animal = event.getCause().first(Animal.class);
  228.         if (animal.isPresent()) {
  229.             final Location<World> location = animal.get().getLocation();
  230.             if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, location)) {
  231.                 event.setCancelled(true);
  232.             }
  233.         }
  234.     }
  235.  
  236.     // TODO doesn't prevent spawn eggs
  237.     // TODO spawn in over time
  238.     @Listener(order = Order.FIRST)
  239.     public void onGolem(final SpawnEntityEvent event) {
  240.         final Optional<Golem> golem = event.getCause().first(Golem.class);
  241.         if (golem.isPresent()) {
  242.             final Location<World> location = golem.get().getLocation();
  243.             if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, location)) {
  244.                 event.setCancelled(true);
  245.             }
  246.         }
  247.     }
  248.  
  249.     // TODO doesn't prevent spawn eggs
  250.     // TODO spawn in over time
  251.     @Listener(order = Order.FIRST)
  252.     public void onVillager(final SpawnEntityEvent event) {
  253.         final Optional<Villager> villager = event.getCause().first(Villager.class);
  254.         if (villager.isPresent()) {
  255.             final Location<World> location = villager.get().getLocation();
  256.             if (!zoneService.checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, location)) {
  257.                 event.setCancelled(true);
  258.             }
  259.         }
  260.     }
  261.  
  262.     // TODO need to test
  263.     @Listener(order = Order.FIRST)
  264.     public void onPassive(final DisplaceEntityEvent event) {
  265.         final Entity entity = event.getTargetEntity();
  266.         if (EntityUtil.isPassive(entity)) {
  267.             final Location<World> fromLocation = event.getFromTransform().getLocation();
  268.             final Location<World> toLocation = event.getToTransform().getLocation();
  269.             final Optional<Pair<Optional<Zone>, Optional<Zone>>> transition = zoneService
  270.                     .getZoneTransition(fromLocation, toLocation);
  271.             if (transition.isPresent()) {
  272.                 // Check exit
  273.                 final Optional<Zone> fromZone = transition.get().getLeft();
  274.                 if (fromZone.isPresent() && !zoneService
  275.                         .checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_EXIT, fromZone.get())) {
  276.                     event.setCancelled(true);
  277.                 }
  278.  
  279.                 // Check entry
  280.                 final Optional<Zone> toZone = transition.get().getRight();
  281.                 if (toZone.isPresent() && !zoneService
  282.                         .checkZonePermission(Zone.PERM_ENVIRONMENT_PASSIVE_ENTER, toZone.get())) {
  283.                     event.setCancelled(true);
  284.                 }
  285.             }
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement