/** @param event */
  @EventHandler(priority = EventPriority.HIGH)
  public void onVehicleDestroy(VehicleDestroyEvent event) {
    Vehicle vehicle = event.getVehicle();

    Field field =
        plugin
            .getForceFieldManager()
            .getEnabledSourceField(vehicle.getLocation(), FieldFlag.PREVENT_VEHICLE_DESTROY);

    if (field != null) {
      if (event.getAttacker() instanceof Player) {
        Player player = (Player) event.getAttacker();

        if (FieldFlag.PREVENT_VEHICLE_DESTROY.applies(field, player)) {
          if (plugin.getPermissionsManager().has(player, "preciousstones.bypass.destroy")) {
            plugin.getCommunicationManager().notifyBypassDestroyVehicle(player, vehicle, field);
          } else {
            event.setCancelled(true);
            plugin.getCommunicationManager().warnDestroyVehicle(player, vehicle, field);
            return;
          }
        }
      }
    }
  }
Exemplo n.º 2
0
  // set targetLoc only if not current player location; set returnLocationOnly to true to have new
  // Location returned if they need to be moved to one, instead of directly handling it
  public static Location checkPlayer(
      Player player, Location targetLoc, boolean returnLocationOnly) {
    if (player == null || !player.isOnline()) return null;

    Location loc = (targetLoc == null) ? player.getLocation() : targetLoc;
    if (loc == null) return null;

    World world = loc.getWorld();
    if (world == null) return null;
    BorderData border = Config.Border(world.getName());
    if (border == null) return null;

    if (border.insideBorder(loc.getX(), loc.getZ(), Config.ShapeRound())) return null;

    if (player.hasPermission("worldborder.ignoreborder")) return null;

    Location newLoc = newLocation(player, loc, border);

    if (Config
        .whooshEffect()) { // give some particle and sound effects where the player was beyond the
      // border
      world.playEffect(loc, Effect.ENDER_SIGNAL, 0);
      world.playEffect(loc, Effect.ENDER_SIGNAL, 0);
      world.playEffect(loc, Effect.SMOKE, 4);
      world.playEffect(loc, Effect.SMOKE, 4);
      world.playEffect(loc, Effect.SMOKE, 4);
      world.playEffect(loc, Effect.GHAST_SHOOT, 0);
    }

    if (returnLocationOnly) return newLoc;

    if (!player.isInsideVehicle()) player.teleport(newLoc);
    else {
      Vehicle ride = (Vehicle) player.getVehicle();
      if (ride != null) { // vehicles need to be offset vertically and have velocity stopped
        double vertOffset = ride.getLocation().getY() - loc.getY();
        newLoc.setY(newLoc.getY() + vertOffset);
        ride.setVelocity(new Vector(0, 0, 0));
        ride.teleport(newLoc);
      } else { // if player.getVehicle() returns null (when riding a pig on older Bukkit releases,
        // for instance), player has to be ejected
        player.leaveVehicle();
        player.teleport(newLoc);
      }
    }

    return null;
  }
  /** @param event */
  @EventHandler(priority = EventPriority.HIGH)
  public void onVehicleMove(VehicleMoveEvent event) {
    Vehicle v = event.getVehicle();
    Entity entity = v.getPassenger();

    if (plugin.getSettingsManager().isBlacklistedWorld(v.getLocation().getWorld())) {
      return;
    }

    if (!(entity instanceof Player)) {
      return;
    }

    plugin
        .getPlayerListener()
        .onPlayerMove(new PlayerMoveEvent((Player) entity, event.getFrom(), event.getTo()));
  }
  @EventHandler(priority = EventPriority.HIGHEST)
  public void onVehicleDamage(VehicleDamageEvent event) {
    if (event.isCancelled()) return;

    if (event.getAttacker() instanceof Player) {
      final Player player = (Player) event.getAttacker();
      final Vehicle vehicle = event.getVehicle();

      if (player.getWorld() != plugin.getIslandWorld()) return;
      if (player.isOp()) return;
      if (player.hasPermission("islandworld.bypass.island")) return;

      if (vehicle != null
          && !(plugin.getConfig().getBoolean("flags.use-vehicle", false)
              || plugin.canBuildOnLocation(player, vehicle.getLocation())))
        event.setCancelled(true);

      plugin.debug("Event :" + event.getEventName() + ", cancelled:" + event.isCancelled());
    }
  }
  /** @param event */
  @EventHandler(priority = EventPriority.HIGH)
  public void onVehicleExit(VehicleExitEvent event) {
    Vehicle vehicle = event.getVehicle();

    Entity passenger = vehicle.getPassenger();

    if (passenger instanceof Player) {
      Player player = (Player) passenger;

      Field field =
          plugin
              .getForceFieldManager()
              .getEnabledSourceField(player.getLocation(), FieldFlag.PREVENT_VEHICLE_EXIT);

      if (field != null) {
        if (FieldFlag.PREVENT_VEHICLE_EXIT.applies(field, player)) {
          event.setCancelled(true);
        }
      }
    }
  }
  @Before
  public void setupTestCase() {
    mockVehicle = mock(Vehicle.class);
    mockCreature = mock(Creature.class);
    DataManager mockDataManager = mock(DataManager.class);
    Location mockLocation = mock(Location.class);
    mockChunk = mock(ChunkData.class);
    systemUnderTest = new EntityEventHandler(mockDataManager);

    when(mockVehicle.getLocation()).thenReturn(mockLocation);
    when(mockCreature.getLocation()).thenReturn(mockLocation);
    when(mockDataManager.getChunkAt(mockLocation)).thenReturn(mockChunk);
    when(mockChunk.getOwnerName()).thenReturn("APlayer");
  }
