@Override
  public boolean run(
      CommandSender sender,
      Player sender_p,
      Command cmd,
      String commandLabel,
      String[] args,
      boolean senderIsConsole) {
    Player targetPlayer = sender_p;

    if (args.length == 1) {

      targetPlayer = getPlayer(args[0]);

      if (targetPlayer == null) {
        sender.sendMessage(TotalFreedomMod.PLAYER_NOT_FOUND);
        return true;
      }
    }

    if (senderIsConsole) {
      if (targetPlayer == null) {
        sender.sendMessage(
            "When used from the console, you must define a target player: /cartsit <player>");
        return true;
      }
    } else if (targetPlayer != sender_p && !TFM_AdminList.isSuperAdmin(sender)) {
      sender.sendMessage("Only superadmins can select another player as a /cartsit target.");
      return true;
    }

    if (targetPlayer.isInsideVehicle()) {
      targetPlayer.getVehicle().eject();
    } else {
      Minecart nearest_cart = null;
      for (Minecart cart : targetPlayer.getWorld().getEntitiesByClass(Minecart.class)) {
        if (cart.isEmpty()) {
          if (nearest_cart == null) {
            nearest_cart = cart;
          } else {
            if (cart.getLocation().distanceSquared(targetPlayer.getLocation())
                < nearest_cart.getLocation().distanceSquared(targetPlayer.getLocation())) {
              nearest_cart = cart;
            }
          }
        }
      }

      if (nearest_cart != null) {
        nearest_cart.setPassenger(targetPlayer);
      } else {
        sender.sendMessage("There are no empty minecarts in the target world.");
      }
    }

    return true;
  }
Exemple #2
0
  @EventHandler
  public void onVehicleImpact(CartBlockImpactEvent event) {

    // validate
    if (!event.getBlocks().matches(getMaterial())) return;
    if (!event.getBlocks().hasSign()) return;
    if (event.isMinor()) return;
    if (!(event.getBlocks().matches("cartlift up") || event.getBlocks().matches("cartlift down")))
      return;

    Minecart cart = (Minecart) event.getVehicle();

    // go
    boolean up = event.getBlocks().matches("cartlift up");
    Block destination = event.getBlocks().sign;

    BlockFace face;
    if (up) face = BlockFace.UP;
    else face = BlockFace.DOWN;

    while (true) {

      if (destination.getLocation().getBlockY() <= 0 && !up) return;
      if (destination.getLocation().getBlockY() >= destination.getWorld().getMaxHeight() - 1 && up)
        return;

      destination = destination.getRelative(face);

      if (SignUtil.isSign(destination)
          && event.getBlocks().base.getTypeId()
              == destination.getRelative(BlockFace.UP, 1).getTypeId()) {

        ChangedSign state = BukkitUtil.toChangedSign(destination);
        String testLine = state.getLine(1);

        if (testLine.equalsIgnoreCase("[CartLift Up]")
            || testLine.equalsIgnoreCase("[CartLift Down]")
            || testLine.equalsIgnoreCase("[CartLift]")) {
          destination = destination.getRelative(BlockFace.UP, 2);
          break;
        }
      }
    }

    CartUtils.teleport(
        cart,
        new Location(
            destination.getWorld(),
            destination.getX(),
            destination.getY(),
            destination.getZ(),
            cart.getLocation().getYaw(),
            cart.getLocation().getPitch()));
  }