Beispiel #1
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();
    }
  }
  /** @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;
          }
        }
      }
    }
  }
  @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");
  }
  // 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());
    }
  }