Exemplo n.º 7
0
  /**
   * Called on vehicle movement.
   *
   * @param event Relevant event details
   */
  @EventHandler
  public void onVehicleMove(VehicleMoveEvent event) {
    Vehicle vehicle = event.getVehicle();
    if (vehicle.getPassenger() == null || !(vehicle.getPassenger() instanceof Player)) return;
    Player player = (Player) vehicle.getPassenger();

    if (event.getFrom().getBlockX() != event.getTo().getBlockX()
        || event.getFrom().getBlockY() != event.getTo().getBlockY()
        || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {

      if (sessions.getAdminSession(player).isFrozen()) {

        player.sendMessage(ChatColor.RED + "You are frozen.");

        vehicle.setVelocity(new org.bukkit.util.Vector(0, 0, 0));
        vehicle.teleport(event.getFrom());
      }
    }
  }
Exemplo n.º 8
0
  public void teleport(final Vehicle vehicle) {
    Location traveller =
        new Location(
            this.world,
            vehicle.getLocation().getX(),
            vehicle.getLocation().getY(),
            vehicle.getLocation().getZ());
    Location exit = getExit(traveller);

    double velocity = vehicle.getVelocity().length();

    // Stop and teleport
    vehicle.setVelocity(new Vector());

    // Get new velocity
    final Vector newVelocity = new Vector();
    switch ((int) id.getBlock().getData()) {
      case 2:
        newVelocity.setZ(-1);
        break;
      case 3:
        newVelocity.setZ(1);
        break;
      case 4:
        newVelocity.setX(-1);
        break;
      case 5:
        newVelocity.setX(1);
        break;
    }
    newVelocity.multiply(velocity);

    final Entity passenger = vehicle.getPassenger();
    if (passenger != null) {
      final Vehicle v = exit.getWorld().spawn(exit, vehicle.getClass());
      vehicle.eject();
      vehicle.remove();
      passenger.teleport(exit);
      Stargate.server
          .getScheduler()
          .scheduleSyncDelayedTask(
              Stargate.stargate,
              new Runnable() {
                public void run() {
                  v.setPassenger(passenger);
                  v.setVelocity(newVelocity);
                }
              },
              1);
    } else {
      Vehicle mc = exit.getWorld().spawn(exit, vehicle.getClass());
      if (mc instanceof StorageMinecart) {
        StorageMinecart smc = (StorageMinecart) mc;
        smc.getInventory().setContents(((StorageMinecart) vehicle).getInventory().getContents());
      }
      mc.setVelocity(newVelocity);
      vehicle.remove();
    }
  }
Exemplo n.º 9
0
  /**
   * Teleports the passed in Entity through the portal.
   *
   * <p>Given an Entity object, attempts to teleport them through the portal. This involves many
   * steps. 1) Verify the portal on the other end still exists. If it doesn't, mark this as such. 2)
   * If there is no counterpart, figure out where it would be, and get it. This may involve
   * generating and placing a portal into the world. 3) Assuming we now have a counterpart, figure
   * out where to teleport the entity to. 3a) Figure out the entity's position relative to the entry
   * portal. 3b) Translate this to a position relative to the exit portal. 3c) Preserve the entity's
   * camera's orientation relative to the portal. 4) Teleport the entity. 4a) If the entity is a
   * Player in a vehicle, we do a dance. - Raise the destination by 1 (vehicles have to 'fall' into
   * the portal to avoid losing momentum, so they should be one higher). - Make the player leave the
   * vehicle. - Spawn a new minecart at the destination. - Teleport the player to the destination. -
   * Make the player a passenger of the minecart. - Give the new minecart the (properly translated)
   * velocity of the old vehicle. - Remove the old vehicle.
   *
   * @param e The entity to teleport.
   * @return The location the entity was teleported to, or null if the entity was not teleported.
   */
  public Location teleport(Entity e, Location interaction) {
    if (this.counterpart != null) {
      if (!this.counterpart.isValid()) {
        PortalUtil.removePortal(this.counterpart);
        this.counterpart = null;
        PortalUtil.getCounterpartPortalFor(this);
      } else {
        BlockData bd = this.getWorldBlockType();
        if (bd != null
            && !bd.m.equals(Material.AIR)
            && !bd.m.equals(Material.OBSIDIAN)
            && !this.counterpart
                .getKeyBlock()
                .getWorld()
                .equals(PortalUtil.getDestWorldFor(this))) {

          // Did my keyblock change, and if so, change my destination.
          this.counterpart = null;
          PortalUtil.getCounterpartPortalFor(this);
        }
      }
    } else {
      PortalUtil.getCounterpartPortalFor(this);
    }

    if (this.counterpart == null) {
      // Could not establish a link, for whatever reason.
      return null;
    }

    double destX, destY, destZ;
    float destPitch, destYaw;
    int rotateVehicleVelocity = 0;

    Vector offset = interaction.toVector().subtract(this.keyBlock.getLocation().toVector());

    Vector finalOffset;

    if (this.facingNorth) {
      if (offset.getX() < .5) {
        // Player moving south.
        offset.setX(offset.getX() + OFFSET);
      } else {
        // Player moving north.
        offset.setX(offset.getX() - OFFSET);
      }

      if (this.counterpart.isFacingNorth()) {
        destYaw = e.getLocation().getYaw();
        finalOffset = offset;
      } else {
        destYaw = e.getLocation().getYaw() - 90;
        finalOffset = new Vector(offset.getZ(), offset.getY(), -offset.getX() + OFFSET);
        rotateVehicleVelocity = 1;
      }
    } else {
      if (offset.getZ() < .5) {
        // Player moving west
        offset.setZ(offset.getZ() + OFFSET);
      } else {
        // Player moving east.
        offset.setZ(offset.getZ() - OFFSET);
      }
      if (this.counterpart.isFacingNorth()) {
        destYaw = e.getLocation().getYaw() + 90;
        finalOffset = new Vector(-offset.getZ() + OFFSET, offset.getY(), offset.getX());
        rotateVehicleVelocity = 2;
      } else {
        destYaw = e.getLocation().getYaw();
        finalOffset = offset;
      }
    }

    World destWorld = this.counterpart.getKeyBlock().getWorld();
    String permission = "nethrar.block." + destWorld.getName();

    if ((Nethrar.getPlugin().shouldUsePermissions())
        && ((e instanceof Player))
        && (((Player) e).hasPermission(permission))
        && !((Player) e).isOp()) {
      return null;
    }

    destX = this.counterpart.getKeyBlock().getX() + finalOffset.getX();
    destY = this.counterpart.getKeyBlock().getY() + finalOffset.getY();
    destZ = this.counterpart.getKeyBlock().getZ() + finalOffset.getZ();

    destPitch = e.getLocation().getPitch();

    // Jitter the location just a bit so the resulting minecart doesn't
    // end up underground, if there is a minecart being teleported.
    if ((e instanceof Player && ((Player) e).isInsideVehicle())
        || (e instanceof Vehicle && !(e instanceof Pig))) {
      // +.11 is necessary to get a minecart to spawn on top of, instead
      // of inside, rails on the same level on the other side. However,
      // if there are *not* rails on the other side, then the minecart
      // will fall into the block underneath, unless a +1 is added.
      destY += 1.0;
    }

    Location dest;
    dest = new Location(destWorld, destX, destY, destZ, destYaw, destPitch);

    // Bug: Player camera orientation not preserved when teleporting
    // in a vehicle. Probably because vehicle takes over player
    // camera.
    Vehicle oldV = null, newV = null;
    if (e instanceof Player) {
      if (((Player) e).isInsideVehicle()) {
        oldV = (Vehicle) ((Player) e).getVehicle();
        ((Player) e).leaveVehicle();
      }
    } else if (e instanceof StorageMinecart || e instanceof Minecart || e instanceof Boat) {

      oldV = ((Vehicle) e);
    }

    if (oldV != null) {
      if (oldV instanceof StorageMinecart) {
        newV = destWorld.spawn(dest, StorageMinecart.class);
        ((StorageMinecart) newV)
            .getInventory()
            .setContents(((StorageMinecart) oldV).getInventory().getContents());
      } else if (oldV instanceof Minecart) {
        newV = destWorld.spawn(dest, Minecart.class);
      } else if (oldV instanceof Boat) {
        newV = destWorld.spawn(dest, Boat.class);
      } else {
        log.warning("[NETHRAR] Unsupported vehicle hit a portal.");
      }

      Vector oldVelocity = oldV.getVelocity();
      Vector newVelocity;
      switch (rotateVehicleVelocity) {
          // Left-handed system - clockwise is positive.
        case 1:
          // In a north-facing portal, out a west-facing portal.
          // Rotate 90 degrees counterclockwise.
          newVelocity = new Vector(oldVelocity.getZ(), oldVelocity.getY(), oldVelocity.getX() * -1);
          break;
        case 2:
          // In a west-facing portal, out a north-facing portal.
          // Rotate 90 degrees clockwise.
          newVelocity = new Vector(oldVelocity.getZ() * -1, oldVelocity.getY(), oldVelocity.getX());
          break;
        default:
          newVelocity = oldVelocity;
          break;
      }

      PortalUtil.markTeleported(e);
      Bukkit.getServer()
          .getScheduler()
          .scheduleSyncDelayedTask(
              PortalUtil.getPlugin(), new NethrarTeleporter(e, dest, newV, newVelocity, oldV));
    } else {
      PortalUtil.markTeleported(e);
      // Regular player teleportation doesn't need to be delayed.
      NethrarTeleporter tp = new NethrarTeleporter(e, dest);
      tp.run();
    }
    return null;
  }